File: MechanicalMatrixVisitor.h

package info (click to toggle)
sofa-framework 1.0~beta4-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 88,688 kB
  • ctags: 27,205
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 67
file content (391 lines) | stat: -rw-r--r-- 14,663 bytes parent folder | download | duplicates (5)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library 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 library 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 library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/
#ifndef SOFA_SIMULATION_MECHANICALMATRIXVISITOR_H
#define SOFA_SIMULATION_MECHANICALMATRIXVISITOR_H

#if !defined(__GNUC__) || (__GNUC__ > 3 || (_GNUC__ == 3 && __GNUC_MINOR__ > 3))
#pragma once
#endif


#include <sofa/simulation/common/Visitor.h>
#include <sofa/core/componentmodel/behavior/BaseMechanicalState.h>
#include <sofa/core/componentmodel/behavior/BaseMechanicalMapping.h>
#include <sofa/core/componentmodel/behavior/Mass.h>
#include <sofa/core/componentmodel/behavior/ForceField.h>
#include <sofa/core/componentmodel/behavior/InteractionForceField.h>
#include <sofa/core/componentmodel/behavior/InteractionConstraint.h>
#include <sofa/core/componentmodel/behavior/Constraint.h>
#include <sofa/defaulttype/BaseMatrix.h>
#include <sofa/defaulttype/BaseVector.h>
#include <iostream>
using std::cerr;
using std::endl;

namespace sofa
{

  namespace simulation
  {


    using namespace sofa::defaulttype;
    /** Base class for easily creating new actions for mechanical matrix manipulation

	During the first traversal (top-down), method processNodeTopDown(simulation::Node*) is applied to each simulation::Node. Each component attached to this node is processed using the appropriate method, prefixed by fwd.

	During the second traversal (bottom-up), method processNodeBottomUp(simulation::Node*) is applied to each simulation::Node. Each component attached to this node is processed using the appropriate method, prefixed by bwd.

	The default behavior of the fwd* and bwd* is to do nothing. Derived actions typically overload these methods to implement the desired processing.

    */
    class SOFA_SIMULATION_COMMON_API MechanicalMatrixVisitor : public Visitor
    {
    public:
      typedef sofa::core::componentmodel::behavior::BaseMechanicalState::VecId VecId;

	/// Return a class name for this visitor
	/// Only used for debugging / profiling purposes
	virtual const char* getClassName() const { return "MechanicalMatrixVisitor"; }

      /**@name Forward processing
	 Methods called during the forward (top-down) traversal of the data structure.
	 Method processNodeTopDown(simulation::Node*) calls the fwd* methods in the order given here. When there is a mapping, it is processed first, then method fwdMappedMechanicalState is applied to the BaseMechanicalState.
	 When there is no mapping, the BaseMechanicalState is processed first using method fwdMechanicalState.
	 Then, the other fwd* methods are applied in the given order.
      */
      ///@{

      /// This method calls the fwd* methods during the forward traversal. You typically do not overload it.
      virtual Result processNodeTopDown(simulation::Node* node);

      /// Process the OdeSolver
      virtual Result fwdOdeSolver(simulation::Node* /*node*/, core::componentmodel::behavior::OdeSolver* /*solver*/)
      {
        return RESULT_CONTINUE;
      }

      /// Process the BaseMechanicalMapping
      virtual Result fwdMechanicalMapping(simulation::Node* /*node*/, core::componentmodel::behavior::BaseMechanicalMapping* /*map*/)
      {
        return RESULT_CONTINUE;
      }

      /// Process the BaseMechanicalState if it is mapped from the parent level
      virtual Result fwdMappedMechanicalState(simulation::Node* /*node*/, core::componentmodel::behavior::BaseMechanicalState* /*mm*/)
      {
        return RESULT_PRUNE;
      }

      /// Process the BaseMechanicalState if it is not mapped from the parent level
      virtual Result fwdMechanicalState(simulation::Node* /*node*/, core::componentmodel::behavior::BaseMechanicalState* /*mm*/)
      {
        return RESULT_CONTINUE;
      }

      /// Process the BaseMass
      virtual Result fwdMass(simulation::Node* /*node*/, core::componentmodel::behavior::BaseMass* /*mass*/)
      {
        return RESULT_CONTINUE;
      }

      /// Process all the BaseForceField
      virtual Result fwdForceField(simulation::Node* /*node*/, core::componentmodel::behavior::BaseForceField* /*ff*/)
      {
        return RESULT_CONTINUE;
      }


      /// Process all the InteractionForceField
      virtual Result fwdInteractionForceField(simulation::Node* node, core::componentmodel::behavior::InteractionForceField* ff)
      {
        return fwdForceField(node, ff);
      }

      /// Process all the BaseConstraint
      virtual Result fwdConstraint(simulation::Node* /*node*/, core::componentmodel::behavior::BaseConstraint* /*c*/)
      {
        return RESULT_CONTINUE;
      }

