Skip to content

Commit

Permalink
Add save_to_csv functionality to simulationResults
Browse files Browse the repository at this point in the history
ToDo: add test
  • Loading branch information
r.jaepel authored and schmoelder committed Oct 17, 2023
1 parent cad1ad0 commit 20e4b66
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions CADETProcess/simulationResults.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import copy
import os
import typing

import numpy as np
import addict
Expand All @@ -28,7 +29,6 @@
Dict, String, List, UnsignedInteger, UnsignedFloat
)


__all__ = ['SimulationResults']


Expand Down Expand Up @@ -88,7 +88,7 @@ def __init__(
process,
solution_cycles, sensitivity_cycles, system_state,
chromatograms
):
):
self.solver_name = solver_name
self.solver_parameters = solver_parameters

Expand Down Expand Up @@ -211,7 +211,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
Expand All @@ -230,19 +230,58 @@ 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: typing.Union[str, list] = None):
"""
Save solution to csv file.
Parameters
----------
case_dir : str
Directory to store the solution in.
filename : str
Filename for the solution csv file.
units : str or list of strings
Names of unit operations to write out.
"""
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 filename is None:
filename = 'output.csv'
if not filename.endswith(".csv"):
filename += ".csv"

if type(units) is str:
units = [units]

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

file_path = os.path.join(path, unit + "_" + filename)
with open(file_path, "w") as file_handle:
np.savetxt(file_handle, full_solution, delimiter=",")
Empty file.

0 comments on commit 20e4b66

Please sign in to comment.