From e72c6c59051040a2025602f80271f38bbb437b11 Mon Sep 17 00:00:00 2001
From: Giovanni La Mura <giovanni.lamura@inaf.it>
Date: Fri, 12 Jan 2024 13:41:20 +0100
Subject: [PATCH] Build trapping as a single program

---
 src/trapping/Makefile        | 10 +++++-----
 src/trapping/frfme.cpp       | 13 ++++++-------
 src/trapping/lffft.cpp       | 11 ++++-------
 src/trapping/np_trapping.cpp | 29 +++++++++++++++++++++++++++++
 4 files changed, 44 insertions(+), 19 deletions(-)
 create mode 100644 src/trapping/np_trapping.cpp

diff --git a/src/trapping/Makefile b/src/trapping/Makefile
index f9fcde44..6ef02302 100644
--- a/src/trapping/Makefile
+++ b/src/trapping/Makefile
@@ -6,7 +6,7 @@ CXX=g++
 CXXFLAGS=-O2 -ggdb -pg -coverage
 CXXLFLAGS=-L/usr/lib64 -lhdf5_hl -lhdf5
 
-all: frfme lffft np_frfme np_lffft
+all: frfme lffft np_trapping
 
 frfme: frfme.o
 	$(FC) $(FCFLAGS) -o $(BUILDDIR)/frfme $(BUILDDIR)/frfme.o $(LFLAGS)
@@ -14,11 +14,11 @@ frfme: frfme.o
 lffft: lffft.o
 	$(FC) $(FCFLAGS) -o $(BUILDDIR)/lffft $(BUILDDIR)/lffft.o $(LFLAGS)
 
-np_frfme: $(BUILDDIR)/cfrfme.o $(BUILDDIR)/Parsers.o $(BUILDDIR)/Commons.o $(BUILDDIR)/Configuration.o $(BUILDDIR)/file_io.o $(BUILDDIR)/sph_subs.o $(BUILDDIR)/tra_subs.o
-	$(CXX) $(CXXFLAGS) $(CXXLFLAGS) -o $(BUILDDIR)/np_frfme $(BUILDDIR)/cfrfme.o $(BUILDDIR)/file_io.o $(BUILDDIR)/Parsers.o $(BUILDDIR)/Commons.o $(BUILDDIR)/Configuration.o $(BUILDDIR)/sph_subs.o $(BUILDDIR)/tra_subs.o
+np_trapping: $(BUILDDIR)/np_trapping.o $(BUILDDIR)/cfrfme.o $(BUILDDIR)/clffft.o $(BUILDDIR)/Parsers.o $(BUILDDIR)/Commons.o $(BUILDDIR)/Configuration.o $(BUILDDIR)/file_io.o $(BUILDDIR)/sph_subs.o $(BUILDDIR)/tra_subs.o
+	$(CXX) $(CXXFLAGS) $(CXXLFLAGS) -o $(BUILDDIR)/np_trapping $(BUILDDIR)/np_trapping.o $(BUILDDIR)/cfrfme.o $(BUILDDIR)/clffft.o $(BUILDDIR)/file_io.o $(BUILDDIR)/Parsers.o $(BUILDDIR)/Commons.o $(BUILDDIR)/Configuration.o $(BUILDDIR)/sph_subs.o $(BUILDDIR)/tra_subs.o
 
-np_lffft: $(BUILDDIR)/clffft.o $(BUILDDIR)/Parsers.o $(BUILDDIR)/Commons.o $(BUILDDIR)/Configuration.o $(BUILDDIR)/file_io.o $(BUILDDIR)/sph_subs.o $(BUILDDIR)/tra_subs.o
-	$(CXX) $(CXXFLAGS) $(CXXLFLAGS) -o $(BUILDDIR)/np_lffft $(BUILDDIR)/clffft.o $(BUILDDIR)/file_io.o $(BUILDDIR)/Parsers.o $(BUILDDIR)/Commons.o $(BUILDDIR)/Configuration.o $(BUILDDIR)/sph_subs.o $(BUILDDIR)/tra_subs.o
+$(BUILDDIR)/np_trapping.o:
+	$(CXX) $(CXXFLAGS) np_trapping.cpp -c -o $(BUILDDIR)/np_trapping.o
 
 $(BUILDDIR)/cfrfme.o:
 	$(CXX) $(CXXFLAGS) frfme.cpp -c -o $(BUILDDIR)/cfrfme.o
diff --git a/src/trapping/frfme.cpp b/src/trapping/frfme.cpp
index bf5ab5aa..251ea911 100644
--- a/src/trapping/frfme.cpp
+++ b/src/trapping/frfme.cpp
@@ -29,9 +29,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.
  */
