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
14 changes: 7 additions & 7 deletions process/core/caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,13 @@ def call_models_and_write_output(self, xc: np.ndarray, ifail: int):
for _ in range(10):
# Divert OUT.DAT and MFILE.DAT output to scratch files for
# idempotence checking
OutputFileManager.open_idempotence_files()
OutputFileManager.open_idempotence_files(self.data.globals.output_prefix)
self._call_models_once(xc)
# Write mfile
finalise(self.models, self.data, ifail)

# Extract data from intermediate idempotence-checking mfile
mfile_path = (
data_structure.global_variables.output_prefix
) + "IDEM_MFILE.DAT"
mfile_path = (self.data.globals.output_prefix) + "IDEM_MFILE.DAT"
mfile = MFile(mfile_path)
# Create mfile dict of float values: only compare floats
mfile_data = {
Expand Down Expand Up @@ -204,7 +202,9 @@ def call_models_and_write_output(self, xc: np.ndarray, ifail: int):
logger.debug("Mfiles idempotent, returning")
# Divert OUT.DAT and MFILE.DAT output back to original files
# now idempotence checking complete
OutputFileManager.close_idempotence_files()
OutputFileManager.close_idempotence_files(
self.data.globals.output_prefix
)
# Write final output file and mfile
finalise(self.models, self.data, ifail)
return
Expand All @@ -230,12 +230,12 @@ def call_models_and_write_output(self, xc: np.ndarray, ifail: int):
)

# Close idempotence files, write final output file and mfile
OutputFileManager.close_idempotence_files()
OutputFileManager.close_idempotence_files(self.data.globals.output_prefix)

