Skip to content

Commit c340711

Browse files
committed
Add option to run a series of time-steps with varying input parameters that preserve the previous state
1 parent 4bf21a4 commit c340711

22 files changed

+1179
-440
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
*.DS_Store
2+
13
# Prerequisites
24
*.d
35

bin/ePhotosynthesis.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ void param2ygg(const std::map<std::string, T>& v,
9393
#define varSearchD(x) varSearch(x, convD)
9494
#define varSearchI(x) varSearch(x, convI)
9595
#define varSearchS(x) varSearch(x, convS)
96+
#define varSearchB(x) varSearch(x, convB)
9697
#define varSet(xcli, xfile) (result.count(#xcli) || inputs.count(#xfile))
9798
#define fileProvided(xcli, xfile) varSet(xcli, xfile)
9899

@@ -118,10 +119,12 @@ int main(int argc, const char* argv[]) {
118119
try {
119120
bool record = false;
120121
bool useC3 = false;
122+
bool iterationsPreserveState = false;
121123
cxxopts::Options options("ePhoto", "Command line interface to C++ implementation of the matlab original");
122124
options.show_positional_help();
123125
std::string evn, atpcost, optionsFile, enzymeFile, grnFile,
124-
outputFile, outputVars, outputParamVars, outputParamBase;
126+
outputFile, outputVars, outputParamVars, outputParamBase,
127+
iterationsFile;
125128
double stoptime, begintime, stepsize;
126129
double abstol, reltol;
127130
double Tp;
@@ -137,6 +140,10 @@ int main(int argc, const char* argv[]) {
137140
("n,enzyme", "The file (including path) containing enzyme activities like InputEnzyme.txt", cxxopts::value<std::string>(enzymeFile)->default_value(""))
138141
("g,grn", "The file (including path) containing protein ratios for relevant genes like InputGRNC.txt",
139142
cxxopts::value<std::string>(grnFile)->default_value(""))
143+
("iterations", "The file (including path) times and variables that should be updated at those times",
144+
cxxopts::value<std::string>(iterationsFile)->default_value(""))
145+
("iterationsPreserveState", "Preserve the internal state when iterations are performed (including for yggdrasil iterations)",
146+
cxxopts::value<bool>(iterationsPreserveState)->default_value("false"))
140147
("b,begintime", "The starting time for the calculations.", cxxopts::value<double>(begintime)->default_value("0.0"))
141148
("s,stoptime", "The time to stop calculations.", cxxopts::value<double>(stoptime)->default_value("5000.0"))
142149
("z,stepsize", "The step size to use in the calculations.", cxxopts::value<double>(stepsize)->default_value("1.0"))
@@ -253,6 +260,7 @@ int main(int argc, const char* argv[]) {
253260
varSearchS(enzymeFile);
254261
varSearchS(grnFile);
255262
varSearchS(outputFile);
263+
varSearchS(iterationsFile);
256264
varSearchD(begintime);
257265
varSearchD(stoptime);
258266
varSearchD(stepsize);
@@ -266,6 +274,7 @@ int main(int argc, const char* argv[]) {
266274
varSearchI(outputParam);
267275
varSearchS(outputParamBase);
268276
varSearchS(outputParamVars);
277+
varSearchB(iterationsPreserveState);
269278
}
270279
driverChoice = static_cast<DriverType>(driver);
271280
// TODO: Do yggdrasil initial input before this point so driver
@@ -312,6 +321,7 @@ int main(int argc, const char* argv[]) {
312321
if (!fileProvided(atpcost, atpcost)) atpcost = "";
313322
if (!fileProvided(enzyme, enzymeFile)) enzymeFile = "";
314323
if (!fileProvided(grn, grnFile)) grnFile = "";
324+
if (!fileProvided(iterations, iterationsFile)) iterationsFile = "";
315325

316326
std::vector<std::string> outputVarsV;
317327
if (!outputVars.empty())
@@ -323,10 +333,11 @@ int main(int argc, const char* argv[]) {
323333
begintime, stoptime, stepsize, maxSubSteps,
324334
abstol, reltol, inputs, useC3,
325335
evn, atpcost, enzymeFile, grnFile,
336+
iterationsFile,
326337
outputFile, outputVarsV, outputParam,
327338
outputParamBase, outputParamVarsV,
328339
dbglvl, debugDelta, debugInternal, record,
329-
theVars);
340+
iterationsPreserveState, theVars);
330341
return (EXIT_SUCCESS);
331342
} catch (std::exception& e) {
332343
std::cout << "An error occurred: " << e.what() << std:: endl;

include/ValueSet.hpp

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,9 +1058,11 @@ namespace ePhotosynthesis {
10581058
}
10591059
std::map<std::string, std::string>::const_iterator it_alias = key_aliases.find(iname);
10601060
if (!utils::contains(skip_keys, iname)) {
1061-
out << space << std::setw(pad + tab_size) << std::left;
10621061
if (it_alias != key_aliases.end())
10631062
iname = it_alias->second;
1063+
if (iname.length() > pad)
1064+
pad = iname.length();
1065+
out << space << std::setw(pad + tab_size) << std::left;
10641066
out << iname;
10651067
print_value(val, out, show_pointers);
10661068
out << std::endl;
@@ -1110,7 +1112,7 @@ namespace ePhotosynthesis {
11101112
}
11111113
/**
11121114
Display the values in a value map.
1113-
\tparam V Type of value in value map.
1115+
\tparam V Value map value type.
11141116
\param vals Value map to display.
11151117
\param out Output stream.
11161118
\param tab Number of tabs to prefix each line in the output with.
@@ -1146,6 +1148,44 @@ namespace ePhotosynthesis {
11461148
}
11471149
return out;
11481150
}
1151+
/**
1152+
Display the values in a value map.
1153+
\tparam V Value map value type.
1154+
\param vals Value map to display.
1155+
\param out Output stream.
1156+
\param tab Number of tabs to prefix each line in the output with.
1157+
\param pad Number of characters that key names should be padded
1158+
to fill.
1159+
\param includePrefixes If true, the module & parameter type
1160+
prefixes will be added to the member names.
1161+
\param includeSkipped If true, skipped keys will be output.
1162+
\param skip_keys Key strings to skip in output.
1163+
\param key_aliases String aliases to use for keys.
1164+
\param show_pointers If true, the pointers will be displayed
1165+
instead of the values.
1166+
\returns Output stream.
1167+
*/
1168+
template<typename V>
1169+
static std::ostream& print_value_map(const std::map<std::string, V>& vals,
1170+
std::ostream &out,
1171+
const uint tab=0,
1172+
std::size_t pad=0,
1173+
bool includePrefixes=false,
1174+
bool includeSkipped=false,
1175+
const std::vector<std::string>& skip_keys={},
1176+
const std::map<std::string, std::string>& key_aliases={},
1177+
bool show_pointers=false) {
1178+
check_value_map(vals, "print_value_map: ");
1179+
if (pad == 0)
1180+
pad = field_width_value_map(vals, includePrefixes);
1181+
for (typename std::map<std::string, V>::const_iterator it = vals.begin();
1182+
it != vals.end(); it++) {
1183+
print_value(it->first, it->second,
1184+
out, tab, pad, includePrefixes, includeSkipped,
1185+
skip_keys, key_aliases, show_pointers, it->first);
1186+
}
1187+
return out;
1188+
}
11491189
template<typename V>
11501190
static std::string to_string_with_precision(V const a_value,
11511191
const int n = COMPARE_PRECISION) {
@@ -1248,12 +1288,13 @@ namespace ePhotosynthesis {
12481288
const std::string& context = "");
12491289
/**
12501290
Throw an error if a map is empty;
1251-
\tparam V Value map type.
1291+
\tparam K Value map key type.
1292+
\tparam V Value map value type.
12521293
\param vals Value map to check.
12531294
\param context String to prefix the error message with.
12541295
*/
1255-
template<typename V>
1256-
static void check_value_map(const std::map<EnumType, V>& vals,
1296+
template<typename K, typename V>
1297+
static void check_value_map(const std::map<K, V>& vals,
12571298
const std::string& context = "") {
12581299
if (vals.empty() && !defaults.empty()) {
12591300
ERROR_VALUE_SET(context, "Value map empty");

include/Variables.hpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,17 @@ class Variables : public VALUE_SET_PARENT(Variables, Variables, MODULE_ALL, PARA
149149

150150
protected:
151151
static void _initStaticMembers();
152-
152+
153+
/**
154+
Copy non-member attributes.
155+
\param[in] other Variables instance to copy.
156+
*/
157+
void __copyNonMembers(const Variables& other);
153158
/**
154159
Copy additional members.
155160
Some variables are not included in the default copy (e.g. alfa,
156161
fc, lightParam, CO2A, RedoxReg_MP, module *_Param values, &
157-
rates). To include these, use deepcopy.
162+
rates). To include these, use deepcopy (or __copyNonMembers).
158163
\param[in] other Variables instance to copy.
159164
*/
160165
void __copyMembers(const Variables& other) override;
@@ -166,16 +171,37 @@ class Variables : public VALUE_SET_PARENT(Variables, Variables, MODULE_ALL, PARA
166171
bool noChildren = false) const override;
167172
public:
168173

174+
/**
175+
Check if a variable was updated since the last time the input
176+
was finalized.
177+
\param[in] name Variable name.
178+
\param[in] ignoreClassFlag If true, only return true if the
179+
specific variable was updated.
180+
*/
181+
EPHOTO_API bool inputUpdated(const std::string& name,
182+
const bool ignoreClassFlag=false) const;
183+
/**
184+
Throw an error if the named variable was updated.
185+
*/
186+
EPHOTO_API void assertCalcNotUpdated(const std::string& name) const;
169187
/**
170188
Finalize variables read from input including any unit changes.
189+
\param[in] dontReset If true, don't set the inputsFinalized flag
190+
to true or clear inputsUpdated.
171191
*/
172-
EPHOTO_API void finalizeInputs();
192+
EPHOTO_API void finalizeInputs(const bool dontReset=false);
173193
/**
174194
Create a deep copy of this instance including variables excluded
175195
from a default copy.
176196
\returns New instance with all parameters copied.
177197
*/
178198
EPHOTO_API Variables* deepcopy() const;
199+
/**
200+
Copy another instance including variables excluded from a default
201+
copy.
202+
\param[in] Instance to copy.
203+
*/
204+
EPHOTO_API void deepcopy(const Variables& rhs);
179205
/** \copydoc ValueSet::equals */
180206
bool equals(const ValueSet_t& b,
181207
const bool noChildren = false) const override;
@@ -211,6 +237,7 @@ class Variables : public VALUE_SET_PARENT(Variables, Variables, MODULE_ALL, PARA
211237
\param[in] conditions Map of conditions for composite modules that
212238
are not stored on Variables instances.
213239
\param[in] subset Subset of parameters to output.
240+
\param[in] additionalVars Map of additional names & values to dump.
214241
*/
215242
void dump(const std::string& filename,
216243
const bool includeSkipped = false,
@@ -219,7 +246,8 @@ class Variables : public VALUE_SET_PARENT(Variables, Variables, MODULE_ALL, PARA
219246
const std::vector<std::string>& skip_keys={},
220247
const std::map<std::string, std::string>& key_aliases={},
221248
const std::map<MODULE, const ValueSet_t*>& conditions={},
222-
const std::vector<std::string>& subset={}) const;
249+
const std::vector<std::string>& subset={},
250+
const std::map<std::string, double>& additionalVars={}) const;
223251
/**
224252
Serialize all parameters attached to this instance to an output
225253
stream.
@@ -234,6 +262,7 @@ class Variables : public VALUE_SET_PARENT(Variables, Variables, MODULE_ALL, PARA
234262
\param[in] conditions Map of conditions for composite modules that
235263
are not stored on Variables instances.
236264
\param[in] subset Subset of parameters to output.
265+
\param[in] additionalVars Map of additional names & values to dump.
237266
\returns Updated output stream.
238267
*/
239268
std::ostream& dump(std::ostream& out,
@@ -243,7 +272,8 @@ class Variables : public VALUE_SET_PARENT(Variables, Variables, MODULE_ALL, PARA
243272
const std::vector<std::string>& skip_keys={},
244273
const std::map<std::string, std::string>& key_aliases={},
245274
const std::map<MODULE, const ValueSet_t*>& conditions={},
246-
const std::vector<std::string>& subset={}) const;
275+
const std::vector<std::string>& subset={},
276+
const std::map<std::string, double>& additionalVars={}) const;
247277
/**
248278
Serialize parameters for a single value set to an output stream.
249279
\param[in] module ID for module that should be serialized.
@@ -810,6 +840,7 @@ class Variables : public VALUE_SET_PARENT(Variables, Variables, MODULE_ALL, PARA
810840
EPHOTO_API void readGRN(const std::string& fname);
811841

812842
bool inputsFinalized = false;
843+
std::map<std::string, bool> inputsUpdated; /**< Name of variables that were updated after inputs were finalized. */
813844
bool record = false;
814845
int GP = 0;
815846
int GRNC = 0; /**< Control parameter; if 1, VfactorCp values will be used to scale enzyme activities in the PS, PR, & SUCS modules when CO2 > 0 */

include/drivers/CM_Driver.hpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,6 @@ class CMDriver : public DriverBase<CMDriver, MODULE_CM> {
4646
const std::size_t para = 0, const double ratio = 0,
4747
const bool showWarn = false,
4848
const std::vector<std::string>& outVars = {});
49-
50-
/**
51-
\copydoc drivers::Driver::setup
52-
*/
53-
void setup() override;
54-
55-
/**
56-
\copydoc drivers::Driver::getResults
57-
*/
58-
void getResults() override;
5949

6050
};
6151

include/drivers/DynaPS_Driver.hpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,6 @@ class DynaPSDriver : public DriverBase<DynaPSDriver, MODULE_DynaPS> {
4949
const bool showWarn = false,
5050
const std::vector<std::string>& outVars = {});
5151

52-
/**
53-
\copydoc drivers::Driver::setup
54-
*/
55-
void setup() override;
56-
57-
/**
58-
\copydoc drivers::Driver::getResults
59-
*/
60-
void getResults() override;
61-
6252
};
6353

6454
} // namespace drivers

include/drivers/EPS_Driver.hpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,10 @@ class EPSDriver : public DriverBase<EPSDriver, MODULE_EPS> {
4747
const bool showWarn = false,
4848
const std::vector<std::string>& outVars = {});
4949

50-
/**
51-
\copydoc drivers::Driver::setup
52-
*/
53-
void setup() override;
54-
55-
/**
56-
\copydoc drivers::Driver::getResults
57-
*/
58-
void getResults() override;
59-
6050
private:
6151

6252
//double Ca;
63-
//double Li;
53+
double Li;
6454
double AtpCost;
6555
};
6656

0 commit comments

Comments
 (0)