      /// Process all the InteractionConstraint
      virtual Result fwdInteractionConstraint(simulation::Node* node, core::componentmodel::behavior::InteractionConstraint* c)
      {
        return fwdConstraint(node, c);
      }

      ///@}

      /**@name Backward processing
	 Methods called during the backward (bottom-up) traversal of the data structure.
	 Method processNodeBottomUp(simulation::Node*) calls the bwd* methods.
	 When there is a mapping, method bwdMappedMechanicalState is applied to the BaseMechanicalState.
	 When there is no mapping, the BaseMechanicalState is processed using method bwdMechanicalState.
	 Finally, the mapping (if any) is processed using method bwdMechanicalMapping.
      */
      ///@{

      /// This method calls the bwd* methods during the backward traversal. You typically do not overload it.
      virtual void processNodeBottomUp(simulation::Node* node);

      /// Process the BaseMechanicalState when it is not mapped from parent level
      virtual void bwdMechanicalState(simulation::Node* /*node*/, core::componentmodel::behavior::BaseMechanicalState* /*mm*/)
      {}

      /// Process the BaseMechanicalState when it is mapped from parent level
      virtual void bwdMappedMechanicalState(simulation::Node* /*node*/, core::componentmodel::behavior::BaseMechanicalState* /*mm*/)
      {}

      /// Process the BaseMechanicalMapping
      virtual void bwdMechanicalMapping(simulation::Node* /*node*/, core::componentmodel::behavior::BaseMechanicalMapping* /*map*/)
      {}

      /// Process the OdeSolver
      virtual void bwdOdeSolver(simulation::Node* /*node*/, core::componentmodel::behavior::OdeSolver* /*solver*/)
      {}

      ///@}


      /// Return a category name for this action.
      /// Only used for debugging / profiling purposes
      virtual const char* getCategoryName() const
      {
        return "animate";
      }

      unsigned int offsetOnEnter, offsetOnExit;
    };


    /** Compute the size of a dynamics matrix (mass or stiffness) of the whole scene */
    class SOFA_SIMULATION_COMMON_API MechanicalGetMatrixDimensionVisitor : public MechanicalMatrixVisitor
    {
    public:
      unsigned int * const nbRow;
      unsigned int * const nbCol;
    MechanicalGetMatrixDimensionVisitor(unsigned int * const _nbRow, unsigned int * const _nbCol)
      : nbRow(_nbRow), nbCol(_nbCol)
      {}

      virtual Result fwdMechanicalState(simulation::Node* /*node*/, core::componentmodel::behavior::BaseMechanicalState* ms)
      {
	ms->contributeToMatrixDimension(nbRow, nbCol);
	return RESULT_CONTINUE;
      }
	/// Return a class name for this visitor
	/// Only used for debugging / profiling purposes
	virtual const char* getClassName() const { return "MechanicalGetMatrixDimensionVisitor"; }

    };


    /** Accumulate the entries of a dynamics matrix (mass or stiffness) of the whole scene */
    class SOFA_SIMULATION_COMMON_API MechanicalAddMBK_ToMatrixVisitor : public MechanicalMatrixVisitor
    {
    public:
      BaseMatrix *mat;
      double m, b, k;
      //    unsigned int offset, offsetBckUp;

    MechanicalAddMBK_ToMatrixVisitor(BaseMatrix *_mat, double _m=0.0, double _b=0.0, double _k=0.0, unsigned int _offset=0)
      : mat(_mat),m(_m),b(_b),k(_k)
      {
	offsetOnEnter = _offset;
	offsetOnExit = _offset;
      }

	/// Return a class name for this visitor
	/// Only used for debugging / profiling purposes
	virtual const char* getClassName() const { return "MechanicalAddMBK_ToMatrixVisitor"; }

      virtual Result fwdMechanicalState(simulation::Node* /*node*/, core::componentmodel::behavior::BaseMechanicalState* ms)
      {
	ms->setOffset(offsetOnExit);
	return RESULT_CONTINUE;
      }

      virtual Result fwdForceField(simulation::Node* /*node*/, core::componentmodel::behavior::BaseForceField* ff)
      {
	if ((mat != NULL)&&(k!=0.0))
	  {
	    //offsetOnExit = offsetOnEnter;
	      ff->addMBKToMatrix(mat,m,b,k,offsetOnEnter);
	  }

        return RESULT_CONTINUE;
      }

      //Masses are now added in the addMBKToMatrix call for all ForceFields
      /*
      virtual Result fwdMass(simulation::Node*, core::componentmodel::behavior::BaseMass* mass)
      {
	if ((mat != NULL)&&(m!=0.0))
	  {
	    //offsetOnExit = offsetOnEnter;
	    mass->addMToMatrix(mat,m,offsetOnEnter);
	  }

        return RESULT_CONTINUE;
      }
      */

