Skip to content

Commit

Permalink
Add save_to_csv functionality to simulationResults
Browse files Browse the repository at this point in the history
  • Loading branch information
ronald-jaepel authored and r.jaepel committed Oct 10, 2023
1 parent 2386f81 commit 3824307
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions CADETProcess/simulationResults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -88,7 +87,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 +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
Expand All @@ -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=",")

0 comments on commit 3824307

Please sign in to comment.