From 4f6a26e464006354f543ec8a3d5528eafa31369a Mon Sep 17 00:00:00 2001 From: Liwei Ji Date: Tue, 25 Jun 2024 00:48:54 +0000 Subject: [PATCH] Multipole: beautify class ModeArray --- Multipole/src/multipole.cxx | 6 ++--- Multipole/src/multipole.hxx | 50 +++++++++++++++++++++---------------- Multipole/src/utils.cxx | 6 ++--- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/Multipole/src/multipole.cxx b/Multipole/src/multipole.cxx index e9851f24..74305b25 100644 --- a/Multipole/src/multipole.cxx +++ b/Multipole/src/multipole.cxx @@ -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" diff --git a/Multipole/src/multipole.hxx b/Multipole/src/multipole.hxx index 764fe2db..f51bd261 100644 --- a/Multipole/src/multipole.hxx +++ b/Multipole/src/multipole.hxx @@ -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(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(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 modes; + const int numVars_; + const int numRadii_; + const int maxL_; + std::vector modes_; // 1D array to store the modes }; inline bool int_in_array(int a, const int array[], int len) { diff --git a/Multipole/src/utils.cxx b/Multipole/src/utils.cxx index 270ba37c..9561106f 100644 --- a/Multipole/src/utils.cxx +++ b/Multipole/src/utils.cxx @@ -147,7 +147,7 @@ void OutputComplexToH5File(CCTK_ARGUMENTS, const variable_desc vars[], // false static map 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; @@ -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"