Skip to content

Commit 29b6a6f

Browse files
author
Martin D. Weinberg
committed
Move from std::vector to Eigen::Vector returns for getAccel
1 parent 5af5071 commit 29b6a6f

3 files changed

Lines changed: 39 additions & 51 deletions

File tree

expui/BiorthBasis.H

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ namespace BasisClasses
8585
virtual const std::string harmonic() = 0;
8686

8787
//@{
88-
//! Compute acceleration helper
88+
//! Compute acceleration helper for each derived class
8989
virtual void computeAccel(double x, double y, double z,
90-
std::vector<double>& vstore) = 0;
90+
Eigen::Ref<Eigen::Vector3d> vstore) = 0;
9191

92-
//! C++ local storage for getAccel
93-
std::vector<double> vstore {0.0, 0.0, 0.0};
92+
//! C++ local storage for computeAccel
93+
Eigen::Vector3d vstore;
9494

9595
//! C++ local storage for getAccelArray
96-
std::vector<std::vector<double>> vstore_array;
96+
RowMatrixXd varray;
9797
//@}
9898

9999
public:
@@ -184,43 +184,31 @@ namespace BasisClasses
184184

185185
//! Evaluate acceleration in Cartesian coordinates in centered
186186
//! coordinate system
187-
virtual std::vector<double>& getAccel(double x, double y, double z)
187+
virtual Eigen::Vector3d& getAccel(double x, double y, double z)
188188
{
189189
computeAccel(x, y, z, vstore);
190190
return vstore;
191191
}
192192

193193
//! Evaluate acceleration in Cartesian coordinates in centered
194194
//! coordinate system for a collections of points
195-
virtual RowMatrixXd getAccelArray(Eigen::VectorXd& x,
196-
Eigen::VectorXd& y,
197-
Eigen::VectorXd& z)
195+
virtual RowMatrixXd& getAccelArray(Eigen::VectorXd& x,
196+
Eigen::VectorXd& y,
197+
Eigen::VectorXd& z)
198198
{
199199
// Sanity check
200200
if (x.size() != y.size() || x.size() != z.size())
201201
throw std::runtime_error("BiorthBasis::getAccelArray: "
202202
"x, y, z vectors must be of the same size");
203203

204-
int max_threads = 1;
205-
#if defined(_OPENMP)
206-
max_threads = omp_get_max_threads();
207-
#endif
208-
vstore_array.resize(max_threads);
209-
for (auto & v : vstore_array) v.resize(3);
210-
211-
RowMatrixXd ret(x.size(), 3);
212-
std::vector<double> v(3);
213-
int tid = 0; // Default thread ID if no OpenMP
204+
// Resize the return array
205+
varray.resize(x.size(), 3);
214206
#pragma omp parallel for
215207
for (int i=0; i<x.size(); ++i) {
216-
#if defined(_OPENMP)
217-
tid = omp_get_thread_num();
218-
#endif
219-
computeAccel(x(i), y(i), z(i), vstore_array[tid]);
220-
for (int k=0; k<3; k++) ret(i, k) = vstore_array[tid][k];
208+
computeAccel(x(i), y(i), z(i), varray.row(i));
221209
}
222210

223-
return ret;
211+
return varray;
224212
}
225213
};
226214

@@ -315,7 +303,7 @@ namespace BasisClasses
315303
//! Evaluate acceleration in Cartesian coordinates in centered
316304
//! coordinate system
317305
virtual void computeAccel(double x, double y, double z,
318-
std::vector<double>& vstore);
306+
Eigen::Ref<Eigen::Vector3d> vstore);
319307

320308
public:
321309

@@ -558,7 +546,7 @@ namespace BasisClasses
558546
//! Evaluate acceleration in Cartesian coordinates in centered
559547
//! coordinate system
560548
void computeAccel(double x, double y, double z,
561-
std::vector<double>& vstore);
549+
Eigen::Ref<Eigen::Vector3d> vstore);
562550

563551
public:
564552

@@ -714,7 +702,7 @@ namespace BasisClasses
714702
//! Evaluate acceleration in Cartesian coordinates in centered
715703
//! coordinate system
716704
void computeAccel(double x, double y, double z,
717-
std::vector<double>& vstore);
705+
Eigen::Ref<Eigen::Vector3d> vstore);
718706

719707
public:
720708

