-
Notifications
You must be signed in to change notification settings - Fork 1
/
FEMGridBehaviorModel.h
153 lines (108 loc) · 6.69 KB
/
FEMGridBehaviorModel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_EXTERNALBEHAVIORMODEL_FEMGRIDBEHAVIORMODEL_H
#define SOFA_EXTERNALBEHAVIORMODEL_FEMGRIDBEHAVIORMODEL_H
// plugin includes
#include <ExternalBehaviorModel/config.h>
// SOFA includes
#include <ExternalBehaviorModel/InteractingBehaviorModel.h>
// internal stuff, here it is using SOFA components that could be replaced by any library
#include <sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceField.h>
#include <sofa/component/mass/UniformMass.h>
#include <sofa/component/topology/container/grid/RegularGridTopology.h>
#include <sofa/simulation/Node.h>
namespace sofa
{
namespace externalBehaviorModel
{
/// Demo of how to implement a InteractingBehaviorModel to plug an external behavior model in a SOFA scene
/// In this simple example, the input SOFA dofs are an hexahedra (only 8 vertices) that are link by an elastic behavior model.
/// The behavior model is a fine FEM grid. All the "inside" dofs are invisible from the SOFA side, they are only visible inside this component.
/// All these dofs are independant, but on the SOFA side, the independant dofs are only the 8 hexahedron corners.
/// @warning Note that the internal behavior model is based on existing sofa components for simplicity,
/// but it could (should?) be built on any independant code or existing library
/// @warning Only a subset of the complete API is implemented, allowing only not assembled, implicit system solving.
// DataTypes describes the dof type
// here it is compiled for Vec3 corresponding to particles
template<class DataTypes>
class FEMGridBehaviorModel : public component::misc::InteractingBehaviorModel<DataTypes>
{
public:
// SOFA black magic
SOFA_CLASS( SOFA_TEMPLATE(FEMGridBehaviorModel, DataTypes), SOFA_TEMPLATE(component::misc::InteractingBehaviorModel, DataTypes) );
typedef component::misc::InteractingBehaviorModel<DataTypes> Inherited;
typedef typename Inherited::Dofs Dofs;
// retrieve interesting types from DataTypes
typedef typename DataTypes::Real Real;
typedef typename DataTypes::Coord Coord; // encoding a dof position
typedef typename DataTypes::Deriv Deriv; // encoding a dof variation of position or a force
typedef typename DataTypes::VecCoord VecCoord;
typedef typename DataTypes::VecDeriv VecDeriv;
typedef Data<typename DataTypes::VecCoord> DataVecCoord;
typedef Data<typename DataTypes::VecDeriv> DataVecDeriv;
typedef typename type::Vec<3,Real> Vec3;
/// @name Component API
/// @{
virtual void init() override; /// call when initializing the simulation
virtual void draw( const core::visual::VisualParams* vparams ) override; /// debug drawing
virtual void handleEvent(sofa::core::objectmodel::Event *event) override;
/// @}
/// @name Internal force API
/// @{
/// f += K( x, v ) -> build the right part of the sytem (including gravity)
virtual void addForce( const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v ) override;
/// df += K.dx -> call at each iteration by unassembled system solvers
/// necessary to perform an implicit interaction
virtual void addDForce( const core::MechanicalParams* mparams, DataVecDeriv& df, const DataVecDeriv& dx ) override;
/// @}
/// @name Mass API
/// @{
/// f += factor M dx (for implicit solvers)
virtual void addMDx( const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecDeriv& dx, double factor ) override;
/// @}
bool isDiagonal() const override { return false; }
// Data fields will automatically appear in qt-based GUI and in read/write XML scene files
// in this simple example, there is only one young modulus for all elements and one similar mass to every particle
// Note that Data must be initialized in the constructor, giving the default value, the field name and a description
Data<Real> _youngModulus; ///< Uniform Young modulus
Data<Real> _poissonRatio; ///< Uniform Poisson ratio
Data<Real> _totalMass; ///< Total Mass (lumped and uniformly distributed on particles
Data<unsigned> _subdivisions; ///< nb grid subdivisions
protected:
FEMGridBehaviorModel();
virtual ~FEMGridBehaviorModel() {}
/// @name internal sofa stuff that could be replaced by any library
/// @{
typename Dofs::SPtr m_internalDofs;
sofa::component::topology::container::grid::RegularGridTopology::SPtr m_internalTopology;
typename sofa::component::solidmechanics::fem::elastic::HexahedronFEMForceField<DataTypes>::SPtr m_internalForceField;
typename component::mass::UniformMass<DataTypes>::SPtr m_internalMass;
sofa::simulation::Node::SPtr m_internalNode;
int mapExposedInternalIndices[8]; ///< identity mapping between exposed SOFA dofs and internal model dofs
/// @}
}; // class FEMGridBehaviorModel
#if !defined(SOFA_EXTERNALBEHAVIORMODEL_FEMGRIDBEHAVIORMODEL_CPP)
extern template class SOFA_ExternalBehaviorModel_API FEMGridBehaviorModel<defaulttype::Vec3Types>;
#endif
} // namespace externalBehaviorModel
} // namespace sofa
#endif // SOFA_EXTERNALBEHAVIORMODEL_FEMGRIDBEHAVIORMODEL_H