-int main() {
-  string data_file = "../../test_data/trapping/DFRFME";
-  string output_path = ".";
+void frfme(string data_file, string output_path) {
   string tfrfme_name = output_path + "/c_TFRFME";
   fstream tfrfme;
   char namef[7];
@@ -87,7 +85,8 @@ int main() {
       for (int yi = 0; yi < nxv; yi++) tfrfme.read(reinterpret_cast<char *>(&(yv[yi])), sizeof(double));
       for (int zi = 0; zi < nxv; zi++) tfrfme.read(reinterpret_cast<char *>(&(zv[zi])), sizeof(double));
       fstream temptape2;
-      temptape2.open("c_TEMPTAPE2", ios::in | ios::binary);
+      string tempname2 = output_path + "c_TEMPTAPE2";
+      temptape2.open(tempname2.c_str(), ios::in | ios::binary);
       if (temptape2.is_open()) {
 	//vkv = new double[nkv]();
 	for (int jx = 0; jx < nkv; jx++) temptape2.read(reinterpret_cast<char *>(&(vkv[jx])), sizeof(double));
@@ -180,7 +179,8 @@ int main() {
       regex_search(str_target, m, re);
       int ixi = stoi(m.str());
       fstream tedf;
-      tedf.open(namef, ios::in | ios::binary);
+      string tedf_name = output_path + "/" + namef;
+      tedf.open(tedf_name.c_str(), ios::in | ios::binary);
       if (tedf.is_open()) {
 	int iduml, idum;
 	tedf.read(reinterpret_cast<char *>(&iduml), sizeof(int));
@@ -334,7 +334,7 @@ int main() {
 	  w = new complex<double>*[nkv];
 	  for (int wi = 0; wi < nkv; wi++) w[wi] = new complex<double>[nkv]();
 	  for (int j80 = jlmf - 1; j80 < jlml; j80++) {
-	    temptape1.open("c_TEMPTAPE1", ios::in | ios::binary);
+	    temptape1.open(temp_name1.c_str(), ios::in | ios::binary);
 	    for (int jy50 = 0; jy50 < nkv; jy50++) {
 	      for (int jx50 = 0; jx50 < nkv; jx50++) {
 		for (int i = 0; i < nlmmt; i++) {
@@ -449,5 +449,4 @@ int main() {
     delete[] w;
   }
   printf("Done.\n");
-  return 0;
 }
diff --git a/src/trapping/lffft.cpp b/src/trapping/lffft.cpp
index 98bfd19c..d41fb77d 100644
--- a/src/trapping/lffft.cpp
+++ b/src/trapping/lffft.cpp
@@ -29,14 +29,12 @@ using namespace std;
  *  \param data_file: `string` Name of the input data file.
  *  \param output_path: `string` Directory to write the output files in.
  */
-int main() {
+void lffft(string data_file, string output_path) {
   const complex<double> uim(0.0, 1.0);
   const double sq2i = 1.0 / sqrt(2.0);
   const complex<double> sq2iti = sq2i * uim;
 
   fstream tlfff, tlfft;
-  string data_file = "../../test_data/trapping/DLFFFT";
-  string output_path = ".";
   double ****zpv = NULL;
   double *xv = NULL, *yv = NULL, *zv = NULL;
   complex<double> *ac = NULL, *ws = NULL, *wsl = NULL;
@@ -73,7 +71,7 @@ int main() {
     cil->le = le;
     cil->nlem = le * (le + 2);
     cil->nlemt = cil->nlem + cil->nlem;
-    if (is > 2222) { // label 120
+    if (is >= 2222) { // label 120
       tms = new complex<double>*[le];
       for (int ti = 0; ti < le; ti++) tms[ti] = new complex<double>[3]();
       // QUESTION|WARNING: original code uses LM without defining it. Where does it come from?
@@ -90,7 +88,7 @@ int main() {
 	ttms.read(reinterpret_cast<char *>(&vimag), sizeof(double));
 	tms[i][2] = complex<double>(vreal, vimag);
       } // i loop
-    } else if (is > 1111) { // label 125
+    } else if (is >= 1111) { // label 125
       tmsm = new complex<double>[le]();
       tmse = new complex<double>[le]();
       for (int i = 0; i < le; i++) {
@@ -143,7 +141,7 @@ int main() {
     fstream binary_input;
     string binary_name;
     if (jss != 1) binary_name = output_path + "/c_TFRFME";
-    else binary_name = output_path + "c_TWS";
+    else binary_name = output_path + "/c_TWS";
     binary_input.open(binary_name, ios::in | ios::binary);
     if (binary_input.is_open()) {
       int lmode, lm, nkv, nxv, nyv, nzv;
@@ -390,5 +388,4 @@ int main() {
   delete ccr;
   delete[] file_lines;
   printf("Done.\n");
-  return 0;
 }
diff --git a/src/trapping/np_trapping.cpp b/src/trapping/np_trapping.cpp
new file mode 100644
index 00000000..cb3d9be0
--- /dev/null
+++ b/src/trapping/np_trapping.cpp
@@ -0,0 +1,29 @@
+/*! \file trapping.cpp
+ */
+#include <cstdio>
+#include <string>
+
+using namespace std;
+
+extern void frfme(string data_file, string output_path);
+extern void lffft(string data_file, string output_path);
+
+/* \brief Main program execution body.
+ *
+ * \param argc: `int`
+ * \param argv: `char **`
+ * \return result: `int`
+ */
+int main(int argc, char **argv) {
+  string frfme_data_file = "../../test_data/trapping/DFRFME";
+  string lffft_data_file = "../../test_data/trapping/DLFFFT";
+  string output_path = ".";
+  if (argc == 4) {
+    frfme_data_file = string(argv[1]);
+    lffft_data_file = string(argv[2]);
+    output_path = string(argv[3]);
+  }
+  frfme(frfme_data_file, output_path);
+  lffft(lffft_data_file, output_path);
+  return 0;
+}
-- 
GitLab