PolyFEM
Loading...
Searching...
No Matches
AssemblerUtils.cpp
Go to the documentation of this file.
1
2#include "AssemblerUtils.hpp"
3
33
36
37// #include <unsupported/Eigen/SparseExtra>
38
39namespace polyfem
40{
41 using namespace basis;
42 using namespace utils;
43
44 namespace assembler
45 {
46
47 std::string AssemblerUtils::other_assembler_name(const std::string &formulation)
48 {
49 if (formulation == "Bilaplacian")
50 return "BilaplacianAux";
51 else if (formulation == "Stokes" || formulation == "NavierStokes" || formulation == "NavierStokesFSI" || formulation == "OperatorSplitting")
52 return "StokesPressure";
53 else if (formulation == "IncompressibleLinearElasticity")
54 return "IncompressibleLinearElasticityPressure";
55
56 return "";
57 }
58
59 std::shared_ptr<Assembler> AssemblerUtils::make_assembler(const std::string &formulation)
60 {
61 if (formulation == "Helmholtz")
62 return std::make_shared<Helmholtz>();
63 else if (formulation == "Laplacian")
64 return std::make_shared<Laplacian>();
65 else if (formulation == "Electrostatics")
66 return std::make_shared<Electrostatics>();
67
68 else if (formulation == "Bilaplacian")
69 return std::make_shared<BilaplacianMain>();
70 else if (formulation == "BilaplacianAux")
71 return std::make_shared<BilaplacianAux>();
72
73 else if (formulation == "LinearElasticity")
74 return std::make_shared<LinearElasticity>();
75 else if (formulation == "HookeLinearElasticity")
76 return std::make_shared<HookeLinearElasticity>();
77 else if (formulation == "IncompressibleLinearElasticity")
78 return std::make_shared<IncompressibleLinearElasticityDispacement>();
79 else if (formulation == "IncompressibleLinearElasticityPressure")
80 return std::make_shared<IncompressibleLinearElasticityPressure>();
81
82 else if (formulation == "SaintVenant")
83 return std::make_shared<SaintVenantElasticity>();
84 else if (formulation == "NeoHookean")
85 return std::make_shared<NeoHookeanElasticity>();
86 else if (formulation == "IsochoricNeoHookean")
87 return std::make_shared<IsochoricNeoHookean>();
88 else if (formulation == "MooneyRivlin")
89 return std::make_shared<MooneyRivlinElasticity>();
90 else if (formulation == "MooneyRivlin3Param")
91 return std::make_shared<MooneyRivlin3ParamElasticity>();
92 else if (formulation == "MooneyRivlin3ParamSymbolic")
93 return std::make_shared<MooneyRivlin3ParamSymbolic>();
94 else if (formulation == "MultiModels")
95 return std::make_shared<MultiModel>();
96 else if (formulation == "MaterialSum")
97 return std::make_shared<SumModel>();
98 else if (formulation == "UnconstrainedOgden")
99 return std::make_shared<UnconstrainedOgdenElasticity>();
100 else if (formulation == "IncompressibleOgden")
101 return std::make_shared<IncompressibleOgdenElasticity>();
102 else if (formulation == "VolumePenalty")
103 return std::make_shared<VolumePenalty>();
104 else if (formulation == "InversionBarrier")
105 return std::make_shared<InversionBarrier>();
106
107 else if (formulation == "HGOFiber")
108 return std::make_shared<HGOFiber>();
109 else if (formulation == "HGODispersion")
110 return std::make_shared<HGODispersion>();
111
112 else if (formulation == "ActiveFiber")
113 return std::make_shared<ActiveFiber>();
114
115 else if (formulation == "Stokes")
116 return std::make_shared<StokesVelocity>();
117 else if (formulation == "StokesPressure")
118 return std::make_shared<StokesPressure>();
119 else if (formulation == "NavierStokes")
120 return std::make_shared<NavierStokesVelocity>();
121 else if (formulation == "NavierStokesFSI")
122 return std::make_shared<NavierStokesVelocity>();
123 else if (formulation == "OperatorSplitting")
124 return std::make_shared<OperatorSplitting>();
125
126 else if (formulation == "AMIPS")
127 return std::make_shared<AMIPSEnergy>();
128 else if (formulation == "FixedCorotational")
129 return std::make_shared<FixedCorotational>();
130
131 log_and_throw_error("Inavalid assembler name {}", formulation);
132 }
133
134 std::shared_ptr<MixedAssembler> AssemblerUtils::make_mixed_assembler(const std::string &formulation)
135 {
136 if (formulation == "Bilaplacian")
137 return std::make_shared<BilaplacianMixed>();
138 else if (formulation == "IncompressibleLinearElasticity")
139 return std::make_shared<IncompressibleLinearElasticityMixed>();
140 else if (formulation == "Stokes" || formulation == "NavierStokes" || formulation == "NavierStokesFSI" || formulation == "OperatorSplitting")
141 return std::make_shared<StokesMixed>();
142
143 log_and_throw_error("Inavalid mixed assembler name {}", formulation);
144 }
145
146 std::shared_ptr<MixedNLAssembler> AssemblerUtils::make_mixed_nl_assembler(const std::string &formulation)
147 {
148 if (formulation == "ThermoElasticity")
149 return std::make_shared<ThermoElasticity>();
150 else if (formulation == "GrowthElasticity")
151 return std::make_shared<GrowthElasticity>();
152
153 log_and_throw_error("Inavalid mixed nonlinear assembler name {}", formulation);
154 }
155
157 const int n_bases, const int n_pressure_bases, const int problem_dim, const bool add_average,
158 const StiffnessMatrix &velocity_stiffness, const StiffnessMatrix &mixed_stiffness, const StiffnessMatrix &pressure_stiffness,
159 StiffnessMatrix &stiffness)
160 {
161 assert(velocity_stiffness.rows() == velocity_stiffness.cols());
162 assert(velocity_stiffness.rows() == n_bases * problem_dim);
163
164 assert(mixed_stiffness.size() == 0 || mixed_stiffness.rows() == n_bases * problem_dim);
165 assert(mixed_stiffness.size() == 0 || mixed_stiffness.cols() == n_pressure_bases);
166
167 assert(pressure_stiffness.size() == 0 || pressure_stiffness.rows() == n_pressure_bases);
168 assert(pressure_stiffness.size() == 0 || pressure_stiffness.cols() == n_pressure_bases);
169
170 const int avg_offset = add_average ? 1 : 0;
171
172 std::vector<Eigen::Triplet<double>> blocks;
173 blocks.reserve(velocity_stiffness.nonZeros() + 2 * mixed_stiffness.nonZeros() + pressure_stiffness.nonZeros() + 2 * avg_offset * velocity_stiffness.rows());
174
175 for (int k = 0; k < velocity_stiffness.outerSize(); ++k)
176 {
177 for (StiffnessMatrix::InnerIterator it(velocity_stiffness, k); it; ++it)
178 {
179 blocks.emplace_back(it.row(), it.col(), it.value());
180 }
181 }
182
183 for (int k = 0; k < mixed_stiffness.outerSize(); ++k)
184 {
185 for (StiffnessMatrix::InnerIterator it(mixed_stiffness, k); it; ++it)
186 {
187 blocks.emplace_back(it.row(), n_bases * problem_dim + it.col(), it.value());
188 blocks.emplace_back(it.col() + n_bases * problem_dim, it.row(), it.value());
189 }
190 }
191
192 for (int k = 0; k < pressure_stiffness.outerSize(); ++k)
193 {
194 for (StiffnessMatrix::InnerIterator it(pressure_stiffness, k); it; ++it)
195 {
196 blocks.emplace_back(n_bases * problem_dim + it.row(), n_bases * problem_dim + it.col(), it.value());
197 }
198 }
199
200 if (add_average)
201 {
202 const double val = 1.0 / n_pressure_bases;
203 for (int i = 0; i < n_pressure_bases; ++i)
204 {
205 blocks.emplace_back(n_bases * problem_dim + i, n_bases * problem_dim + n_pressure_bases, val);
206 blocks.emplace_back(n_bases * problem_dim + n_pressure_bases, n_bases * problem_dim + i, val);
207 }
208 }
209
210 stiffness.resize(n_bases * problem_dim + n_pressure_bases + avg_offset, n_bases * problem_dim + n_pressure_bases + avg_offset);
211 stiffness.setFromTriplets(blocks.begin(), blocks.end());
212 stiffness.makeCompressed();
213
214 // static int c = 0;
215 // Eigen::saveMarket(stiffness, "stiffness.txt");
216 // Eigen::saveMarket(velocity_stiffness, "velocity_stiffness.txt");
217 // Eigen::saveMarket(mixed_stiffness, "mixed_stiffness.txt");
218 // Eigen::saveMarket(pressure_stiffness, "pressure_stiffness.txt");
219 }
220
221 int AssemblerUtils::quadrature_order(const std::string &assembler, const int basis_degree, const BasisType &b_type, const int dim)
222 {
223 // note: minimum quadrature order is always 1
224 if (assembler == "Mass")
225 {
226 // multiply by two since we are multiplying phi_i by phi_j
227 if (b_type == BasisType::SIMPLEX_LAGRANGE || b_type == BasisType::CUBE_LAGRANGE)
228 return std::max(basis_degree * 2, 1);
229 else
230 return basis_degree * 2 + 1;
231 }
232 else if (assembler == "NavierStokes" || assembler == "NavierStokesFSI")
233 {
234 if (b_type == BasisType::SIMPLEX_LAGRANGE)
235 return std::max((basis_degree - 1) + basis_degree, 1);
236 else if (b_type == BasisType::CUBE_LAGRANGE)
237 return std::max(basis_degree * 2, 1);
238 else
239 return basis_degree * 2 + 1;
240 }
241 else
242 {
243 // subtract one since we take a derivative (lowers polynomial order by 1)
244 // multiply by two since we are multiplying grad phi_i by grad phi_j
245 if (b_type == BasisType::SIMPLEX_LAGRANGE)
246 {
247 return std::max((basis_degree - 1) * 2, 1);
248 }
249 else if (b_type == BasisType::CUBE_LAGRANGE || b_type == BasisType::PRISM_LAGRANGE || b_type == BasisType::PYRAMID_LAGRANGE)
250 {
251 // in this case we have a tensor product basis
252 // this computes the quadrature order along a single axis
253 // the Quadrature itself takes a tensor product of the given quadrature points
254 // to form the full quadrature for the basis
255 // taking a gradient leaves at least one variable whose power remains unchanged
256 // thus, we don't subtract 1
257 // note that this is overkill for the variable that was differentiated
258 return std::max(basis_degree * 2, 1);
259 }
260 else
261 {
262 return (basis_degree - 1) * 2 + 1;
263 }
264 }
265 }
266
267 std::vector<std::string> AssemblerUtils::elastic_materials()
268 {
269 const static std::vector<std::string> elastic_materials = {
270 "LinearElasticity",
271 "HookeLinearElasticity",
272 "SaintVenant",
273 "NeoHookean",
274 "MooneyRivlin",
275 "MooneyRivlin3Param",
276 "MooneyRivlin3ParamSymbolic",
277 "UnconstrainedOgden",
278 "IncompressibleOgden",
279 "IsochoricNeoHookean",
280 "HGOFiber",
281 "HGODispersion",
282 "ActiveFiber",
283 "FixedCorotational",
284 "VolumePenalty",
285 "AMIPS",
286 "MaterialSum",
287 "MultiModels"};
288
289 return elastic_materials;
290 }
291
292 bool AssemblerUtils::is_elastic_material(const std::string &material)
293 {
294 for (const auto &m : elastic_materials())
295 {
296 if (material == m)
297 return true;
298 }
299 return false;
300 }
301
303 {
304 for (const auto &m : AssemblerUtils::elastic_materials())
305 {
306 // skip multimodels
307 // this is a special case where we have multiple models
308 // and we need to create a new assembler for each model
309 // this is handled in the MultiModel class
310 // and not here
311 if (m == "MultiModels")
312 continue;
313 const auto assembler = AssemblerUtils::make_assembler(m);
314 // cast assembler to elasticity assembler
315 elastic_material_map_[m] = std::dynamic_pointer_cast<ElasticityNLAssembler>(assembler);
316 assert(elastic_material_map_[m] != nullptr);
317 }
318 }
319
320 void AllElasticMaterials::set_size(const int size)
321 {
322 for (auto &it : elastic_material_map_)
323 {
324 it.second->set_size(size);
325 }
326 }
327
328 void AllElasticMaterials::add_multimaterial(const int index, const json &params, const Units &units, const io::ResourceIO &resources)
329 {
330 for (auto &it : elastic_material_map_)
331 {
332 it.second->add_multimaterial(index, params, units, resources);
333 }
334 }
335
336 std::shared_ptr<assembler::ElasticityNLAssembler> AllElasticMaterials::get_assembler(const std::string &name) const
337 {
338 return elastic_material_map_.at(name);
339 }
340
341 std::map<std::string, Assembler::ParamFunc> AllElasticMaterials::parameters() const
342 {
343 std::map<std::string, Assembler::ParamFunc> params;
344 for (const auto &m : elastic_material_map_)
345 {
346 const auto assembler = m.second;
347 auto p = assembler->parameters();
348 for (auto &it : p)
349 {
350 params[m.first + "/" + it.first] = it.second;
351 }
352 }
353 return params;
354 }
355 } // namespace assembler
356} // namespace polyfem
double val
void add_multimaterial(const int index, const json &params, const Units &units, const io::ResourceIO &resources)
std::shared_ptr< assembler::ElasticityNLAssembler > get_assembler(const std::string &name) const
std::map< std::string, Assembler::ParamFunc > parameters() const
std::unordered_map< std::string, std::shared_ptr< assembler::ElasticityNLAssembler > > elastic_material_map_
static std::shared_ptr< MixedAssembler > make_mixed_assembler(const std::string &formulation)
static std::string other_assembler_name(const std::string &formulation)
static int quadrature_order(const std::string &assembler, const int basis_degree, const BasisType &b_type, const int dim)
utility for retrieving the needed quadrature order to precisely integrate the given form on the given...
static std::shared_ptr< MixedNLAssembler > make_mixed_nl_assembler(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 bool is_elastic_material(const std::string &material)
utility to check if material is one of the elastic materials
static std::vector< std::string > elastic_materials()
list of all elastic materials
static std::shared_ptr< Assembler > make_assembler(const std::string &formulation)
Read-only interface to a hierarchical collection of simulation resources.
nlohmann::json json
Definition Common.hpp:9
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:73
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:24