@@ -865,7 +853,7 @@ namespace BasisClasses
865853
//! Evaluate acceleration in Cartesian coordinates in centered
866854
//! coordinate system
867855
void computeAccel(double x, double y, double z,
868-
std::vector<double>& vstore);
856+
Eigen::Ref<Eigen::Vector3d> vstore);
869857

870858
public:
871859

@@ -1024,7 +1012,7 @@ namespace BasisClasses
10241012
//! Evaluate acceleration in Cartesian coordinates in centered
10251013
//! coordinate system
10261014
void computeAccel(double x, double y, double z,
1027-
std::vector<double>& vstore);
1015+
Eigen::Ref<Eigen::Vector3d> vstore);
10281016

10291017
public:
10301018

@@ -1159,7 +1147,7 @@ namespace BasisClasses
11591147
//! Evaluate acceleration in Cartesian coordinates in centered
11601148
//! coordinate system
11611149
void computeAccel(double x, double y, double z,
1162-
std::vector<double>& vstore);
1150+
Eigen::Ref<Eigen::Vector3d> vstore);
11631151

11641152

11651153
public:

expui/BiorthBasis.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ namespace BasisClasses
693693
}
694694

695695
void Spherical::computeAccel(double x, double y, double z,
696-
std::vector<double>& acc)
696+
Eigen::Ref<Eigen::Vector3d> acc)
697697
{
698698
// Get polar coordinates
699699
double R = sqrt(x*x + y*y);
@@ -795,7 +795,7 @@ namespace BasisClasses
795795

796796
// Return force not potential gradient
797797
//
798-
acc = {tpotx, tpoty, potz};
798+
acc << tpotx, tpoty, potz;
799799
}
800800

801801

@@ -1600,7 +1600,7 @@ namespace BasisClasses
16001600

16011601
// Evaluate in cartesian coordinates
16021602
void Cylindrical::computeAccel(double x, double y, double z,
1603-
std::vector<double>& acc)
1603+
Eigen::Ref<Eigen::Vector3d> acc)
16041604
{
16051605
double R = sqrt(x*x + y*y);
16061606
double phi = atan2(y, x);
@@ -1614,7 +1614,7 @@ namespace BasisClasses
16141614
double tpotx = tpotR*x/R - tpotp*y/R ;
16151615
double tpoty = tpotR*y/R + tpotp*x/R ;
16161616

1617-
acc = {tpotx, tpoty, tpotz};
1617+
acc << tpotx, tpoty, tpotz;
16181618
}
16191619

16201620
// Evaluate in cylindrical coordinates
@@ -2182,7 +2182,7 @@ namespace BasisClasses
21822182
}
21832183

21842184
void FlatDisk::computeAccel(double x, double y, double z,
2185-
std::vector<double>& acc)
2185+
Eigen::Ref<Eigen::Vector3d> acc)
21862186
{
21872187
// Get thread id
21882188
int tid = omp_get_thread_num();
@@ -2206,7 +2206,7 @@ namespace BasisClasses
22062206
rpot = -totalMass*R/(r*r2 + 10.0*std::numeric_limits<double>::min());
22072207
zpot = -totalMass*z/(r*r2 + 10.0*std::numeric_limits<double>::min());
22082208

2209-
acc = {rpot, zpot, ppot};
2209+
acc << rpot, zpot, ppot;
22102210
}
22112211

22122212
// Get the basis fields
@@ -2270,7 +2270,7 @@ namespace BasisClasses
22702270
double potx = rpot*x/R - ppot*y/R;
22712271
double poty = rpot*y/R + ppot*x/R;
22722272

2273-
acc = {potx, poty, zpot};
2273+
acc << potx, poty, zpot;
22742274
}
22752275

22762276

@@ -2958,7 +2958,7 @@ namespace BasisClasses
29582958

29592959

29602960
void CBDisk::computeAccel(double x, double y, double z,
2961-
std::vector<double>& acc)
2961+
Eigen::Ref<Eigen::Vector3d> acc)
29622962
{
29632963
// Get thread id
29642964
int tid = omp_get_thread_num();
@@ -3023,7 +3023,7 @@ namespace BasisClasses
30233023
double potx = rpot*x/R - ppot*y/R;
30243024
double poty = rpot*y/R + ppot*x/R;
30253025

3026-
acc = {potx, poty, zpot};
3026+
acc << potx, poty, zpot;
30273027
}
30283028

