b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2dynamic_nonlinear_solver.H
1//------------------------------------------------------------------------
2// b2dynamic_nonlinear_solver.H --
3//
4//
5// written by Mathias Doreille
6// Neda Ebrahimi Pour <neda.ebrahimipour@dlr.de>
7//
8// (c) 2004-2012 SMR Engineering & Development SA
9// 2502 Bienne, Switzerland
10//
11// (c) 2023 Deutsches Zentrum für Luft- und Raumfahrt (DLR) e.V.
12// Linder Höhe, 51147 Köln
13//
14// All Rights Reserved. Proprietary source code. The contents of
15// this file may not be disclosed to third parties, copied or
16// duplicated in any form, in whole or in part, without the prior
17// written permission of SMR.
18//------------------------------------------------------------------------
19
20#ifndef B2DYNAMIC_NONLINEAR_SOLVER_H_
21#define B2DYNAMIC_NONLINEAR_SOLVER_H_
22
23#include "b2dynamic_nonlinear_utile.H"
24#include "b2ppconfig.h"
26#include "model/b2domain.H"
28#include "model/b2solution.H"
29#include "model/b2solver.H"
30#include "utils/b2logging.H"
31#include "utils/b2object.H"
32
33namespace b2000::solver {
34
35template <typename T, typename MATRIX_FORMAT>
36class DynamicNonLinearSolver : public Solver {
37public:
38 using mrbc_t = TypedModelReductionBoundaryCondition<T>;
39 using type_t = ObjectTypeComplete<DynamicNonLinearSolver, Solver::type_t>;
40
41 DynamicNonLinearSolver()
42 : residue_function(0),
43 implicit_parameter(nullptr),
44 explicit_parameter(nullptr),
45 bdf_solver(0) {}
46
47 ~DynamicNonLinearSolver() override {
48 if (residue_function) { residue_function->decref_or_free(); }
49 if (implicit_parameter) { implicit_parameter->decref_or_free(); }
50 if (explicit_parameter) { explicit_parameter->decref_or_free(); }
51 if (bdf_solver) { bdf_solver->decref_or_free(); }
52 }
53
54 void solve() override {
55 while (solve_iteration()) { ; }
56 }
57
58 bool solve_iteration() override {
59 if (case_ == nullptr) { solve_init(); }
60 bool res;
61 try {
62 res = bdf_solver->next();
63 } catch (...) {
64 SolutionDynamicNonlinear<T>& solution =
65 dynamic_cast<SolutionDynamicNonlinear<T>&>(model_->get_solution());
66 solution.terminate_stage(false);
67 RTable attributes;
68 solution.terminate_case(false, attributes);
69 throw;
70 }
71 if (!res) {
72 RTable attributes;
73 dynamic_cast<SolutionDynamicNonlinear<T>&>(model_->get_solution())
74 .terminate_case(true, attributes);
75 case_ = nullptr;
76 solve_init();
77 if (case_ == nullptr) { return false; }
78 }
79 return true;
80 }
81
82 static type_t type;
83
84protected:
85 void solve_init() {
86 if (model_ == nullptr) { Exception() << THROW; }
87
88 if (case_iterator.get() == nullptr) {
89 case_iterator = model_->get_case_list().get_case_iterator();
90 }
91
92 case_ = case_iterator->next();
93 if (case_ == nullptr) { return; }
94 if (case_->get_number_of_stage() == 0) {
95 Exception() << "The dynamic nonlinear solver must have a case that contains at least "
96 "one stage but the case "
97 << case_->get_id() << " does not have a stage." << THROW;
98 }
99
101
102 if (residue_function) { residue_function->decref_or_free(); }
103 if (implicit_parameter) { implicit_parameter->decref_or_free(); }
104 if (explicit_parameter) { explicit_parameter->decref_or_free(); }
105 if (bdf_solver) { bdf_solver->decref_or_free(); }
106
107 b2linalg::Matrix<T, b2linalg::Mrectangle> dof;
108 double time;
109 int stage;
110 std::string domain_state_id;
111
112 dynamic_cast<TypedInitialCondition<T>&>(model_->get_initial_condition())
113 .get_dynamic_initial_condition_value(dof, time, stage, domain_state_id);
114 case_->set_stage(stage);
115 if (!domain_state_id.empty()) { model_->get_domain().load_state(domain_state_id); }
116
117 std::string residue_function_name = case_->get_string("RESIDUE_FUNCTION_TYPE", "ORDER_N");
118 if (residue_function_name != "") { residue_function_name += "_DYNAMIC_RESIDUE_FUNCTION"; }
119 residue_function_name += type_name<T, MATRIX_FORMAT>();
120 typename DynamicResidueFunction<T, MATRIX_FORMAT>::type_t* residue_function_type =
121 DynamicResidueFunction<T, MATRIX_FORMAT>::type.get_subtype(
122 residue_function_name, solver::module);
123 residue_function = residue_function_type->new_object(allocator);
124 residue_function->set_case(case_);
125 residue_function->init(*model_, *case_);
126
127 std::string implicit_parameter_name =
128 case_->get_string("IMPLICIT_NORDSIECK_PARAMETER", "STANDARD_BDF");
129 if (implicit_parameter_name != "") { implicit_parameter_name += "_PARAMETER"; }
130 NordsieckParameter::type_t* implicit_parameter_type =
131 NordsieckParameter::type.get_subtype(implicit_parameter_name, solver::module);
132 implicit_parameter = implicit_parameter_type->new_object(allocator);
133 implicit_parameter->set_attribute(*case_);
134
135 std::string explicit_parameter_name =
136 case_->get_string("EXPLICIT_NORDSIECK_PARAMETER", "ADAMS_BASHFORTH");
137 if (explicit_parameter_name != "") { explicit_parameter_name += "_PARAMETER"; }
138 NordsieckParameter::type_t* explicit_parameter_type =
139 NordsieckParameter::type.get_subtype(explicit_parameter_name, solver::module);
140 explicit_parameter = explicit_parameter_type->new_object(allocator);
141 explicit_parameter->set_attribute(*case_);
142
143 mrbc_t& mrbc = dynamic_cast<mrbc_t&>(
144 model_->get_model_reduction_boundary_condition(mrbc_t::type.get_subtype(
145 "TypedModelReductionBoundaryConditionListComponent" + type_name<T>())));
146 b2linalg::Matrix<T, b2linalg::Mrectangle> dof_red(
147 mrbc.get_size(false, b2linalg::Vector<T, b2linalg::Vdense>::null, time), dof.size2());
148 mrbc.get_nonlinear_inverse_value(dof, time, dof_red);
149
150 bdf_solver = NordsieckSolver<T, MATRIX_FORMAT>::type.new_object(allocator);
151 bdf_solver->init(
152 *model_, residue_function, implicit_parameter, explicit_parameter, dof_red, time,
153 case_);
154 }
155
156 CaseList::case_iterator case_iterator;
157
158 Allocator allocator;
159
160 DynamicResidueFunction<T, MATRIX_FORMAT>* residue_function;
161 NordsieckParameter* implicit_parameter;
162 NordsieckParameter* explicit_parameter;
163 NordsieckSolver<T, MATRIX_FORMAT>* bdf_solver;
164};
165
166template <typename T, typename MATRIX_FORMAT>
167typename DynamicNonLinearSolver<T, MATRIX_FORMAT>::type_t
168 DynamicNonLinearSolver<T, MATRIX_FORMAT>::type(
169 "DynamicNonLinearSolver", type_name<T, MATRIX_FORMAT>(),
170 StringList("DYNAMIC_NONLINEAR", "TRANSIENT_NONLINEAR"), b2000::solver::module,
171 &Solver::type);
172
173} // namespace b2000::solver
174
175#endif // B2DYNAMIC_NONLINEAR_SOLVER_H_
#define THROW
Definition b2exception.H:198
Interface to C++ representations of FE solvers.
virtual void set_stage(int stage)=0
virtual std::string get_id() const =0
virtual size_t get_number_of_stage() const =0
virtual std::string get_string(const std::string &key) const =0
virtual void load_state(const std::string &state_id)=0
virtual Solution & get_solution()=0
virtual CaseList & get_case_list()=0
virtual void set_case(Case &case_)=0
virtual InitialCondition & get_initial_condition()=0
virtual Domain & get_domain()=0
virtual ModelReductionBoundaryCondition & get_model_reduction_boundary_condition(ObjectTypeIncomplete< ModelReductionBoundaryCondition > *bc_type)=0
Case * case_
This also.
Definition b2solver.H:93
Model * model_
Definition b2solver.H:91