24 for (
auto& phase : m_phases) {
25 if (phase->name() == soln->name()) {
26 if (phase.get() == soln.get()) {
33 "Duplicate phase name '{}'", soln->name());
37 m_phases.push_back(soln);
38 if (includeAdjacent) {
39 for (
size_t i = 0; i < soln->nAdjacent(); i++) {
46 shared_ptr<Kinetics> kin,
47 shared_ptr<Transport> tran) {
49 soln->setThermo(thermo);
50 soln->setKinetics(kin);
51 soln->setTransport(tran);
60 output[
"description"] =
m_header[
"description"];
62 output[
"generator"] =
"YamlWriter";
63 output[
"cantera-version"] = CANTERA_VERSION;
65 time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
66 output[
"date"] =
trimCopy(std::ctime(&now));
68 output[
"description"].
setLoc(-6, 0);
70 output[
"generator"].
setLoc(-5, 0);
71 output[
"cantera-version"].
setLoc(-4, 0);
72 output[
"git-commit"].
setLoc(-3, 0);
73 output[
"date"].
setLoc(-2, 0);
76 set<string> exclude = {
77 "description",
"generator",
"cantera-version",
"git-commit",
"date"};
78 for (
const auto& [key, value] :
m_header) {
79 if (!exclude.count(key)) {
85 vector<AnyMap> phaseDefs(m_phases.size());
86 size_t nspecies_total = 0;
87 for (
size_t i = 0; i < m_phases.size(); i++) {
89 if (m_phases[i]->nAdjacent()) {
90 vector<string> adj_names;
91 for (
size_t j = 0; j < m_phases[i]->nAdjacent(); j++) {
92 adj_names.push_back(m_phases[i]->adjacent(j)->name());
94 phaseDefs[i][
"adjacent-phases"] = adj_names;
96 nspecies_total += m_phases[i]->thermo()->nSpecies();
98 output[
"phases"] = phaseDefs;
101 vector<AnyMap> speciesDefs;
102 speciesDefs.reserve(nspecies_total);
103 std::unordered_map<string, size_t> speciesDefIndex;
104 for (
const auto& phase : m_phases) {
105 const auto thermo = phase->thermo();
106 for (
const auto& name : thermo->speciesNames()) {
107 const auto& species = thermo->species(name);
110 if (speciesDefIndex.count(name) == 0) {
111 speciesDefs.emplace_back(speciesDef);
112 speciesDefIndex[name] = speciesDefs.
size() - 1;
113 }
else if (speciesDefs[speciesDefIndex[name]] != speciesDef) {
115 "Multiple species with different definitions are not "
116 "supported:\n>>>>>>\n{}\n======\n{}\n<<<<<<\n",
117 speciesDef.toYamlString(),
118 speciesDefs[speciesDefIndex[name]].toYamlString());
122 output[
"species"] = speciesDefs;
125 map<string, vector<AnyMap>> allReactions;
126 for (
const auto& phase : m_phases) {
127 const auto kin = phase->kinetics();
128 if (!kin || !kin->nReactions()) {
132 kin->checkDuplicates(
false,
true);
133 vector<AnyMap> reactions;
134 for (
size_t i = 0; i < kin->nReactions(); i++) {
137 allReactions[phase->name()] = std::move(reactions);
145 map<string, vector<string>> phaseGroups;
147 for (
const auto& phase : m_phases) {
148 const auto kin = phase->kinetics();
149 string name = phase->name();
150 if (!kin || !kin->nReactions()) {
154 for (
auto& [canonicalPhase, dependentPhases] : phaseGroups) {
155 if (allReactions[canonicalPhase] == allReactions[name]) {
156 dependentPhases.push_back(name);
157 allReactions.erase(name);
163 phaseGroups[name].push_back(name);
168 if (phaseGroups.size() == 1) {
169 output[
"reactions"] = std::move(allReactions[phaseGroups.begin()->first]);
171 for (
const auto& [canonicalPhase, dependentPhases] : phaseGroups) {
173 for (
auto& name : dependentPhases) {
174 groupName += name +
"-";
176 groupName +=
"reactions";
177 output[groupName] = std::move(allReactions[canonicalPhase]);
179 for (
auto& name : dependentPhases) {
180 AnyMap& phaseDef = output[
"phases"].getMapWhere(
"name", name);
181 phaseDef[
"reactions"] = vector<string>{groupName};
188 return output.toYamlString();
193 std::ofstream out(filename);
Base class for kinetics managers and also contains the kineticsmgr module documentation (see Kinetics...
Declaration for class Cantera::Species.
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
Headers for the Transport object, which is the virtual base class for all transport property evaluato...
Declaration for class Cantera::YamlWriter.
void setLoc(int line, int column)
For values which are derived from an input file, set the line and column of this value in that file.
A map of string keys to values whose type can vary at runtime.
size_t size() const
Returns the number of elements in this map.
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
void setMetadata(const string &key, const AnyValue &value)
Set a metadata value that applies to this AnyMap and its children.
void setUnits(const UnitSystem &units)
Set the unit system for this AnyMap.
A wrapper for a variable whose type is determined at runtime.
Base class for exceptions thrown by Cantera classes.
static shared_ptr< Solution > create()
Create an empty Solution object.
void setDefaults(std::initializer_list< string > units)
Set the default units to convert from when explicit units are not provided.
bool m_skip_user_defined
See skipUserDefined()
string toYamlString() const
Return a YAML string that contains the definitions for the added phases, species, and reactions.
void setUnitSystem(const UnitSystem &units=UnitSystem())
Set the units to be used in the output file.
long int m_float_precision
See setPrecision()
AnyMap m_header
Top-level information used in YAML header block.
void setHeader(const AnyMap &header)
Include top-level information used in YAML header block.
void addPhase(shared_ptr< Solution > soln, bool includeAdjacent=true)
Include a phase definition for the specified Solution object.
UnitSystem m_output_units
Top-level units directive for the output file.
void toYamlFile(const string &filename) const
Write the definitions for the added phases, species and reactions to the specified file.
void setUnits(const map< string, string > &units={})
Set the units to be used in the output file.
string trimCopy(const string &input)
Trim.
string gitCommit()
Returns the hash of the git commit from which Cantera was compiled, if known.
Namespace for the Cantera kernel.
Contains declarations for string manipulation functions within Cantera.