Skip to content
Merged
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
20 changes: 10 additions & 10 deletions process/core/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,23 +211,23 @@ def run_summary(data: DataStructure):
# MFile #
mfile = constants.MFILE

process_output.ovarst(mfile, "PROCESS version", "(procver)", f'"{version}"')
process_output.ovarst(mfile, "Date of run", "(date)", f'"{date_string}"')
process_output.ovarst(mfile, "Time of run", "(time)", f'"{time_string}"')
process_output.ovarst(mfile, "User", "(username)", f'"{user}"')
process_output.ovarst(mfile, "PROCESS run title", "(runtitle)", f'"{runtitle}"')
process_output.ovarst(mfile, "PROCESS git tag", "(tagno)", f'"{git_tag}"')
process_output.ovarst(
process_output.ovarre(mfile, "PROCESS version", "(procver)", f'"{version}"')
process_output.ovarre(mfile, "Date of run", "(date)", f'"{date_string}"')
process_output.ovarre(mfile, "Time of run", "(time)", f'"{time_string}"')
process_output.ovarre(mfile, "User", "(username)", f'"{user}"')
process_output.ovarre(mfile, "PROCESS run title", "(runtitle)", f'"{runtitle}"')
process_output.ovarre(mfile, "PROCESS git tag", "(tagno)", f'"{git_tag}"')
process_output.ovarre(
mfile, "PROCESS git branch", "(branch_name)", f'"{git_branch}"'
)
process_output.ovarst(mfile, "Input filename", "(fileprefix)", f'"{fileprefix}"')
process_output.ovarre(mfile, "Input filename", "(fileprefix)", f'"{fileprefix}"')

process_output.ovarin(
process_output.ovarre(
mfile, "Optimisation switch", "(ioptimz)", data.numerics.ioptimz
)
# If optimising, write figure of merit switch
if data.numerics.ioptimz == PROCESSRunMode.OPTIMISATION:
process_output.ovarin(
process_output.ovarre(
mfile, "Figure of merit switch", "(minmax)", data.numerics.minmax
)

Expand Down
33 changes: 22 additions & 11 deletions process/core/process_output.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
"""Handles writing output to PROCESS MFile/OUTFile."""

from contextlib import suppress
from pathlib import Path

import numpy as np

from process.core import constants
from process.core.exceptions import ProcessValueError


class OutputFileManager:
"""Manages the opening of regular/idempotence output files."""

@classmethod
def open_files(cls, output_prefix: str, *, mode="w"):
"""Setup the handlers for MFile and OUTFile for writing."""
cls._outfile = open( # noqa: SIM115
Path(output_prefix + "OUT.DAT"), mode
)
Expand All @@ -18,6 +24,7 @@ def open_files(cls, output_prefix: str, *, mode="w"):

@classmethod
def open_idempotence_files(cls, output_prefix: str):
"""Setup the handlers for idempotence MFile and OUTFile for writing."""
cls._outfile.close()
cls._mfile.close()

Expand All @@ -30,6 +37,9 @@ def open_idempotence_files(cls, output_prefix: str):

@classmethod
def close_idempotence_files(cls, output_prefix: str):
"""Removes idempotence output files, closes the handler,
and opens the main output files.
"""
Path(cls._outfile.name).unlink()
Path(cls._mfile.name).unlink()
cls._outfile.close()
Expand All @@ -38,17 +48,28 @@ def close_idempotence_files(cls, output_prefix: str):

@classmethod
def finish(cls):
"""Closes the file handlers."""
cls._outfile.close()
cls._mfile.close()


def write(file, string: str):
"""Writes a string to the given file identifier.

Raises
------
ProcessValueError
The file is not recognised as an MFile, OUTFile, or terminal.
"""
if file == constants.MFILE:
OutputFileManager._mfile.write(f"{string}\n")
elif file == constants.NOUT:
OutputFileManager._outfile.write(f"{string}\n")
elif file == constants.IOTTY:
print(string)
else:
error_msg = f"Unknown file identifier {file}, it is not recognised as either an MFile, OUTFile, or terminal."
raise ProcessValueError(error_msg)


def ocentr(file, string: str, width: int, *, character="*"):
Expand Down Expand Up @@ -162,6 +183,7 @@ def ocmmnt(file, string: str):


def ovarre(file, descr: str, varnam: str, value, output_flag: str = ""):
"""Write out a variable to a file via its identifier."""
replacement_character = "_"
if file != constants.MFILE:
replacement_character = " "
Expand All @@ -171,9 +193,6 @@ def ovarre(file, descr: str, varnam: str, value, output_flag: str = ""):

if isinstance(value, np.ndarray):
value = value.item()
if isinstance(value, bytes):
# TODO: remove when Fortran is gone
value = value.decode().strip()
if isinstance(value, str):
# try and convert the value to a float
# if it fails, leave as a string
Expand Down Expand Up @@ -203,13 +222,5 @@ def ovarrf(file, descr: str, varnam: str, value, output_flag: str = ""):
ovarre(file, descr, varnam, value, output_flag)


def ovarin(file, descr: str, varnam: str, value, output_flag: str = ""):
ovarre(file, descr, varnam, value, output_flag)


def ovarst(file, descr: str, varnam: str, value, output_flag: str = ""):
ovarre(file, descr, varnam, value, output_flag)


def obuild(file, descr: str, thick: float, total: float, variable_name: str = ""):
write(file, f"{descr:<50}{thick:.3e}{' ':<10}{total:.3e} {variable_name}")
36 changes: 18 additions & 18 deletions process/core/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def post_optimise(self, ifail: int):
constants.NOUT, "PROCESS has performed a VMCON (optimisation) run."
)
if ifail != 1:
process_output.ovarin(constants.NOUT, "Error flag", "(ifail)", ifail)
process_output.ovarre(constants.NOUT, "Error flag", "(ifail)", ifail)
process_output.oheadr(
constants.IOTTY, "PROCESS COULD NOT FIND A FEASIBLE SOLUTION"
)
Expand Down Expand Up @@ -302,7 +302,7 @@ def post_optimise(self, ifail: int):
constants.IOTTY, "PROCESS found a consistent solution"
)
process_output.oblnkl(constants.NOUT)
process_output.ovarin(constants.NOUT, "Error flag", "(ifail)", ifail)
process_output.ovarre(constants.NOUT, "Error flag", "(ifail)", ifail)

if self.data.numerics.sqsumsq >= 1.0e-2:
process_output.oblnkl(constants.NOUT)
Expand Down Expand Up @@ -337,19 +337,19 @@ def post_optimise(self, ifail: int):
f"High final constraint residues. {self.data.numerics.sqsumsq=}"
)

