Skip to content

Commit

Permalink
Multipole: beautify class ModeArray
Browse files Browse the repository at this point in the history
  • Loading branch information
lwJi committed Jun 25, 2024
1 parent 9242a5d commit 4f6a26e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
6 changes: 3 additions & 3 deletions Multipole/src/multipole.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ static void output_modes(CCTK_ARGUMENTS, const variable_desc vars[],

if (output_tsv) {
if (CCTK_MyProc(cctkGH) == 0) {
for (int v = 0; v < modes.get_nvars(); v++) {
for (int i = 0; i < modes.get_nradii(); i++) {
for (int v = 0; v < modes.getNumVars(); v++) {
for (int i = 0; i < modes.getNumRadii(); i++) {
const CCTK_REAL rad = radii[i];
for (int l = 0; l <= modes.get_lmax(); l++) {
for (int l = 0; l <= modes.getMaxL(); l++) {
for (int m = -l; m <= l; m++) {
ostringstream name;
name << "mp_" << vars[v].name << "_l" << l << "_m" << m << "_r"
Expand Down
50 changes: 28 additions & 22 deletions Multipole/src/multipole.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,45 @@ struct variables_desc {
// nradii radii for nvars variables
class ModeArray {
public:
ModeArray(int nvars_, int nradii_, int lmax_)
: nvars(nvars_), nradii(nradii_), lmax(lmax_),
modes(size_t(nvars * nradii * (lmax + 1) * (lmax + 1) * 2)) {}
ModeArray(int numVars, int numRadii, int maxL)
: numVars_(numVars), numRadii_(numRadii), maxL_(maxL),
modes_(static_cast<std::size_t>(numVars * numRadii * (maxL + 1) *
(maxL + 1) * 2)) {}
// Ready for potential inheritance
virtual ~ModeArray() {}
// default copy and assignment is ok

CCTK_REAL &operator()(int v, int ri, int l, int m, bool is_im) {
return modes.at(mode_idx(v, ri, l, m, is_im));
// For writing to modes_
CCTK_REAL &operator()(int v, int ri, int l, int m, bool isImaginary) {
return modes_.at(modeIndex(v, ri, l, m, isImaginary));
}

const CCTK_REAL &operator()(int v, int ri, int l, int m, bool is_im) const {
return modes.at(mode_idx(v, ri, l, m, is_im));
// For reading to modes_
const CCTK_REAL &operator()(int v, int ri, int l, int m,
bool isImaginary) const {
return modes_.at(modeIndex(v, ri, l, m, isImaginary));
}

int get_nvars() const { return nvars; }
int get_nradii() const { return nradii; }
int get_lmax() const { return lmax; }
int getNumVars() const { return numVars_; }
int getNumRadii() const { return numRadii_; }
int getMaxL() const { return maxL_; }

private:
size_t mode_idx(int v, int ri, int l, int m, int is_im) const {
assert(v >= 0 && v < nvars);
assert(ri >= 0 && ri < nradii);
assert(l >= 0 && l <= lmax);
inline std::size_t modeIndex(int v, int ri, int l, int m,
int isImaginary) const {
assert(v >= 0 && v < numVars_);
assert(ri >= 0 && ri < numRadii_);
assert(l >= 0 && l <= maxL_);
assert(m <= l && -m <= l);
return size_t(v * nradii * (lmax + 1) * (lmax + 1) * 2 +
ri * (lmax + 1) * (lmax + 1) * 2 + (l * l + (m + l)) * 2 +
is_im);
return static_cast<std::size_t>(v * numRadii_ * (maxL_ + 1) * (maxL_ + 1) *
2 +
ri * (maxL_ + 1) * (maxL_ + 1) * 2 +
(l * l + (m + l)) * 2 + isImaginary);
}

const int nvars;
const int nradii;
const int lmax;
std::vector<CCTK_REAL> modes;
const int numVars_;
const int numRadii_;
const int maxL_;
std::vector<CCTK_REAL> modes_; // 1D array to store the modes
};

inline bool int_in_array(int a, const int array[], int len) {
Expand Down
6 changes: 3 additions & 3 deletions Multipole/src/utils.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void OutputComplexToH5File(CCTK_ARGUMENTS, const variable_desc vars[],
// false
static map<string, bool> checked;

for (int v = 0; v < modes.get_nvars(); v++) {
for (int v = 0; v < modes.getNumVars(); v++) {
string basename = "mp_" + vars[v].name + ".h5";
string output_name = my_out_dir + string("/") + basename;

Expand All @@ -163,9 +163,9 @@ void OutputComplexToH5File(CCTK_ARGUMENTS, const variable_desc vars[],

checked[output_name] = true;

for (int i = 0; i < modes.get_nradii(); i++) {
for (int i = 0; i < modes.getNumRadii(); i++) {
const CCTK_REAL rad = radii[i];
for (int l = 0; l <= modes.get_lmax(); l++) {
for (int l = 0; l <= modes.getMaxL(); l++) {
for (int m = -l; m <= l; m++) {
ostringstream datasetname;
datasetname << "l" << l << "_m" << m << "_r"
Expand Down

0 comments on commit 4f6a26e

Please sign in to comment.