except Exception:
# If exception in model evaluations delete intermediate idempotence
# files to clean up
OutputFileManager.close_idempotence_files()
OutputFileManager.close_idempotence_files(self.data.globals.output_prefix)
raise
else:
finalise(
Expand Down
23 changes: 11 additions & 12 deletions process/core/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def init_process(data: DataStructure):
iteration_variables.initialise_iteration_variables()

# Creating and open the files MFile and OUTFile
process_output.OutputFileManager.open_files()
process_output.OutputFileManager.open_files(data.globals.output_prefix)

# Input any desired new initial values
inputs = parse_input_file(data)
Expand All @@ -63,7 +63,7 @@ def init_process(data: DataStructure):
# Check input data for errors/ambiguities
check_process(inputs, data)

run_summary()
run_summary(data)


def get_git_summary() -> tuple[str, str]:
Expand Down Expand Up @@ -101,7 +101,7 @@ def get_git_summary() -> tuple[str, str]:
return git_branch, git_tag


def run_summary():
def run_summary(data: DataStructure):
"""Write a summary of the PROCESS run to the output file and MFile"""
# Outfile and terminal #
for outfile in [constants.NOUT, constants.IOTTY]:
Expand Down Expand Up @@ -137,20 +137,20 @@ def run_summary():
process_output.ocmmnt(outfile, f"Computer : {machine}")
process_output.ocmmnt(outfile, f"Directory : {Path.cwd()}")

fileprefix = data_structure.global_variables.fileprefix
fileprefix = data.globals.fileprefix
process_output.ocmmnt(
outfile,
f"Input : {fileprefix}",
)
runtitle = data_structure.global_variables.runtitle
runtitle = data.globals.runtitle
process_output.ocmmnt(
outfile,
f"Run title : {runtitle}",
)

process_output.ocmmnt(
outfile,
f"Run type : Reactor concept design: {data_structure.global_variables.icase}, (c) UK Atomic Energy Authority",
f"Run type : Reactor concept design: {data.globals.icase}, (c) UK Atomic Energy Authority",
)

process_output.oblnkl(outfile)
Expand All @@ -175,7 +175,7 @@ def run_summary():
if data_structure.numerics.ioptimz == PROCESSRunMode.OPTIMISATION:
process_output.ocmmnt(
outfile,
f"Max iterations : {data_structure.global_variables.maxcal}",
f"Max iterations : {data.globals.maxcal}",
)

if data_structure.numerics.minmax > 0:
Expand Down Expand Up @@ -231,7 +231,6 @@ def init_all_module_vars():
"""
logging_model_handler.clear_logs()
data_structure.numerics.init_numerics()
data_structure.global_variables.init_global_variables()
init_scan_variables()
constants.init_constants()

Expand Down Expand Up @@ -559,7 +558,7 @@ def check_process(inputs, data): # noqa: ARG001

# Tight aspect ratio options (ST)
if data.physics.itart == 1:
data_structure.global_variables.icase = "Tight aspect ratio tokamak model"
data.globals.icase = "Tight aspect ratio tokamak model"

# Disabled Forcing that no inboard breeding blanket is used
# Disabled i_blkt_inboard = 0
Expand Down Expand Up @@ -745,7 +744,7 @@ def check_process(inputs, data): # noqa: ARG001

# Pulsed power plant model
if data.pulse.i_pulsed_plant == 1:
data_structure.global_variables.icase = "Pulsed tokamak model"
data.globals.icase = "Pulsed tokamak model"
else:
data.buildings.esbldgm3 = 0.0

Expand Down Expand Up @@ -1151,6 +1150,6 @@ def set_active_constraints():

def set_device_type(data):
if data.ife.ife == 1:
data_structure.global_variables.icase = "Inertial Fusion model"
data.globals.icase = "Inertial Fusion model"
elif data.stellarator.istell != 0:
data_structure.global_variables.icase = "Stellarator model"
data.globals.icase = "Stellarator model"
10 changes: 5 additions & 5 deletions process/core/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ def __post_init__(self):


INPUT_VARIABLES = {
"runtitle": InputVariable(data_structure.global_variables, str),
"verbose": InputVariable(data_structure.global_variables, int, choices=[0, 1]),
"run_tests": InputVariable(data_structure.global_variables, int, choices=[0, 1]),
"runtitle": InputVariable("globals", str),
"verbose": InputVariable("globals", int, choices=[0, 1]),
"run_tests": InputVariable("globals", int, choices=[0, 1]),
"ioptimz": InputVariable(data_structure.numerics, int, choices=[1, -2]),
"epsvmc": InputVariable(data_structure.numerics, float, range=(0.0, 1.0)),
"boundl": InputVariable(data_structure.numerics, float, array=True),
"boundu": InputVariable(data_structure.numerics, float, array=True),
"epsfcn": InputVariable(data_structure.numerics, float, range=(0.0, 1.0)),
"maxcal": InputVariable(data_structure.global_variables, int, range=(0, 10000)),
"maxcal": InputVariable("globals", int, range=(0, 10000)),
"minmax": InputVariable(data_structure.numerics, int),
"neqns": InputVariable(
data_structure.numerics, int, range=(0, ConstraintManager.num_constraints())
Expand Down Expand Up @@ -1144,7 +1144,7 @@ def __post_init__(self):


def parse_input_file(data_structure_obj: DataStructure):
input_file = data_structure.global_variables.fileprefix
input_file = data_structure_obj.globals.fileprefix

input_file_path = Path("IN.DAT")
if input_file != "":
Expand Down
10 changes: 4 additions & 6 deletions process/core/io/vary_run/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import click
from numpy.random import default_rng

from process import data_structure
from process.core.io.in_dat import InDat
from process.core.io.mfile import MFile
from process.core.io.vary_run.tools import (
Expand All @@ -26,6 +25,7 @@
process_warnings,
set_variable_in_indat,
)
from process.core.model import DataStructure

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -261,7 +261,7 @@ def error_status2readme(self, mfile):
def modify_in_dat(self):
"""Modifies the original IN.DAT file"""

def setup(self):
def setup(self, data: DataStructure):
"""Sets up the program for running"""
self.echo()

Expand All @@ -275,10 +275,8 @@ def setup(self):

self.generator = default_rng(seed=self.u_seed)

data_structure.global_variables.output_prefix = str(
self.wdir / self.outfile.strip("MFILE.DAT")
)
data_structure.global_variables.fileprefix = str(self.wdir / self.infile)
data.globals.output_prefix = str(self.wdir / self.outfile.strip("MFILE.DAT"))
data.globals.fileprefix = str(self.wdir / self.infile)

def run_process(self, input_path: Path, solver: str = "vmcon"):
"""Perform a single run of PROCESS, catching any errors.
Expand Down
2 changes: 2 additions & 0 deletions process/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from process.data_structure.divertor_variables import DivertorData
from process.data_structure.first_wall_variables import FirstWallData
from process.data_structure.fwbs_variables import FWBSData
from process.data_structure.global_variables import GlobalData
from process.data_structure.heat_transport_variables import HeatTransportData
from process.data_structure.ife_variables import IFEData
from process.data_structure.impurity_radiation_variables import ImpurityRadiationData
Expand Down Expand Up @@ -75,6 +76,7 @@ class DataStructure:
rebco: RebcoData = initialise_later
tfcoil: TFData = initialise_later
superconducting_tfcoil: SuperconductingTFData = initialise_later
globals: GlobalData = initialise_later

def __post_init__(self):
for f in fields(self):
Expand Down
18 changes: 9 additions & 9 deletions process/core/process_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@
import numpy as np

from process.core import constants
from process.data_structure import global_variables, numerics
from process.data_structure import numerics


class OutputFileManager:
@classmethod
def open_files(cls, *, mode="w"):
def open_files(cls, output_prefix: str, *, mode="w"):
cls._outfile = open( # noqa: SIM115
Path(global_variables.output_prefix + "OUT.DAT"), mode
Path(output_prefix + "OUT.DAT"), mode
)
cls._mfile = open( # noqa: SIM115
Path(global_variables.output_prefix + "MFILE.DAT"), mode
Path(output_prefix + "MFILE.DAT"), mode
)

@classmethod
def open_idempotence_files(cls):
def open_idempotence_files(cls, output_prefix: str):
cls._outfile.close()
cls._mfile.close()

cls._outfile = open( # noqa: SIM115
Path(global_variables.output_prefix + "IDEM_OUT.DAT"), "w"
Path(output_prefix + "IDEM_OUT.DAT"), "w"
)
cls._mfile = open( # noqa: SIM115
Path(global_variables.output_prefix + "IDEM_MFILE.DAT"), "w"
Path(output_prefix + "IDEM_MFILE.DAT"), "w"
)

@classmethod
def close_idempotence_files(cls):
def close_idempotence_files(cls, output_prefix: str):
Path(cls._outfile.name).unlink()
Path(cls._mfile.name).unlink()
cls._outfile.close()
cls._mfile.close()
cls.open_files(mode="a")
cls.open_files(mode="a", output_prefix=output_prefix)

@classmethod
def finish(cls):
Expand Down
31 changes: 15 additions & 16 deletions process/core/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from process.core.solver import constraints
from process.core.solver.solver_handler import SolverHandler
from process.data_structure import (
global_variables,
numerics,
scan_variables,
)
Expand Down Expand Up @@ -386,7 +385,7 @@ def post_optimise(self, ifail: int):
constants.NOUT,
"VMCON convergence parameter",
"(convergence_parameter)",
global_variables.convergence_parameter,
self.data.globals.convergence_parameter,
"OP ",
)
process_output.ovarin(
Expand Down Expand Up @@ -991,8 +990,8 @@ def scan_2d_init(self):
)