process_output.ovarin(
process_output.ovarre(
constants.NOUT,
"Number of iteration variables",
"(nvar)",
self.data.numerics.nvar,
)
process_output.ovarin(
process_output.ovarre(
constants.NOUT,
"Number of constraints (total)",
"(neqns+nineqns)",
self.data.numerics.neqns + self.data.numerics.nineqns,
)
process_output.ovarin(
process_output.ovarre(
constants.NOUT,
"Optimisation switch",
"(ioptimz)",
Expand All @@ -361,7 +361,7 @@ def post_optimise(self, ifail: int):
)
# Objective function output: none for fsolve
if self.solver != "fsolve":
process_output.ovarin(
process_output.ovarre(
constants.NOUT,
"Figure of merit switch",
"(minmax)",
Expand All @@ -372,7 +372,7 @@ def post_optimise(self, ifail: int):

self.data.numerics.objf_name = objf_name

process_output.ovarst(
process_output.ovarre(
constants.NOUT,
"Objective function name",
"(objf_name)",
Expand Down Expand Up @@ -401,7 +401,7 @@ def post_optimise(self, ifail: int):
self.data.globals.convergence_parameter,
"OP ",
)
process_output.ovarin(
process_output.ovarre(
constants.NOUT,
"Number of optimising solver iterations",
"(nviter)",
Expand Down Expand Up @@ -981,37 +981,37 @@ def scan_2d(self):

@staticmethod
def scan_2d_init(scan_data: ScanData):
process_output.ovarin(
process_output.ovarre(
constants.MFILE,
"Number of first variable scan points",
"(isweep)",
scan_data.isweep,
)
process_output.ovarin(
process_output.ovarre(
constants.MFILE,
"Number of second variable scan points",
"(isweep_2)",
scan_data.isweep_2,
)
process_output.ovarin(
process_output.ovarre(
constants.MFILE,
"Scanning first variable number",
"(nsweep)",
scan_data.nsweep,
)
process_output.ovarin(
process_output.ovarre(
constants.MFILE,
"Scanning second variable number",
"(nsweep_2)",
scan_data.nsweep_2,
)
process_output.ovarin(
process_output.ovarre(
constants.MFILE,
"Scanning second variable number",
"(nsweep_2)",
scan_data.nsweep_2,
)
process_output.ovarin(
process_output.ovarre(
constants.MFILE,
"Scanning second variable number",
"(nsweep_2)",
Expand All @@ -1035,7 +1035,7 @@ def scan_1d_write_point_header(self, iscan: int):
)
process_output.ostars(constants.NOUT, 110)
process_output.oblnkl(constants.MFILE)
process_output.ovarin(constants.MFILE, "Scan point number", "(iscan)", iscan)
process_output.ovarre(constants.MFILE, "Scan point number", "(iscan)", iscan)

print(
f"Starting scan point {iscan} of {self.data.scan.isweep} : "
Expand Down Expand Up @@ -1068,7 +1068,7 @@ def scan_2d_write_point_header(self, iscan, iscan_1, iscan_2):
)
process_output.ostars(constants.NOUT, 110)
process_output.oblnkl(constants.MFILE)
process_output.ovarin(constants.MFILE, "Scan point number", "(iscan)", iscan)
process_output.ovarre(constants.MFILE, "Scan point number", "(iscan)", iscan)

print(
f"Starting scan point {iscan}: {self.data.globals.xlabel}, "
Expand All @@ -1082,13 +1082,13 @@ def scan_2d_write_point_header(self, iscan, iscan_1, iscan_2):
@staticmethod
def scan_1d_write_plot(scan_data: ScanData):
if scan_data.first_call_1d:
process_output.ovarin(
process_output.ovarre(
constants.MFILE,
"Number of scan points",
"(isweep)",
scan_data.isweep,
)
process_output.ovarin(
process_output.ovarre(
constants.MFILE,
"Scanning variable number",
"(nsweep)",
Expand Down
8 changes: 4 additions & 4 deletions process/models/availability.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ def calc_u_planned(self, output: bool) -> float:
"(adivflnc)",
self.data.costs.adivflnc,
)
po.ovarin(
po.ovarre(
self.outfile,
"Number of remote handling systems",
"(num_rh_systems)",
Expand Down Expand Up @@ -1006,7 +1006,7 @@ def calc_u_unplanned_bop(self, output: bool) -> float:
po.ovarre(
self.outfile, "Failure rate (1/h)", "(bop_fail_rate)", bop_fail_rate
)
po.ovarin(
po.ovarre(
self.outfile,
"Number of failures in lifetime",
"(bop_num_failures)",
Expand Down Expand Up @@ -1115,14 +1115,14 @@ def calc_u_unplanned_vacuum(self, output: bool) -> float:
if output:
po.ocmmnt(self.outfile, "Vacuum:")
po.oblnkl(self.outfile)
po.ovarin(
po.ovarre(
self.outfile,
"Number of pumps (excluding redundant pumps)",
"(n_vac_pumps_high)",
self.data.vacuum.n_vac_pumps_high,
"OP ",
)
po.ovarin(
po.ovarre(
self.outfile,
"Number of redundant pumps",
"(redun_vac)",
Expand Down
Loading
Loading