netxsimdg
Loading...
Searching...
No Matches
Model.cpp
Go to the documentation of this file.
1
7#include "include/Model.hpp"
8
11#include "include/DevGrid.hpp"
12#include "include/DevStep.hpp"
15#include "include/Module.hpp"
17
18#include <string>
19
20// TODO Replace with real logging
21#include <iostream>
22
23namespace Nextsim {
24
25const std::string Model::restartOptionName = "model.init_file";
26
27template <>
28const std::map<int, std::string> Configured<Model>::keyMap = {
29 { Model::RESTARTFILE_KEY, Model::restartOptionName },
30 { Model::STARTTIME_KEY, "model.start" },
31 { Model::STOPTIME_KEY, "model.stop" },
32 { Model::RUNLENGTH_KEY, "model.run_length" },
33 { Model::TIMESTEP_KEY, "model.time_step" },
34 { Model::MISSINGVALUE_KEY, "model.missing_value" },
35};
36
37Model::Model()
38{
39 iterator.setIterant(&modelStep);
40
41 finalFileName = "restart.nc";
42}
43
44Model::~Model()
45{
46 /*
47 * Try writing out a valid restart file. If the model and computer are in a
48 * state where this can be completed, great! If they are not then the
49 * restart file is unlikely to be valid or otherwise stored properly, and
50 * we abandon the writing.
51 */
52 try {
53 writeRestartFile();
54 } catch (std::exception& e) {
55 // If there are any exceptions at all, fail without writing
56 }
57}
58
60{
61 // Configure logging
63
64 startTimeStr = Configured::getConfiguration(keyMap.at(STARTTIME_KEY), std::string());
65 stopTimeStr = Configured::getConfiguration(keyMap.at(STOPTIME_KEY), std::string());
66 durationStr = Configured::getConfiguration(keyMap.at(RUNLENGTH_KEY), std::string());
67 stepStr = Configured::getConfiguration(keyMap.at(TIMESTEP_KEY), std::string());
68
69 TimePoint timeNow = iterator.parseAndSet(startTimeStr, stopTimeStr, durationStr, stepStr);
70 m_etadata.setTime(timeNow);
71
72 MissingData::value
73 = Configured::getConfiguration(keyMap.at(MISSINGVALUE_KEY), MissingData::defaultValue);
74
75 initialFileName = Configured::getConfiguration(keyMap.at(RESTARTFILE_KEY), std::string());
76
77 pData.configure();
78
79 modelStep.init();
80 modelStep.setInitFile(initialFileName);
81
82 ModelState initialState(StructureFactory::stateFromFile(initialFileName));
83 modelStep.setData(pData);
84 modelStep.setMetadata(m_etadata);
85 pData.setData(initialState.data);
86}
87
88ConfigMap Model::getConfig() const
89{
90 ConfigMap cMap = {
91 { keyMap.at(STARTTIME_KEY), startTimeStr },
92 { keyMap.at(STOPTIME_KEY), stopTimeStr },
93 { keyMap.at(RUNLENGTH_KEY), durationStr },
94 { keyMap.at(TIMESTEP_KEY), stepStr },
95 { keyMap.at(MISSINGVALUE_KEY), MissingData::value },
96 };
97
98 return cMap;
99}
100
101Model::HelpMap& Model::getHelpText(HelpMap& map, bool getAll)
102{
103 map["Model"] = {
104 { keyMap.at(STARTTIME_KEY), ConfigType::STRING, {}, "", "",
105 "Model start time, formatted as an ISO8601 date. "
106 "Non-calendretical runs can start from year 0 or 1. " },
107 { keyMap.at(STOPTIME_KEY), ConfigType::STRING, {}, "", "",
108 "Model stop time, formatted as an ISO8601 data. "
109 " Will be overridden if a model run length is set. " },
110 { keyMap.at(RUNLENGTH_KEY), ConfigType::STRING, {}, "", "",
111 "Model run length, formatted as an ISO8601 duration (P prefix). "
112 "Overrides the stop time if set. " },
113 { keyMap.at(TIMESTEP_KEY), ConfigType::STRING, {}, "", "",
114 "Model physics timestep, formatted a ISO8601 duration (P prefix). " },
115 { keyMap.at(RESTARTFILE_KEY), ConfigType::STRING, {}, "", "",
116 "The file path to the restart file to use for the run." },
117 { keyMap.at(MISSINGVALUE_KEY), ConfigType::NUMERIC, { "-∞", "∞" }, "-2³⁰⁰", "",
118 "Missing data indicator used for input and output." },
119 };
120
121 return map;
122}
123
124Model::HelpMap& Model::getHelpRecursive(HelpMap& map, bool getAll)
125{
126 getHelpText(map, getAll);
127 PrognosticData::getHelpRecursive(map, getAll);
128 Module::getHelpRecursive<IDiagnosticOutput>(map, getAll);
129 return map;
130}
131
132void Model::run() { iterator.run(); }
133
134void Model::writeRestartFile()
135{
136 // TODO Replace with real logging
137 Logged::notice(std::string(" Writing state-based restart file: ") + finalFileName + '\n');
138 // Copy the configuration from the ModelState to the ModelMetadata
139 ConfigMap modelConfig = getConfig();
140 modelConfig.merge(pData.getStateRecursive(true).config);
142 m_etadata.setConfig(modelConfig);
143 StructureFactory::fileFromState(pData.getState(), m_etadata, finalFileName, true);
144}
145
146ModelMetadata& Model::metadata() { return m_etadata; }
147} /* namespace Nextsim */
virtual ConfigMap getConfiguration() const
Returns the current configuration of the object.
static const std::map< int, std::string > keyMap
static ConfigMap getAllModuleConfigurations()
void setMetadata(ModelMetadata &metadata) override
Sets the metadata object that will be used within the timesteps.
Definition DevStep.hpp:28
void setData(PrognosticData &pDat) override
Sets the data object that will be used within the timesteps.
Definition DevStep.hpp:27
void init() override
Initializes the model, based on some environment stored in the implementing class.
Definition DevStep.cpp:15
void setInitFile(const std::string &filePath)
Sets the path to the initial file for later reference.
void run()
Run the Iterant over the specified time period.
Definition Iterator.cpp:54
TimePoint parseAndSet(const std::string &startTimeStr, const std::string &stopTimeStr, const std::string &durationStr, const std::string &stepStr)
Parses the four strings and sets the time parameters from them.
Definition Iterator.cpp:34
void setIterant(Iterant *iterant)
Sets the iterant to be iterated using a pointer.
Definition Iterator.cpp:25
static void configure()
Static function that configures the logger.
Definition Logged.cpp:55
static void notice(const std::string &message)
Logs a message at level::NOTICE, intended for messages that would appear during normal execution.
Definition Logged.hpp:77
void configure() override
The configuration function.
Definition Model.cpp:59
void run()
Run the model.
Definition Model.cpp:132
ModelMetadata & metadata()
Gets the model metadata instance.
Definition Model.cpp:146
void setConfig(const ConfigMap &config)
Sets the configuration metadata.
void setTime(const TimePoint &time)
Sets the initial or current model time.
ModelState getStateRecursive(const OutputSpec &os) const override
Returns the state of the ModelComponent and any ModelComponents it depends on.
void setData(const ModelState::DataMap &ms) override
Set the initial data of the component from the passed ModelState.
void configure() override
The configuration function.
ModelState getState() const override
Returns a ModelState from this component.
static void fileFromState(const ModelState &state, const ModelMetadata &meta, const std::string &filePath, bool isRestart=false)
Takes a ModelState and a template file name to write the state out to a target file path.
static ModelState stateFromFile(const std::string &filePath)
Returns the ModelState of the named restart file.