Commit c450c6ce authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Create a model_maker python script

parent 0d69e7e7
Loading
Loading
Loading
Loading
+109 −0
Original line number Diff line number Diff line
#!/bin/python3

#   Copyright (C) 2024   INAF - Osservatorio Astronomico di Cagliari
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   A copy of the GNU General Public License is distributed along with
#   this program in the COPYING file. If not, see: <https://www.gnu.org/licenses/>.

## @package pycompare
#  \brief Script to build models from YAML configuration files

import cmath
import yaml

from sys import argv

## \brief Main execution code
#
# `main()` is the function that handles the creation of the code configuration.
# It returns an integer value as exit code, using 0 to signal successful execution.
#
# \returns result: `int` Number of detected error-level inconsistencies.
def main():
    result = 0
    if (len(argv) < 2):
        print_help()
    else:
        model = load_model(argv[1])
        if model is not None:
            result = write_sconf(model)
        else:
            print("ERROR: could not create configuration.")
            result = 1
    return result

def load_model(model_file):
    result = None
    try:
        with open(model_file, 'r') as stream:
            result = yaml.safe_load(stream)
    except yaml.YAMLError:
        print("ERROR: " + model_file + " is not a valid configuration!")
        result = None
    except FileNotFoundError:
        print("ERROR: " + model_file + " was not found!")
    return result

## \brief Print a command-line help summary.
def print_help():
    print("                                               ")
    print("***              MODEL_MAKER                ***")
    print("                                               ")
    print("Create input files for FORTRAN and C++ code.   ")
    print("                                               ")
    print("Usage: \"./model_maker.py CONFIG [OPTIONS]\"   ")
    print("                                               ")
    print("CONFIG must be a valid YAML configuration file.")
    print("                                               ")
    print("Valid options are:                             ")
    print("--help                Print this help and exit.")
    print("                                               ")

def write_sconf(model, form='legacy'):
    result = 0
    out_file = (
        model['input_settings']['input_folder'] + "/" +
        model['input_settings']['spheres_file']
    )
    #print("DEBUG: out_file = " + out_file)
    nsph = model['particle_settings']['n_spheres']
    ies = 1 if model['particle_settings']['application'] == "inclusion" else 0
    exri= float(model['material_settings']['extern_refr'])
    wp = float(model['radiation_settings']['wp'])
    xip = float(model['radiation_settings']['xip'])
    scale_start = float(model['radiation_settings']['scale_start'])
    scale_end = float(model['radiation_settings']['scale_end'])
    scale_step = float(model['radiation_settings']['scale_step'])
    idfc = int(model['radiation_settings']['diel_flag'])
    instpc = int(model['radiation_settings']['step_flag'])
    xi_flag = 3
    nxi = 1 + int((scale_end - scale_start) / scale_step)
    if form == 'legacy':
        # Write legacy output
        #print("DEBUG: writing to file.")
        output = open(out_file, 'w')
        str_line = " {0:3d}{1:3d}\n".format(nsph, ies)
        output.write(str_line)
        str_line = " {0:12.7E} {1:12.7E} {2:12.7E} {3:2d} {4:7d} {5:4d} {6:3d}\n".format(
            exri, wp, xip, idfc, nxi, instpc, xi_flag
        )
        output.write(str_line)
        output.close()
    return result

def write_gconf(model, out_file):
    return

## \brief Exit code (0 for success)
exit_code = main()
exit(exit_code)