def scan_1d_write_point_header(self, iscan: int):
global_variables.iscan_global = iscan
global_variables.vlabel, global_variables.xlabel = self.scan_select(
self.data.globals.iscan_global = iscan
self.data.globals.vlabel, self.data.globals.xlabel = self.scan_select(
scan_variables.nsweep, scan_variables.sweep, iscan
)

Expand All @@ -1001,8 +1000,8 @@ def scan_1d_write_point_header(self, iscan: int):

process_output.write(
constants.NOUT,
f"***** Scan point {iscan} of {scan_variables.isweep} : {global_variables.xlabel}"
f", {global_variables.vlabel} = {scan_variables.sweep[iscan - 1]} "
f"***** Scan point {iscan} of {scan_variables.isweep} : {self.data.globals.xlabel}"
f", {self.data.globals.vlabel} = {scan_variables.sweep[iscan - 1]} "
"*****",
)
process_output.ostars(constants.NOUT, 110)
Expand All @@ -1011,20 +1010,20 @@ def scan_1d_write_point_header(self, iscan: int):

print(
f"Starting scan point {iscan} of {scan_variables.isweep} : "
f"{global_variables.xlabel} , {global_variables.vlabel}"
f"{self.data.globals.xlabel} , {self.data.globals.vlabel}"
f" = {scan_variables.sweep[iscan - 1]}"
)

def scan_2d_write_point_header(self, iscan, iscan_1, iscan_2):
iscan_r = scan_variables.isweep_2 - iscan_2 + 1 if iscan_1 % 2 == 0 else iscan_2

# Makes iscan available globally (read-only)
global_variables.iscan_global = iscan
self.data.globals.iscan_global = iscan

global_variables.vlabel, global_variables.xlabel = self.scan_select(
self.data.globals.vlabel, self.data.globals.xlabel = self.scan_select(
scan_variables.nsweep, scan_variables.sweep, iscan_1
)
global_variables.vlabel_2, global_variables.xlabel_2 = self.scan_select(
self.data.globals.vlabel_2, self.data.globals.xlabel_2 = self.scan_select(
scan_variables.nsweep_2, scan_variables.sweep_2, iscan_r
)

Expand All @@ -1034,19 +1033,19 @@ def scan_2d_write_point_header(self, iscan, iscan_1, iscan_2):
process_output.write(
constants.NOUT,
f"***** 2D Scan point {iscan} of {scan_variables.isweep * scan_variables.isweep_2} : "
f"{global_variables.vlabel} = {scan_variables.sweep[iscan_1 - 1]} and"
f" {global_variables.vlabel_2} = {scan_variables.sweep_2[iscan_r - 1]} "
f"{self.data.globals.vlabel} = {scan_variables.sweep[iscan_1 - 1]} and"
f" {self.data.globals.vlabel_2} = {scan_variables.sweep_2[iscan_r - 1]} "
"*****",
)
process_output.ostars(constants.NOUT, 110)
process_output.oblnkl(constants.MFILE)
process_output.ovarin(constants.MFILE, "Scan point number", "(iscan)", iscan)

print(
f"Starting scan point {iscan}: {global_variables.xlabel}, "
f"{global_variables.vlabel} = {scan_variables.sweep[iscan_1 - 1]}"
f" and {global_variables.xlabel_2}, "
f"{global_variables.vlabel_2} = {scan_variables.sweep_2[iscan_r - 1]} "
f"Starting scan point {iscan}: {self.data.globals.xlabel}, "
f"{self.data.globals.vlabel} = {scan_variables.sweep[iscan_1 - 1]}"
f" and {self.data.globals.xlabel_2}, "
f"{self.data.globals.vlabel_2} = {scan_variables.sweep_2[iscan_r - 1]} "
)

return iscan_r
Expand Down
3 changes: 1 addition & 2 deletions process/core/solver/evaluators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from process.core.caller import Caller
from process.core.model import DataStructure
from process.data_structure import global_variables as gv
from process.data_structure import numerics

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -64,7 +63,7 @@ def fcnvmc1(self, _n, m, xv, ifail):
objf, conf = self.caller.call_models(xv, m)

# Verbose diagnostics
if gv.verbose == 1:
if self.data.globals.verbose == 1:
summ = 0.0
for i in range(m):
summ += conf[i] ** 2
Expand Down
4 changes: 2 additions & 2 deletions process/core/solver/iteration_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ def load_iteration_variables(data):
# warn of the iteration variable is also a scan variable because this will cause
# the optimiser and scan to overwrite the same variable and conflict
if iteration_variable.name in {
data_structure.global_variables.vlabel,
data_structure.global_variables.vlabel_2,
data.globals.vlabel,
data.globals.vlabel_2,
}:
warn(
(
Expand Down
Loading
Loading