18#include <unsupported/Eigen/SparseExtra>
20#include <polysolve/linear/FEMSolver.hpp>
30 const std::string full_mat_path =
args[
"output"][
"data"][
"full_mat"];
31 if (!full_mat_path.empty())
32 Eigen::saveMarket(stiffness, full_mat_path);
61 const bool is_time_dependent =
args.contains(
"time") && !
args[
"time"].is_null();
70 if (!
args.contains(
"preset_problem"))
72 problem = std::make_shared<assembler::GenericScalarProblem>(
"GenericScalar");
76 tmp[
"is_time_dependent"] = is_time_dependent;
79 auto bc =
args[
"boundary_conditions"];
94 t0 = is_time_dependent ?
args[
"time"][
"t0"].get<
double>() : 0.0;
95 time_steps = is_time_dependent ?
args[
"time"][
"time_steps"].get<
int>() : 0;
96 dt = is_time_dependent ?
args[
"time"][
"dt"].get<
double>() : 0.0;
115 Eigen::VectorXi space_disc_orders;
118 if (
args[
"space"][
"use_p_ref"])
122 args[
"space"][
"advanced"][
"B"],
123 args[
"space"][
"advanced"][
"h1_formula"],
124 args[
"space"][
"discr_order"],
125 args[
"space"][
"advanced"][
"discr_order_max"],
129 logger().info(
"min p: {} max p: {}", space_disc_orders.minCoeff(), space_disc_orders.maxCoeff());
136 args[
"space"][
"basis_type"],
137 args[
"space"][
"poly_basis_type"],
140 args[
"space"][
"advanced"][
"quadrature_order"],
141 args[
"space"][
"advanced"][
"mass_quadrature_order"],
142 args[
"space"][
"advanced"][
"use_corner_quadrature"],
143 args[
"space"][
"advanced"][
"n_harmonic_samples"],
144 args[
"space"][
"advanced"][
"integral_constraints"],
162 std::vector<int> unused_neumann_boundary_nodes;
170 unused_neumann_boundary_nodes,
189 if (
problem->is_nodal_dimension_dirichlet(n_id, tag, 0))
199 if (
args[
"space"][
"advanced"][
"count_flipped_els"])
202 const int n_samples = 10;
212 logger().info(
"Building cache...");
216 logger().info(
" took {}s", timer.getElapsedTime());
228 json rhs_solver_params =
args[
"solver"][
"linear"];
229 if (!rhs_solver_params.contains(
"Pardiso"))
230 rhs_solver_params[
"Pardiso"] = {};
231 rhs_solver_params[
"Pardiso"][
"mtype"] = -2;
238 args[
"space"][
"advanced"][
"bc_method"],
252 delta = (max - min) / 2. + min;
254 p_params[
"bbox_center"] = {delta(0), delta(1), delta(2)};
256 p_params[
"bbox_center"] = {delta(0), delta(1)};
263 logger().info(
"Assigning rhs...");
276 if (!
problem->is_time_dependent())
287 logger().info(
"Assembling mass mat...");
291 assert(
mass_.size() > 0);
294 for (
int k = 0; k <
mass_.outerSize(); ++k)
296 for (StiffnessMatrix::InnerIterator it(
mass_, k); it; ++it)
298 assert(it.col() == k);
306 if (
args[
"solver"][
"advanced"][
"lump_mass_matrix"])
328 if (!was_solution_loaded)
330 if (
problem->is_time_dependent())
334 solution.resize(
rhs_.size(), 1);
344 logger().error(
"Load the mesh first!");
347 if (solution.size() <= 0)
349 logger().error(
"Solve the problem first!");
353 logger().info(
"Saving json...");
355 const Eigen::MatrixXd stats_solution =
356 solution.rows() >= primary_size
357 ? solution.topRows(primary_size).eval()
365 args[
"output"][
"advanced"][
"sol_at_node"], j);
366 out << j.dump(4) << std::endl;
374 for (
int e = 0; e < output_orders.size(); ++e)
376 if (
mesh_->is_prism(e))
396 if (!
args[
"output"][
"advanced"][
"compute_error"])
400 if (!
args[
"time"].is_null())
401 tend =
args[
"time"][
"tend"];
412 logger().error(
"Load the mesh first!");
415 if (solution.size() <= 0)
417 logger().error(
"Solve the problem first!");
424 const bool has_time =
args.contains(
"time") && !
args[
"time"].is_null();
425 double tend = has_time ?
args[
"time"][
"tend"].get<
double>() : 1.0;
440 if (!solution_path.empty())
442 const int primary_ndof = std::min<int>(solution.rows(),
space_.
n_bases);
443 const Eigen::MatrixXd primary_solution = solution.topRows(primary_ndof);
446 const Eigen::MatrixXd nodal_solution =
utils::unflatten(primary_solution, 1);
447 Eigen::MatrixXd reordered = Eigen::MatrixXd::Zero(nodal_solution.rows(), nodal_solution.cols());
451 if (node >= 0 && node < nodal_solution.rows() && input_node < reordered.rows())
452 reordered.row(input_node) = nodal_solution.row(node);
463 if (!nodes_path.empty())
468 for (
const auto &global : basis.global())
469 nodes.row(global.index) = global.node;
477 Eigen::MatrixXd stress;
478 Eigen::VectorXd mises;
483 if (!stress_path.empty())
485 if (!mises_path.empty())
492 const Eigen::MatrixXd &solution,
495 std::vector<io::OutputField> fields;
502 const int primary_ndof = std::min<int>(solution.rows(),
space_.
n_bases);
503 const Eigen::MatrixXd primary_solution = solution.topRows(primary_ndof);
505 const auto sample_dof_field = [&](
const Eigen::MatrixXd &dof_values, Eigen::MatrixXd &values, Eigen::MatrixXd *gradients =
nullptr) ->
bool {
506 if (dof_values.size() <= 0)
509 if (has_element_samples)
521 gradients->row(i).setZero();
525 Eigen::MatrixXd local_sol, local_grad;
528 element_id, sample.
local_points.row(i), dof_values, local_sol, local_grad);
529 values(i) = local_sol(0);
531 gradients->row(i) = local_grad;
534 if (output_rows > values.rows())
536 const int previous_rows = values.rows();
537 values.conservativeResize(output_rows, Eigen::NoChange);
538 values.bottomRows(output_rows - previous_rows).setZero();
541 gradients->conservativeResize(output_rows, Eigen::NoChange);
542 gradients->bottomRows(output_rows - previous_rows).setZero();
550 values.resize(sample.
node_ids.size(), 1);
551 for (
int i = 0; i < sample.
node_ids.size(); ++i)
553 const int node_id = sample.
node_ids(i);
554 if (node_id < 0 || node_id >= dof_values.rows())
556 values(i) = dof_values(node_id);
558 return sample.
points.rows() == 0 || sample.
points.rows() == values.rows();
564 const auto ¶view_options =
args[
"output"][
"paraview"][
"options"];
565 if (has_element_samples &&
problem->has_exact_sol() && sample.
points.rows() == output_rows)
567 Eigen::MatrixXd exact;
569 if (exact.rows() == output_rows)
575 Eigen::MatrixXd values;
576 if (sample_dof_field(primary_solution, values))
582 if ((paraview_options[
"nodes"] || (!options.
fields.empty() && options.
export_field(
"nodes")))
583 && has_element_samples
586 Eigen::MatrixXd dof_ids(primary_ndof, 1);
587 dof_ids.col(0).setLinSpaced(primary_ndof, 0, primary_ndof - 1);
588 Eigen::MatrixXd values;
589 if (sample_dof_field(dof_ids, values))
593 if ((paraview_options[
"jacobian_validity"] || (!options.
fields.empty() && options.
export_field(
"validity")))
594 && has_element_samples
595 &&
mesh_->dimension() == 1
599 Eigen::MatrixXd validity = Eigen::MatrixXd::Zero(output_rows, 1);
600 for (
int i = 0; i < sample.
element_ids.size(); ++i)
601 validity(i) = std::find(invalid_elements.begin(), invalid_elements.end(), sample.
element_ids(i)) != invalid_elements.end();
605 const bool export_solution_gradient =
607 if (options.
export_field(
"solution") || export_solution_gradient)
609 Eigen::MatrixXd values, gradients;
610 if (sample_dof_field(
612 export_solution_gradient ? &gradients :
nullptr))
616 if (export_solution_gradient)
621 if (paraview_options[
"material"] && has_element_samples)
623 const auto ¶ms = primary_assembler_->parameters();
624 std::map<std::string, Eigen::MatrixXd> param_values;
625 for (
const auto &[p, _] : params)
626 param_values[p].setZero(output_rows, 1);
628 Eigen::MatrixXd rhos = Eigen::MatrixXd::Zero(output_rows, 1);
629 const auto &density = mass_assembler_->density();
630 for (
int i = 0; i < sample.local_points.rows(); ++i)
632 const int element_id = sample.element_ids(i);
636 for (
const auto &[p, func] : params)
637 param_values.at(p)(i) = func(sample.local_points.row(i), sample.points.row(i), sample.time, element_id);
638 rhos(i) = density(sample.local_points.row(i), sample.points.row(i), sample.time, element_id);
641 for (
const auto &[name, values] : param_values)
642 if (options.export_field(name))
644 if (options.export_field(
"rho"))
648 if (paraview_options[
"body_ids"] && options.export_field(
"body_ids") && has_element_samples)
650 Eigen::MatrixXd ids = Eigen::MatrixXd::Zero(output_rows, 1);
651 for (
int i = 0; i < sample.element_ids.size(); ++i)
653 const int element_id = sample.element_ids(i);
655 ids(i) = mesh_->get_body_id(element_id);
667 logger().info(
"Assembling stiffness mat...");
668 assert(primary_assembler_->is_linear());
669 assert(problem->is_scalar());
671 primary_assembler_->assemble(mesh_->is_volume(),
space_.n_bases,
space_.basis_list(),
space_.geometry_basis_list(), ass_vals_cache_, 0, stiffness);
674 timings.assembling_stiffness_mat_time = timer.getElapsedTime();
675 logger().info(
" took {}s", timings.assembling_stiffness_mat_time);
677 stats.nn_zero = stiffness.nonZeros();
678 stats.num_dofs = stiffness.rows();
679 stats.mat_size = (
long long)stiffness.rows() * (
long long)stiffness.cols();
680 logger().info(
"sparsity: {}/{}", stats.nn_zero, stats.mat_size);
685 void ScalarVarForm::solve_linear_system(
686 const std::unique_ptr<polysolve::linear::Solver> &solver,
689 const bool compute_spectrum,
690 Eigen::MatrixXd &sol)
692 assert(primary_assembler_->is_linear());
693 assert(problem->is_scalar());
694 assert(rhs_assembler_ !=
nullptr);
697 stats.spectrum = dirichlet_solve(
701 boundary_.boundary_nodes,
704 args[
"output"][
"data"][
"stiffness_mat"],
710 solver->get_info(stats.solver_info);
712 const auto error = (A *
x - b).norm();
714 logger().error(
"Solver error: {}", error);
716 logger().debug(
"Solver error: {}", error);
719 void ScalarVarForm::solve_static(Eigen::MatrixXd &sol)
721 auto solver = polysolve::linear::Solver::create(args[
"solver"][
"linear"],
logger());
722 logger().info(
"{}...", solver->name());
724 const int gdiscr_order = mesh_->orders().size() <= 0 ? 1 : mesh_->orders().maxCoeff();
725 const QuadratureOrders boundary_samples = n_boundary_samples(
space_.disc_orders.maxCoeff(), gdiscr_order);
727 rhs_assembler_->set_bc(
728 boundary_.local_boundary, boundary_.boundary_nodes, boundary_samples,
729 (primary_assembler_->name() !=
"Bilaplacian") ? boundary_.local_neumann_boundary : std::vector<mesh::LocalBoundary>(), rhs_);
732 build_stiffness_mat(A);
734 Eigen::VectorXd b = rhs_;
735 solve_linear_system(solver, A, b, args[
"output"][
"advanced"][
"spectrum"], sol);
738 void ScalarVarForm::solve_transient(Eigen::MatrixXd &sol)
740 assert(problem->is_time_dependent());
741 assert(rhs_assembler_ !=
nullptr);
743 auto solver = polysolve::linear::Solver::create(args[
"solver"][
"linear"],
logger());
744 logger().info(
"{}...", solver->name());
747 args[
"time"][
"integrator"]);
748 bdf->init(sol, Eigen::VectorXd::Zero(sol.size()), Eigen::VectorXd::Zero(sol.size()), dt);
749 time_integrator = bdf;
751 save_timestep(t0, 0, t0, dt, sol);
753 Eigen::MatrixXd current_rhs = rhs_;
756 build_stiffness_mat(stiffness);
758 const int gdiscr_order = mesh_->orders().size() <= 0 ? 1 : mesh_->orders().maxCoeff();
760 for (
int t = 1; t <= time_steps; ++t)
762 const double time = t0 + t * dt;
764 rhs_assembler_->compute_energy_grad(
765 boundary_.local_boundary, boundary_.boundary_nodes, mass_assembler_->density(), n_b_samples,
766 boundary_.local_neumann_boundary, rhs_, time, current_rhs);
768 rhs_assembler_->set_bc(
769 boundary_.local_boundary, boundary_.boundary_nodes, n_b_samples, boundary_.local_neumann_boundary, current_rhs, sol, time);
772 Eigen::VectorXd b = (mass_ * bdf->weighted_sum_x_prevs()) / bdf->beta_dt();
773 for (
int i : boundary_.boundary_nodes)
777 solve_linear_system(solver, A, b, args[
"output"][
"advanced"][
"spectrum"].get<bool>() && t == time_steps, sol);
779 bdf->update_quantities(sol);
780 save_timestep(time, t, t0, dt, sol);
781 save_step_state(t0, dt, t, time_integrator.get());
783 logger().info(
"{}/{} t={}", t, time_steps, time);
784 notify_time_step(t, time_steps, t0, dt);
788 void ScalarVarForm::solve_problem(Eigen::MatrixXd &sol)
790 stats.spectrum.setZero();
794 logger().info(
"Solving {}", primary_assembler_->name());
800 prepare_initial_solution(sol);
803 sol.conservativeResize(Eigen::NoChange, 1);
806 time_integrator =
nullptr;
807 if (problem->is_time_dependent())
808 solve_transient(sol);
813 timings.solving_time = timer.getElapsedTime();
814 logger().info(
" took {}s", timings.solving_time);
std::array< Matrix< int, 3, 3 >, 3 > space_
#define POLYFEM_SCOPED_TIMER(...)
static std::shared_ptr< Assembler > make_assembler(const std::string &formulation)
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 assigning_rhs_time
time to computing the rhs
double assembling_mass_mat_time
time to assembly mass
int n_flipped
number of flipped elements, compute only when using count_flipped_els (false 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.
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.
virtual int get_node_id(const int node_id) const
Get the boundary selection of a node.
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
static std::shared_ptr< BDF > construct_bdf_integrator(const json ¶ms, DynamicOrder dynamic_order=DynamicOrder::Second)
Construct a BDF integrator for algorithms using BDF-specific operations.
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, const unsigned max_iter)
spdlog::logger & logger()
Retrieves the current logger.
std::array< int, 2 > QuadratureOrders
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
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