From f9e9f4b499925db0f54a8eab93054b2bbe52550a Mon Sep 17 00:00:00 2001
From: Giovanni La Mura <giovanni.lamura@inaf.it>
Date: Wed, 24 Apr 2024 17:15:18 +0200
Subject: [PATCH] Build COMMON objects from configuration structures

---
 src/cluster/cluster.cpp | 13 ++++++++++---
 src/include/Commons.h   | 21 ++++++++++++---------
 src/libnptm/Commons.cpp | 37 ++++++++++++++++++++-----------------
 src/sphere/sphere.cpp   |  4 ++--
 4 files changed, 44 insertions(+), 31 deletions(-)

diff --git a/src/cluster/cluster.cpp b/src/cluster/cluster.cpp
index 609da988..6ccfa80d 100644
--- a/src/cluster/cluster.cpp
+++ b/src/cluster/cluster.cpp
@@ -61,8 +61,11 @@ int cluster_jxi488_cycle(int jxi488, ScattererConfiguration *sconf, GeometryConf
  *  \param data_file: `string` Name of the input data file.
  *  \param output_path: `string` Directory to write the output files in.
  */
-void cluster(string config_file, string data_file, string output_path) {
+void cluster(const string& config_file, const string& data_file, const string& output_path) {
   chrono::time_point<chrono::high_resolution_clock> t_start = chrono::high_resolution_clock::now();
+  string timing_name = output_path + "/c_timing.log";
+  FILE *timing_file = fopen(timing_name.c_str(), "w");
+  Logger *time_logger = new Logger(LOG_DEBG, timing_file);
   Logger *logger = new Logger(LOG_INFO);
   logger->log("INFO: making legacy configuration...", LOG_INFO);
   ScattererConfiguration *sconf = NULL;
@@ -121,7 +124,7 @@ void cluster(string config_file, string data_file, string output_path) {
     if (le > lm) lm = le;
     C1 *c1 = new C1(gconf, sconf);
     C3 *c3 = new C3();
-    C4 *c4 = new C4(li, le, nsph);
+    C4 *c4 = new C4(gconf);
     C1_AddOns *c1ao = new C1_AddOns(c4);
     // End of add-ons initialization
     C6 *c6 = new C6(c4->lmtpo);
@@ -131,9 +134,10 @@ void cluster(string config_file, string data_file, string output_path) {
     dcomplex arg = 0.0 + 0.0 * I;
     dcomplex ccsam = 0.0 + 0.0 * I;
     int configurations = (int)sconf->get_param("configurations");
-    C2 *c2 = new C2(nsph, configurations, npnt, npntts);
+    C2 *c2 = new C2(gconf, sconf);
     np_int ndit = 2 * nsph * c4->nlim;
     logger->log("INFO: Size of matrices to invert: " + to_string((int64_t)ndit) + " x " + to_string((int64_t)ndit) +".\n");
+    time_logger->log("INFO: Size of matrices to invert: " + to_string((int64_t)ndit) + " x " + to_string((int64_t)ndit) +".\n");
     const int ndi = c4->nsph * c4->nlim;
     C9 *c9 = new C9(ndi, c4->nlem, 2 * ndi, 2 * c4->nlem);
     double *gaps = new double[nsph]();
@@ -802,6 +806,9 @@ void cluster(string config_file, string data_file, string output_path) {
   string message = "Calculation lasted " + to_string(elapsed.count()) + ".\n";
   logger->log(message);
   logger->log("Finished: output written to " + output_path + "/c_OCLU\n");
+  time_logger->log(message);
+  fclose(timing_file);
+  delete time_logger;
   delete logger;
 }
 
diff --git a/src/include/Commons.h b/src/include/Commons.h
index 1022fb3b..04c29745 100644
--- a/src/include/Commons.h
+++ b/src/include/Commons.h
@@ -140,12 +140,10 @@ public:
 
   /*! \brief C2 instance constructor.
    *
-   * \param ns: `int` Number of spheres.
-   * \param nl: `int`
-   * \param npnt: `int`
-   * \param npntts: `int`
+   * \param gconf: `GeometryConfiguration*` Pointer to a GeometryConfiguration instance.
+   * \param sconf: `ScattererConfiguration*` Pointer to a ScattererConfiguration instance.
    */
-  C2(int ns, int nl, int npnt, int npntts);
+  C2(GeometryConfiguration *gconf, ScattererConfiguration *sconf);
 
   /*! \brief C2 instance constructor copying its contents from preexisting instance.
    *
@@ -216,14 +214,19 @@ public:
   //! \brief QUESTION: definition?
   int nv3j;
 
-  /*! \brief C3 instance constructor.
+  /*! \brief C4 instance constructor.
+   *
+   * \param gconf: `GeometryConfiguration*` Pointer to a GeometryConfiguration instance.
    */
-  C4(int li, int le, int nsph);
-  /*! \brief C3 instance constructor copying its contents from a preexisting object.
+  C4(GeometryConfiguration *gconf);
+  
+  /*! \brief C4 instance constructor copying its contents from a preexisting object.
+   *
+   * \param rhs: `C4&` Reference of the object to be copied.
    */
   C4(const C4& rhs);
 
-  /*! \brief C3 instance destroyer.
+  /*! \brief C4 instance destroyer.
    */
   ~C4();
 };
diff --git a/src/libnptm/Commons.cpp b/src/libnptm/Commons.cpp
index d5a9cb4e..0dca548d 100644
--- a/src/libnptm/Commons.cpp
+++ b/src/libnptm/Commons.cpp
@@ -366,11 +366,14 @@ C1_AddOns::~C1_AddOns() {
   delete[] ecsc;
 }
 
-C2::C2(int ns, int _nl, int npnt, int npntts) {
-  nsph = ns;
+C2::C2(GeometryConfiguration *gconf, ScattererConfiguration *sconf) {
+  nsph = (int)gconf->get_param("nsph");
+  int npnt = (int)gconf->get_param("npnt");
+  int npntts = (int)gconf->get_param("npntts");
   int max_n = (npnt > npntts) ? npnt : npntts;
   nhspo = 2 * max_n - 1;
-  nl = _nl;
+  nl = (int)sconf->get_param("configurations");
+  if (nsph == 1 && nl == 1) nl = 5;
   ris = new dcomplex[nhspo]();
   dlri = new dcomplex[nhspo]();
   vkt = new dcomplex[nsph]();
@@ -436,20 +439,20 @@ C3::~C3() {
   delete[] tsas;
 }
 
-C4::C4(int _li, int _le, int _nsph) {
-    li = _li;
-    le = _le;
-    lm = (li > le) ? li : le;
-    nv3j = (lm * (lm  + 1) * (2 * lm + 7)) / 6;
-    nsph = _nsph;
-    // The following is needed to initialize C1_AddOns
-    litpo = li + li + 1;
-    litpos = litpo * litpo;
-    lmtpo = li + le + 1;
-    lmtpos = lmtpo * lmtpo;
-    nlim = li * (li + 2);
-    nlem = le * (le + 2);
-    lmpo = lm + 1;
+C4::C4(GeometryConfiguration *gconf) {
+  li = (int)gconf->get_param("li");
+  le = (int)gconf->get_param("le");
+  lm = (li > le) ? li : le;
+  nv3j = (lm * (lm  + 1) * (2 * lm + 7)) / 6;
+  nsph = (int)gconf->get_param("nsph");
+  // The following is needed to initialize C1_AddOns
+  litpo = li + li + 1;
+  litpos = litpo * litpo;
+  lmtpo = li + le + 1;
+  lmtpos = lmtpo * lmtpo;
+  nlim = li * (li + 2);
+  nlem = le * (le + 2);
+  lmpo = lm + 1;
 }
 
 C4::C4(const C4& rhs) {
diff --git a/src/sphere/sphere.cpp b/src/sphere/sphere.cpp
index c851f401..5e5c5e65 100644
--- a/src/sphere/sphere.cpp
+++ b/src/sphere/sphere.cpp
@@ -45,7 +45,7 @@ using namespace std;
  *  \param data_file: `string` Name of the input data file.
  *  \param output_path: `string` Directory to write the output files in.
  */
-void sphere(string config_file, string data_file, string output_path) {
+void sphere(const string& config_file, const string& data_file, const string& output_path) {
   Logger *logger = new Logger(LOG_INFO);
   dcomplex arg, s0, tfsas;
   double th, ph;
@@ -133,7 +133,7 @@ void sphere(string config_file, string data_file, string output_path) {
     double sc_phi_start = gconf->get_param("sc_phi_start");
     double sc_phi_step = gconf->get_param("sc_phi_step");
     double sc_phi_end = gconf->get_param("sc_phi_end");
-    C2 *c2 = new C2(nsph, 5, npnt, npntts);
+    C2 *c2 = new C2(gconf, sconf);
     argi = new double[1];
     args = new double[1];
     gaps = new double[2];
-- 
GitLab