From 49d0ff833efcb24b75964c90dc75fe4c8904d187 Mon Sep 17 00:00:00 2001
From: Giovanni La Mura <giovanni.lamura@inaf.it>
Date: Sat, 5 Apr 2025 21:59:22 +0200
Subject: [PATCH] Ensure all ScattererConfiguration building methods use
 max_layers

---
 src/libnptm/Configuration.cpp | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/libnptm/Configuration.cpp b/src/libnptm/Configuration.cpp
index 4407d111..68e07663 100644
--- a/src/libnptm/Configuration.cpp
+++ b/src/libnptm/Configuration.cpp
@@ -356,14 +356,17 @@ ScattererConfiguration::ScattererConfiguration(
   _iog_vec = iog_vector;
   _radii_of_spheres = ros_vector;
   _nshl_vec = nshl_vector;
+  _use_external_sphere = is_external;
   _max_layers = 0;
   for (int li = 0; li < _configurations; li++) {
-    if (_max_layers < _nshl_vec[li]) _max_layers = _nshl_vec[li];
+    if (_max_layers < _nshl_vec[li]) {
+      _max_layers = _nshl_vec[li];
+      if (li == 0 && _use_external_sphere) _max_layers += 1;
+    }
   }
   _rcf = rcf_vector;
   _idfc = dielectric_func_type;
   _dc0_matrix = dc_matrix;
-  _use_external_sphere = is_external;
   _exdc = ex;
   _wp = w;
   _xip = x;
@@ -719,8 +722,9 @@ ScattererConfiguration* ScattererConfiguration::from_dedfb(const std::string& de
     re = regex("[0-9]+");
     regex_search(str_target, m, re);
     nshl_vector[last_configuration - 1] = stoi(m.str());
-    if (max_layers < nshl_vector[last_configuration - 1] + 1)
-      max_layers = nshl_vector[last_configuration - 1] + 1;
+    if (max_layers < nshl_vector[last_configuration - 1])
+      max_layers = nshl_vector[last_configuration - 1];
+    if (last_configuration == 1 && fies == 1) max_layers += 1;
     str_target = m.suffix().str();
     re = regex("-?[0-9]+\\.[0-9]+([eEdD][-+]?[0-9]+)?");
     regex_search(str_target, m, re);
@@ -824,12 +828,17 @@ ScattererConfiguration* ScattererConfiguration::from_hdf5(const std::string& fil
     ros_vector = new double[configuration_count]();
     rcf_vector = new double*[configuration_count];
     last_configuration = 0;
+    int max_layers = 0;
     for (int i115 = 1; i115 <= configuration_count; i115++) {
       if (iog[i115 - 1] < i115) continue;
       else last_configuration++;
       str_name = "NSHL_" + to_string(last_configuration);
       str_type = "INT32_(1)";
       status = hdf_file->read(str_name, str_type, (nshl_vector + last_configuration - 1));
+      if (max_layers < nshl_vector[last_configuration - 1]) {
+	max_layers = nshl_vector[last_configuration - 1];
+	if (last_configuration == 1 && ies == 1) max_layers += 1;
+      }
       str_name = "ROS_" + to_string(last_configuration);
       str_type = "FLOAT64_(1)";
       status = hdf_file->read(str_name, str_type, (ros_vector + last_configuration - 1));
@@ -845,14 +854,14 @@ ScattererConfiguration* ScattererConfiguration::from_hdf5(const std::string& fil
     status = hdf_file->read(str_name, str_type, xi_vec);
       
     int dim3 = (_idfc == 0) ? nxi : 1;
-    int element_size = 2 * dim3 * nsph * configuration_count;
+    int element_size = 2 * dim3 * nsph * max_layers;
     double *elements = new double[element_size]();
     str_name = "DC0M";
     str_type = "FLOAT64_(" + to_string(element_size) + ")";
     status = hdf_file->read(str_name, str_type, elements);
-    dc0m = new dcomplex**[configuration_count];
+    dc0m = new dcomplex**[max_layers];
     int dc_index = 0;
-    for (int di = 0; di < configuration_count; di++) {
+    for (int di = 0; di < max_layers; di++) {
       dc0m[di] = new dcomplex*[nsph];
       for (int dj = 0; dj < nsph; dj++) {
 	dc0m[di][dj] = new dcomplex[dim3]();
@@ -927,8 +936,9 @@ ScattererConfiguration* ScattererConfiguration::from_legacy(const std::string& f
   int max_layers = 0;
   for (int li = 0; li < configurations; li++) {
     if (max_layers < _nshl_vec[li]) max_layers = _nshl_vec[li];
+    if (li == 0 && _ies == 1) max_layers += 1;
   }
-  _dc0m = new dcomplex**[configurations];
+  _dc0m = new dcomplex**[max_layers];
   int dim3 = (_idfc == 0) ? _nxi : 1;
   for (int di = 0; di < max_layers; di++) {
     _dc0m[di] = new dcomplex*[_nsph];
-- 
GitLab