      virtual Result fwdConstraint(simulation::Node* /*node*/, core::componentmodel::behavior::BaseConstraint* c)
      {
	if (mat != NULL)
	  {
	    //offsetOnExit = offsetOnEnter;
	    c->applyConstraint(mat, offsetOnEnter);
	  }

        return RESULT_CONTINUE;
      }
    };

#if 0 // deprecated: as dx is stored in MechanicalState, compute df there and then convert to BaseVector using MechanicalMultiVector2BaseVectorVisitor
    /** Accumulate the entries of a dynamics vector (e.g. force) of the whole scene */
    class SOFA_SIMULATION_COMMON_API MechanicalAddMBKdx_ToVectorVisitor : public MechanicalMatrixVisitor
    {
    public:
      BaseVector *vect;
      VecId dx;
      double m, b, k;
      //    unsigned int offset, offsetBckUp;

    MechanicalAddMBKdx_ToVectorVisitor(BaseVector *_vect, VecId _dx, double _m=0.0, double _b=0.0, double _k=0.0, unsigned int _offset=0)
      : vect(_vect), dx(_dx), m(_m),b(_b),k(_k)
      {
	offsetOnEnter = _offset;
	offsetOnExit = _offset;
      }

      virtual Result fwdMechanicalState(simulation::Node* /*node*/, core::componentmodel::behavior::BaseMechanicalState* mm)
      {
	mm->setOffset(offsetOnExit);

        if (!dx.isNull())
	  mm->setDx(dx);
        return RESULT_CONTINUE;
      }

      virtual Result fwdForceField(simulation::Node* /*node*/, core::componentmodel::behavior::BaseForceField* ff)
      {
	if ((vect != NULL)&&(k != 0.0))
	  {
	    //	offsetOnExit = offsetOnEnter;
	    ff->addKDxToVector(vect,k,offsetOnEnter);
	  }

        return RESULT_CONTINUE;
      }

      virtual Result fwdMass(simulation::Node* /*node*/, core::componentmodel::behavior::BaseMass* mass)
      {
	if ((vect != NULL)&&(m != 0.0))
	  {
	    //	offsetOnExit = offsetOnEnter;
	    if (dx.isNull())
	      std::cout << "Dx Null\n";
	    else
	      std::cout << "Dx Not Null\n";

	    mass->addMDxToVector(vect,m,offsetOnEnter,dx.isNull());
	  }

        return RESULT_CONTINUE;
      }

      virtual Result fwdConstraint(simulation::Node* /*node*/, core::componentmodel::behavior::BaseConstraint* c)
      {
	if (vect != NULL)
	  {
	    //	offsetOnExit = offsetOnEnter;
	    c->applyConstraint(vect, offsetOnEnter);
	  }

        return RESULT_CONTINUE;
      }
    };
#endif

    class SOFA_SIMULATION_COMMON_API MechanicalMultiVector2BaseVectorVisitor : public MechanicalMatrixVisitor
    {
    public:
      VecId src;
      BaseVector *vect;
      unsigned int offset;

	/// Return a class name for this visitor
	/// Only used for debugging / profiling purposes
	virtual const char* getClassName() const { return "MechanicalMultiVector2BaseVectorVisitor"; }

    MechanicalMultiVector2BaseVectorVisitor(VecId _src, defaulttype::BaseVector * _vect, unsigned int _offset=0)
      : src(_src),vect(_vect),offset(_offset)
      {
      }

      virtual Result fwdMechanicalState(simulation::Node* /*node*/, core::componentmodel::behavior::BaseMechanicalState* mm)
      {
	if (vect!= NULL)
	  {
	    mm->loadInBaseVector(vect, src, offset);
	  }

        return RESULT_CONTINUE;
      }
    };

    class SOFA_SIMULATION_COMMON_API MechanicalMultiVectorPeqBaseVectorVisitor : public MechanicalMatrixVisitor
    {
    public:
      BaseVector *src;
      VecId dest;
      unsigned int offset;

	/// Return a class name for this visitor
	/// Only used for debugging / profiling purposes
	virtual const char* getClassName() const { return "MechanicalMultiVectorPeqBaseVectorVisitor"; }

    MechanicalMultiVectorPeqBaseVectorVisitor(VecId _dest, defaulttype::BaseVector * _src, unsigned int _offset=0)
      : src(_src),dest(_dest),offset(_offset)
      {
      }

      virtual Result fwdMechanicalState(simulation::Node* /*node*/, core::componentmodel::behavior::BaseMechanicalState* mm)
      {
	if (src!= NULL)
	  {
	    mm->addBaseVectorToState(dest, src, offset);
	  }

        return RESULT_CONTINUE;
      }
    };

  } // namespace simulation

} // namespace sofa

#endif