20#include <polysolve/linear/FEMSolver.hpp>
24 using namespace varform::internal;
56 const bool is_time_dependent =
args.contains(
"time") && !
args[
"time"].is_null();
64 if (!
args.contains(
"preset_problem"))
66 problem = std::make_shared<assembler::GenericScalarProblem>(
"GenericScalar");
69 tmp[
"is_time_dependent"] = is_time_dependent;
72 auto bc =
args[
"boundary_conditions"];
86 t0 = is_time_dependent ?
args[
"time"][
"t0"].get<
double>() : 0.0;
87 time_steps = is_time_dependent ?
args[
"time"][
"time_steps"].get<
int>() : 0;
88 dt = is_time_dependent ?
args[
"time"][
"dt"].get<
double>() : 0.0;
95 logger().error(
"Load the mesh first!");
98 if (solution.size() <= 0)
100 logger().error(
"Solve the problem first!");
104 logger().info(
"Saving json...");
105 const Eigen::MatrixXd stats_solution =
115 args[
"output"][
"advanced"][
"sol_at_node"], j);
116 out << j.dump(4) << std::endl;
124 for (
int e = 0; e < output_orders.size(); ++e)
126 if (
mesh_->is_prism(e))
146 if (!
args[
"output"][
"advanced"][
"compute_error"])
150 if (!
args[
"time"].is_null())
151 tend =
args[
"time"][
"tend"];
153 Eigen::MatrixXd value, pressure;
164 logger().error(
"Load the mesh first!");
167 if (solution.size() <= 0)
169 logger().error(
"Solve the problem first!");
176 const bool has_time =
args.contains(
"time") && !
args[
"time"].is_null();
177 double tend = has_time ?
args[
"time"][
"tend"].get<
double>() : 1.0;
191 Eigen::MatrixXd value, pressure;
195 if (!solution_path.empty())
197 const int primary_ndof = std::min<int>(value.rows(),
space_.
n_bases);
198 const Eigen::MatrixXd primary_solution = value.topRows(primary_ndof);
201 const Eigen::MatrixXd nodal_solution =
utils::unflatten(primary_solution, 1);
202 Eigen::MatrixXd reordered = Eigen::MatrixXd::Zero(nodal_solution.rows(), nodal_solution.cols());
206 if (node >= 0 && node < nodal_solution.rows() && input_node < reordered.rows())
207 reordered.row(input_node) = nodal_solution.row(node);
218 if (!nodes_path.empty())
223 for (
const auto &global : basis.global())
224 nodes.row(global.index) = global.node;
232 Eigen::MatrixXd stress;
233 Eigen::VectorXd mises;
238 if (!stress_path.empty())
240 if (!mises_path.empty())
260 const std::vector<basis::ElementBases> &bases,
263 json rhs_solver_params =
args[
"solver"][
"linear"];
264 if (!rhs_solver_params.contains(
"Pardiso"))
265 rhs_solver_params[
"Pardiso"] = {};
266 rhs_solver_params[
"Pardiso"][
"mtype"] = -2;
268 return std::make_shared<assembler::RhsAssembler>(
273 args[
"space"][
"advanced"][
"bc_method"],
284 Eigen::VectorXi space_disc_orders;
287 if (
args[
"space"][
"use_p_ref"])
291 args[
"space"][
"advanced"][
"B"],
292 args[
"space"][
"advanced"][
"h1_formula"],
293 args[
"space"][
"discr_order"],
294 args[
"space"][
"advanced"][
"discr_order_max"],
298 logger().info(
"min p: {} max p: {}", space_disc_orders.minCoeff(), space_disc_orders.maxCoeff());
305 args[
"space"][
"basis_type"],
306 args[
"space"][
"poly_basis_type"],
309 args[
"space"][
"advanced"][
"quadrature_order"],
310 args[
"space"][
"advanced"][
"mass_quadrature_order"],
311 args[
"space"][
"advanced"][
"use_corner_quadrature"],
312 args[
"space"][
"advanced"][
"n_harmonic_samples"],
313 args[
"space"][
"advanced"][
"integral_constraints"],
321 if (
args[
"space"][
"advanced"][
"count_flipped_els"])
324 const int n_samples = 10;
334 logger().info(
"Building cache...");
338 logger().info(
" took {}s", timer.getElapsedTime());
354 const bool use_corner_quadrature =
args[
"space"][
"advanced"][
"use_corner_quadrature"];
355 const int quadrature_order =
args[
"space"][
"advanced"][
"quadrature_order"].get<
int>();
356 const int mass_quadrature_order =
args[
"space"][
"advanced"][
"mass_quadrature_order"].get<
int>();
357 const int order =
args[
"space"][
"pressure_discr_order"];
358 Eigen::VectorXi pressure_disc_orders(mesh.
n_elements());
359 pressure_disc_orders.setConstant(order);
361 const std::string pressure_basis_type =
args[
"space"][
"basis_type"].get<std::string>() ==
"Bernstein" ?
"Bernstein" :
"Lagrange";
365 pressure_disc_orders,
367 args[
"space"][
"poly_basis_type"],
371 mass_quadrature_order,
372 use_corner_quadrature,
373 args[
"space"][
"advanced"][
"n_harmonic_samples"],
374 args[
"space"][
"advanced"][
"integral_constraints"],
388 for (
const auto &lb : all_boundary)
431 delta = (max - min) / 2. + min;
433 p_params[
"bbox_center"] = {delta(0), delta(1), delta(2)};
435 p_params[
"bbox_center"] = {delta(0), delta(1)};
442 logger().info(
"Assigning rhs...");
452 const int prev_size =
rhs_.rows();
462 const int gdiscr_order =
mesh_->orders().size() <= 0 ? 1 :
mesh_->orders().maxCoeff();
464 tmp_rhs_assembler->set_bc(
472 if (!
problem->is_time_dependent())
482 logger().info(
"Assembling mass mat...");
485 for (
int k = 0; k <
mass_.outerSize(); ++k)
486 for (StiffnessMatrix::InnerIterator it(
mass_, k); it; ++it)
488 assert(it.col() == k);
492 if (
args[
"solver"][
"advanced"][
"lump_mass_matrix"])
517 if (!was_solution_loaded)
519 if (
problem->is_time_dependent())
523 sol.resize(
rhs_.size(), 1);
529 sol.conservativeResize(Eigen::NoChange, 1);
536 const int cols = std::max(1,
int(stacked.cols()));
539 const int primary_rows = std::min(
space_.
n_bases,
int(stacked.rows()));
540 if (primary_rows > 0)
541 primary.topRows(primary_rows) = stacked.topRows(primary_rows);
545 if (pressure_rows > 0)
546 pressure.topRows(pressure_rows) = stacked.middleRows(
space_.
n_bases, pressure_rows);
554 logger().info(
"Assembling stiffness mat...");
563 main_stiffness, mixed_stiffness, aux_stiffness, stiffness);
570 stats.
mat_size = (
long long)stiffness.rows() * (
long long)stiffness.cols();
575 const std::unique_ptr<polysolve::linear::Solver> &solver,
578 const bool compute_spectrum,
579 Eigen::MatrixXd &sol)
589 args[
"output"][
"data"][
"stiffness_mat"],
599 auto solver = polysolve::linear::Solver::create(
args[
"solver"][
"linear"],
logger());
600 logger().info(
"{}...", solver->name());
601 const int gdiscr_order =
mesh_->orders().size() <= 0 ? 1 :
mesh_->orders().maxCoeff();
608 Eigen::VectorXd b =
rhs_;
614 auto solver = polysolve::linear::Solver::create(
args[
"solver"][
"linear"],
logger());
615 logger().info(
"{}...", solver->name());
617 Eigen::MatrixXd value, pressure;
622 Eigen::MatrixXd::Zero(value.rows(), value.cols()),
623 Eigen::MatrixXd::Zero(value.rows(), value.cols()),
629 Eigen::MatrixXd current_rhs =
rhs_;
633 const int gdiscr_order =
mesh_->orders().size() <= 0 ? 1 :
mesh_->orders().maxCoeff();
638 const double time =
t0 + t *
dt;
647 const int old_rows = current_rhs.rows();
648 current_rhs.conservativeResize(
stacked_ndof(), current_rhs.cols());
650 current_rhs.bottomRows(
stacked_ndof() - old_rows).setZero();
655 Eigen::VectorXd b = Eigen::VectorXd::Zero(
stacked_ndof());
663 bdf->update_quantities(value.col(0));
679 if (
problem->is_time_dependent())
693 const Eigen::MatrixXd &solution,
696 std::vector<io::OutputField> fields;
700 Eigen::MatrixXd value, pressure;
705 const int primary_ndof = std::min<int>(value.rows(),
space_.
n_bases);
706 const Eigen::MatrixXd primary_solution = value.topRows(primary_ndof);
708 const auto sample_dof_field = [&](
const Eigen::MatrixXd &dof_values, Eigen::MatrixXd &values, Eigen::MatrixXd *gradients =
nullptr) ->
bool {
709 if (dof_values.size() <= 0)
712 if (has_element_samples)
724 gradients->row(i).setZero();
728 Eigen::MatrixXd local_sol, local_grad;
731 element_id, sample.
local_points.row(i), dof_values, local_sol, local_grad);
732 values(i) = local_sol(0);
734 gradients->row(i) = local_grad;
737 if (output_rows > values.rows())
739 const int previous_rows = values.rows();
740 values.conservativeResize(output_rows, Eigen::NoChange);
741 values.bottomRows(output_rows - previous_rows).setZero();
744 gradients->conservativeResize(output_rows, Eigen::NoChange);
745 gradients->bottomRows(output_rows - previous_rows).setZero();
753 values.resize(sample.
node_ids.size(), 1);
754 for (
int i = 0; i < sample.
node_ids.size(); ++i)
756 const int node_id = sample.
node_ids(i);
757 if (node_id < 0 || node_id >= dof_values.rows())
759 values(i) = dof_values(node_id);
761 return sample.
points.rows() == 0 || sample.
points.rows() == values.rows();
767 const auto ¶view_options =
args[
"output"][
"paraview"][
"options"];
768 if (has_element_samples &&
problem->has_exact_sol() && sample.
points.rows() == output_rows)
770 Eigen::MatrixXd exact;
772 if (exact.rows() == output_rows)
778 Eigen::MatrixXd values;
779 if (sample_dof_field(primary_solution, values))
785 if ((paraview_options[
"nodes"] || (!options.
fields.empty() && options.
export_field(
"nodes")))
786 && has_element_samples
789 Eigen::MatrixXd dof_ids(primary_ndof, 1);
790 dof_ids.col(0).setLinSpaced(primary_ndof, 0, primary_ndof - 1);
791 Eigen::MatrixXd values;
792 if (sample_dof_field(dof_ids, values))
796 if ((paraview_options[
"jacobian_validity"] || (!options.
fields.empty() && options.
export_field(
"validity")))
797 && has_element_samples
798 &&
mesh_->dimension() == 1
802 Eigen::MatrixXd validity = Eigen::MatrixXd::Zero(output_rows, 1);
803 for (
int i = 0; i < sample.
element_ids.size(); ++i)
804 validity(i) = std::find(invalid_elements.begin(), invalid_elements.end(), sample.
element_ids(i)) != invalid_elements.end();
808 const bool export_solution_gradient =
810 if (options.
export_field(
"solution") || export_solution_gradient)
812 Eigen::MatrixXd values, gradients;
813 if (sample_dof_field(
815 export_solution_gradient ? &gradients :
nullptr))
819 if (export_solution_gradient)
824 if (paraview_options[
"material"] && has_element_samples)
826 const auto ¶ms = primary_assembler_->parameters();
827 std::map<std::string, Eigen::MatrixXd> param_values;
828 for (
const auto &[p, _] : params)
829 param_values[p].setZero(output_rows, 1);
831 Eigen::MatrixXd rhos = Eigen::MatrixXd::Zero(output_rows, 1);
832 const auto &density = mass_assembler_->density();
833 for (
int i = 0; i < sample.local_points.rows(); ++i)
835 const int element_id = sample.element_ids(i);
839 for (
const auto &[p, func] : params)
840 param_values.at(p)(i) = func(sample.local_points.row(i), sample.points.row(i), sample.time, element_id);
841 rhos(i) = density(sample.local_points.row(i), sample.points.row(i), sample.time, element_id);
844 for (
const auto &[name, values] : param_values)
845 if (options.export_field(name))
847 if (options.export_field(
"rho"))
851 if (paraview_options[
"body_ids"] && options.export_field(
"body_ids") && has_element_samples)
853 Eigen::MatrixXd ids = Eigen::MatrixXd::Zero(output_rows, 1);
854 for (
int i = 0; i < sample.element_ids.size(); ++i)
856 const int element_id = sample.element_ids(i);
858 ids(i) = mesh_->get_body_id(element_id);
863 const bool export_pressure_gradient =
864 !options.fields.empty() && options.export_field(
"pressure_gradient");
865 if (mesh_ && (options.export_field(
"pressure") || export_pressure_gradient || (!options.fields.empty() && options.export_field(
"auxiliary"))))
867 Eigen::MatrixXd values, gradients;
869 *mesh_, pressure_space_.basis_list(),
space_.geometry_basis_list(), sample, pressure, values,
870 export_pressure_gradient ? &gradients : nullptr))
872 if (options.export_field(
"pressure"))
874 if (export_pressure_gradient)
876 if (!options.fields.empty() && options.export_field(
"auxiliary"))
std::array< Matrix< int, 3, 3 >, 3 > space_
static std::shared_ptr< MixedAssembler > make_mixed_assembler(const std::string &formulation)
static std::string other_assembler_name(const std::string &formulation)
static void merge_mixed_matrices(const int n_bases, const int n_pressure_bases, const int problem_dim, const bool add_average, const StiffnessMatrix &velocity_stiffness, const StiffnessMatrix &mixed_stiffness, const StiffnessMatrix &pressure_stiffness, StiffnessMatrix &stiffness)
utility to merge 3 blocks of mixed matrices, A=velocity_stiffness, B=mixed_stiffness,...
static std::shared_ptr< Assembler > make_assembler(const std::string &formulation)
Caches basis evaluation and geometric mapping at every element.
void init(const bool is_volume, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const bool is_mass=false)
computes the basis evaluation and geometric mapping for each of the given ElementBases in bases initi...
void init_empty(const bool is_mass=false)
initialize an empty cache.
Represents one basis function and its gradient.
Stores the basis functions for a given element in a mesh (facet in 2d, cell in 3d).
static void interpolate_at_local_vals(const mesh::Mesh &mesh, const bool is_problem_scalar, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const int el_index, const Eigen::MatrixXd &local_pts, const Eigen::MatrixXd &fun, Eigen::MatrixXd &result, Eigen::MatrixXd &result_grad)
interpolate solution and gradient at element (calls interpolate_at_local_vals with sol)
static void compute_stress_at_quadrature_points(const mesh::Mesh &mesh, const bool is_problem_scalar, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const Eigen::VectorXi &disc_orders, const Eigen::VectorXi &disc_ordersq, const assembler::Assembler &assembler, const Eigen::MatrixXd &fun, const double t, Eigen::MatrixXd &result, Eigen::VectorXd &von_mises)
compute von mises stress at quadrature points for the function fun, also compute the interpolated fun...
void export_data(const OutputSpace &space, const OutputFieldFunction &output_fields, const bool is_time_dependent, const double tend_in, const double dt, const ExportOptions &opts, const std::string &vis_mesh_path) const
exports everytihng, txt, vtu, etc
double assembling_stiffness_mat_time
time to assembly
double assigning_rhs_time
time to computing the rhs
double assembling_mass_mat_time
time to assembly mass
double solving_time
time to solve
int n_flipped
number of flipped elements, compute only when using count_flipped_els (false by default)
json solver_info
information of the solver, eg num iteration, time, errors, etc the informations varies depending on t...
Eigen::Vector4d spectrum
spectrum of the stiffness matrix, enable only if POLYSOLVE_WITH_SPECTRA is ON (off by default)
void count_flipped_elements(const polyfem::mesh::Mesh &mesh, const std::vector< polyfem::basis::ElementBases > &gbases)
counts the number of flipped elements
void compute_errors(const int n_bases, const std::vector< polyfem::basis::ElementBases > &bases, const std::vector< polyfem::basis::ElementBases > &gbases, const polyfem::mesh::Mesh &mesh, const assembler::Problem &problem, const double tend, const Eigen::MatrixXd &sol)
compute errors
void compute_mesh_size(const polyfem::mesh::Mesh &mesh_in, const std::vector< polyfem::basis::ElementBases > &bases_in, const int n_samples, const bool use_curved_mesh_size)
computes the mesh size, it samples every edges n_samples times uses curved_mesh_size (false by defaul...
long long nn_zero
non zeros and sytem matrix size num dof is the total dof in the system
double mesh_size
max edge lenght
void save_json(const nlohmann::json &args, const int n_bases, const int n_pressure_bases, const Eigen::MatrixXd &sol, const mesh::Mesh &mesh, const Eigen::VectorXi &disc_orders, const Eigen::VectorXi &disc_ordersq, const assembler::Problem &problem, const OutRuntimeData &runtime, const std::string &formulation, const bool isoparametric, const int sol_at_node_id, nlohmann::json &j) const
saves the output statistic to a json object
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
int n_elements() const
utitlity to return the number of elements, cells or faces in 3d and 2d
virtual void bounding_box(RowVectorNd &min, RowVectorNd &max) const =0
computes the bbox of the mesh
virtual bool is_volume() const =0
checks if mesh is volume
void update_nodes(const Eigen::VectorXi &in_node_to_node)
Update the node ids to reorder them.
static const ProblemFactory & factory()
std::shared_ptr< assembler::Problem > get_problem(const std::string &problem) const
static void p_refine(const mesh::Mesh &mesh, const double B, const bool h1_formula, const int base_p, const int discr_order_max, io::OutStatsData &stats, Eigen::VectorXi &disc_orders)
compute a priori prefinement
bool write_matrix(const std::string &path, const Mat &mat)
Writes a matrix to a file. Determines the file format based on the path's extension.
Eigen::SparseMatrix< double > lump_matrix(const Eigen::SparseMatrix< double > &M)
Lump each row of a matrix into the diagonal.
Eigen::MatrixXd unflatten(const Eigen::VectorXd &x, int dim)
Unflatten rowwises, so every dim elements in x become a row.
std::vector< int > count_invalid(const int dim, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const Eigen::VectorXd &u)
spdlog::logger & logger()
Retrieves the current logger.
std::array< int, 2 > QuadratureOrders
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
void log_and_throw_error(const std::string &msg)
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
bool export_field(const std::string &field) const
std::vector< std::string > fields
Eigen::VectorXi primitive_ids
Eigen::VectorXi element_ids
Eigen::MatrixXd local_points