Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add save_to_csv functionality to simulationResults #58

Closed
wants to merge 1 commit into from
Closed
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
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.
Loading