| 995 | } |
| 996 | |
| 997 | void SolutionArray::writeEntry(const string& fname, bool overwrite, const string& basis) |
| 998 | { |
| 999 | if (apiNdim() != 1) { |
| 1000 | throw CanteraError("SolutionArray::writeEntry", |
| 1001 | "Tabular output of CSV data only works for 1D SolutionArray objects."); |
| 1002 | } |
| 1003 | set<string> speciesNames; |
| 1004 | for (const auto& species : m_sol->thermo()->speciesNames()) { |
| 1005 | speciesNames.insert(species); |
| 1006 | } |
| 1007 | bool mole; |
| 1008 | if (basis == "") { |
| 1009 | const auto& nativeState = m_sol->thermo()->nativeState(); |
| 1010 | mole = nativeState.find("X") != nativeState.end(); |
| 1011 | } else if (basis == "X" || basis == "mole") { |
| 1012 | mole = true; |
| 1013 | } else if (basis == "Y" || basis == "mass") { |
| 1014 | mole = false; |
| 1015 | } else { |
| 1016 | throw CanteraError("SolutionArray::writeEntry", |
| 1017 | "Invalid species basis '{}'.", basis); |
| 1018 | } |
| 1019 | |
| 1020 | auto names = componentNames(); |
| 1021 | size_t last = names.size() - 1; |
| 1022 | vector<AnyValue> components; |
| 1023 | vector<bool> isSpecies; |
| 1024 | fmt::memory_buffer header; |
| 1025 | for (const auto& key : names) { |
| 1026 | string label = key; |
| 1027 | size_t col; |
| 1028 | if (speciesNames.find(key) == speciesNames.end()) { |
| 1029 | // Pre-read component vectors |
| 1030 | isSpecies.push_back(false); |
| 1031 | components.emplace_back(getComponent(key)); |
| 1032 | col = components.size() - 1; |
| 1033 | if (!components[col].isVector<double>() && |
| 1034 | !components[col].isVector<long int>() && |
| 1035 | !components[col].isVector<string>()) |
| 1036 | { |
| 1037 | throw CanteraError("SolutionArray::writeEntry", |
| 1038 | "Multi-dimensional column '{}' is not supported for CSV output.", |
| 1039 | key); |
| 1040 | } |
| 1041 | } else { |
| 1042 | // Delay reading species data as base can be either mole or mass |
| 1043 | isSpecies.push_back(true); |
| 1044 | components.emplace_back(AnyValue()); |
| 1045 | col = components.size() - 1; |
| 1046 | if (mole) { |
| 1047 | label = "X_" + label; |
| 1048 | } else { |
| 1049 | label = "Y_" + label; |
| 1050 | } |
| 1051 | } |
| 1052 | if (label.find("\"") != string::npos || label.find("\n") != string::npos) { |
| 1053 | throw NotImplementedError("SolutionArray::writeEntry", |
| 1054 | "Detected column name containing double quotes or line feeds: '{}'.", |
no test coverage detected