Commit 39f9afbe authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Merge branch 'script_devel' into 'master'

Model maker for INCLUSION

See merge request giacomo.mulas/np_tmcode!111
parents 4dd3b91d cf5925cc
Loading
Loading
Loading
Loading
+5 −7
Original line number Diff line number Diff line
@@ -2082,25 +2082,23 @@ long InclusionIterationData::get_size(GeometryConfiguration *gconf, ScattererCon
int InclusionIterationData::update_orders(double **rcf, int inner_order, int outer_order) {
  int result = 0;
  int old_lm = c1->lm;
  ((ParticleDescriptorInclusion *)c1)->update_orders(inner_order, outer_order);
  for (int zi = 0; zi < old_lm; zi++) {
    for (int zj = 0; zj < 3; zj++) {
      for (int zk = 0; zk < 2; zk++) {
	delete[] zpv[zi][zj][zk];
      }
      delete[] zpv[zi][zj];
    }
    delete[] zpv[zi];
  }
  delete[] zpv;
  delete[] vec_zpv;
  ((ParticleDescriptorInclusion *)c1)->update_orders(inner_order, outer_order);
  vec_zpv = new double[c1->lm * 12]();
  zpv = new double***[c1->lm];
  for (int zi = 0; zi < c1->lm; zi++) {
    zpv[zi] = new double**[3];
    for (int zj = 0; zj < 3; zj++) {
      zpv[zi][zj] = new double*[2];
      for (int zk = 0; zk < 2; zk++) {
	zpv[zi][zj][zk] = new double[2]();
      }
      zpv[zi][zj][0] = vec_zpv + (zi * 12) + (zj * 4);
      zpv[zi][zj][1] = vec_zpv + (zi * 12) + (zj * 4) + 2;
    }
  }
  instr(rcf, c1);
+1 −1
Original line number Diff line number Diff line
@@ -1165,7 +1165,7 @@ double ScattererConfiguration::get_min_radius() {
double ScattererConfiguration::get_particle_radius(GeometryConfiguration *gc) {
  double result = 0.0;
  if (_use_external_sphere) {
    result = _radii_of_spheres[0] * _rcf[0][_nshl_vec[0] - 1];
    result = _radii_of_spheres[0] * _rcf[0][_nshl_vec[0]];
  } else {
    double dist2, max_dist;
    double max_dist2 = 0.0;
+66 −25
Original line number Diff line number Diff line
@@ -154,6 +154,7 @@ def load_model(model_file):
    except FileNotFoundError:
        print("ERROR: " + model_file + " was not found!")
    if model is not None:
        max_rad = 0.0
        make_3d = False
        try:
            make_3d = False if model['system_settings']['make_3D'] == "0" else True
@@ -215,7 +216,7 @@ def load_model(model_file):
                        print("ERROR: Declared materials in type %d do not match the number of layers!"%(i + 1))
                        return (None, None)
                    else:
                        for j in range(1 + int(sconf['nshl'][i] / 2)):
                        for j in range(expected_layer_num):
                            sconf['dielec_id'][i][j] = float(model['particle_settings']['dielec_id'][i][j])
        if (model['radiation_settings']['scale_name'] == "XI"):
            sconf['insn'] = 1
@@ -420,6 +421,11 @@ def load_model(model_file):
        gconf['vec_sph_x'] = [0.0 for i in range(gconf['nsph'])]
        gconf['vec_sph_y'] = [0.0 for i in range(gconf['nsph'])]
        gconf['vec_sph_z'] = [0.0 for i in range(gconf['nsph'])]
        gconf['n_core'] = 0
        try:
            gconf['n_core'] = int(model['particle_settings']['num_core'])
        except KeyError as ex:
            gconf['n_core'] = 1
        if (gconf['application'] != "SPHERE" or gconf['nsph'] != 1):
            len_vec_x = len(model['geometry_settings']['x_coords'])
            len_vec_y = len(model['geometry_settings']['y_coords'])
@@ -436,7 +442,6 @@ def load_model(model_file):
            if (len_vec_x == 0):
                # Generate random cluster
                rnd_seed = int(model['system_settings']['rnd_seed'])
                max_rad = 0.0
                bbox = (0.0, 0.0, 0.0)
                try:
                    max_rad = float(model['particle_settings']['max_rad'])
@@ -692,17 +697,20 @@ def print_model_summary(scatterer, geometry):
#  model is directly saved in the parameters of the scatterer and geometry
#  configuration dictionaries.
#
#  \param scatterer: `dict` Scatterer configuration dictionary (gets modified)
#  \param geometry: `dict` Geometry configuration dictionary (gets modified)
#  \param seed: `int` Seed for the random sequence generation
#  \param max_rad: `float` Maximum allowed radial extension of the aggregate
#  \param max_attempts: `int` Maximum number of attempts to place a particle in any direction
#  \param[in,out] scatterer: `dict` Scatterer configuration dictionary.
#  \param[in,out] geometry: `dict` Geometry configuration dictionary.
#  \param[in] seed: `int` Seed for the random sequence generation.
#  \param[in] max_rad: `float` Maximum allowed radial extension of the aggregate.
#  \param[in] max_attempts: `int` Maximum number of attempts to place a particle in any direction
#  (optional, defaults to 100).
#  \param[in] bbox: `tuple(float, float, float)` Particle's bounding box in meters (optional).
#  \return result: `int` Function exit code (0 for success, otherwise number of
#  spheres that could not be placed)
def random_aggregate(scatterer, geometry, seed, max_rad, max_attempts=100, bbox=(0.0, 0.0, 0.0)):
    result = 0
    random.seed(seed)
    nsph = scatterer['nsph']
    n_core = geometry['n_core']
    vec_thetas = [0.0 for i in range(nsph)]
    vec_phis = [0.0 for i in range(nsph)]
    vec_rads = [0.0 for i in range(nsph)]
@@ -728,8 +736,8 @@ def random_aggregate(scatterer, geometry, seed, max_rad, max_attempts=100, bbox=
    max_z = max_rad
    min_z = -max_rad
    if (bbox[0] > 0.0):
        max_x = bbox[0] / 2.0 + vec_rads[0]
        min_x = -bbox[0] / 2.0 + vec_rads[0]
        max_x = bbox[0] / 2.0
        min_x = -bbox[0] / 2.0
        max_y = bbox[1] / 2.0
        min_y = -bbox[1] / 2.0
        max_z = bbox[2] / 2.0
@@ -740,6 +748,8 @@ def random_aggregate(scatterer, geometry, seed, max_rad, max_attempts=100, bbox=
        vec_spheres.append({'itype': sph_type_index + 1, 'x': rho, 'y': 0.0, 'z': 0.0})
        vec_types.append(sph_type_index + 1)
        placed_spheres += 1
        if (placed_spheres <= n_core):
            recenter_aggregate(vec_spheres)
    # End of bounding box placement
    attempts = 0
    half_pi = math.pi / 2.0
@@ -824,6 +834,8 @@ def random_aggregate(scatterer, geometry, seed, max_rad, max_attempts=100, bbox=
            placed_spheres += 1
            vec_types.append(sph_type_index + 1)
            attempts = 0
            if (placed_spheres <= n_core):
                recenter_aggregate(vec_spheres)
        # end while loop not is placed loop
    # end for i loop
    scatterer['vec_types'] = vec_types
@@ -1310,29 +1322,51 @@ def write_obj(scatterer, geometry, max_rad):
        "olive", "magenta", "turquoise", "gray", "pink", "orange", "vanilla",
        "emerald"
    ]
    num_colors = len(color_names)
    used_strings = []
    used_names = []
    for ui in range(scatterer['configurations']):
        used_strings.append(color_strings[ui % (num_colors - 1) + 1])
        used_names.append(color_names[ui % (num_colors - 1) + 1])
    mtl_file = open(str(out_material_path), "w")
    for mi in range(len(color_strings)):
        mtl_line = "newmtl "  + color_names[mi] + "\n"
    for mi in range(len(used_strings)):
        mtl_line = "newmtl "  + used_names[mi] + "\n"
        mtl_file.write(mtl_line)
        color_line = used_strings[mi]
        mtl_file.write("   Ka " + color_line)
        mtl_file.write("   Ks " + color_line)
        mtl_file.write("   Kd " + color_line)
        mtl_file.write("   Ns 100.0\n")
        mtl_file.write("   d 1.0\n")
        mtl_file.write("   illum 2\n\n")
    if (scatterer['application'] == 'INCLUSION'):
        mtl_line = "newmtl coating\n"
        mtl_file.write(mtl_line)
        color_line = color_strings[mi]
        color_line = "0.16 0.38 0.37\n" # coating colour is always transparent turquoise
        mtl_file.write("   Ka " + color_line)
        mtl_file.write("   Ks " + color_line)
        mtl_file.write("   Kd " + color_line)
        mtl_file.write("   Ns 100.0\n")
        mtl_file.write("   Tr 0.0\n")
        mtl_file.write("   d 0.5\n")
        mtl_file.write("   illum 2\n\n")
    mtl_file.close()
    pl = pv.Plotter()
    for si in range(scatterer['nsph']):
        sph_type_index = scatterer['vec_types'][si]
        # color_index = 1 + (sph_type_index % (len(color_strings) - 1))
        # color_by_name = color_names[sph_type_index]
        radius = scatterer['ros'][sph_type_index - 1] / max_rad
        x = geometry['vec_sph_x'][si] / max_rad
        y = geometry['vec_sph_y'][si] / max_rad
        z = geometry['vec_sph_z'][si] / max_rad
        mesh = pv.Sphere(radius, (x, y, z))
        pl.add_mesh(mesh, color=None)
    if (scatterer['application'] == 'INCLUSION'):
        factor = scatterer['rcf'][0][-1]
        radius = factor * scatterer['ros'][0] / max_rad
        x = 0.0
        y = 0.0
        z = 0.0
        mesh = pv.Sphere(radius, (x, y, z))
        pl.add_mesh(mesh, color=None)
    pl.export_obj(str(Path(str(out_dir), "TMP_MODEL.obj")))
    tmp_model_file = open(str(Path(str(out_dir), "TMP_MODEL.obj")), "r")
    out_model_file = open(str(out_model_path), "w")
@@ -1346,17 +1380,24 @@ def write_obj(scatterer, geometry, max_rad):
            str_line = mtl_line
        elif (str_line.startswith("g ")):
            sph_index += 1
            if (sph_index <= len(scatterer['vec_types'])):
                sph_type_index = scatterer['vec_types'][sph_index - 1]
                if (sph_type_index == old_sph_type_index):
                    str_line = tmp_model_file.readline()
                    str_line = tmp_model_file.readline()
            else:
                else: # sphere type change
                    old_sph_type_index = sph_type_index
                color_index = 1 + (sph_type_index - 1) % (len(color_names) - 1)
                    color_index = (sph_type_index - 1) % len(used_names)
                    str_line = "g grp{0:04d}\n".format(sph_type_index - 1)
                    out_model_file.write(str_line)
                    str_line = tmp_model_file.readline()
                str_line = "usemtl {0:s}\n".format(color_names[color_index])
                    str_line = "usemtl {0:s}\n".format(used_names[color_index])
            else:
                if (scatterer['application'] == 'INCLUSION'):
                    str_line = "g grpcoating\n"
                    out_model_file.write(str_line)
                    str_line = tmp_model_file.readline()
                    str_line = "usemtl coating\n"
        out_model_file.write(str_line)
        str_line = tmp_model_file.readline()
    out_model_file.close()