Chi-Tech
chi_solver.cc
Go to the documentation of this file.
1#include "chi_solver.h"
2
3#include "utils/chi_utils.h"
4
5#include "chi_runtime.h"
6#include "chi_log.h"
7
9
10#include "ChiObjectFactory.h"
11
12namespace chi_physics
13{
14
15/**Returns the input parameters.*/
17{
19
20 params.AddRequiredParameter<std::string>(
21 "name",
22 "A text name to associate with the solver. This name will be used "
23 "in status messages and verbose iterative convergence monitors.");
24
25 params.AddOptionalParameter("dt", 0.01, "Desired initial timestep size.");
26 params.AddOptionalParameter("time", 0.0, "Current time of the solver.");
28 "start_time", 0.0, "Transient start-time if applicable.");
30 "end_time", 1.0, "Transient end-time if applicable.");
32 "max_time_steps",
33 -1,
34 "Maximum number of timesteps to allow. Negative values disables this.");
35
37 "timestepper",
38 0,
39 "Handle to a timestepper. If not supplied then a ConstantTimeStepper "
40 "will be created.");
41
42 using namespace chi_data_types;
43 params.ConstrainParameterRange("dt", AllowableRangeLowLimit::New(1.0e-12));
44
45 return params;
46}
47
48Solver::Solver(std::string in_text_name)
49 : timestepper_(InitTimeStepper(GetInputParameters())),
50 text_name_(std::move(in_text_name))
51{
52}
53
54Solver::Solver(std::string in_text_name,
55 std::initializer_list<BasicOption> in_options)
56 : basic_options_(in_options),
57 timestepper_(InitTimeStepper(GetInputParameters())),
58 text_name_(std::move(in_text_name))
59{
60}
61
63 : ChiObject(params),
64 timestepper_(InitTimeStepper(params)),
65 text_name_(params.GetParamValue<std::string>("name"))
66{
67}
68
69std::shared_ptr<TimeStepper>
71{
72 const auto& user_params = params.ParametersAtAssignment();
73
74 if (user_params.Has("timestepper"))
75 {
76 auto stepper = Chi::GetStackItemPtrAsType<TimeStepper>(
77 /*stack=*/Chi::object_stack,
78 /*handle=*/params.GetParamValue<size_t>("timestepper"),
79 /*calling_function_name=*/__FUNCTION__);
80
81 stepper->SetTimeStepSize(params.GetParamValue<double>("dt"));
82 stepper->SetTime(params.GetParamValue<double>("time"));
83 stepper->SetStartTime(params.GetParamValue<double>("start_time"));
84 stepper->SetEndTime(params.GetParamValue<double>("end_time"));
85 stepper->SetMaxTimeSteps(params.GetParamValue<int>("max_time_steps"));
86
87 return stepper;
88 }
89 else
90 {
91 auto& factory = ChiObjectFactory::GetInstance();
92
93 const std::string obj_type = "chi_physics::ConstantTimeStepper";
94 auto valid_params = factory.GetRegisteredObjectParameters(obj_type);
95 chi::ParameterBlock custom_params;
96
97 if (params.NumParameters() != 0)
98 {
99 custom_params.AddParameter(params.GetParam("dt"));
100 custom_params.AddParameter(params.GetParam("time"));
101 custom_params.AddParameter(params.GetParam("start_time"));
102 custom_params.AddParameter(params.GetParam("end_time"));
103 custom_params.AddParameter(params.GetParam("max_time_steps"));
104 }
105
106 valid_params.AssignParameters(custom_params);
107
108 auto stepper = std::make_shared<ConstantTimeStepper>(valid_params);
109 Chi::object_stack.push_back(stepper);
110 stepper->SetStackID(Chi::object_stack.size() - 1);
111
112 return stepper;
113 }
114}
115
116std::string Solver::TextName() const { return text_name_; }
117
119
121
122std::vector<std::shared_ptr<FieldFunctionGridBased>>&
124{
125 return field_functions_;
126}
127
129{
130 ChiLogicalErrorIf(not timestepper_, "Bad trouble: Timestepper not assigned.");
131 return *timestepper_;
132}
133
135{
136 ChiLogicalErrorIf(not timestepper_, "Bad trouble: Timestepper not assigned.");
137 return *timestepper_;
138}
139
140const std::vector<std::shared_ptr<FieldFunctionGridBased>>&
142{
143 return field_functions_;
144}
145
147{
148 Chi::log.Log() << "\"Initialize()\" method not defined for " << TextName();
149}
150
152{
153 Chi::log.Log() << "\"Execute()\" method not defined for " << TextName();
154}
155
157{
158 Chi::log.Log() << "\"Step()\" method not defined for " << TextName();
159}
160
162{
163 Chi::log.Log() << "\"Advance()\" method not defined for " << TextName();
164}
165
167{
168 return chi::ParameterBlock{};
169}
170
173{
174 if (not params.Has("name"))
175 {
176 Chi::log.LogAllWarning() << "chi_physics::Solver::GetInfo called without "
177 "\"name\" in the parameter list";
178 return chi::ParameterBlock{};
179 }
180 return GetInfo(params);
181}
182
183/**\addtogroup SolverBase
184*
185* \section Properties Properties that can be set
186* The following properties can be set via the lua call
187* `chi_lua::chiSolverSetProperties`
188* \copydoc chi_physics::Solver::SetProperties*/
189
190/**
191Base solver settable properties:
192* - `dt`, Timestep size
193* - `time`, Current time
194* */
196{
197 for (const auto& param : params)
198 {
199 const std::string param_name = param.Name();
200
201 if (param_name == "dt")
202 timestepper_->SetTimeStepSize(param.GetValue<double>());
203 if (param_name == "time")
204 timestepper_->SetTime(param.GetValue<double>());
205 if (param_name == "start_time")
206 timestepper_->SetStartTime(param.GetValue<double>());
207 if (param_name == "end_time")
208 timestepper_->SetEndTime(param.GetValue<double>());
209 if (param_name == "max_time_steps")
210 timestepper_->SetMaxTimeSteps(param.GetValue<int>());
211 if (param_name == "dt_min")
212 timestepper_->SetMinimumTimeStepSize(param.GetValue<int>());
213 }
214}
215
216} // namespace chi_physics
#define ChiLogicalErrorIf(condition, message)
static std::vector< ChiObjectPtr > object_stack
Definition: chi_runtime.h:96
static chi::ChiLog & log
Definition: chi_runtime.h:81
static ChiObjectFactory & GetInstance() noexcept
static chi::InputParameters GetInputParameters()
Definition: ChiObject.cc:4
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
LogStream LogAllWarning()
Definition: chi_log.h:238
void AddRequiredParameter(const std::string &name, const std::string &doc_string)
const ParameterBlock & ParametersAtAssignment() const
void ConstrainParameterRange(const std::string &param_name, AllowableRangePtr allowable_range)
void AddOptionalParameter(const std::string &name, T value, const std::string &doc_string)
bool Has(const std::string &param_name) const
size_t NumParameters() const
void AddParameter(ParameterBlock block)
T GetParamValue(const std::string &param_name) const
ParameterBlock & GetParam(const std::string &param_name)
std::vector< std::shared_ptr< FieldFunctionGridBased > > field_functions_
Definition: chi_solver.h:58
TimeStepper & GetTimeStepper()
Definition: chi_solver.cc:128
std::string TextName() const
Definition: chi_solver.cc:116
virtual void Initialize()
Definition: chi_solver.cc:146
const std::string text_name_
Definition: chi_solver.h:64
static std::shared_ptr< TimeStepper > InitTimeStepper(const chi::InputParameters &params)
Definition: chi_solver.cc:70
static chi::InputParameters GetInputParameters()
Definition: chi_solver.cc:16
BasicOptions & GetBasicOptions()
Definition: chi_solver.cc:118
Solver(std::string in_text_name)
Definition: chi_solver.cc:48
std::shared_ptr< TimeStepper > timestepper_
Definition: chi_solver.h:59
virtual void SetProperties(const chi::ParameterBlock &params)
Definition: chi_solver.cc:195
virtual void Step()
Definition: chi_solver.cc:156
BasicOptions basic_options_
Definition: chi_solver.h:57
virtual void Execute()
Definition: chi_solver.cc:151
virtual void Advance()
Definition: chi_solver.cc:161
std::vector< std::shared_ptr< FieldFunctionGridBased > > & GetFieldFunctions()
Definition: chi_solver.cc:123
virtual chi::ParameterBlock GetInfo(const chi::ParameterBlock &params) const
Definition: chi_solver.cc:166
chi::ParameterBlock GetInfoWithPreCheck(const chi::ParameterBlock &params) const
Definition: chi_solver.cc:172