diff --git a/SU2_PY/SU2/eval/functions.py b/SU2_PY/SU2/eval/functions.py index 759c4294767..ce000f289c2 100644 --- a/SU2_PY/SU2/eval/functions.py +++ b/SU2_PY/SU2/eval/functions.py @@ -624,9 +624,8 @@ def multipoint(config, state=None, step=1e-2): dst = os.path.abspath(dst).rstrip("/") + "/" + "DIRECT" # make unix link - string = "ln -s " + src + " " + dst - stringlist = string.split() - subprocess.Popen(stringlist) + if not os.path.exists(dst): + os.symlink(src, dst) for i in range(len(weight_list) - 1): diff --git a/SU2_PY/SU2/eval/gradients.py b/SU2_PY/SU2/eval/gradients.py index d2245c0291e..2155d60676a 100644 --- a/SU2_PY/SU2/eval/gradients.py +++ b/SU2_PY/SU2/eval/gradients.py @@ -623,9 +623,8 @@ def multipoint(func_name, config, state=None, step=1e-2): dst = os.path.abspath(dst).rstrip("/") + "/" # make unix link - string = "ln -s " + src + " " + dst - string_list = string.split() - subprocess.Popen(string_list) + if not os.path.exists(dst): + os.symlink(src, dst) for i in range(len(weight_list) - 1): @@ -736,9 +735,8 @@ def multipoint(func_name, config, state=None, step=1e-2): src = os.path.abspath(src).rstrip("/") + "/" + ztate.FILES[ADJ_NAME] # make unix link - string = "ln -s " + src + " " + dst - string_list = string.split() - subprocess.Popen(string_list) + if not os.path.exists(dst): + os.symlink(src, dst) # Update MULTPOINT_ADJOINT files in state.FILES state.FILES[MULTIPOINT_ADJ_NAME] = solution_adj_list diff --git a/SU2_PY/SU2/io/config.py b/SU2_PY/SU2/io/config.py index f429b8ceced..108f1d38bd5 100755 --- a/SU2_PY/SU2/io/config.py +++ b/SU2_PY/SU2/io/config.py @@ -329,223 +329,91 @@ def read_config(filename): data_dict = OrderedDict() input_file = open(filename) - - # process each line - while 1: - # read the line - line = input_file.readline() - if not line: - break - - # remove line returns - line = line.strip("\r\n").strip() - - if len(line) == 0: - continue - # make sure it has useful data - if line[0] == "%": - continue - - # --- Check if there is a line continuation character at the - # end of the current line or somewhere in between (the rest is ignored then). - # If yes, read until there is a line without one or an empty line. - # If there is a statement after a cont. char - # throw an error. ---*/ - - while line[0].endswith("\\") or len(line.split("\\")) > 1: - tmp_line = input_file.readline() - tmp_line = tmp_line.strip() - assert len(tmp_line.split("=")) <= 1, ( - "Statement found after line " - "continuation character in config file %s" % tmp_line - ) - if not tmp_line.startswith("%"): - line = line.split("\\")[0] - line += " " + tmp_line - - # split across equals sign - line = line.split("=", 1) - this_param = line[0].strip() - this_value = line[1].strip() - - assert this_param not in data_dict, ( - "Config file has multiple specifications of %s" % this_param - ) - for case in switch(this_param): - - # comma delimited lists of strings with or without paren's - if ( - case("MARKER_EULER") - or case("MARKER_FAR") - or case("MARKER_PLOTTING") - or case("MARKER_MONITORING") - or case("MARKER_SYM") - or case("DV_KIND") - ): - # remove white space - this_value = "".join(this_value.split()) - # remove parens - this_value = this_value.strip("()") - # split by comma - data_dict[this_param] = this_value.split(",") - break - - # semicolon delimited lists of comma delimited lists of floats - if case("DV_PARAM"): - # remove white space - info_General = "".join(this_value.split()) - # split by semicolon - info_General = info_General.split(";") - # build list of dv params, convert string to float - dv_Parameters = [] - dv_FFDTag = [] - dv_Size = [] - - for this_dvParam in info_General: - this_dvParam = this_dvParam.strip("()") - this_dvParam = this_dvParam.split(",") - this_dvSize = 1 - - # if FFD change the first element to work with numbers and float(x) - if data_dict["DV_KIND"][0] in [ - "FFD_SETTING", - "FFD_ANGLE_OF_ATTACK", - "FFD_CONTROL_POINT", - "FFD_NACELLE", - "FFD_GULL", - "FFD_TWIST", - "FFD_ROTATION", - "FFD_CAMBER", - "FFD_THICKNESS", - "FFD_CONTROL_POINT_2D", - "FFD_CAMBER_2D", - "FFD_THICKNESS_2D", - ]: - this_dvFFDTag = this_dvParam[0] - this_dvParam[0] = "0" - else: - this_dvFFDTag = [] - - if not data_dict["DV_KIND"][0] in ["NO_DEFORMATION"]: - this_dvParam = [float(x) for x in this_dvParam] - - if data_dict["DV_KIND"][0] in ["FFD_CONTROL_POINT_2D"]: - if this_dvParam[3] == 0 and this_dvParam[4] == 0: - this_dvSize = 2 - - if data_dict["DV_KIND"][0] in ["FFD_CONTROL_POINT"]: - if ( - this_dvParam[4] == 0 - and this_dvParam[5] == 0 - and this_dvParam[6] == 0 - ): - this_dvSize = 3 - - dv_FFDTag = dv_FFDTag + [this_dvFFDTag] - dv_Parameters = dv_Parameters + [this_dvParam] - dv_Size = dv_Size + [this_dvSize] - - # store in a dictionary - dv_Definitions = { - "FFDTAG": dv_FFDTag, - "PARAM": dv_Parameters, - "SIZE": dv_Size, - } - - data_dict[this_param] = dv_Definitions - break - - # comma delimited lists of floats - if case("DV_VALUE_OLD") or case("DV_VALUE_NEW") or case("DV_VALUE"): - # remove white space - this_value = "".join(this_value.split()) - # split by comma, map to float, store in dictionary - data_dict[this_param] = list(map(float, this_value.split(","))) - break - - # float parameters - if ( - case("MACH_NUMBER") - or case("AOA") - or case("FIN_DIFF_STEP") - or case("CFL_NUMBER") - or case("HB_PERIOD") - or case("WRT_SOL_FREQ") - ): - data_dict[this_param] = float(this_value) - break - - # int parameters - if ( - case("NUMBER_PART") - or case("AVAILABLE_PROC") - or case("ITER") - or case("TIME_INSTANCES") - or case("UNST_ADJOINT_ITER") - or case("ITER_AVERAGE_OBJ") - or case("INNER_ITER") - or case("OUTER_ITER") - or case("TIME_ITER") - or case("ADAPT_CYCLES") - ): - data_dict[this_param] = int(this_value) + try: + # process each line + while 1: + # read the line + line = input_file.readline() + if not line: break - if case("OUTPUT_FILES"): - data_dict[this_param] = this_value.strip("()").split(",") - data_dict[this_param] = [i.strip(" ") for i in data_dict[this_param]] - break - if case("CONFIG_LIST"): - data_dict[this_param] = this_value.strip("()").split(",") - data_dict[this_param] = [i.strip(" ") for i in data_dict[this_param]] - break - if case("HISTORY_OUTPUT"): - data_dict[this_param] = this_value.strip("()").split(",") - data_dict[this_param] = [i.strip(" ") for i in data_dict[this_param]] - break + # remove line returns + line = line.strip("\r\n").strip() + + if len(line) == 0: + continue + # make sure it has useful data + if line[0] == "%": + continue + + # --- Check if there is a line continuation character at the + # end of the current line or somewhere in between (the rest is ignored then). + # If yes, read until there is a line without one or an empty line. + # If there is a statement after a cont. char + # throw an error. ---*/ + + while line[0].endswith("\\") or len(line.split("\\")) > 1: + tmp_line = input_file.readline() + tmp_line = tmp_line.strip() + assert len(tmp_line.split("=")) <= 1, ( + "Statement found after line " + "continuation character in config file %s" % tmp_line + ) + if not tmp_line.startswith("%"): + line = line.split("\\")[0] + line += " " + tmp_line + + # split across equals sign + line = line.split("=", 1) + this_param = line[0].strip() + this_value = line[1].strip() + + assert this_param not in data_dict, ( + "Config file has multiple specifications of %s" % this_param + ) + for case in switch(this_param): + + # comma delimited lists of strings with or without paren's + if ( + case("MARKER_EULER") + or case("MARKER_FAR") + or case("MARKER_PLOTTING") + or case("MARKER_MONITORING") + or case("MARKER_SYM") + or case("DV_KIND") + ): + # remove white space + this_value = "".join(this_value.split()) + # remove parens + this_value = this_value.strip("()") + # split by comma + data_dict[this_param] = this_value.split(",") + break - # unitary design variable definition - if case("DEFINITION_DV"): - # remove white space - this_value = "".join(this_value.split()) - # split into unitary definitions - info_Unitary = this_value.split(";") - # process each Design Variable - dv_Kind = [] - dv_Scale = [] - dv_Markers = [] - dv_FFDTag = [] - dv_Parameters = [] - dv_Size = [] - - for this_General in info_Unitary: - if not this_General: - continue - # split each unitary definition into one general definition - info_General = this_General.strip("()").split( - "|" - ) # check for needed strip()? - # split information for dv Kinds - info_Kind = info_General[0].split(",") - # pull processed dv values - this_dvKind = get_dvKind(int(info_Kind[0])) - this_dvScale = float(info_Kind[1]) - this_dvMarkers = info_General[1].split(",") - this_dvSize = 1 - - if this_dvKind == "MACH_NUMBER" or this_dvKind == "AOA": - this_dvParameters = [] - else: - this_dvParameters = info_General[2].split(",") - # if FFD change the first element to work with numbers and float(x), save also the tag - if this_dvKind in [ + # semicolon delimited lists of comma delimited lists of floats + if case("DV_PARAM"): + # remove white space + info_General = "".join(this_value.split()) + # split by semicolon + info_General = info_General.split(";") + # build list of dv params, convert string to float + dv_Parameters = [] + dv_FFDTag = [] + dv_Size = [] + + for this_dvParam in info_General: + this_dvParam = this_dvParam.strip("()") + this_dvParam = this_dvParam.split(",") + this_dvSize = 1 + + # if FFD change the first element to work with numbers and float(x) + if data_dict["DV_KIND"][0] in [ "FFD_SETTING", "FFD_ANGLE_OF_ATTACK", "FFD_CONTROL_POINT", "FFD_NACELLE", "FFD_GULL", "FFD_TWIST", - "FFD_TWIST_ANGLE", "FFD_ROTATION", "FFD_CAMBER", "FFD_THICKNESS", @@ -553,361 +421,512 @@ def read_config(filename): "FFD_CAMBER_2D", "FFD_THICKNESS_2D", ]: - this_dvFFDTag = this_dvParameters[0] - this_dvParameters[0] = "0" + this_dvFFDTag = this_dvParam[0] + this_dvParam[0] = "0" else: this_dvFFDTag = [] - this_dvParameters = [float(x) for x in this_dvParameters] + if not data_dict["DV_KIND"][0] in ["NO_DEFORMATION"]: + this_dvParam = [float(x) for x in this_dvParam] - if this_dvKind in ["FFD_CONTROL_POINT_2D"]: - if this_dvParameters[3] == 0 and this_dvParameters[4] == 0: + if data_dict["DV_KIND"][0] in ["FFD_CONTROL_POINT_2D"]: + if this_dvParam[3] == 0 and this_dvParam[4] == 0: this_dvSize = 2 - if this_dvKind in ["FFD_CONTROL_POINT"]: + if data_dict["DV_KIND"][0] in ["FFD_CONTROL_POINT"]: if ( - this_dvParameters[4] == 0 - and this_dvParameters[5] == 0 - and this_dvParameters[6] == 0 + this_dvParam[4] == 0 + and this_dvParam[5] == 0 + and this_dvParam[6] == 0 ): this_dvSize = 3 - # add to lists - dv_Kind = dv_Kind + [this_dvKind] - dv_Scale = dv_Scale + [this_dvScale] - dv_Markers = dv_Markers + [this_dvMarkers] - dv_FFDTag = dv_FFDTag + [this_dvFFDTag] - dv_Parameters = dv_Parameters + [this_dvParameters] - dv_Size = dv_Size + [this_dvSize] - # store in a dictionary - dv_Definitions = { - "KIND": dv_Kind, - "SCALE": dv_Scale, - "MARKER": dv_Markers, - "FFDTAG": dv_FFDTag, - "PARAM": dv_Parameters, - "SIZE": dv_Size, - } - - # save to output dictionary - data_dict[this_param] = dv_Definitions - break + dv_FFDTag = dv_FFDTag + [this_dvFFDTag] + dv_Parameters = dv_Parameters + [this_dvParam] + dv_Size = dv_Size + [this_dvSize] - # unitary objective definition - if case("OPT_OBJECTIVE"): - # remove white space - this_value = "".join(this_value.split()) - # split by ; - this_def = OrderedDict() - this_value = this_value.split(";") - - for this_obj in this_value: - # split by scale - this_obj = this_obj.split("*") - this_name = this_obj[0] - this_scale = 1.0 - if len(this_obj) > 1: - this_scale = float(this_obj[1]) - # check for penalty-based constraint function - for this_sgn in ["<", ">", "="]: - if this_sgn in this_name: - break - this_obj = this_name.strip("()").split(this_sgn) - if len(this_obj) > 1: - this_type = this_sgn - this_val = this_obj[1] - else: - this_type = "DEFAULT" - this_val = 0.0 - this_name = this_obj[0] - # Print an error and exit if the same key appears twice - if this_name in this_def: - raise SystemExit( - "Multiple occurrences of the same objective in the OPT_OBJECTIVE definition are not currently supported. To evaluate one objective over multiple surfaces, list the objective once." - ) - # Set up dict for objective, including scale, whether it is a penalty, and constraint value - this_def.update( - { - this_name: { - "SCALE": this_scale, - "OBJTYPE": this_type, - "VALUE": this_val, + # store in a dictionary + dv_Definitions = { + "FFDTAG": dv_FFDTag, + "PARAM": dv_Parameters, + "SIZE": dv_Size, + } + + data_dict[this_param] = dv_Definitions + break + + # comma delimited lists of floats + if case("DV_VALUE_OLD") or case("DV_VALUE_NEW") or case("DV_VALUE"): + # remove white space + this_value = "".join(this_value.split()) + # split by comma, map to float, store in dictionary + data_dict[this_param] = list(map(float, this_value.split(","))) + break + + # float parameters + if ( + case("MACH_NUMBER") + or case("AOA") + or case("FIN_DIFF_STEP") + or case("CFL_NUMBER") + or case("HB_PERIOD") + or case("WRT_SOL_FREQ") + ): + data_dict[this_param] = float(this_value) + break + + # int parameters + if ( + case("NUMBER_PART") + or case("AVAILABLE_PROC") + or case("ITER") + or case("TIME_INSTANCES") + or case("UNST_ADJOINT_ITER") + or case("ITER_AVERAGE_OBJ") + or case("INNER_ITER") + or case("OUTER_ITER") + or case("TIME_ITER") + or case("ADAPT_CYCLES") + ): + data_dict[this_param] = int(this_value) + break + + if case("OUTPUT_FILES"): + data_dict[this_param] = this_value.strip("()").split(",") + data_dict[this_param] = [ + i.strip(" ") for i in data_dict[this_param] + ] + break + if case("CONFIG_LIST"): + data_dict[this_param] = this_value.strip("()").split(",") + data_dict[this_param] = [ + i.strip(" ") for i in data_dict[this_param] + ] + break + if case("HISTORY_OUTPUT"): + data_dict[this_param] = this_value.strip("()").split(",") + data_dict[this_param] = [ + i.strip(" ") for i in data_dict[this_param] + ] + break + + # unitary design variable definition + if case("DEFINITION_DV"): + # remove white space + this_value = "".join(this_value.split()) + # split into unitary definitions + info_Unitary = this_value.split(";") + # process each Design Variable + dv_Kind = [] + dv_Scale = [] + dv_Markers = [] + dv_FFDTag = [] + dv_Parameters = [] + dv_Size = [] + + for this_General in info_Unitary: + if not this_General: + continue + # split each unitary definition into one general definition + info_General = this_General.strip("()").split( + "|" + ) # check for needed strip()? + # split information for dv Kinds + info_Kind = info_General[0].split(",") + # pull processed dv values + this_dvKind = get_dvKind(int(info_Kind[0])) + this_dvScale = float(info_Kind[1]) + this_dvMarkers = info_General[1].split(",") + this_dvSize = 1 + + if this_dvKind == "MACH_NUMBER" or this_dvKind == "AOA": + this_dvParameters = [] + else: + this_dvParameters = info_General[2].split(",") + # if FFD change the first element to work with numbers and float(x), save also the tag + if this_dvKind in [ + "FFD_SETTING", + "FFD_ANGLE_OF_ATTACK", + "FFD_CONTROL_POINT", + "FFD_NACELLE", + "FFD_GULL", + "FFD_TWIST", + "FFD_TWIST_ANGLE", + "FFD_ROTATION", + "FFD_CAMBER", + "FFD_THICKNESS", + "FFD_CONTROL_POINT_2D", + "FFD_CAMBER_2D", + "FFD_THICKNESS_2D", + ]: + this_dvFFDTag = this_dvParameters[0] + this_dvParameters[0] = "0" + else: + this_dvFFDTag = [] + + this_dvParameters = [float(x) for x in this_dvParameters] + + if this_dvKind in ["FFD_CONTROL_POINT_2D"]: + if ( + this_dvParameters[3] == 0 + and this_dvParameters[4] == 0 + ): + this_dvSize = 2 + + if this_dvKind in ["FFD_CONTROL_POINT"]: + if ( + this_dvParameters[4] == 0 + and this_dvParameters[5] == 0 + and this_dvParameters[6] == 0 + ): + this_dvSize = 3 + + # add to lists + dv_Kind = dv_Kind + [this_dvKind] + dv_Scale = dv_Scale + [this_dvScale] + dv_Markers = dv_Markers + [this_dvMarkers] + dv_FFDTag = dv_FFDTag + [this_dvFFDTag] + dv_Parameters = dv_Parameters + [this_dvParameters] + dv_Size = dv_Size + [this_dvSize] + # store in a dictionary + dv_Definitions = { + "KIND": dv_Kind, + "SCALE": dv_Scale, + "MARKER": dv_Markers, + "FFDTAG": dv_FFDTag, + "PARAM": dv_Parameters, + "SIZE": dv_Size, + } + + # save to output dictionary + data_dict[this_param] = dv_Definitions + break + + # unitary objective definition + if case("OPT_OBJECTIVE"): + # remove white space + this_value = "".join(this_value.split()) + # split by ; + this_def = OrderedDict() + this_value = this_value.split(";") + + for this_obj in this_value: + # split by scale + this_obj = this_obj.split("*") + this_name = this_obj[0] + this_scale = 1.0 + if len(this_obj) > 1: + this_scale = float(this_obj[1]) + # check for penalty-based constraint function + for this_sgn in ["<", ">", "="]: + if this_sgn in this_name: + break + this_obj = this_name.strip("()").split(this_sgn) + if len(this_obj) > 1: + this_type = this_sgn + this_val = this_obj[1] + else: + this_type = "DEFAULT" + this_val = 0.0 + this_name = this_obj[0] + # Print an error and exit if the same key appears twice + if this_name in this_def: + raise SystemExit( + "Multiple occurrences of the same objective in the OPT_OBJECTIVE definition are not currently supported. To evaluate one objective over multiple surfaces, list the objective once." + ) + # Set up dict for objective, including scale, whether it is a penalty, and constraint value + this_def.update( + { + this_name: { + "SCALE": this_scale, + "OBJTYPE": this_type, + "VALUE": this_val, + } } + ) + # OPT_OBJECTIVE has to appear after MARKER_MONITORING in the .cfg, maybe catch that here + if len(data_dict["MARKER_MONITORING"]) > 1: + this_def[this_name]["MARKER"] = data_dict[ + "MARKER_MONITORING" + ][len(this_def) - 1] + else: + this_def[this_name]["MARKER"] = data_dict[ + "MARKER_MONITORING" + ][0] + + # save to output dictionary + data_dict[this_param] = this_def + break + + # unitary constraint definition + if case("OPT_CONSTRAINT"): + # remove white space + this_value = "".join(this_value.split()) + # check for none case + if this_value == "NONE": + data_dict[this_param] = { + "EQUALITY": OrderedDict(), + "INEQUALITY": OrderedDict(), } - ) - # OPT_OBJECTIVE has to appear after MARKER_MONITORING in the .cfg, maybe catch that here - if len(data_dict["MARKER_MONITORING"]) > 1: - this_def[this_name]["MARKER"] = data_dict["MARKER_MONITORING"][ - len(this_def) - 1 - ] - else: - this_def[this_name]["MARKER"] = data_dict["MARKER_MONITORING"][ - 0 - ] - - # save to output dictionary - data_dict[this_param] = this_def - break + break + # split definitions + this_value = this_value.split(";") + this_def = OrderedDict() + for this_con in this_value: + if not this_con: + continue # if no definition + # defaults + this_obj = "NONE" + this_sgn = "=" + this_scl = 1.0 + this_val = 0.0 + # split scale if present + this_con = this_con.split("*") + if len(this_con) > 1: + this_scl = float(this_con[1]) + this_con = this_con[0] + # find sign + for this_sgn in ["<", ">", "="]: + if this_sgn in this_con: + break + # split sign, store objective and value + this_con = this_con.strip("()").split(this_sgn) + assert len(this_con) == 2, "incorrect constraint definition" + this_obj = this_con[0] + this_val = float(this_con[1]) + # store in dictionary + this_def[this_obj] = { + "SIGN": this_sgn, + "VALUE": this_val, + "SCALE": this_scl, + } + #: for each constraint definition + # sort constraints by type + this_sort = {"EQUALITY": OrderedDict(), "INEQUALITY": OrderedDict()} + for key, value in this_def.items(): + if value["SIGN"] == "=": + this_sort["EQUALITY"][key] = value + else: + this_sort["INEQUALITY"][key] = value + #: for each definition + # save to output dictionary + data_dict[this_param] = this_sort + break - # unitary constraint definition - if case("OPT_CONSTRAINT"): - # remove white space - this_value = "".join(this_value.split()) - # check for none case - if this_value == "NONE": - data_dict[this_param] = { - "EQUALITY": OrderedDict(), - "INEQUALITY": OrderedDict(), - } + # otherwise + # string parameters + if case(): + data_dict[this_param] = this_value break - # split definitions - this_value = this_value.split(";") - this_def = OrderedDict() - for this_con in this_value: - if not this_con: - continue # if no definition - # defaults - this_obj = "NONE" - this_sgn = "=" - this_scl = 1.0 - this_val = 0.0 - # split scale if present - this_con = this_con.split("*") - if len(this_con) > 1: - this_scl = float(this_con[1]) - this_con = this_con[0] - # find sign - for this_sgn in ["<", ">", "="]: - if this_sgn in this_con: - break - # split sign, store objective and value - this_con = this_con.strip("()").split(this_sgn) - assert len(this_con) == 2, "incorrect constraint definition" - this_obj = this_con[0] - this_val = float(this_con[1]) - # store in dictionary - this_def[this_obj] = { - "SIGN": this_sgn, - "VALUE": this_val, - "SCALE": this_scl, - } - #: for each constraint definition - # sort constraints by type - this_sort = {"EQUALITY": OrderedDict(), "INEQUALITY": OrderedDict()} - for key, value in this_def.items(): - if value["SIGN"] == "=": - this_sort["EQUALITY"][key] = value - else: - this_sort["INEQUALITY"][key] = value - #: for each definition - # save to output dictionary - data_dict[this_param] = this_sort - break - # otherwise - # string parameters - if case(): - data_dict[this_param] = this_value - break + #: if case DEFINITION_DV + + #: for case - #: if case DEFINITION_DV - - #: for case - - #: for line - - if "OPT_CONSTRAINT" in data_dict: - if ( - "BUFFET" in data_dict["OPT_CONSTRAINT"]["EQUALITY"] - or "BUFFET" in data_dict["OPT_CONSTRAINT"]["INEQUALITY"] - ): - data_dict["BUFFET_MONITORING"] = "YES" - - if "OPT_OBJECTIVE" in data_dict: - if "BUFFET" in data_dict["OPT_OBJECTIVE"]: - data_dict["BUFFET_MONITORING"] = "YES" - - # hack - twl - if "DV_VALUE_NEW" not in data_dict: - data_dict["DV_VALUE_NEW"] = data_dict.get("DV_VALUE", [0]) - if "DV_VALUE_OLD" not in data_dict: - data_dict["DV_VALUE_OLD"] = data_dict.get("DV_VALUE", [0]) - if "OPT_ITERATIONS" not in data_dict: - data_dict["OPT_ITERATIONS"] = 100 - if "OPT_ACCURACY" not in data_dict: - data_dict["OPT_ACCURACY"] = 1e-10 - if "OPT_RELAX_FACTOR" not in data_dict: - data_dict["OPT_RELAX_FACTOR"] = 1.0 - if "OPT_GRADIENT_FACTOR" not in data_dict: - data_dict["OPT_GRADIENT_FACTOR"] = 1.0 - if "OPT_BOUND_UPPER" not in data_dict: - data_dict["OPT_BOUND_UPPER"] = 1e10 - if "OPT_BOUND_LOWER" not in data_dict: - data_dict["OPT_BOUND_LOWER"] = -1e10 - if "OPT_COMBINE_OBJECTIVE" not in data_dict: - data_dict["OPT_COMBINE_OBJECTIVE"] = "NO" - if "OPT_CONSTRAINT" not in data_dict: - data_dict["OPT_CONSTRAINT"] = { - "INEQUALITY": OrderedDict(), - "EQUALITY": OrderedDict(), - } - if "VALUE_OBJFUNC_FILENAME" not in data_dict: - data_dict["VALUE_OBJFUNC_FILENAME"] = "of_eval" - if "GRAD_OBJFUNC_FILENAME" not in data_dict: - data_dict["GRAD_OBJFUNC_FILENAME"] = "of_grad" - if "AOA" not in data_dict: - data_dict["AOA"] = 0.0 - if "SIDESLIP_ANGLE" not in data_dict: - data_dict["SIDESLIP_ANGLE"] = 0.0 - if "MACH_NUMBER" not in data_dict: - data_dict["MACH_NUMBER"] = 0.0 - if "REYNOLDS_NUMBER" not in data_dict: - data_dict["REYNOLDS_NUMBER"] = 0.0 - if "TARGET_CL" not in data_dict: - data_dict["TARGET_CL"] = 0.0 - if "FREESTREAM_PRESSURE" not in data_dict: - data_dict["FREESTREAM_PRESSURE"] = 101325.0 - if "FREESTREAM_TEMPERATURE" not in data_dict: - data_dict["FREESTREAM_TEMPERATURE"] = 288.15 - if "MARKER_OUTLET" not in data_dict: - data_dict["MARKER_OUTLET"] = "(NONE)" - - # - # Multipoints requires some particular default values - # - multipoints = 1 - if "MULTIPOINT_WEIGHT" not in data_dict: - data_dict["MULTIPOINT_WEIGHT"] = "(1.0)" + #: for line + + if "OPT_CONSTRAINT" in data_dict: + if ( + "BUFFET" in data_dict["OPT_CONSTRAINT"]["EQUALITY"] + or "BUFFET" in data_dict["OPT_CONSTRAINT"]["INEQUALITY"] + ): + data_dict["BUFFET_MONITORING"] = "YES" + + if "OPT_OBJECTIVE" in data_dict: + if "BUFFET" in data_dict["OPT_OBJECTIVE"]: + data_dict["BUFFET_MONITORING"] = "YES" + + # hack - twl + if "DV_VALUE_NEW" not in data_dict: + data_dict["DV_VALUE_NEW"] = data_dict.get("DV_VALUE", [0]) + if "DV_VALUE_OLD" not in data_dict: + data_dict["DV_VALUE_OLD"] = data_dict.get("DV_VALUE", [0]) + if "OPT_ITERATIONS" not in data_dict: + data_dict["OPT_ITERATIONS"] = 100 + if "OPT_ACCURACY" not in data_dict: + data_dict["OPT_ACCURACY"] = 1e-10 + if "OPT_RELAX_FACTOR" not in data_dict: + data_dict["OPT_RELAX_FACTOR"] = 1.0 + if "OPT_GRADIENT_FACTOR" not in data_dict: + data_dict["OPT_GRADIENT_FACTOR"] = 1.0 + if "OPT_BOUND_UPPER" not in data_dict: + data_dict["OPT_BOUND_UPPER"] = 1e10 + if "OPT_BOUND_LOWER" not in data_dict: + data_dict["OPT_BOUND_LOWER"] = -1e10 + if "OPT_COMBINE_OBJECTIVE" not in data_dict: + data_dict["OPT_COMBINE_OBJECTIVE"] = "NO" + if "OPT_CONSTRAINT" not in data_dict: + data_dict["OPT_CONSTRAINT"] = { + "INEQUALITY": OrderedDict(), + "EQUALITY": OrderedDict(), + } + if "VALUE_OBJFUNC_FILENAME" not in data_dict: + data_dict["VALUE_OBJFUNC_FILENAME"] = "of_eval" + if "GRAD_OBJFUNC_FILENAME" not in data_dict: + data_dict["GRAD_OBJFUNC_FILENAME"] = "of_grad" + if "AOA" not in data_dict: + data_dict["AOA"] = 0.0 + if "SIDESLIP_ANGLE" not in data_dict: + data_dict["SIDESLIP_ANGLE"] = 0.0 + if "MACH_NUMBER" not in data_dict: + data_dict["MACH_NUMBER"] = 0.0 + if "REYNOLDS_NUMBER" not in data_dict: + data_dict["REYNOLDS_NUMBER"] = 0.0 + if "TARGET_CL" not in data_dict: + data_dict["TARGET_CL"] = 0.0 + if "FREESTREAM_PRESSURE" not in data_dict: + data_dict["FREESTREAM_PRESSURE"] = 101325.0 + if "FREESTREAM_TEMPERATURE" not in data_dict: + data_dict["FREESTREAM_TEMPERATURE"] = 288.15 + if "MARKER_OUTLET" not in data_dict: + data_dict["MARKER_OUTLET"] = "(NONE)" + + # + # Multipoints requires some particular default values + # multipoints = 1 - else: - multipoints = len( - data_dict["MULTIPOINT_WEIGHT"].replace("(", "").replace(")", "").split(",") - ) - - if "MULTIPOINT_MACH_NUMBER" not in data_dict: - Mach_Value = data_dict["MACH_NUMBER"] - Mach_List = "(" - for i in range(multipoints): - if i != 0: - Mach_List += ", " - Mach_List += str(Mach_Value) - Mach_List += ")" - data_dict["MULTIPOINT_MACH_NUMBER"] = Mach_List - - if "MULTIPOINT_AOA" not in data_dict: - Alpha_Value = data_dict["AOA"] - Alpha_List = "(" - for i in range(multipoints): - if i != 0: - Alpha_List += ", " - Alpha_List += str(Alpha_Value) - Alpha_List += ")" - data_dict["MULTIPOINT_AOA"] = Alpha_List - - if "MULTIPOINT_SIDESLIP_ANGLE" not in data_dict: - Beta_Value = data_dict["SIDESLIP_ANGLE"] - Beta_List = "(" - for i in range(multipoints): - if i != 0: - Beta_List += ", " - Beta_List += str(Beta_Value) - Beta_List += ")" - data_dict["MULTIPOINT_SIDESLIP_ANGLE"] = Beta_List - - if "MULTIPOINT_REYNOLDS_NUMBER" not in data_dict: - Reynolds_Value = data_dict["REYNOLDS_NUMBER"] - Reynolds_List = "(" - for i in range(multipoints): - if i != 0: - Reynolds_List += ", " - Reynolds_List += str(Reynolds_Value) - Reynolds_List += ")" - data_dict["MULTIPOINT_REYNOLDS_NUMBER"] = Reynolds_List - - if "MULTIPOINT_TARGET_CL" not in data_dict: - TargetCLValue = data_dict["TARGET_CL"] - TargetCL_List = "(" - for i in range(multipoints): - if i != 0: - TargetCL_List += ", " - TargetCL_List += str(TargetCLValue) - TargetCL_List += ")" - data_dict["MULTIPOINT_TARGET_CL"] = TargetCL_List - - if "MULTIPOINT_FREESTREAM_PRESSURE" not in data_dict: - Pressure_Value = data_dict["FREESTREAM_PRESSURE"] - Pressure_List = "(" - for i in range(multipoints): - if i != 0: - Pressure_List += ", " - Pressure_List += str(Pressure_Value) - Pressure_List += ")" - data_dict["MULTIPOINT_FREESTREAM_PRESSURE"] = Pressure_List - - if "MULTIPOINT_FREESTREAM_TEMPERATURE" not in data_dict: - Temperature_Value = data_dict["FREESTREAM_TEMPERATURE"] - Temperature_List = "(" - for i in range(multipoints): - if i != 0: - Temperature_List += ", " - Temperature_List += str(Temperature_Value) - Temperature_List += ")" - data_dict["MULTIPOINT_FREESTREAM_TEMPERATURE"] = Temperature_List - - if "MULTIPOINT_OUTLET_VALUE" not in data_dict: - if "NONE" in data_dict["MARKER_OUTLET"]: - Outlet_Value = 0.0 + if "MULTIPOINT_WEIGHT" not in data_dict: + data_dict["MULTIPOINT_WEIGHT"] = "(1.0)" + multipoints = 1 else: - Outlet_Value = ( - data_dict["MARKER_OUTLET"] + multipoints = len( + data_dict["MULTIPOINT_WEIGHT"] .replace("(", "") .replace(")", "") - .split(",")[1] + .split(",") ) - Outlet_Value_List = "(" - for i in range(multipoints): - if i != 0: - Outlet_Value_List += ", " - Outlet_Value_List += str(Outlet_Value) - Outlet_Value_List += ")" - data_dict["MULTIPOINT_OUTLET_VALUE"] = Outlet_Value_List - - if "MULTIPOINT_MESH_FILENAME" not in data_dict: - Mesh_Filename = data_dict["MESH_FILENAME"] - Mesh_List = "(" - for i in range(multipoints): - if i != 0: - Mesh_List += ", " - Mesh_List += str(Mesh_Filename) - Mesh_List += ")" - data_dict["MULTIPOINT_MESH_FILENAME"] = Mesh_List - - if "HISTORY_OUTPUT" not in data_dict: - data_dict["HISTORY_OUTPUT"] = ["ITER", "RMS_RES"] - - # - # Default values for optimization parameters (needed for some eval functions - # that can be called outside of an opt. context. - # - if "OBJECTIVE_FUNCTION" not in data_dict: - data_dict["OBJECTIVE_FUNCTION"] = "DRAG" - if "DV_KIND" not in data_dict: - data_dict["DV_KIND"] = ["FFD_SETTING"] - if "DV_PARAM" not in data_dict: - data_dict["DV_PARAM"] = {"FFDTAG": ["1"], "PARAM": [[0.0, 0.5]], "SIZE": [1]} - if "DEFINITION_DV" not in data_dict: - data_dict["DEFINITION_DV"] = { - "FFDTAG": [[]], - "KIND": ["HICKS_HENNE"], - "MARKER": [["WING"]], - "PARAM": [[0.0, 0.05]], - "SCALE": [1.0], - "SIZE": [1], - } - if "VALUE_OBJFUNC_FILENAME" not in data_dict: - data_dict["VALUE_OBJFUNC_FILENAME"] = "of_eval" - if "GRAD_OBJFUNC_FILENAME" not in data_dict: - data_dict["GRAD_OBJFUNC_FILENAME"] = "of_grad" + + if "MULTIPOINT_MACH_NUMBER" not in data_dict: + Mach_Value = data_dict["MACH_NUMBER"] + Mach_List = "(" + for i in range(multipoints): + if i != 0: + Mach_List += ", " + Mach_List += str(Mach_Value) + Mach_List += ")" + data_dict["MULTIPOINT_MACH_NUMBER"] = Mach_List + + if "MULTIPOINT_AOA" not in data_dict: + Alpha_Value = data_dict["AOA"] + Alpha_List = "(" + for i in range(multipoints): + if i != 0: + Alpha_List += ", " + Alpha_List += str(Alpha_Value) + Alpha_List += ")" + data_dict["MULTIPOINT_AOA"] = Alpha_List + + if "MULTIPOINT_SIDESLIP_ANGLE" not in data_dict: + Beta_Value = data_dict["SIDESLIP_ANGLE"] + Beta_List = "(" + for i in range(multipoints): + if i != 0: + Beta_List += ", " + Beta_List += str(Beta_Value) + Beta_List += ")" + data_dict["MULTIPOINT_SIDESLIP_ANGLE"] = Beta_List + + if "MULTIPOINT_REYNOLDS_NUMBER" not in data_dict: + Reynolds_Value = data_dict["REYNOLDS_NUMBER"] + Reynolds_List = "(" + for i in range(multipoints): + if i != 0: + Reynolds_List += ", " + Reynolds_List += str(Reynolds_Value) + Reynolds_List += ")" + data_dict["MULTIPOINT_REYNOLDS_NUMBER"] = Reynolds_List + + if "MULTIPOINT_TARGET_CL" not in data_dict: + TargetCLValue = data_dict["TARGET_CL"] + TargetCL_List = "(" + for i in range(multipoints): + if i != 0: + TargetCL_List += ", " + TargetCL_List += str(TargetCLValue) + TargetCL_List += ")" + data_dict["MULTIPOINT_TARGET_CL"] = TargetCL_List + + if "MULTIPOINT_FREESTREAM_PRESSURE" not in data_dict: + Pressure_Value = data_dict["FREESTREAM_PRESSURE"] + Pressure_List = "(" + for i in range(multipoints): + if i != 0: + Pressure_List += ", " + Pressure_List += str(Pressure_Value) + Pressure_List += ")" + data_dict["MULTIPOINT_FREESTREAM_PRESSURE"] = Pressure_List + + if "MULTIPOINT_FREESTREAM_TEMPERATURE" not in data_dict: + Temperature_Value = data_dict["FREESTREAM_TEMPERATURE"] + Temperature_List = "(" + for i in range(multipoints): + if i != 0: + Temperature_List += ", " + Temperature_List += str(Temperature_Value) + Temperature_List += ")" + data_dict["MULTIPOINT_FREESTREAM_TEMPERATURE"] = Temperature_List + + if "MULTIPOINT_OUTLET_VALUE" not in data_dict: + if "NONE" in data_dict["MARKER_OUTLET"]: + Outlet_Value = 0.0 + else: + Outlet_Value = ( + data_dict["MARKER_OUTLET"] + .replace("(", "") + .replace(")", "") + .split(",")[1] + ) + Outlet_Value_List = "(" + for i in range(multipoints): + if i != 0: + Outlet_Value_List += ", " + Outlet_Value_List += str(Outlet_Value) + Outlet_Value_List += ")" + data_dict["MULTIPOINT_OUTLET_VALUE"] = Outlet_Value_List + + if "MULTIPOINT_MESH_FILENAME" not in data_dict: + Mesh_Filename = data_dict["MESH_FILENAME"] + Mesh_List = "(" + for i in range(multipoints): + if i != 0: + Mesh_List += ", " + Mesh_List += str(Mesh_Filename) + Mesh_List += ")" + data_dict["MULTIPOINT_MESH_FILENAME"] = Mesh_List + + if "HISTORY_OUTPUT" not in data_dict: + data_dict["HISTORY_OUTPUT"] = ["ITER", "RMS_RES"] + + # + # Default values for optimization parameters (needed for some eval functions + # that can be called outside of an opt. context. + # + if "OBJECTIVE_FUNCTION" not in data_dict: + data_dict["OBJECTIVE_FUNCTION"] = "DRAG" + if "DV_KIND" not in data_dict: + data_dict["DV_KIND"] = ["FFD_SETTING"] + if "DV_PARAM" not in data_dict: + data_dict["DV_PARAM"] = { + "FFDTAG": ["1"], + "PARAM": [[0.0, 0.5]], + "SIZE": [1], + } + if "DEFINITION_DV" not in data_dict: + data_dict["DEFINITION_DV"] = { + "FFDTAG": [[]], + "KIND": ["HICKS_HENNE"], + "MARKER": [["WING"]], + "PARAM": [[0.0, 0.05]], + "SCALE": [1.0], + "SIZE": [1], + } + if "VALUE_OBJFUNC_FILENAME" not in data_dict: + data_dict["VALUE_OBJFUNC_FILENAME"] = "of_eval" + if "GRAD_OBJFUNC_FILENAME" not in data_dict: + data_dict["GRAD_OBJFUNC_FILENAME"] = "of_grad" + + finally: + input_file.close() return data_dict @@ -925,288 +944,298 @@ def write_config(filename, param_dict): temp_filename = filename + "_tmp" shutil.copy(filename, temp_filename) - output_file = open(filename, "w") # break pointers param_dict = copy.deepcopy(param_dict) - for raw_line in open(temp_filename): - # remove line returns - line = raw_line.strip("\r\n") - - # make sure it has useful data - if not "=" in line: - output_file.write(raw_line) - continue - - # split across equals sign - line = line.split("=") - this_param = line[0].strip() - old_value = line[1].strip() - - # skip if parameter unwanted - if this_param not in param_dict: - output_file.write(raw_line) - continue - - # start writing parameter - new_value = param_dict[this_param] - output_file.write(this_param + "= ") - - # handle parameter types - for case in switch(this_param): - - # comma delimited list of floats - if case("DV_VALUE_NEW"): - pass - if case("DV_VALUE_OLD"): - pass - if case("DV_VALUE"): - n_lists = len(new_value) - for i_value in range(n_lists): - output_file.write("%s" % new_value[i_value]) - if i_value + 1 < n_lists: - output_file.write(", ") - break - - # comma delimited list of strings no paren's - if case("DV_KIND"): - pass - if case("TASKS"): - pass - if case("GRADIENTS"): - if not isinstance(new_value, list): - new_value = [new_value] - n_lists = len(new_value) - for i_value in range(n_lists): - output_file.write(new_value[i_value]) - if i_value + 1 < n_lists: - output_file.write(", ") - break - - # comma delimited list of strings inside paren's - if case("MARKER_EULER"): - pass - if case("MARKER_FAR"): - pass - if case("MARKER_PLOTTING"): - pass - if case("MARKER_MONITORING"): - pass - if case("MARKER_SYM"): - pass - if case("DV_MARKER"): - if not isinstance(new_value, list): - new_value = [new_value] - output_file.write("( ") - n_lists = len(new_value) - for i_value in range(n_lists): - output_file.write(new_value[i_value]) - if i_value + 1 < n_lists: - output_file.write(", ") - output_file.write(" )") - break - if case("OUTPUT_FILES"): - n_lists = len(new_value) - output_file.write("(") - for i_value in range(n_lists): - output_file.write(new_value[i_value]) - if i_value + 1 < n_lists: - output_file.write(", ") - output_file.write(")") - break + output_file = open(filename, "w") + temp_file = open(temp_filename) + try: + for raw_line in temp_file: + # remove line returns + line = raw_line.strip("\r\n") + + # make sure it has useful data + if not "=" in line: + output_file.write(raw_line) + continue + + # split across equals sign + line = line.split("=") + this_param = line[0].strip() + old_value = line[1].strip() + + # skip if parameter unwanted + if this_param not in param_dict: + output_file.write(raw_line) + continue + + # start writing parameter + new_value = param_dict[this_param] + output_file.write(this_param + "= ") + + # handle parameter types + for case in switch(this_param): + + # comma delimited list of floats + if case("DV_VALUE_NEW"): + pass + if case("DV_VALUE_OLD"): + pass + if case("DV_VALUE"): + n_lists = len(new_value) + for i_value in range(n_lists): + output_file.write("%s" % new_value[i_value]) + if i_value + 1 < n_lists: + output_file.write(", ") + break - if case("CONFIG_LIST"): - n_lists = len(new_value) - output_file.write("(") - for i_value in range(n_lists): - output_file.write(new_value[i_value]) - if i_value + 1 < n_lists: - output_file.write(", ") - output_file.write(")") - break + # comma delimited list of strings no paren's + if case("DV_KIND"): + pass + if case("TASKS"): + pass + if case("GRADIENTS"): + if not isinstance(new_value, list): + new_value = [new_value] + n_lists = len(new_value) + for i_value in range(n_lists): + output_file.write(new_value[i_value]) + if i_value + 1 < n_lists: + output_file.write(", ") + break - if case("HISTORY_OUTPUT"): - n_lists = len(new_value) - for i_value in range(n_lists): - output_file.write(new_value[i_value]) - if i_value + 1 < n_lists: - output_file.write(", ") - break + # comma delimited list of strings inside paren's + if case("MARKER_EULER"): + pass + if case("MARKER_FAR"): + pass + if case("MARKER_PLOTTING"): + pass + if case("MARKER_MONITORING"): + pass + if case("MARKER_SYM"): + pass + if case("DV_MARKER"): + if not isinstance(new_value, list): + new_value = [new_value] + output_file.write("( ") + n_lists = len(new_value) + for i_value in range(n_lists): + output_file.write(new_value[i_value]) + if i_value + 1 < n_lists: + output_file.write(", ") + output_file.write(" )") + break + if case("OUTPUT_FILES"): + n_lists = len(new_value) + output_file.write("(") + for i_value in range(n_lists): + output_file.write(new_value[i_value]) + if i_value + 1 < n_lists: + output_file.write(", ") + output_file.write(")") + break - # semicolon delimited lists of comma delimited lists - if case("DV_PARAM"): + if case("CONFIG_LIST"): + n_lists = len(new_value) + output_file.write("(") + for i_value in range(n_lists): + output_file.write(new_value[i_value]) + if i_value + 1 < n_lists: + output_file.write(", ") + output_file.write(")") + break - assert isinstance( - new_value["PARAM"], list - ), "incorrect specification of DV_PARAM" - if not isinstance(new_value["PARAM"][0], list): - new_value = [new_value] + if case("HISTORY_OUTPUT"): + n_lists = len(new_value) + for i_value in range(n_lists): + output_file.write(new_value[i_value]) + if i_value + 1 < n_lists: + output_file.write(", ") + break - for i_value in range(len(new_value["PARAM"])): + # semicolon delimited lists of comma delimited lists + if case("DV_PARAM"): - output_file.write("( ") - this_param_list = new_value["PARAM"][i_value] - this_ffd_list = new_value["FFDTAG"][i_value] - n_lists = len(this_param_list) - - if this_ffd_list != []: - output_file.write("%s, " % this_ffd_list) - for j_value in range(1, n_lists): - output_file.write("%s" % this_param_list[j_value]) - if j_value + 1 < n_lists: - output_file.write(", ") - else: - for j_value in range(n_lists): - output_file.write("%s" % this_param_list[j_value]) - if j_value + 1 < n_lists: - output_file.write(", ") + assert isinstance( + new_value["PARAM"], list + ), "incorrect specification of DV_PARAM" + if not isinstance(new_value["PARAM"][0], list): + new_value = [new_value] - output_file.write(") ") - if i_value + 1 < len(new_value["PARAM"]): - output_file.write("; ") - break + for i_value in range(len(new_value["PARAM"])): - # int parameters - if case("NUMBER_PART"): - pass - if case("ADAPT_CYCLES"): - pass - if case("TIME_INSTANCES"): - pass - if case("AVAILABLE_PROC"): - pass - if case("UNST_ADJOINT_ITER"): - pass - if ( - case("ITER") - or case("TIME_ITER") - or case("INNER_ITER") - or case("OUTER_ITER") - ): - output_file.write("%i" % new_value) - break + output_file.write("( ") + this_param_list = new_value["PARAM"][i_value] + this_ffd_list = new_value["FFDTAG"][i_value] + n_lists = len(this_param_list) - if case("DEFINITION_DV"): - n_dv = len(new_value["KIND"]) - if not n_dv: - output_file.write("NONE") - for i_dv in range(n_dv): - this_kind = new_value["KIND"][i_dv] - output_file.write("( ") - output_file.write("%i , " % get_dvID(this_kind)) - output_file.write("%s " % new_value["SCALE"][i_dv]) - output_file.write("| ") - # markers - n_mark = len(new_value["MARKER"][i_dv]) - for i_mark in range(n_mark): - output_file.write("%s " % new_value["MARKER"][i_dv][i_mark]) - if i_mark + 1 < n_mark: - output_file.write(", ") - #: for each marker - if not this_kind in ["AOA", "MACH_NUMBER"]: - output_file.write(" | ") - # params - if this_kind in [ - "FFD_SETTING", - "FFD_ANGLE_OF_ATTACK", - "FFD_CONTROL_POINT", - "FFD_NACELLE", - "FFD_GULL", - "FFD_TWIST_ANGLE", - "FFD_TWIST", - "FFD_ROTATION", - "FFD_CAMBER", - "FFD_THICKNESS", - "FFD_CONTROL_POINT_2D", - "FFD_CAMBER_2D", - "FFD_THICKNESS_2D", - ]: - n_param = len(new_value["PARAM"][i_dv]) - output_file.write("%s , " % new_value["FFDTAG"][i_dv]) - for i_param in range(1, n_param): - output_file.write( - "%s " % new_value["PARAM"][i_dv][i_param] - ) - if i_param + 1 < n_param: + if this_ffd_list != []: + output_file.write("%s, " % this_ffd_list) + for j_value in range(1, n_lists): + output_file.write("%s" % this_param_list[j_value]) + if j_value + 1 < n_lists: output_file.write(", ") else: - n_param = len(new_value["PARAM"][i_dv]) - for i_param in range(n_param): - output_file.write( - "%s " % new_value["PARAM"][i_dv][i_param] - ) - if i_param + 1 < n_param: + for j_value in range(n_lists): + output_file.write("%s" % this_param_list[j_value]) + if j_value + 1 < n_lists: output_file.write(", ") - #: for each param - output_file.write(" )") - if i_dv + 1 < n_dv: - output_file.write("; ") - #: for each dv - break + output_file.write(") ") + if i_value + 1 < len(new_value["PARAM"]): + output_file.write("; ") + break - if case("OPT_OBJECTIVE"): - n_obj = 0 - for name, value in new_value.items(): - if n_obj > 0: - output_file.write("; ") - if value["OBJTYPE"] == "DEFAULT": - output_file.write("%s * %s " % (name, value["SCALE"])) - else: - output_file.write( - "( %s %s %s ) * %s" - % (name, value["OBJTYPE"], value["VALUE"], value["SCALE"]) - ) - n_obj += 1 - break + # int parameters + if case("NUMBER_PART"): + pass + if case("ADAPT_CYCLES"): + pass + if case("TIME_INSTANCES"): + pass + if case("AVAILABLE_PROC"): + pass + if case("UNST_ADJOINT_ITER"): + pass + if ( + case("ITER") + or case("TIME_ITER") + or case("INNER_ITER") + or case("OUTER_ITER") + ): + output_file.write("%i" % new_value) + break - if case("OPT_CONSTRAINT"): - i_con = 0 - for con_type in ["EQUALITY", "INEQUALITY"]: - this_con = new_value[con_type] - for name, value in this_con.items(): - if i_con > 0: + if case("DEFINITION_DV"): + n_dv = len(new_value["KIND"]) + if not n_dv: + output_file.write("NONE") + for i_dv in range(n_dv): + this_kind = new_value["KIND"][i_dv] + output_file.write("( ") + output_file.write("%i , " % get_dvID(this_kind)) + output_file.write("%s " % new_value["SCALE"][i_dv]) + output_file.write("| ") + # markers + n_mark = len(new_value["MARKER"][i_dv]) + for i_mark in range(n_mark): + output_file.write("%s " % new_value["MARKER"][i_dv][i_mark]) + if i_mark + 1 < n_mark: + output_file.write(", ") + #: for each marker + if not this_kind in ["AOA", "MACH_NUMBER"]: + output_file.write(" | ") + # params + if this_kind in [ + "FFD_SETTING", + "FFD_ANGLE_OF_ATTACK", + "FFD_CONTROL_POINT", + "FFD_NACELLE", + "FFD_GULL", + "FFD_TWIST_ANGLE", + "FFD_TWIST", + "FFD_ROTATION", + "FFD_CAMBER", + "FFD_THICKNESS", + "FFD_CONTROL_POINT_2D", + "FFD_CAMBER_2D", + "FFD_THICKNESS_2D", + ]: + n_param = len(new_value["PARAM"][i_dv]) + output_file.write("%s , " % new_value["FFDTAG"][i_dv]) + for i_param in range(1, n_param): + output_file.write( + "%s " % new_value["PARAM"][i_dv][i_param] + ) + if i_param + 1 < n_param: + output_file.write(", ") + else: + n_param = len(new_value["PARAM"][i_dv]) + for i_param in range(n_param): + output_file.write( + "%s " % new_value["PARAM"][i_dv][i_param] + ) + if i_param + 1 < n_param: + output_file.write(", ") + + #: for each param + output_file.write(" )") + if i_dv + 1 < n_dv: output_file.write("; ") - output_file.write( - "( %s %s %s ) * %s" - % (name, value["SIGN"], value["VALUE"], value["SCALE"]) - ) - i_con += 1 - #: for each constraint - #: for each constraint type - if not i_con: - output_file.write("NONE") - break + #: for each dv + break - # default, assume string, integer or unformatted float - if case(): - output_file.write("%s" % new_value) - break + if case("OPT_OBJECTIVE"): + n_obj = 0 + for name, value in new_value.items(): + if n_obj > 0: + output_file.write("; ") + if value["OBJTYPE"] == "DEFAULT": + output_file.write("%s * %s " % (name, value["SCALE"])) + else: + output_file.write( + "( %s %s %s ) * %s" + % ( + name, + value["OBJTYPE"], + value["VALUE"], + value["SCALE"], + ) + ) + n_obj += 1 + break + + if case("OPT_CONSTRAINT"): + i_con = 0 + for con_type in ["EQUALITY", "INEQUALITY"]: + this_con = new_value[con_type] + for name, value in this_con.items(): + if i_con > 0: + output_file.write("; ") + output_file.write( + "( %s %s %s ) * %s" + % (name, value["SIGN"], value["VALUE"], value["SCALE"]) + ) + i_con += 1 + #: for each constraint + #: for each constraint type + if not i_con: + output_file.write("NONE") + break + + # default, assume string, integer or unformatted float + if case(): + output_file.write("%s" % new_value) + break - #: for case + #: for case - # remove from param dictionary - del param_dict[this_param] + # remove from param dictionary + del param_dict[this_param] - # next line - output_file.write("\n") + # next line + output_file.write("\n") - #: for each line + #: for each line - # check that all params were used - for this_param in param_dict.keys(): - if not this_param in ["JOB_NUMBER"]: - print( - "Warning: Parameter %s not found in config file and was not written" - % (this_param) - ) + # check that all params were used + for this_param in param_dict.keys(): + if not this_param in ["JOB_NUMBER"]: + print( + "Warning: Parameter %s not found in config file and was not written" + % (this_param) + ) - output_file.close() - os.remove(temp_filename) + output_file.close() + os.remove(temp_filename) + #: def write_config() -#: def write_config() + finally: + output_file.close() + temp_file.close() def dump_config(filename, config): @@ -1218,10 +1247,9 @@ def dump_config(filename, config): if "DV_VALUE_NEW" in config: config.DV_VALUE = config.DV_VALUE_NEW - config_file = open(filename, "w") - # write dummy file - for key in config.keys(): - config_file.write("%s= 0 \n" % key) - config_file.close() + with open(filename, "w") as config_file: + # write dummy file + for key in config.keys(): + config_file.write("%s= 0 \n" % key) # dump data write_config(filename, config)