FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
hyperelasticity_functional.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
9#include <kernel/assembly/slip_filter_assembler.hpp>
10#include <kernel/assembly/unit_filter_assembler.hpp>
11#include <kernel/geometry/export_vtk.hpp>
12#include <kernel/geometry/mesh_node.hpp>
13#include <kernel/lafem/filter_chain.hpp>
14#include <kernel/lafem/filter_sequence.hpp>
15#include <kernel/lafem/slip_filter.hpp>
16#include <kernel/lafem/unit_filter_blocked.hpp>
17#include <kernel/meshopt/mesh_concentration_function.hpp>
18#include <kernel/meshopt/mesh_quality_functional.hpp>
19#include <kernel/meshopt/rumpf_trafo.hpp>
20#include <kernel/util/dist.hpp>
21
22#include <map>
23
24// This is for the explicit template instantiation below
25#ifdef FEAT_EICKT
26#include <kernel/meshopt/rumpf_functional.hpp>
27#include <kernel/meshopt/rumpf_functionals/p1.hpp>
28#include <kernel/meshopt/rumpf_functionals/q1.hpp>
29#endif
30
31
32namespace FEAT
33{
34 namespace Meshopt
35 {
36
48 {
49 undefined = 0,
50 once_uniform,
51 once_cellsize,
52 once_concentration,
53 current_uniform,
54 current_cellsize,
55 current_concentration,
56 iter_concentration
57 };
58
60
63 inline std::ostream& operator<<(std::ostream& os, ScaleComputation scale_computation)
64 {
65 switch(scale_computation)
66 {
67 case ScaleComputation::undefined:
68 return os << "undefined";
69 case ScaleComputation::once_uniform:
70 return os << "once_uniform";
71 case ScaleComputation::once_cellsize:
72 return os << "once_cellsize";
73 case ScaleComputation::once_concentration:
74 return os << "once_concentration";
75 case ScaleComputation::current_uniform:
76 return os << "current_uniform";
77 case ScaleComputation::current_cellsize:
78 return os << "current_cellsize";
79 case ScaleComputation::current_concentration:
80 return os << "current_concentration";
81 case ScaleComputation::iter_concentration:
82 return os << "iter_concentration";
83 default:
84 return os << "-unknown-";
85 }
86 }
87
88 inline void operator<<(ScaleComputation& scale_computation, const String& sc_name)
89 {
90 if(sc_name == "undefined")
91 scale_computation = ScaleComputation::undefined;
92 else if(sc_name == "once_uniform")
93 scale_computation = ScaleComputation::once_uniform;
94 else if(sc_name == "once_cellsize")
95 scale_computation = ScaleComputation::once_cellsize;
96 else if(sc_name == "once_concentration")
97 scale_computation = ScaleComputation::once_concentration;
98 else if(sc_name == "current_uniform")
99 scale_computation = ScaleComputation::current_uniform;
100 else if(sc_name == "current_cellsize")
101 scale_computation = ScaleComputation::current_cellsize;
102 else if(sc_name == "current_concentration")
103 scale_computation = ScaleComputation::current_concentration;
104 else if(sc_name == "iter_concentration")
105 scale_computation = ScaleComputation::iter_concentration;
106 else
107 XABORTM("Unknown ScaleComputation identifier string " +sc_name);
108 }
110
165 template
166 <
167 typename DT_,
168 typename IT_,
169 typename Trafo_,
170 typename CellFunctionalType_,
171 typename RefCellTrafo_ = RumpfTrafo<Trafo_, typename Trafo_::MeshType::CoordType>
172 >
174 public MeshQualityFunctional<typename Trafo_::MeshType>
175 {
176 public :
178 typedef Trafo_ TrafoType;
180 typedef typename TrafoType::MeshType MeshType;
182 typedef typename MeshType::CoordType CoordType;
184 typedef CellFunctionalType_ CellFunctionalType;
185
187 typedef RefCellTrafo_ RefCellTrafo;
188
193
196
198 static constexpr int block_height = MeshType::world_dim;
200 static constexpr int block_width = MeshType::world_dim;
201
203 typedef typename MeshType::ShapeType ShapeType;
204
219
221 typedef typename Intern::TrafoFE<TrafoType>::Space SpaceType;
222
231
233 static_assert(std::is_same<ShapeType, typename CellFunctionalType::ShapeType>::value,
234 "ShapeTypes of the transformation / cell functional have to agree" );
235
236 protected:
238 std::shared_ptr<CellFunctionalType> _cell_functional;
244 // In the optimal case, every cell in the mesh has the size lambda(cell)
249 std::map<String, std::shared_ptr<Assembly::UnitFilterAssembler<MeshType>>> _dirichlet_asm;
251 std::map<String, std::shared_ptr<Assembly::SlipFilterAssembler<TrafoType>>> _slip_asm;
253 std::shared_ptr<MeshConcentrationFunctionBase<Trafo_, RefCellTrafo_>> _mesh_conc;
254
255 public:
259 std::map<String, DataType*> sync_scalars;
261 std::set<VectorTypeR*> sync_vecs;
262
263 private:
270
275
283
284 public:
309 TrafoType& trafo,
310 const std::deque<String>& dirichlet_list,
311 const std::deque<String>& slip_list,
312 std::shared_ptr<CellFunctionalType_> cell_functional_)
313 : BaseClass(rmn_),
314 _cell_functional(cell_functional_),
315 _trafo(trafo),
316 _mu(rmn_->get_mesh()->get_num_entities(ShapeType::dimension), DataType(1)),
317 _lambda(rmn_->get_mesh()->get_num_entities(ShapeType::dimension)),
318 _h(rmn_->get_mesh()->get_num_entities(ShapeType::dimension)),
320 _slip_asm(),
321 _mesh_conc(nullptr),
322 trafo_space(trafo),
323 sync_scalars(),
324 sync_vecs(),
325 _columns(trafo_space.get_num_dofs()),
326 _rows(trafo_space.get_num_dofs()),
330 _sum_det(0),
331 _sum_lambda(0),
332 _sum_mu(0)
333 {
334
335 XASSERTM(cell_functional_ != nullptr, "cell functional must not be nullptr");
336
337 // For every MeshPart specified in the list of Dirichlet boundaries, create a UnitFilterAssembler and
338 // insert it into the std::map
339 for(auto& it : dirichlet_list)
340 {
341 // Create empty assembler
342 auto new_asm = std::make_shared<Assembly::UnitFilterAssembler<MeshType>>();
343
344 // Add the MeshPart to the assembler if it is there. There are legitimate reasons for it NOT to be
345 // there, i.e. we are in parallel and our patch is not adjacent to that MeshPart
346 auto* mpp = rmn_->find_mesh_part(it);
347 if(mpp != nullptr)
348 {
349 new_asm->add_mesh_part(*mpp);
350 }
351
352 // Insert into the map
353 String identifier(it);
354 _dirichlet_asm.emplace(identifier, new_asm);
355 }
356
357 // For every MeshPart specified in the list of slip boundaries, create a SlipFilterAssembler and
358 // insert it into the std::map
359 for(auto& it : slip_list)
360 {
361 // Create empty assembler
362 auto new_asm = std::make_shared<Assembly::SlipFilterAssembler<TrafoType>>(this->_trafo);
363
364 // Add the MeshPart to the assembler if it is there. There are legitimate reasons for it NOT to be
365 // there, i.e. we are in parallel and our patch is not adjacent to that MeshPart
366 auto* mpp = rmn_->find_mesh_part(it);
367 if(mpp != nullptr)
368 {
369 new_asm->add_mesh_part(*mpp);
370 }
371
372 // Insert into the map
373 String identifier(it);
374 _slip_asm.emplace(identifier, new_asm);
375 }
376
377 Memory::TypedView<DT_> vmu = _mu.elements_view_r();
378 for(Index i(0); i < _mu.size(); ++i)
379 {
380 _sum_mu += vmu(i);
381 }
382
383 sync_scalars.emplace("_sum_mu",&_sum_mu);
384
385 // Compute desired element size distribution
387 }
388
420 TrafoType& trafo,
421 const std::deque<String>& dirichlet_list,
422 const std::deque<String>& slip_list,
423 std::shared_ptr<CellFunctionalType_> cell_functional_,
424 ScaleComputation scale_computation_,
425 std::shared_ptr<MeshConcentrationFunctionBase<Trafo_, RefCellTrafo_>> mesh_conc_ = nullptr,
426 DataType penalty_param_ = DataType(0))
427 : BaseClass(rmn_),
428 _cell_functional(cell_functional_),
429 _trafo(trafo),
430 _mu(rmn_->get_mesh()->get_num_entities(ShapeType::dimension), DataType(1)),
431 _lambda(rmn_->get_mesh()->get_num_entities(ShapeType::dimension)),
432 _h(rmn_->get_mesh()->get_num_entities(ShapeType::dimension)),
434 _slip_asm(),
435 _mesh_conc(mesh_conc_),
436 trafo_space(trafo),
437 sync_scalars(),
438 sync_vecs(),
439 _columns(trafo_space.get_num_dofs()),
440 _rows(trafo_space.get_num_dofs()),
441 _scale_computation(scale_computation_),
442 _penalty_param(penalty_param_),
444 _sum_det(0),
445 _sum_lambda(0),
446 _sum_mu(0)
447 {
448
449 XASSERTM(cell_functional_ != nullptr, "cell functional must not be nullptr!\n");
450 XASSERTM(_penalty_param >= DataType(0), "penalty_param must be >= 0!\n");
451
452 // For every MeshPart specified in the list of Dirichlet boundaries, create a UnitFilterAssembler and
453 // insert it into the std::map
454 for(auto& it : dirichlet_list)
455 {
456 // Create empty assembler
457 auto new_asm = std::make_shared<Assembly::UnitFilterAssembler<MeshType>>();
458
459 // Add the MeshPart to the assembler if it is there. There are legitimate reasons for it NOT to be
460 // there, i.e. we are in parallel and our patch is not adjacent to that MeshPart
461 auto* mpp = rmn_->find_mesh_part(it);
462 if(mpp != nullptr)
463 {
464 new_asm->add_mesh_part(*mpp);
465 }
466
467 // Insert into the map
468 String identifier(it);
469 _dirichlet_asm.emplace(identifier, new_asm);
470 }
471
472 // For every MeshPart specified in the list of slip boundaries, create a SlipFilterAssembler and
473 // insert it into the std::map
474 for(auto& it : slip_list)
475 {
476 // Create empty assembler
477 auto new_asm = std::make_shared<Assembly::SlipFilterAssembler<TrafoType>>(this->_trafo);
478
479 // Add the MeshPart to the assembler if it is there. There are legitimate reasons for it NOT to be
480 // there, i.e. we are in parallel and our patch is not adjacent to that MeshPart
481 auto* mpp = rmn_->find_mesh_part(it);
482 if(mpp != nullptr)
483 {
484 new_asm->add_mesh_part(*mpp);
485 }
486
487 // Insert into the map
488 String identifier(it);
489 _slip_asm.emplace(identifier, new_asm);
490 }
491
492 Memory::TypedView<DT_> vmu = _mu.elements_view_r();
493 for(Index i(0); i < _mu.size(); ++i)
494 {
495 _sum_mu += vmu(i);
496 }
497 sync_scalars.emplace("_sum_mu",&_sum_mu);
498
499 if(mesh_conc_ != nullptr)
500 {
501 _mesh_conc = mesh_conc_->create_empty_clone();
502 _mesh_conc->set_mesh_node(rmn_);
503 _mesh_conc->add_sync_vecs(sync_vecs);
504 }
505
506 // Perform one time scal computation
508 }
509
514
517 {
518 }
519
525 bool empty() const
526 {
527 return (trafo_space.get_num_dofs() == Index(0));
528 }
529
534 {
535 return VectorTypeL(trafo_space.get_num_dofs());
536 }
537
542 {
543 return VectorTypeR(trafo_space.get_num_dofs());
544 }
545
552 {
553 return _columns;
554 }
555
562 {
563 return _rows;
564 }
565
571 static String name()
572 {
573 return "HyperelasticityFunctional<"+MeshType::name()+">";
574 }
575
579 virtual String info() const
580 {
581 const Index pad_width(30);
582
583 String msg = name() + String("\nScale computation").pad_back(pad_width, '.')
584 + String(": ") + stringify(_scale_computation) + "\n";
585
586 msg += _cell_functional->info() + "\n";
587 if(_mesh_conc != nullptr)
588 {
589 msg += _mesh_conc->info() + "\n";
590 }
591
592 return msg;
593 }
594
607 {
608 exporter.add_cell_scalar("h", this->_h);
609 exporter.add_cell_scalar("lambda", this->_lambda);
610 exporter.add_cell_scalar("mu", this->_mu);
611
612
613 DataType* fval_norm(new DataType[this->get_mesh()->get_num_entities(MeshType::shape_dim)]);
614 DataType* fval_det(new DataType[this->get_mesh()->get_num_entities(MeshType::shape_dim)]);
615 DataType* fval_rec_det(new DataType[this->get_mesh()->get_num_entities(MeshType::shape_dim)]);
616
617 DataType fval(0);
618 eval_fval_cellwise(fval, fval_norm, fval_det, fval_rec_det);
619 exporter.add_cell_vector("fval", fval_norm, fval_det, fval_rec_det);
620
621 if(_mesh_conc != nullptr)
622 {
623 exporter.add_vertex_scalar("dist", _mesh_conc->get_dist());
624 exporter.add_vertex_vector("grad_dist", _mesh_conc->get_grad_dist());
625
626 if(this->_penalty_param > DataType(0))
627 {
628 DataType* constraint_at_cell = new DataType[this->get_mesh()->get_num_entities(MeshType::shape_dim)];
629
630 this->_mesh_conc->compute_constraint(constraint_at_cell);
631 exporter.add_cell_scalar("alignment_constraint", constraint_at_cell);
632
633 delete[] constraint_at_cell;
634 }
635 }
636
637 delete [] fval_norm;
638 delete [] fval_det;
639 delete [] fval_rec_det;
640
641 }
642
649 {
650 return _penalty_param;
651 }
652
660 void set_penalty_param(const DataType penalty_param_)
661 {
662 XASSERTM(penalty_param_ >= DataType(0),"Penalty parameter must be >= 0!");
663 XASSERTM(penalty_param_ <= Math::pow(Math::huge<DataType>(), DataType(0.25)),
664 "Excessively large penalty parameter.");
665
666 _penalty_param = penalty_param_;
667 }
668
677 {
679 }
680
681 //void set_mu(std::vector<CoordType>& cells, const CoordType& weight)
682 //{
683 // for(Index i(0); i < cells.size(); ++i)
684 // {
685 // _mu(cells(i), weight);
686 // }
687
688 // _sum_mu = CoordType(0);
689 // for(Index i(0); i < _mu.size(); ++i)
690 // {
691 // _sum_mu += _mu(i);
692 // }
693 //}
694
701 virtual void init() override
702 {
705 }
706
712 virtual void init_pre_sync()
713 {
714 // Write any potential changes to the mesh
715 this->buffer_to_mesh();
716
717 // Compute distances if necessary
718 if((this->_scale_computation == ScaleComputation::current_concentration) ||
719 (this->_scale_computation == ScaleComputation::iter_concentration) ||
720 (_penalty_param > DataType(0)) )
721 {
722 if(_mesh_conc == nullptr)
723 XABORTM("Scale computation set to "+stringify(_scale_computation)+"and alignment penalty factor to "+
724 stringify_fp_sci(_penalty_param)+", but no concentration function was given");
725
726 _mesh_conc->compute_dist();
727 }
728
729 // Compute desired element size distribution
730 this->_compute_scales_init();
731 }
732
738 virtual void init_post_sync()
739 {
740 // Scale mu so that || mu ||_1 = 1
741 // For this, _sum_mu needs to have been summed up over all patches in the synchronization phase.
742 if(_sum_mu != DataType(1))
743 {
745 _sum_mu = DataType(1);
746 }
747
748 // Scale lambda so that || lambda ||_1 = 1
749 // For this, _sum_lambda needs to have been summed up over all patches in the synchronization phase.
750 if(_sum_lambda != DataType(1))
751 {
754 }
755
756 // Compute the new optimal scales. For this, lambda needs to be scaled correctly and _sum_det needs to be
757 // summed up over all patches in the synchronization phase.
758 RefCellTrafo_::compute_h(_h, _lambda, _sum_det);
759 }
760
773 virtual void prepare(const VectorTypeR& vec_state, FilterType& filter)
774 {
775 prepare_pre_sync(vec_state, filter);
776 prepare_post_sync(vec_state, filter);
777 }
778
791 virtual void prepare_pre_sync(const VectorTypeR& vec_state, FilterType& filter)
792 {
793 // Download state if necessary
794 //CoordsBufferType vec_buf;
795 //vec_buf.convert(vec_state);
796
797 // Copy to buffer and mesh
798 this->_coords_buffer.copy(vec_state);
799 this->buffer_to_mesh();
800
801 auto& dirichlet_filters = filter.template at<1>();
802
803 for(auto& it : dirichlet_filters)
804 {
805 const auto& assembler = _dirichlet_asm.find(it.first);
806
807 if(assembler == _dirichlet_asm.end())
808 XABORTM("Could not find unit filter assembler for filter with key "+it.first);
809
810 assembler->second->assemble(it.second, trafo_space, vec_state);
811 }
812
813 // The slip filter contains the outer unit normal, so reassemble it
814 auto& slip_filters = filter.template at<0>();
815 for(const auto& it:slip_filters)
816 this->_mesh_node->adapt_by_name(it.first);
817
818 // Copy back to the buffer as adaption might have changed the mesh
819 this->mesh_to_buffer();
820
821 for(auto& it : slip_filters)
822 {
823 const auto& assembler = _slip_asm.find(it.first);
824
825 if(assembler == _slip_asm.end())
826 {
827 XABORTM("Could not find slip filter assembler for filter with key "+it.first);
828 }
829
830 assembler->second->assemble(it.second, trafo_space);
831 }
832
833 if( (this->_scale_computation == ScaleComputation::iter_concentration) ||
835 {
836 if(_mesh_conc == nullptr)
837 {
838 XABORTM("Scale computation set to "+stringify(_scale_computation)+"and alignment penalty factor to "+
839 stringify_fp_sci(_penalty_param)+", but no concentration function was given");
840 }
841
842 _mesh_conc->compute_dist();
843 }
844
846
847 if(_penalty_param > DataType(0))
848 {
849 _alignment_constraint = this->_mesh_conc->compute_constraint();
850 this->sync_scalars.emplace("_alignment_constraint",&_alignment_constraint);
851 }
852
853 }
854
867 virtual void prepare_post_sync(const VectorTypeR& DOXY(vec_state), FilterType& DOXY(filter))
868 {
869 if(_sum_lambda != DataType(1))
870 {
873 }
874
875 RefCellTrafo_::compute_h(_h, _lambda, _sum_det);
876
877 if(this->_scale_computation == ScaleComputation::iter_concentration)
878 {
879 _mesh_conc->compute_grad_h(this->_sum_det);
880 }
881 }
882
919 virtual CoordType compute_cell_size_defect(CoordType& lambda_min, CoordType& lambda_max, CoordType& vol_min,
920 CoordType& vol_max, CoordType& vol) const override
921 {
922
923 compute_cell_size_defect_pre_sync(vol_min, vol_max, vol);
924 return compute_cell_size_defect_post_sync(lambda_min, lambda_max, vol_min, vol_max, vol);
925
926 }
927
950 {
951 vol = CoordType(0);
952
953 vol_min = Math::huge<CoordType>();
954 vol_max = CoordType(0);
955
956 for(Index cell(0); cell < this->get_mesh()->get_num_entities(ShapeType::dimension); ++cell)
957 {
958 CoordType my_vol = this->_trafo.template compute_vol<ShapeType, CoordType>(cell);
959 vol_min = Math::min(vol_min, my_vol);
960 vol_max = Math::max(vol_min, my_vol);
961 vol += my_vol;
962 }
963 }
964
993 CoordType& vol_min, CoordType& vol_max, const CoordType& vol) const
994 {
995 CoordType size_defect(0);
996 lambda_min = Math::huge<CoordType>();
997 lambda_max = CoordType(0);
998
999 const Memory::TypedView<DT_> vl = this->_lambda.elements_view_r();
1000 for(Index cell(0); cell < this->get_mesh()->get_num_entities(ShapeType::dimension); ++cell)
1001 {
1002 size_defect += Math::abs(this->_trafo.template
1003 compute_vol<ShapeType, CoordType>(cell)/vol - vl(cell));
1004
1005 lambda_min = Math::min(lambda_min, vl(cell));
1006 lambda_max = Math::max(lambda_max, vl(cell));
1007 }
1008
1009 vol_min /= vol;
1010 vol_max/= vol;
1011
1012 return size_defect;
1013 }
1014
1032 virtual void eval_fval_grad(CoordType& fval, VectorTypeL& grad, const bool& add_penalty_fval = true)
1033 {
1034 typedef typename TrafoType::template Evaluator<ShapeType, DataType>::Type TrafoEvaluator;
1035 typedef typename SpaceType::template Evaluator<TrafoEvaluator>::Type SpaceEvaluator;
1036
1037 // Increase number of functional evaluations
1038 this->_num_func_evals++;
1039 this->_num_grad_evals++;
1040
1041 // Total number of cells in the mesh
1042 Index ncells(this->get_mesh()->get_num_entities(ShapeType::dimension));
1043
1044 // Index set for local/global numbering
1045 auto& idx = this->get_mesh()->template get_index_set<ShapeType::dimension,0>();
1046
1047 // This will hold the coordinates for one element for passing to other routines
1048 FEAT::Tiny::Matrix <CoordType, Shape::FaceTraits<ShapeType,0>::count, MeshType::world_dim> x;
1049 // This will hold the local gradient for one element for passing to other routines
1051
1052 // Clear gradient vector
1053 grad.format();
1054 fval = DataType(0);
1055
1056 TrafoEvaluator trafo_eval(this->_trafo);
1057 SpaceEvaluator space_eval(this->trafo_space);
1058
1059 const auto vcb = this->_coords_buffer.elements_view_r();
1060 const auto vh = this->_h.elements_view_r();
1061 const auto vmu = this->_mu.elements_view_r();
1062 auto vgrad = grad.elements_view_rw();
1063
1064 // Compute the functional value for each cell
1065 for(Index cell(0); cell < ncells; ++cell)
1066 {
1067 DataType fval_loc(0);
1068 trafo_eval.prepare(cell);
1069 space_eval.prepare(trafo_eval);
1070
1071 // Get local coordinates
1072 for(int j(0); j < Shape::FaceTraits<ShapeType,0>::count; ++j)
1073 {
1074 x[j] = vcb(idx(cell,j));
1075 }
1076
1077 auto mat_tensor = RefCellTrafo_::compute_mat_tensor(x, vh(cell));
1078
1079 this->_cell_functional->eval_fval_grad(
1080 fval_loc, grad_loc, mat_tensor, trafo_eval, space_eval, x, vh(cell));
1081
1082 // Add the contribution from the dependence of h on the vertex coordinates
1083 if(this->_mesh_conc != nullptr && this->_mesh_conc->use_derivative())
1084 {
1085 const auto grad_h = this->_mesh_conc->get_grad_h().elements_view_r();
1086 this->_cell_functional->add_grad_h_part(
1087 grad_loc, mat_tensor, trafo_eval, space_eval, x, vh(cell), grad_h(cell));
1088 }
1089
1090 fval += vmu(cell)*fval_loc;
1091 // Add local contributions to global gradient vector
1092 for(int j(0); j < Shape::FaceTraits<ShapeType,0>::count; ++j)
1093 {
1094 vgrad[idx(cell,j)] += vmu(cell)*grad_loc[j];
1095 }
1096 }
1097
1098 vgrad.release();
1099 if(this->_penalty_param > DataType(0))
1100 {
1101 XASSERTM(this->_mesh_conc != nullptr,
1102 "You need a mesh concentration function for imposing a mesh alignment constraint!");
1103
1104 if(add_penalty_fval)
1105 {
1106 // Add the quadratic penalty term to the functional value
1107 fval += this->_penalty_param*DataType(0.5)*Math::sqr(this->_alignment_constraint);
1108 }
1109
1110 // Add the gradient of the penalty term
1111 this->_mesh_conc->add_constraint_grad(grad, this->_alignment_constraint, this->_penalty_param);
1112
1113 }
1114 }
1115
1132 virtual void eval_fval_cellwise(CoordType& fval, CoordType* fval_norm, CoordType* fval_cof,
1133 CoordType* fval_det) const
1134 {
1135 typedef typename TrafoType::template Evaluator<ShapeType, DataType>::Type TrafoEvaluator;
1136 typedef typename SpaceType::template Evaluator<TrafoEvaluator>::Type SpaceEvaluator;
1137
1138 // Total number of cells in the mesh
1139 Index ncells(this->get_mesh()->get_num_entities(ShapeType::dimension));
1140
1141 // Index set for local/global numbering
1142 auto& idx = this->get_mesh()->template get_index_set<ShapeType::dimension,0>();
1143
1144 // This will hold the coordinates for one element for passing to other routines
1145 FEAT::Tiny::Matrix <CoordType, Shape::FaceTraits<ShapeType,0>::count, MeshType::world_dim> x;
1146 // Local cell dimensions for passing to other routines
1147 CoordType h(0);
1148 // This will hold the local gradient for one element for passing to other routines
1150
1151 fval = DataType(0);
1152
1153 TrafoEvaluator trafo_eval(this->_trafo);
1154 SpaceEvaluator space_eval(this->trafo_space);
1155
1156 const auto vcb = this->_coords_buffer.elements_view_r();
1157 const auto vh = this->_h.elements_view_r();
1158 const auto vmu = this->_mu.elements_view_r();
1159
1160 // Compute the functional value for each cell
1161 for(Index cell(0); cell < ncells; ++cell)
1162 {
1163 DataType fval_loc(0);
1164 trafo_eval.prepare(cell);
1165 space_eval.prepare(trafo_eval);
1166
1167 h = vh(cell);
1168 // Get local coordinates
1169 for(int j(0); j < Shape::FaceTraits<ShapeType,0>::count; ++j)
1170 {
1171 x[j] = vcb(idx(cell,j));
1172 }
1173
1174 auto mat_tensor = RefCellTrafo_::compute_mat_tensor(x, vh(cell));
1175
1176 this->_cell_functional->eval_fval_cellwise(fval_loc, mat_tensor, trafo_eval, space_eval, x, h,
1177 fval_norm[cell], fval_cof[cell], fval_det[cell]);
1178
1179 fval += vmu(cell)*fval_loc;
1180 }
1181
1182 } // eval_fval_cellwise
1183
1184 protected:
1187 {
1188 Index ncells(this->get_mesh()->get_num_entities(ShapeType::dimension));
1189
1190 Memory::TypedView<DT_> vl = this->_lambda.elements_view_w();
1191 _sum_lambda = DataType(0);
1192 for(Index cell(0); cell < ncells; ++cell)
1193 {
1194 vl[cell] = this->_trafo.template compute_vol<ShapeType, CoordType>(cell);
1195 _sum_lambda += vl(cell);
1196 }
1197
1198 this->sync_scalars.emplace("_sum_lambda",&_sum_lambda);
1199
1200 }
1201
1204 {
1206
1208 this->sync_scalars.emplace("_sum_lambda",&_sum_lambda);
1209 }
1210
1213 {
1214 _mesh_conc->compute_conc();
1215 this->sync_scalars.emplace("_sum_conc",&(_mesh_conc->get_sum_conc()));
1216
1217 _mesh_conc->compute_grad_conc();
1218
1219 _mesh_conc->compute_grad_sum_det(this->_coords_buffer);
1220
1221 _lambda.copy(_mesh_conc->get_conc());
1222
1223 const Memory::TypedView<DT_> vl = this->_lambda.elements_view_r();
1224 _sum_lambda = DataType(0);
1225 for(Index cell(0); cell < this->get_mesh()->get_num_entities(ShapeType::dimension); ++cell)
1226 {
1227 _sum_lambda += vl(cell);
1228 }
1229 this->sync_scalars.emplace("_sum_lambda",&_sum_lambda);
1230
1231 }
1232
1237 {
1238
1239 switch(_scale_computation)
1240 {
1241 case ScaleComputation::once_uniform:
1242 case ScaleComputation::once_cellsize:
1243 case ScaleComputation::once_concentration:
1244 return;
1245 case ScaleComputation::current_uniform:
1247 break;
1248 case ScaleComputation::current_cellsize:
1250 break;
1251 case ScaleComputation::current_concentration:
1252 case ScaleComputation::iter_concentration:
1254 break;
1255 default:
1256 return;
1257 }
1258
1259 _sum_det = RefCellTrafo_::compute_sum_det(this->_coords_buffer, *(this->get_mesh()));
1260 this->sync_scalars.emplace("_sum_det",&_sum_det);
1261
1262 }
1263
1270 {
1271 switch(_scale_computation)
1272 {
1273 case ScaleComputation::iter_concentration:
1275 break;
1276 default:
1277 return;
1278 }
1279
1280 _sum_det = RefCellTrafo_::compute_sum_det(this->_coords_buffer, *(this->get_mesh()));
1281 this->sync_scalars.emplace("_sum_det",&_sum_det);
1282 }
1283
1290 {
1291 switch(_scale_computation)
1292 {
1293 case ScaleComputation::once_uniform:
1295 break;
1296 case ScaleComputation::once_cellsize:
1298 break;
1299 case ScaleComputation::once_concentration:
1301 break;
1302 default:
1303 return;
1304 }
1305
1306 _sum_det = RefCellTrafo_::compute_sum_det(this->_coords_buffer, *(this->get_mesh()));
1307 this->sync_scalars.emplace("_sum_det",&_sum_det);
1308
1309 }
1310
1311 }; // class HyperelasticityFunctional
1312
1313#ifdef FEAT_EICKT
1315 extern template class HyperelasticityFunctional
1316 <
1317 double, Index,
1320 <
1321 double,
1323 >
1324 >;
1325
1326 extern template class HyperelasticityFunctional
1327 <
1328 double, Index,
1331 <
1332 double,
1334 >
1335 >;
1336
1337 extern template class HyperelasticityFunctional
1338 <
1339 double, Index,
1342 <
1343 double,
1345 >
1346 >;
1347
1348 extern template class HyperelasticityFunctional
1349 <
1350 double, Index,
1353 <
1354 double,
1356 >
1357 >;
1359#endif // FEAT_EICKT
1360
1361 } // namespace Meshopt
1362} // namespace FEAT
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
VTK exporter class template.
Definition: export_vtk.hpp:196
void add_vertex_vector(const String &name, const T_ *x, const T_ *y=nullptr, const T_ *z=nullptr, double scaling_factor=1.0)
Adds a vector-field vertex variable to the exporter.
Definition: export_vtk.hpp:522
void add_cell_vector(const String &name, const T_ *x, const T_ *y=nullptr, const T_ *z=nullptr, double scaling_factor=1.0)
Adds a vector-field cell variable to the exporter.
Definition: export_vtk.hpp:723
void add_cell_scalar(const String &name, const T_ *data, double scaling_factor=1.0)
Adds a scalar cell variable to the exporter.
Definition: export_vtk.hpp:662
void add_vertex_scalar(const String &name, const T_ *data, double scaling_factor=1.0)
Adds a scalar vertex variable to the exporter.
Definition: export_vtk.hpp:461
bool adapt_by_name(const String &part_name, bool recursive=false)
Adapts this mesh node.
Definition: mesh_node.hpp:523
MeshPartType * find_mesh_part(const String &part_name)
Searches this container for a MeshPart.
Definition: mesh_node.hpp:398
Root mesh node class template.
Definition: mesh_node.hpp:748
void format()
Reset all elements of the container to a given value or zero if missing.
Definition: container.hpp:831
Blocked Dense data vector class template.
void copy(const DenseVectorBlocked &x, bool full=false)
Performs .
Dense data vector class template.
void scale(const DenseVector &x, const DT_ alpha)
Calculate .
void copy(const VT_ &a)
Performs .
Index size() const
Returns the size of this vector, i.e. its number of entries.
Filter Chainclass template.
Sequence of filters of the same type.
Slip Filter class template.
Definition: slip_filter.hpp:67
Unit Filter Blocked class template.
Typed memory view class.
Baseclass for a family of variational mesh optimization algorithms.
LAFEM::DenseVectorBlocked< CoordType, IndexType, MeshType::world_dim > VectorType
Vector type for coordinate vectors etc.
DataType _sum_mu
The sum of all mu (= cell weights) over all cells before normalization.
std::shared_ptr< MeshConcentrationFunctionBase< Trafo_, RefCellTrafo_ > > _mesh_conc
The mesh concentration function (if any)
virtual void init() override
Performs intializations that cannot be done in the constructor.
ScalarVectorType _h
Size parameters for the local reference element.
Index num_rows() const
Returns the number of rows.
DataType _sum_lambda
The sum of all lambda (= optimal cell size) over all cells before normalization.
DataType get_constraint()
Gets the last computed (alignment) constraint.
DataType _alignment_constraint
Last computed contraint (violation)
virtual void _compute_lambda_uniform()
Computes the uniformly distributed weights _lambda.
virtual void _compute_lambda_cellsize()
Computes the weights _lambda according to the current mesh.
virtual void add_to_vtk_exporter(Geometry::ExportVTK< typename Trafo_::MeshType > &exporter) const override
Adds relevant quantities of this object to a VTK exporter.
virtual void init_pre_sync()
Performs initializations that cannot be done in the constructor, pre synchronization phase.
Index num_cols() const
Returns the number of columns.
bool empty() const
Checks if the functional is empty (= the null functional)
std::map< String, std::shared_ptr< Assembly::UnitFilterAssembler< MeshType > > > _dirichlet_asm
Assembler for Dirichlet boundary conditions.
std::map< String, std::shared_ptr< Assembly::SlipFilterAssembler< TrafoType > > > _slip_asm
Assembler for slip boundary conditions.
std::map< String, DataType * > sync_scalars
These are the scalars that need to be synchronized in init() or prepare()
CoordType DataType
We always use the precision of the mesh.
DataType get_penalty_param() const
Gets the penalty parameter.
VectorTypeL create_vector_l() const
Creates an L-vector for the functional's gradient.
LAFEM::DenseVectorBlocked< DT_, IT_, MeshType::world_dim > VectorTypeR
Input vector type of the operator.
MeshQualityFunctional< MeshType > BaseClass
Our base class.
VectorTypeR create_vector_r() const
Creates an R-vector for the functional and its gradient.
TrafoType::MeshType MeshType
The mesh the transformation is defined on.
virtual void eval_fval_cellwise(CoordType &fval, CoordType *fval_norm, CoordType *fval_cof, CoordType *fval_det) const
Computes the functional value parts on every cell.
LAFEM::SlipFilter< DT_, IT_, MeshType::world_dim > SlipFilterType
Filter for slip boundary conditions.
const Index _rows
This is the number of DoFs in the test space (= number of mesh vertices)
virtual void _compute_scales_init()
Recomputes the optimal scales, every call to the solver.
TrafoType & _trafo
The transformation defining the physical mesh.
HyperelasticityFunctional()=delete
Explicitly delete default constructor.
virtual String info() const
Prints some characteristics of the HyperelasticityFunctional object.
MeshType::CoordType CoordType
The precision of the mesh coordinates.
ScalarVectorType _lambda
Weights for local mesh size.
LAFEM::UnitFilterBlocked< DT_, IT_, MeshType::world_dim > DirichletFilterType
Filter for Dirichlet boundary conditions.
virtual void _compute_scales_iter()
Recomputes the optimal scales, every iteration.
LAFEM::FilterSequence< DirichletFilterType > DirichletFilterSequence
Sequence of Dirichlet filters for several different boundary parts.
HyperelasticityFunctional(Geometry::RootMeshNode< MeshType > *rmn_, TrafoType &trafo, const std::deque< String > &dirichlet_list, const std::deque< String > &slip_list, std::shared_ptr< CellFunctionalType_ > cell_functional_)
Constructor.
void compute_cell_size_defect_pre_sync(CoordType &vol_min, CoordType &vol_max, CoordType &vol) const
Computes a quality indicator concerning the cell sizes, pre synchronization phase.
DataType _penalty_param
Factor for the alignment penalty term.
CellFunctionalType_ CellFunctionalType
Type for the cell functional.
virtual void _compute_scales_once()
Computes the scales, just once.
virtual void eval_fval_grad(CoordType &fval, VectorTypeL &grad, const bool &add_penalty_fval=true)
Evaluates the functional's value and gradient at the current state.
SpaceType trafo_space
The FE space for the transformation, needed for filtering.
static constexpr int block_width
Block Weight of the operator's gradient.
virtual void prepare(const VectorTypeR &vec_state, FilterType &filter)
Prepares the functional for evaluation.
LAFEM::FilterChain< SlipFilterSequence, DirichletFilterSequence > FilterType
Combined filter.
VectorTypeR GradientType
Type of the gradient vector.
virtual CoordType compute_cell_size_defect_post_sync(CoordType &lambda_min, CoordType &lambda_max, CoordType &vol_min, CoordType &vol_max, const CoordType &vol) const
Computes a quality indicator concerning the cell sizes, pre synchronization phase.
LAFEM::DenseVector< CoordType, IndexType > ScalarVectorType
Vector type for element sizes etc.
const Index _columns
This is the number of DoFs in the trial space (= number of mesh vertices)
virtual void _compute_lambda_conc()
Computes _lambda according to the concentration function given by _mesh_conc.
MeshType::ShapeType ShapeType
ShapeType of said mesh.
HyperelasticityFunctional(Geometry::RootMeshNode< MeshType > *rmn_, TrafoType &trafo, const std::deque< String > &dirichlet_list, const std::deque< String > &slip_list, std::shared_ptr< CellFunctionalType_ > cell_functional_, ScaleComputation scale_computation_, std::shared_ptr< MeshConcentrationFunctionBase< Trafo_, RefCellTrafo_ > > mesh_conc_=nullptr, DataType penalty_param_=DataType(0))
Constructor.
static constexpr int block_height
Block height of the operator's gradient.
virtual CoordType compute_cell_size_defect(CoordType &lambda_min, CoordType &lambda_max, CoordType &vol_min, CoordType &vol_max, CoordType &vol) const override
Computes a quality indicator concerning the cell sizes.
virtual void prepare_post_sync(const VectorTypeR &vec_state, FilterType &filter)
Prepares the functional for evaluation, post synchronization phase.
ScalarVectorType _mu
Weights for the local contributions to the global functional value.
ScaleComputation _scale_computation
How to compute the optimal scales.
RefCellTrafo_ RefCellTrafo
Type of the reference cell trafo for the mesh quality.
std::shared_ptr< CellFunctionalType > _cell_functional
Since the cell functional contains a ShapeType, these have to be the same.
void set_penalty_param(const DataType penalty_param_)
Sets the penalty parameter.
Intern::TrafoFE< TrafoType >::Space SpaceType
Finite Element space for the transformation.
BaseClass::CoordsBufferType CoordsBufferType
Type for exchanging information between state variable and mesh.
virtual void init_post_sync()
Performs initializations that cannot be done in the constructor, post synchronization phase.
LAFEM::DenseVectorBlocked< DT_, IT_, MeshType::world_dim > VectorTypeL
Output vector type of the operator.
HyperelasticityFunctional(const HyperelasticityFunctional &)=delete
Explicitly delete copy constructor.
LAFEM::FilterSequence< SlipFilterType > SlipFilterSequence
Sequence of Slip filters for several different boundary parts.
virtual void prepare_pre_sync(const VectorTypeR &vec_state, FilterType &filter)
Prepares the functional for evaluation, pre synchronization phase.
std::set< VectorTypeR * > sync_vecs
These are the vectors that need to be synchronized (type-0 to type-1)
Base class for mesh concentration functions.
Baseclass for mesh optimization algorithms.
virtual void mesh_to_buffer()
Gets the coordinates from the underlying mesh and saves them in _coords_buffer.
Geometry::RootMeshNode< MeshType > * _mesh_node
The mesh for the underlying transformation.
Index _num_func_evals
Counter for number of function evaluations.
Index _num_grad_evals
Counter for number of gradient evaluations.
virtual void buffer_to_mesh()
Sets the coordinates in the underlying mesh to _coords_buffer.
CoordsBufferType _coords_buffer
Coordinates, used for setting new boundary values etc.
Functionals for measuring and optimising mesh quality.
String class implementation.
Definition: string.hpp:47
String pad_back(size_type len, char c=' ') const
Pads the back of the string up to a desired length.
Definition: string.hpp:416
Tiny Matrix class template.
Standard transformation mapping class template.
Definition: mapping.hpp:39
T_ abs(T_ x)
Returns the absolute value.
Definition: math.hpp:275
T_ pow(T_ x, T_ y)
Returns x raised to the power of y.
Definition: math.hpp:643
T_ sqr(T_ x)
Returns the square of a value.
Definition: math.hpp:95
T_ min(T_ a, T_ b)
Returns the minimum of two values.
Definition: math.hpp:123
T_ max(T_ a, T_ b)
Returns the maximum of two values.
Definition: math.hpp:137
ScaleComputation
Enum class for different types of scale computations.
FEAT namespace.
Definition: adjactor.hpp:12
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:993
String stringify_fp_sci(DataType_ value, int precision=0, int width=0, bool sign=false)
Prints a floating point value to a string in scientific notation.
Definition: string.hpp:1137
@ grad
specifies whether the space should supply basis function gradients
std::uint64_t Index
Index data type.