PolyFEM
Loading...
Searching...
No Matches
StateSolve.cpp
Go to the documentation of this file.
1#include <polyfem/State.hpp>
2
5
6namespace polyfem
7{
8 using namespace assembler;
9 using namespace io;
10 using namespace utils;
11
12 void State::init_solve(Eigen::MatrixXd &sol, Eigen::MatrixXd &pressure)
13 {
14 POLYFEM_SCOPED_TIMER("Setup RHS");
15
17
19 if (sol.cols() > 1) // ignore previous solutions
20 sol.conservativeResize(Eigen::NoChange, 1);
21
22 if (mixed_assembler != nullptr)
23 {
24 const int actual_dim = problem->is_scalar() ? 1 : mesh->dimension();
25
26 pressure.resize(0, 0);
27 sol.conservativeResize(rhs.size(), sol.cols());
28 // Zero initial pressure
29 sol.middleRows(n_bases * actual_dim, n_pressure_bases).setZero();
30 sol(sol.size() - 1) = 0;
31
32 sol_to_pressure(sol, pressure);
33 }
34
35 if (problem->is_time_dependent())
36 save_timestep(0, 0, 0, 0, sol, pressure);
37 }
38
39 namespace
40 {
41 bool read_initial_x_from_file(
42 const std::string &state_path,
43 const std::string &x_name,
44 const bool reorder,
45 const Eigen::VectorXi &in_node_to_node,
46 const int dim,
47 Eigen::MatrixXd &x)
48 {
49 if (state_path.empty())
50 return false;
51
52 if (!read_matrix(state_path, x_name, x))
53 {
54 logger().debug("Unable to read initial {} from file ({})", x_name, state_path);
55 return false;
56 }
57
58 if (reorder)
59 {
60 const int ndof = in_node_to_node.size() * dim;
61 // only reorder the first ndof rows
62 x.topRows(ndof) = utils::reorder_matrix(x.topRows(ndof), in_node_to_node, -1, dim);
63 }
64
65 return true;
66 }
67 } // namespace
68
69 void State::initial_solution(Eigen::MatrixXd &solution) const
70 {
71 assert(solve_data.rhs_assembler != nullptr);
72
73 const bool was_solution_loaded = read_initial_x_from_file(
74 resolve_input_path(args["input"]["data"]["state"]), "u",
75 args["input"]["data"]["reorder"], in_node_to_node,
76 mesh->dimension(), solution);
77
78 if (!was_solution_loaded)
79 {
80 if (problem->is_time_dependent())
81 solve_data.rhs_assembler->initial_solution(solution);
82 else
83 {
84 solution.resize(rhs.size(), 1);
85 solution.setZero();
86 }
87 }
88 }
89
90 void State::initial_velocity(Eigen::MatrixXd &velocity) const
91 {
92 assert(solve_data.rhs_assembler != nullptr);
93
94 const bool was_velocity_loaded = read_initial_x_from_file(
95 resolve_input_path(args["input"]["data"]["state"]), "v",
96 args["input"]["data"]["reorder"], in_node_to_node,
97 mesh->dimension(), velocity);
98
99 if (!was_velocity_loaded)
100 solve_data.rhs_assembler->initial_velocity(velocity);
101 }
102
103 void State::initial_acceleration(Eigen::MatrixXd &acceleration) const
104 {
105 assert(solve_data.rhs_assembler != nullptr);
106
107 const bool was_acceleration_loaded = read_initial_x_from_file(
108 resolve_input_path(args["input"]["data"]["state"]), "a",
109 args["input"]["data"]["reorder"], in_node_to_node,
110 mesh->dimension(), acceleration);
111
112 if (!was_acceleration_loaded)
113 solve_data.rhs_assembler->initial_acceleration(acceleration);
114 }
115} // namespace polyfem
int x
#define POLYFEM_SCOPED_TIMER(...)
Definition Timer.hpp:10
int n_bases
number of bases
Definition State.hpp:178
int n_pressure_bases
number of pressure bases
Definition State.hpp:180
void sol_to_pressure(Eigen::MatrixXd &sol, Eigen::MatrixXd &pressure)
splits the solution in solution and pressure for mixed problems
Definition State.cpp:386
Eigen::VectorXi in_node_to_node
Inpute nodes (including high-order) to polyfem nodes, only for isoparametric.
Definition State.hpp:450
void save_timestep(const double time, const int t, const double t0, const double dt, const Eigen::MatrixXd &sol, const Eigen::MatrixXd &pressure)
saves a timestep
void initial_solution(Eigen::MatrixXd &solution) const
Load or compute the initial solution.
std::unique_ptr< mesh::Mesh > mesh
current mesh, it can be a Mesh2D or Mesh3D
Definition State.hpp:466
std::shared_ptr< assembler::Problem > problem
current problem, it contains rhs and bc
Definition State.hpp:168
json args
main input arguments containing all defaults
Definition State.hpp:101
void initial_velocity(Eigen::MatrixXd &velocity) const
Load or compute the initial velocity.
void initial_acceleration(Eigen::MatrixXd &acceleration) const
Load or compute the initial acceleration.
std::shared_ptr< assembler::RhsAssembler > build_rhs_assembler() const
build a RhsAssembler for the problem
Definition State.hpp:248
void init_solve(Eigen::MatrixXd &sol, Eigen::MatrixXd &pressure)
initialize solver
std::string resolve_input_path(const std::string &path, const bool only_if_exists=false) const
Resolve input path relative to root_path() if the path is not absolute.
solver::SolveData solve_data
timedependent stuff cached
Definition State.hpp:324
std::shared_ptr< assembler::MixedAssembler > mixed_assembler
Definition State.hpp:159
Eigen::MatrixXd rhs
System right-hand side.
Definition State.hpp:207
std::shared_ptr< assembler::RhsAssembler > rhs_assembler
bool read_matrix(const std::string &path, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &mat)
Reads a matrix from a file. Determines the file format based on the path's extension.
Definition MatrixIO.cpp:18
Eigen::MatrixXd reorder_matrix(const Eigen::MatrixXd &in, const Eigen::VectorXi &in_to_out, int out_blocks=-1, const int block_size=1)
Reorder row blocks in a matrix.
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:42