20YamlWriter::YamlWriter()
21 : m_float_precision(15)
22 , m_skip_user_defined(false)
26void YamlWriter::setHeader(
const AnyMap& header) {
30void YamlWriter::addPhase(shared_ptr<Solution> soln,
bool includeAdjacent) {
31 for (
auto& phase : m_phases) {
32 if (phase->name() == soln->name()) {
33 if (phase.get() == soln.get()) {
40 "Duplicate phase name '{}'", soln->name());
44 m_phases.push_back(soln);
45 if (includeAdjacent) {
46 for (
size_t i = 0; i < soln->nAdjacent(); i++) {
47 addPhase(soln->adjacent(i));
52void YamlWriter::addPhase(shared_ptr<ThermoPhase> thermo,
53 shared_ptr<Kinetics> kin,
54 shared_ptr<Transport> tran) {
55 auto soln = Solution::create();
56 soln->setThermo(thermo);
57 soln->setKinetics(kin);
58 soln->setTransport(tran);
62std::string YamlWriter::toYamlString()
const
65 bool hasDescription = m_header.
hasKey(
"description");
67 output[
"description"] = m_header[
"description"];
69 output[
"generator"] =
"YamlWriter";
70 output[
"cantera-version"] = CANTERA_VERSION;
72 time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
73 output[
"date"] =
trimCopy(std::ctime(&now));
75 output[
"description"].
setLoc(-6, 0);
77 output[
"generator"].
setLoc(-5, 0);
78 output[
"cantera-version"].
setLoc(-4, 0);
79 output[
"git-commit"].
setLoc(-3, 0);
80 output[
"date"].
setLoc(-2, 0);
83 std::set<std::string> exclude = {
84 "description",
"generator",
"cantera-version",
"git-commit",
"date"};
85 for (
const auto& item : m_header) {
86 std::string key = item.first;
87 if (!exclude.count(key)) {
88 output[key] = item.second;
93 std::vector<AnyMap> phaseDefs(m_phases.size());
94 size_t nspecies_total = 0;
95 for (
size_t i = 0; i < m_phases.size(); i++) {
96 phaseDefs[i] = m_phases[i]->parameters(!m_skip_user_defined);
97 if (m_phases[i]->nAdjacent()) {
98 std::vector<std::string> adj_names;
99 for (
size_t j = 0; j < m_phases[i]->nAdjacent(); j++) {
100 adj_names.push_back(m_phases[i]->adjacent(j)->name());
102 phaseDefs[i][
"adjacent-phases"] = adj_names;
104 nspecies_total += m_phases[i]->thermo()->nSpecies();
106 output[
"phases"] = phaseDefs;
109 std::vector<AnyMap> speciesDefs;
110 speciesDefs.reserve(nspecies_total);
111 std::unordered_map<std::string, size_t> speciesDefIndex;
112 for (
const auto& phase : m_phases) {
113 const auto thermo = phase->thermo();
114 for (
const auto& name : thermo->speciesNames()) {
115 const auto& species = thermo->species(name);
116 AnyMap speciesDef = species->parameters(thermo.get(), !m_skip_user_defined);
118 if (speciesDefIndex.count(name) == 0) {
119 speciesDefs.emplace_back(speciesDef);
120 speciesDefIndex[name] = speciesDefs.
size() - 1;
121 }
else if (speciesDefs[speciesDefIndex[name]] != speciesDef) {
123 "Multiple species with different definitions are not "
124 "supported:\n>>>>>>\n{}\n======\n{}\n<<<<<<\n",
125 speciesDef.toYamlString(),
126 speciesDefs[speciesDefIndex[name]].toYamlString());
130 output[
"species"] = speciesDefs;
133 std::map<std::string, std::vector<AnyMap>> allReactions;
134 for (
const auto& phase : m_phases) {
135 const auto kin = phase->kinetics();
136 if (!kin || !kin->nReactions()) {
139 std::vector<AnyMap> reactions;
140 for (
size_t i = 0; i < kin->nReactions(); i++) {
141 reactions.push_back(kin->reaction(i)->parameters(!m_skip_user_defined));
143 allReactions[phase->name()] = std::move(reactions);
151 std::map<std::string, std::vector<std::string>> phaseGroups;
153 for (
const auto& phase : m_phases) {
154 const auto kin = phase->kinetics();
155 std::string name = phase->name();
156 if (!kin || !kin->nReactions()) {
160 for (
auto& group : phaseGroups) {
161 if (allReactions[group.first] == allReactions[name]) {
162 group.second.push_back(name);
163 allReactions.erase(name);
169 phaseGroups[name].push_back(name);
174 if (phaseGroups.size() == 1) {
175 output[
"reactions"] = std::move(allReactions[phaseGroups.begin()->first]);
177 for (
const auto& group : phaseGroups) {
178 std::string groupName;
179 for (
auto& name : group.second) {
180 groupName += name +
"-";
182 groupName +=
"reactions";
183 output[groupName] = std::move(allReactions[group.first]);
185 for (
auto& name : group.second) {
186 AnyMap& phaseDef = output[
"phases"].getMapWhere(
"name", name);
187 phaseDef[
"reactions"] = std::vector<std::string>{groupName};
194 return output.toYamlString();
197void YamlWriter::toYamlFile(
const std::string& filename)
const
199 std::ofstream out(filename);
200 out << toYamlString();
203void YamlWriter::setUnits(
const std::map<std::string, std::string>& units)
206 m_output_units.setDefaults(units);
211 m_output_units = units;
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 std::string &key) const
Returns true if the map contains an item named key.
void setMetadata(const std::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.
Namespace for the Cantera kernel.
std::string trimCopy(const std::string &input)
Trim.
std::string gitCommit()
Returns the hash of the git commit from which Cantera was compiled, if known.
Contains declarations for string manipulation functions within Cantera.