Skip to content

Commit

Permalink
[oneD] Replace write_csv by save
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Jun 22, 2023
1 parent 9a37030 commit f1c795e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
11 changes: 9 additions & 2 deletions include/cantera/oneD/Sim1D.h
Expand Up @@ -142,15 +142,22 @@ class Sim1D : public OneDim
const std::string& desc, int loglevel);

/**
* Save the current solution to a container file.
* Save the current solution to a container or CSV file.
*
* For HDF and YAML, the entire content of the object is saved; for CSV, only the
* main 1D domain is saved.
*
* @param fname Name of output container file
* @param id Identifier of solution within the container file
* @param desc Description of the solution
* @param overwrite Force overwrite if name exists; optional (default=false)
* @param compression Compression level (optional; HDF only)
* @param basis Output mass ("Y"/"mass") or mole ("X"/"mole") fractions (CSV only);
* if omitted (default=""), the native storage mode is used
*/
void save(const std::string& fname, const std::string& id,
const std::string& desc, bool overwrite=false, int compression=0);
const std::string& desc, bool overwrite=false, int compression=0,
const string& basis="");

/**
* Save the residual of the current solution to a container file.
Expand Down
2 changes: 1 addition & 1 deletion interfaces/cython/cantera/_onedim.pxd
Expand Up @@ -114,7 +114,7 @@ cdef extern from "cantera/oneD/Sim1D.h":
void refine(int) except +translate_exception
void setRefineCriteria(size_t, double, double, double, double) except +translate_exception
vector[double] getRefineCriteria(int) except +translate_exception
void save(string&, string&, string&, cbool, int) except +translate_exception
void save(string&, string&, string&, cbool, int, string&) except +translate_exception
CxxAnyMap restore(string&, string&) except +translate_exception
void writeStats(int) except +translate_exception
void clearStats()
Expand Down
16 changes: 11 additions & 5 deletions interfaces/cython/cantera/_onedim.pyx
Expand Up @@ -1537,20 +1537,26 @@ cdef class Sim1D:
return self.sim.fixedTemperatureLocation()

def save(self, filename='soln.yaml', name='solution', description=None,
loglevel=None, overwrite=False, compression=0):
loglevel=None, *, overwrite=False, compression=0, basis=None):
"""
Save the solution in YAML or HDF format.
Save the solution to a container or CSV format.
For HDF and YAML files, the entire content of the `Sim1D` object is saved; for
CSV, only the main 1D domain is saved.
:param filename:
solution file
:param name:
solution name within the file
solution name within the file (HDF/YAML only)
:param description:
custom description text
custom description text (HDF/YAML only)
:param overwrite:
Force overwrite if name exists; optional (default=`False`)
:param compression:
compression level 0..9; optional (HDF only)
:param basis:
Output mass (``Y``/``mass``) or mole (``X``/``mole``) fractions (CSV only);
if not specified (`None`), the native storage mode is used
>>> s.save(filename='save.yaml', name='energy_off',
... description='solution with energy eqn. disabled')
Expand All @@ -1562,7 +1568,7 @@ cdef class Sim1D:
warnings.warn("Argument 'loglevel' is deprecated and will be ignored.",
DeprecationWarning)
self.sim.save(stringify(str(filename)), stringify(name),
stringify(description), overwrite, compression)
stringify(description), overwrite, compression, stringify(basis))

def restore(self, filename='soln.yaml', name='solution', loglevel=None):
"""Set the solution vector to a previously-saved solution.
Expand Down
8 changes: 7 additions & 1 deletion interfaces/cython/cantera/onedim.py
Expand Up @@ -406,7 +406,13 @@ def write_csv(self, filename, species='X', quiet=True, normalize=True):
:param normalize:
Boolean flag to indicate whether the mole/mass fractions should
be normalized.
.. deprecated:: 3.0
Method to be removed after Cantera 3.0; superseded by `save`.
"""
warnings.warn("'write_csv' is superseded by 'save' and will be removed "
"after Cantera 3.0.", DeprecationWarning)

# save data
cols = ('extra', 'T', 'D', species)
Expand Down Expand Up @@ -773,7 +779,7 @@ def getter(self):
'entropy_mass', 'g', 'gibbs_mole', 'gibbs_mass', 'cv',
'cv_mole', 'cv_mass', 'cp', 'cp_mole', 'cp_mass',
'isothermal_compressibility', 'thermal_expansion_coeff',
'sound_speed', 'viscosity', 'thermal_conductivity',
'sound_speed', 'viscosity', 'thermal_conductivity',
'heat_release_rate', 'mean_molecular_weight']:
setattr(FlameBase, _attr, _array_property(_attr))
FlameBase.volume = _array_property('v') # avoid confusion with velocity gradient 'V'
Expand Down
17 changes: 16 additions & 1 deletion src/oneD/Sim1D.cpp
Expand Up @@ -126,10 +126,25 @@ void Sim1D::save(const std::string& fname, const std::string& id,
}

void Sim1D::save(const std::string& fname, const std::string& id,
const std::string& desc, bool overwrite, int compression)
const std::string& desc, bool overwrite, int compression,
const string& basis)
{
size_t dot = fname.find_last_of(".");
string extension = (dot != npos) ? toLowerCopy(fname.substr(dot+1)) : "";
if (extension == "csv") {
for (auto dom : m_dom) {
auto arr = dom->asArray(m_state->data() + dom->loc());
if (dom->size() > 1) {
arr->writeEntry(fname, overwrite, basis);
break;
}
}
return;
}
if (basis != "") {
warn_user("Sim1D::save",
"Species basis '{}' not implemented for HDF5 or YAML output.", basis);
}
if (extension == "h5" || extension == "hdf" || extension == "hdf5") {
SolutionArray::writeHeader(fname, id, desc, overwrite);
for (auto dom : m_dom) {
Expand Down

0 comments on commit f1c795e

Please sign in to comment.