30293029

@@ -3431,7 +3431,7 @@ namespace BasisClasses
34313431

34323432

34333433
void Slab::computeAccel(double x, double y, double z,
3434-
std::vector<double>& acc)
3434+
Eigen::Ref<Eigen::Vector3d> acc)
34353435
{
34363436
// Loop indices
34373437
//
@@ -3504,7 +3504,7 @@ namespace BasisClasses
35043504
}
35053505
}
35063506

3507-
acc = {accx.real(), accy.real(), accz.real()};
3507+
acc << accx.real(), accy.real(), accz.real();
35083508
}
35093509

35103510

@@ -3870,7 +3870,7 @@ namespace BasisClasses
38703870
}
38713871

38723872
void Cube::computeAccel(double x, double y, double z,
3873-
std::vector<double>& acc)
3873+
Eigen::Ref<Eigen::Vector3d> acc)
38743874
{
38753875
// Get thread id
38763876
int tid = omp_get_thread_num();
@@ -3881,7 +3881,7 @@ namespace BasisClasses
38813881
// Get the basis fields
38823882
auto frc = ortho->get_force(expcoef, pos);
38833883

3884-
acc = {-frc(0).real(), -frc(1).real(), -frc(2).real()};
3884+
acc << -frc(0).real(), -frc(1).real(), -frc(2).real();
38853885
}
38863886

38873887
std::vector<double> Cube::cyl_eval(double R, double z, double phi)

pyEXP/BasisWrappers.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ void BasisFactoryClasses(py::module &m)
408408
}
409409

410410
void computeAccel(double x, double y, double z,
411-
std::vector<double>& v) override
411+
Eigen::Ref<Eigen::Vector3d> v) override
412412
{
413413
PYBIND11_OVERRIDE_PURE(void, BiorthBasis, computeAccel, x, y, z, v);
414414
}
@@ -468,7 +468,7 @@ void BasisFactoryClasses(py::module &m)
468468
}
469469

470470
void computeAccel(double x, double y, double z,
471-
std::vector<double>& v) override
471+
Eigen::Ref<Eigen::Vector3d> v) override
472472
{
473473
PYBIND11_OVERRIDE(void, Spherical, computeAccel, x, y, z, v);
474474
}
@@ -522,7 +522,7 @@ void BasisFactoryClasses(py::module &m)
522522
}
523523

524524
void computeAccel(double x, double y, double z,
525-
std::vector<double>& v) override
525+
Eigen::Ref<Eigen::Vector3d> v) override
526526
{
527527
PYBIND11_OVERRIDE(void, Cylindrical, computeAccel, x, y, z, v);
528528
}
@@ -590,7 +590,7 @@ void BasisFactoryClasses(py::module &m)
590590
}
591591

592592
void computeAccel(double x, double y, double z,
593-
std::vector<double>& v) override
593+
Eigen::Ref<Eigen::Vector3d> v) override
594594
{
595595
PYBIND11_OVERRIDE(void, FlatDisk, computeAccel, x, y, z, v);
596596
}
@@ -662,7 +662,7 @@ void BasisFactoryClasses(py::module &m)
662662
}
663663

664664
void computeAccel(double x, double y, double z,
665-
std::vector<double>& v) override
665+
Eigen::Ref<Eigen::Vector3d> v) override
666666
{
667667
PYBIND11_OVERRIDE(void, CBDisk, computeAccel, x, y, z, v);
668668
}
@@ -736,7 +736,7 @@ void BasisFactoryClasses(py::module &m)
736736
}
737737

738738
void computeAccel(double x, double y, double z,
739-
std::vector<double>& v) override
739+
Eigen::Ref<Eigen::Vector3d> v) override
740740
{
741741
PYBIND11_OVERRIDE(void, Slab, computeAccel, x, y, z, v);
742742
}
@@ -810,7 +810,7 @@ void BasisFactoryClasses(py::module &m)
810810
}
811811

812812
void computeAccel(double x, double y, double z,
813-
std::vector<double>& v) override
813+
Eigen::Ref<Eigen::Vector3d> v) override
814814
{
815815
PYBIND11_OVERRIDE(void, Cube, computeAccel, x, y, z, v);
816816
}

0 commit comments

Comments
 (0)