PRISMS-PF Manual
Loading...
Searching...
No Matches
solver_base.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: © 2025 PRISMS Center at the University of Michigan
2// SPDX-License-Identifier: GNU Lesser General Public Version 2.1
3
4#pragma once
5
6#include <deal.II/base/exceptions.h>
7#include <deal.II/numerics/vector_tools.h>
8
9#include <boost/geometry/core/cs.hpp>
10
18#include <prismspf/core/types.h>
19
21
23
24#include <prismspf/config.h>
25
27
28template <unsigned int dim, unsigned int degree, typename number>
30{
31public:
36 SolverBase(SolveBlock _solve_block,
37 const SolveContext<dim, degree, number> &_solve_context)
38 : solve_block(std::move(_solve_block))
39 , solve_context(&_solve_context)
41 solve_context->get_field_attributes(),
42 solve_context->get_matrix_free_manager())
43 {}
44
48 virtual ~SolverBase() = default;
49
55 SolverBase(const SolverBase &solver_base) = delete;
56
63 operator=(const SolverBase &solver_base) = delete;
64
70 SolverBase(SolverBase &&solver_base) noexcept = delete;
71
78 operator=(SolverBase &&solver_base) noexcept = delete;
79
83 virtual void
84 init(const std::list<SolveBlock> &all_solve_blocks)
85 {
86 NewDependencyExtents extents(solve_block.field_indices, all_solve_blocks);
87 solutions.init(extents);
88
89 // Apply constraints.
90 solutions.apply_constraints();
91
92 if (solutions.num_levels() > 0)
93 {
94 solutions.reinit_mg_transfer(solve_context->get_dof_manager());
95 }
96 }
97
101 virtual void
103 {
104 // Apply constraints.
105 solutions.reinit();
106 solutions.apply_constraints();
107 if (solutions.num_levels() > 0)
108 {
109 solutions.reinit_mg_transfer(solve_context->get_dof_manager());
110 solutions.mg_transfer_down(
111 solve_context->get_dof_manager(),
112 solve_context->get_user_inputs().spatial_discretization.global_refinement,
113 true);
114 }
115 }
116
120 virtual void
122 {}
123
127 virtual void
129 {
130 if (solve_context->get_simulation_timer().get_increment() == 0 &&
131 solve_block.solve_timing == SolveTiming::Primary)
132 {
133 // Set the initial condition
135 }
136 else
137 {
138 this->solve_impl();
139 }
140 if (solve_context->get_simulation_timer().get_increment() == 0)
141 {
142 solutions.apply_initial_condition_for_old_fields();
143 }
144 if (solutions.num_levels() > 0)
145 {
146 solutions.mg_transfer_down(
147 solve_context->get_dof_manager(),
148 solve_context->get_user_inputs().spatial_discretization.global_refinement,
149 false);
150 }
151 }
152
156 virtual void
158 {
159 solutions.update();
160 }
161
165 virtual void
167 {
168 solutions.update_ghosts();
169 }
170
174 void
176 {
177 solutions.prepare_for_solution_transfer();
178 }
179
183 void
185 {
186 solutions.execute_solution_transfer();
187 }
188
192 virtual void
194 {}
195
199 void
201 {
202 for (const auto &global_index : solve_block.field_indices)
203 {
204 bool initialized_from_file = false;
205
206 // First, try to find this variable in IC files
207 const auto &initial_condition_parameters =
208 solve_context->get_user_inputs().load_ic_parameters;
209
210 for (const auto &initial_condition_file :
211 initial_condition_parameters.get_initial_condition_files())
212 {
213 auto name_it =
214 std::find(initial_condition_file.simulation_variable_names.begin(),
215 initial_condition_file.simulation_variable_names.end(),
216 solve_context->get_field_attributes()[global_index].name);
217 if (name_it != initial_condition_file.simulation_variable_names.end())
218 {
219 // Found in file - read from file
220 solutions.get_solution_vector(global_index).zero_out_ghost_values();
221 dealii::VectorTools::interpolate(
223 solve_context->get_dof_manager().get_field_dof_handler(global_index),
225 *name_it,
226 solve_context->get_field_attributes()[global_index].field_type,
227 initial_condition_file,
228 solve_context->get_user_inputs().spatial_discretization),
229 solutions.get_solution_vector(global_index));
230
231 initialized_from_file = true;
232 break; // Stop searching once found
233 }
234 }
235 // If not found in files, use programmatic IC
236 if (!initialized_from_file)
237 {
238 solutions.get_solution_vector(global_index).zero_out_ghost_values();
239 dealii::VectorTools::interpolate(
241 solve_context->get_dof_manager().get_field_dof_handler(global_index),
243 global_index,
244 solve_context->get_field_attributes()[global_index].field_type,
245 solve_context->get_pde_operator()),
246 solutions.get_solution_vector(global_index));
247 }
248 solutions.apply_initial_condition_for_old_fields();
249 }
250 }
251
255 [[nodiscard]] const GroupSolutionHandler<dim, number> &
257 {
258 return solutions;
259 }
260
266 {
267 return solutions;
268 }
269
273 [[nodiscard]] const SolveBlock &
275 {
276 return solve_block;
277 }
278
279protected:
284
289
294
295 std::vector<SolverBase<dim, degree, number> *> aux_solvers;
296};
297
298PRISMS_PF_END_NAMESPACE
Class that manages solution initialization and swapping with old solutions.
Definition group_solution_handler.h:56
Function for user-implemented initial conditions. These are only ever calculated for explicit time de...
Definition initial_conditions.h:37
Function for read-in of initial conditions.
Definition initial_conditions.h:69
Structure to hold the attributes of a solve-block.
Definition solve_block.h:59
This class provides context for a solver with ptrs to all the relevant dependencies.
Definition solve_context.h:34
void execute_solution_transfer()
Execute solution transfer (for AMR).
Definition solver_base.h:184
void set_initial_condition()
Set the initial conditions.
Definition solver_base.h:200
virtual void init(const std::list< SolveBlock > &all_solve_blocks)
Initialize the solver.
Definition solver_base.h:84
virtual void reinit()
Reinitialize the solution vectors & apply constraints.
Definition solver_base.h:102
const SolveBlock & get_solve_block() const
Get the solver context.
Definition solver_base.h:274
virtual void update()
Update the fields.
Definition solver_base.h:157
std::vector< SolverBase< dim, degree, number > * > aux_solvers
Definition solver_base.h:295
const GroupSolutionHandler< dim, number > & get_solution_manager() const
Get the solution handler.
Definition solver_base.h:256
virtual void solve()
Solve for a single update step.
Definition solver_base.h:128
virtual void print()
Print information about the solver to summary.log.
Definition solver_base.h:193
void prepare_for_solution_transfer()
Prepare for solution transfer (for AMR).
Definition solver_base.h:175
virtual ~SolverBase()=default
Destructor.
SolverBase(const SolverBase &solver_base)=delete
Copy constructor.
SolverBase(SolverBase &&solver_base) noexcept=delete
Move constructor.
SolverBase(SolveBlock _solve_block, const SolveContext< dim, degree, number > &_solve_context)
Constructor.
Definition solver_base.h:36
virtual void solve_impl()
Solve implementation.
Definition solver_base.h:121
SolverBase & operator=(SolverBase &&solver_base) noexcept=delete
Move assignment.
GroupSolutionHandler< dim, number > solutions
Solution vectors for fields handled by this solver.
Definition solver_base.h:293
const SolveContext< dim, degree, number > * solve_context
Solver context provides access to external information.
Definition solver_base.h:288
SolveBlock solve_block
Information about the solve block this handler is responsible for.
Definition solver_base.h:283
SolverBase & operator=(const SolverBase &solver_base)=delete
Copy assignment.
virtual void update_ghosts()
Update the ghosts.
Definition solver_base.h:166
GroupSolutionHandler< dim, number > & get_solution_manager()
Get the solution handler.
Definition solver_base.h:265
static const dealii::MappingQ1< dim > mapping
Mappings to and from reference cell.
Definition system_wide.h:36
Definition conditional_ostreams.cc:20
Definition vectorized_operations.h:17
@ Primary
Primary fields are initialized explicitly through initial conditions rather than through the solver o...
Definition solve_block.h:33
Definition dependency_extents.h:76