Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions process/core/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ def __post_init__(self):
stacklevel=2,
)

def bounds(self) -> tuple[NumberType | None, NumberType | None]:
"""Returns the upper and lower bounds of the input.

Returns
-------
lb:
The lower bound or None if no bound is available.
ub:
The upper bound or None if no bound is available/

Notes
-----
Precedence is given to the `range` if both it and `choices` are specified.
"""
lb, ub = None, None
if self.range is not None:
lb, ub = self.range

elif self.choices is not None and self.type in {int, float}:
lb = min(self.choices)
ub = max(self.choices)

return lb, ub


INPUT_VARIABLES = {
"runtitle": InputVariable("globals", str),
Expand Down
49 changes: 0 additions & 49 deletions process/core/io/data_structure_dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@

INPUT_TYPE_MAP = {int: "int", float: "real", str: "string"}

NON_F_VALUES = [
"f_j_cs_start_pulse_end_flat_top",
"f_c_plasma_non_inductive",
"feffcd",
"f_a_tf_turn_cable_copper",
]

logger = logging.getLogger(__name__)

output_dict = {}
Expand Down Expand Up @@ -106,35 +99,6 @@ def dict_var_type():
return di


def dict_input_bounds():
"""Returns a dictionary matching variable names to dictionary containing
upper and lower bounds that PROCESS checks variable lies between when
reading IN.DAT. Looks in input.f90 for parse_real_variable and
parse_int_variable.

Example of a line we are looking for:
call parse_real_variable('BETA', beta, 0.0D0, 1.0D0, &

Example dictionary entry:
DICT_INPUT_BOUNDS['beta'] = {'lb' : 0.0, 'ub' : 1.0}
"""
di = {}

for var_name, config in INPUT_VARIABLES.items():
lb, ub = None, None
if config.range is not None:
lb, ub = config.range

elif config.choices is not None and config.type in {int, float}:
lb = min(config.choices)
ub = max(config.choices)

if lb is not None:
di[var_name] = {"lb": lb, "ub": ub}

return di


def dict_ixc_full():
"""Function to return a dictionary matching str(ixc_no) to a dictionary
containing the name, lower and upper bounds of that variable.
Expand All @@ -161,16 +125,6 @@ def dict_ixc_bounds():
return ixc_bounds


def dict_ixc_simple():
# Returns dictionary mapping ixc no to iteration variable name
ixc_simple = {}
ixc_full = output_dict["DICT_IXC_FULL"]
for key, value in ixc_full.items():
ixc_simple[key] = value["name"]

return ixc_simple


# cache the output of get_dicts so that it is never re-calculated in a given
# process run.
@cache
Expand All @@ -187,15 +141,12 @@ def get_dicts():
# Some dicts depend on other dicts already existing in output_dicts, so
# be careful if changing the order!
dict_objects.extend([
HardcodedDictionary("NON_F_VALUES", NON_F_VALUES),
HardcodedDictionary("DICT_DEFAULT", {}),
HardcodedDictionary("DICT_MODULE", {}),
HardcodedDictionary("DICT_DESCRIPTIONS", {}),
SourceDictionary("DICT_INPUT_BOUNDS", dict_input_bounds),
SourceDictionary("DICT_VAR_TYPE", dict_var_type),
SourceDictionary("DICT_IXC_FULL", dict_ixc_full),
SourceDictionary("DICT_IXC_BOUNDS", dict_ixc_bounds),
SourceDictionary("DICT_IXC_SIMPLE", dict_ixc_simple),
])

# Make individual dicts within dict objects, process, then add to output_dict
Expand Down
63 changes: 10 additions & 53 deletions process/core/io/in_dat/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from process.core.exceptions import ProcessValidationError
from process.core.io.data_structure_dicts import get_dicts
from process.core.solver.constraints import ConstraintManager
from process.core.solver.iteration_variables import ITERATION_VARIABLES
from process.data_structure.numerics import PROCESSRunMode


Expand Down Expand Up @@ -199,29 +200,6 @@ def find_line_type(line):
return "Parameter"


def find_parameter_group(name):
"""Function to find the module which the parameter belongs to.

Parameters
----------
name:
Parameter name

Returns
-------
:
Return the module the parameter belongs to
"""
# Load dicts from dicts JSON file
dicts = get_dicts()

# Search DICT_MODULES for parameter
for key in dicts["DICT_MODULE"]:
if name in dicts["DICT_MODULE"][key]:
return key
return None # Explicit return


def write_title(title, out_file):
"""Function to write title line to file with fixed width

Expand Down Expand Up @@ -322,21 +300,14 @@ def get_iteration_variables(data: dict):
variable number, comment, upper and/or lower bounds if present
"""
variables = {}

# Load dicts from dicts JSON file
dicts = get_dicts()

# List of variable numbers in IN.DAT
variable_numbers = data["ixc"].value

# Create variable dicts
for variable_number in variable_numbers:
variable = {}

comment = dicts["DICT_IXC_SIMPLE"][
str(variable_number).replace(",", ";").replace(".", ";").replace(":", ";")
]
variable["comment"] = comment
variable["comment"] = ITERATION_VARIABLES[int(variable_number)].name

# Set bounds if there are any
if str(variable_number) in data["bounds"].value:
Expand Down Expand Up @@ -652,7 +623,6 @@ def add_parameter(data, parameter_name, parameter_value):

# Check that the parameter is not already in the dictionary
if parameter_name not in data:
parameter_group = find_parameter_group(parameter_name)
if "f_nd_impurity_electrons" in parameter_name:
comment = dicts["DICT_DESCRIPTIONS"]["f_nd_impurity_electrons"]
else:
Expand All @@ -667,9 +637,7 @@ def add_parameter(data, parameter_name, parameter_value):
)
comment = ""

param_data = INVariable(
parameter_name, parameter_value, "Parameter", parameter_group, comment
)
param_data = INVariable(parameter_name, parameter_value, "Parameter", comment)

data[parameter_name] = param_data

Expand Down Expand Up @@ -995,7 +963,7 @@ def variable_bound_check(bound_number, bound_type):

class INVariable:
"""Class to stores the information of a single variable from the
IN.DAT file
IN.DAT file.

Parameters
----------
Expand All @@ -1005,17 +973,14 @@ class INVariable:
Item value
v_type:
Type of item
parameter_group:
PROCESS variable group item belongs to
comment:
Comment for item
"""

def __init__(self, name, value, v_type, parameter_group, comment):
def __init__(self, name, value, v_type, comment):
self.name = name
self.value = value
self.v_type = v_type
self.parameter_group = parameter_group
self.comment = comment

def __eq__(self, value):
Expand All @@ -1024,16 +989,15 @@ def __eq__(self, value):
self.name == value.name
and self.value == value.value
and self.v_type == value.v_type
and self.parameter_group == value.parameter_group
)

def __hash__(self):
return hash((self.name, self.value, self.v_type, self.parameter_group))
return hash((self.name, self.value, self.v_type))

def __repr__(self):
return (
f"{type(self).__name__}(name={self.name!r}, value={self.value!r}, v_type={self.v_type!r}, "
f"parameter_group={self.parameter_group!r}, comment={self.comment!r})"
f"comment={self.comment!r})"
)

@property
Expand Down Expand Up @@ -1131,7 +1095,7 @@ def process_line(self, line_type, line):
# Create bound variable class using INVariable class if the bounds entry
# doesn't exist
if "bounds" not in self.data:
self.data["bounds"] = INVariable("bounds", {}, "Bound", "Bound", "Bounds")
self.data["bounds"] = INVariable("bounds", {}, "Bound", "Bounds")

# Constraint equations
if line_type == "Constraint Equation":
Expand Down Expand Up @@ -1165,8 +1129,6 @@ def process_line(self, line_type, line):
empty_array = []

if array_name not in self.data:
parameter_group = find_parameter_group(array_name)

# Get parameter comment/description from dictionary
comment = (
dicts["DICT_DESCRIPTIONS"][array_name]
Expand All @@ -1181,7 +1143,7 @@ def process_line(self, line_type, line):
# DICT_DEFAULT; don't want changes to data to change the
# defaults
self.data[array_name] = INVariable(
array_name, empty_array_copy, array_name, parameter_group, comment
array_name, empty_array_copy, array_name, comment
)

self.process_array(line, empty_array)
Expand Down Expand Up @@ -1234,9 +1196,6 @@ def process_parameter(self, line):
)
sys.exit()

# Find group of variables the parameter belongs to
parameter_group = find_parameter_group(name)

# Get parameter comment/description from dictionary
comment = (
dicts["DICT_DESCRIPTIONS"][name]
Expand All @@ -1251,7 +1210,7 @@ def process_parameter(self, line):
self.add_duplicate_variable(name)

# Populate the IN.DAT dictionary with the information
self.data[name] = INVariable(name, value, "Parameter", parameter_group, comment)
self.data[name] = INVariable(name, value, "Parameter", comment)

def process_constraint_equation(self, line):
"""Function to process constraint equation entry in IN.DAT
Expand Down Expand Up @@ -1286,7 +1245,6 @@ def process_constraint_equation(self, line):
"icc",
value,
"Constraint Equation",
"Constraint Equation",
"Constraint Equations",
)

Expand Down Expand Up @@ -1335,7 +1293,6 @@ def process_iteration_variables(self, line):
"ixc",
value,
"Iteration Variable",
"Iteration Variable",
"Iteration Variables",
)

Expand Down
Loading
Loading