From 382430734fbc6455f223766a35f96dffceb0a084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ronald=20J=C3=A4pel?= Date: Fri, 1 Sep 2023 14:50:14 +0200 Subject: [PATCH] Add save_to_csv functionality to simulationResults --- CADETProcess/simulationResults.py | 38 ++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/CADETProcess/simulationResults.py b/CADETProcess/simulationResults.py index a3a40d248..d36b46771 100644 --- a/CADETProcess/simulationResults.py +++ b/CADETProcess/simulationResults.py @@ -23,16 +23,15 @@ from CADETProcess import CADETProcessError from CADETProcess import settings -from CADETProcess.dataStructure import Structure +from CADETProcess.dataStructure import StructMeta from CADETProcess.dataStructure import ( Dict, String, List, UnsignedInteger, UnsignedFloat ) - __all__ = ['SimulationResults'] -class SimulationResults(Structure): +class SimulationResults(metaclass=StructMeta): """Class for storing simulation results including the solver configuration Attributes @@ -88,7 +87,7 @@ def __init__( process, solution_cycles, sensitivity_cycles, system_state, chromatograms - ): + ): self.solver_name = solver_name self.solver_parameters = solver_parameters @@ -211,7 +210,7 @@ def time_complete(self): for i in range(1, self.n_cycles): time_complete = np.hstack(( time_complete, - self.time_cycle[1:] + i*self.process.cycle_time + self.time_cycle[1:] + i * self.process.cycle_time )) self._time_complete = time_complete @@ -230,19 +229,42 @@ def save(self, case_dir=None, unit=None, start=0, end=None): for unit in units: self.solution[unit][-1].plot( - save_path=path + '/' + unit + '_last.png', + save_path=os.path.join(path, unit + '_last.png'), start=start, end=end ) for unit in units: self.solution_complete[unit].plot( - save_path=path + '/' + unit + '_complete.png', + save_path=os.path.join(path, unit + '_complete.png'), start=start, end=end ) for unit in units: self.solution[unit][-1].plot( - save_path=path + '/' + unit + '_overlay.png', + save_path=os.path.join(path, unit + '_overlay.png'), overlay=[cyc.signal for cyc in self.solution[unit][0:-1]], start=start, end=end ) + + def save_csv(self, case_dir=None, filename=None, units=None): + path = settings.working_directory + if case_dir is not None: + path = os.path.join(settings.working_directory, case_dir) + if not os.path.exists(path): + os.makedirs(path) + + if units is None: + units = self.solution.keys() + + for unit in units: + solution = self.solution[unit]["outlet"].solution + solution_times = self.solution[unit]["outlet"].time + + full_solution = np.concatenate([np.atleast_2d(solution_times), solution.T]).T + + if filename is None: + filename = unit + '_output.csv' + + file_path = os.path.join(path, filename) + with open(file_path, "w") as file_handle: + np.savetxt(file_handle, full_solution, delimiter=",")