Soft-Bond Model Implementation

See this page for the documentation of this contact model.

contactmodelsoftbond.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#pragma once
// contactmodelsoftbond.h

#include "contactmodel/src/contactmodelmechanical.h"

#ifdef SOFTBOND_LIB
#  define SOFTBOND_EXPORT EXPORT_TAG
#elif defined(NO_MODEL_IMPORT)
#  define SOFTBOND_EXPORT
#else
#  define SOFTBOND_EXPORT IMPORT_TAG
#endif

namespace cmodelsxd {
    using namespace itasca;

    class ContactModelSoftBond : public ContactModelMechanical {
    public:
        // Constructor: Set default values for contact model properties.
        SOFTBOND_EXPORT ContactModelSoftBond();
        // Destructor, called when contact is deleted: free allocated memory, etc.
        SOFTBOND_EXPORT virtual ~ContactModelSoftBond();
        // Contact model name (used as keyword for commands and FISH).
        virtual QString  getName() const { return "softbond"; }
        // The index provides a quick way to determine the type of contact model.
        // Each type of contact model in PFC must have a unique index; this is assigned
        // by PFC when the contact model is loaded. This index should be set to -1
        virtual void     setIndex(int i) { index_=i;}
        virtual int      getIndex() const {return index_;}
        // Contact model version number (e.g., MyModel05_1). The version number can be
        // accessed during the save-restore operation (within the archive method,
        // testing {stream.getRestoreVersion() == getMinorVersion()} to allow for 
        // future modifications to the contact model data structure.
        virtual uint     getMinorVersion() const;
        // Copy the state information to a newly created contact model.
        // Provide access to state information, for use by copy method.
        virtual void     copy(const ContactModel *c) override;
        // Provide save-restore capability for the state information.
        virtual void     archive(ArchiveStream &); 
        // Enumerator for the properties.
        enum PropertyKeys { 
              kwKn=1
            , kwKs                            
            , kwFric 
            , kwBMul
            , kwTMul
            , kwSBMode
            , kwSBF
            , kwSBM
            , kwSBS
            , kwSBBS
            , kwSBTS
            , kwSBRMul
            , kwSBRadius
            , kwEmod
            , kwKRatio
            , kwDpNRatio 
            , kwDpSRatio
            , kwDpMode 
            , kwDpF
            , kwSBState
            , kwSBTStr
            , kwSBSStr
            , kwSBCoh
            , kwSBFa
            , kwSBMCF
            , kwSBSig
            , kwSBTau
            , kwSBSoft
            , kwSBCut
            , kwSBArea
            , kwUserArea
            , kwRGap
        };
        // Contact model property names in a comma separated list. The order corresponds with
        // the order of the PropertyKeys enumerator above. One can visualize any of these 
        // properties in PFC automatically. 
        virtual QString  getProperties() const { 
            return "kn"
                   ",ks"
                   ",fric"
                   ",sb_bmul"
                   ",sb_tmul"
                   ",sb_mode"
                   ",sb_force"
                   ",sb_moment"
                   ",sb_slip"
                   ",sb_slipb"
                   ",sb_slipt"
                   ",sb_rmul"
                   ",sb_radius"
                   ",emod"
                   ",kratio"
                   ",dp_nratio"
                   ",dp_sratio"
                   ",dp_mode"
                   ",dp_force"
                   ",sb_state"
                   ",sb_ten"
                   ",sb_shear"
                   ",sb_coh"
                   ",sb_fa"
                   ",sb_mcf"
                   ",sb_sigma"
                   ",sb_tau"
                   ",sb_soft"
                   ",sb_cut"
                   ",sb_area"
                   ",user_area"
                   ",rgap"
                ;
        }
        // Enumerator for the energies.
        enum EnergyKeys { 
            kwEStrain=1
          , kwESlip
          , kwEDashpot
        };
        // Contact model energy names in a comma separated list. The order corresponds with
        // the order of the EnergyKeys enumerator above. 
        virtual QString  getEnergies() const { 
            return "energy-strain"
                   ",energy-slip"
                   ",energy-dashpot";
        }
        // Returns the value of the energy (base 1 - getEnergy(1) returns the estrain energy).
        virtual double   getEnergy(uint i) const; 
        // Returns whether or not each energy is accumulated (base 1 - getEnergyAccumulate(1) 
        // returns wther or not the estrain energy is accumulated which is false).
        virtual bool     getEnergyAccumulate(uint i) const;
        // Set an energy value (base 1 - setEnergy(1) sets the estrain energy).
        virtual void     setEnergy(uint i,const double &d); // Base 1
        // Activate the energy. This is only called if the energy tracking is enabled. 
        virtual void     activateEnergy() { if (energies_) return; energies_ = NEWC(Energies());}
        // Returns whether or not the energy tracking has been enabled for this contact.
        virtual bool     getEnergyActivated() const {return (energies_ != 0);}

        // Enumerator for contact model related FISH callback events. 
        enum FishCallEvents {
             fActivated=0
            ,fSlipChange
            ,fBondBreak
        };
        // Contact model FISH callback event names in a comma separated list. The order corresponds with
        // the order of the FishCallEvents enumerator above. 
        virtual QString  getFishCallEvents() const { 
            return 
                "contact_activated"
                ",slip_change"
                ",bond_break"; 
        }

        // Return the specified contact model property.
        virtual QVariant getProperty(uint i,const IContact *) const;
        // The return value denotes whether or not the property corresponds to the global
        // or local coordinate system (TRUE: global system, FALSE: local system). The
        // local system is the contact-plane system (nst) defined as follows.
        // If a vector V is expressed in the local system as (Vn, Vs, Vt), then V is
        // expressed in the global system as {Vn*nc + Vs*sc + Vt*tc} where where nc, sc
        // and tc are unit vectors in directions of the nst axes.
        // This is used when rendering contact model properties that are vectors.
        virtual bool     getPropertyGlobal(uint i) const;
        // Set the specified contact model property, ensuring that it is of the correct type
        // and within the correct range --- if not, then throw an exception.
        // The return value denotes whether or not the update has affected the timestep
        // computation (by having modified the translational or rotational tangent stiffnesses).
        // If true is returned, then the timestep will be recomputed.
        virtual bool     setProperty(uint i,const QVariant &v,IContact *);
        // The return value denotes whether or not the property is read-only
        // (TRUE: read-only, FALSE: read-write).
        virtual bool     getPropertyReadOnly(uint i) const;

        // The return value denotes whether or not the property is inheritable
        // (TRUE: inheritable, FALSE: not inheritable). Inheritance is provided by
        // the endPropertyUpdated method.
        virtual bool     supportsInheritance(uint i) const; 
        // Return whether or not inheritance is enabled for the specified property.
        virtual bool     getInheritance(uint i) const { assert(i<32); quint32 mask = to<quint32>(1 << i);  return (inheritanceField_ & mask) ? true : false; }
        // Set the inheritance flag for the specified property.
        virtual void     setInheritance(uint i,bool b) { assert(i<32); quint32 mask = to<quint32>(1 << i);  if (b) inheritanceField_ |= mask;  else inheritanceField_ &= ~mask; }

        // Enumerator for contact model methods.
        enum MethodKeys { kwDeformability=1, kwBond, kwUnbond, kwArea};
        // Contact model methoid names in a comma separated list. The order corresponds with
        // the order of the MethodKeys enumerator above.  
        virtual QString  getMethods() const { return "deformability,bond,unbond,area";}
        // Return a comma seprated list of the contact model method arguments (base 1).
        virtual QString  getMethodArguments(uint i) const; 
        // Set contact model method arguments (base 1). 
        // The return value denotes whether or not the update has affected the timestep
        // computation (by having modified the translational or rotational tangent stiffnesses).
        // If true is returned, then the timestep will be recomputed.
        virtual bool     setMethod(uint i,const QVector<QVariant> &vl,IContact *con=0); 

        // Prepare for entry into ForceDispLaw. The validate function is called when:
        // (1) the contact is created, (2) a property of the contact that returns a true via
        // the setProperty method has been modified and (3) when a set of cycles is executed
        // via the {cycle N} command.
        // Return value indicates contact activity (TRUE: active, FALSE: inactive).
        virtual bool    validate(ContactModelMechanicalState *state,const double &timestep);
        // The endPropertyUpdated method is called whenever a surface property (with a name
        // that matches an inheritable contact model property name) of one of the contacting
        // pieces is modified. This allows the contact model to update its associated
        // properties. The return value denotes whether or not the update has affected
        // the time step computation (by having modified the translational or rotational
        // tangent stiffnesses). If true is returned, then the time step will be recomputed.  
        virtual bool    endPropertyUpdated(const QString &name,const IContactMechanical *c);
        // The forceDisplacementLaw function is called during each cycle. Given the relative
        // motion of the two contacting pieces (via
        //   state->relativeTranslationalIncrement_ (Ddn, Ddss, Ddst)
        //   state->relativeAngularIncrement_       (Dtt, Dtbs, Dtbt)
        //     Ddn  : relative normal-displacement increment, Ddn > 0 is opening
        //     Ddss : relative  shear-displacement increment (s-axis component)
        //     Ddst : relative  shear-displacement increment (t-axis component)
        //     Dtt  : relative twist-rotation increment
        //     Dtbs : relative  bend-rotation increment (s-axis component)
        //     Dtbt : relative  bend-rotation increment (t-axis component)
        //       The relative displacement and rotation increments:
        //         Dd = Ddn*nc + Ddss*sc + Ddst*tc
        //         Dt = Dtt*nc + Dtbs*sc + Dtbt*tc
        //       where nc, sc and tc are unit vectors in direc. of the nst axes, respectively.
        //       [see {Table 1: Contact State Variables} in PFC Model Components:
        //       Contacts and Contact Models: Contact Resolution]
        // ) and the contact properties, this function must update the contact force and
        // moment.
        //   The force_ is acting on piece 2, and is expressed in the local coordinate system
        //   (defined in getPropertyGlobal) such that the first component positive denotes
        //   compression. If we define the moment acting on piece 2 by Mc, and Mc is expressed
        //   in the local coordinate system (defined in getPropertyGlobal), then we must use the getMechanicalContact()->updateResultingTorquesLocal(...) method to 
        //   get the total moment. 
        // The return value indicates the contact activity status (TRUE: active, FALSE:
        // inactive) during the next cycle.
        // Additional information:
        //   * If state->activated() is true, then the contact has just become active (it was
        //     inactive during the previous time step).
        //   * Fully elastic behavior is enforced during the SOLVE ELASTIC command by having
        //     the forceDispLaw handle the case of {state->canFail_ == true}.
        virtual bool    forceDisplacementLaw(ContactModelMechanicalState *state,const double &timestep);
        // Perform thermal coupling
        virtual bool    thermalCoupling(ContactModelMechanicalState*, ContactModelThermalState*, IContactThermal*, const double&);
        // The getEffectiveXStiffness functions return the translational and rotational
        // tangent stiffnesses used to compute a stable time step. When a contact is sliding,
        // the translational tangent shear stiffness is zero (but this stiffness reduction
        // is typically ignored when computing a stable time step). If the contact model
        // includes a dashpot, then the translational stiffnesses must be increased (see
        // Potyondy (2009)).
        //   [Potyondy, D. 'Stiffness Matrix at a Contact Between Two Clumps,' Itasca
        //   Consulting Group, Inc., Minneapolis, MN, Technical Memorandum ICG6863-L,
        //   December 7, 2009.]
        virtual DVect2  getEffectiveTranslationalStiffness() const { return effectiveTranslationalStiffness_; }
        virtual DAVect  getEffectiveRotationalStiffness() const { return effectiveRotationalStiffness_;}

        // Return a new instance of the contact model. This is used in the CMAT
        // when a new contact is created. 
        virtual ContactModelSoftBond *clone() const override { return NEWC(ContactModelSoftBond()); }
        // The getActivityDistance function is called by the contact-resolution logic when
        // the CMAT is modified. Return value is the activity distance used by the
        // checkActivity function.
        virtual double              getActivityDistance() const {return rgap_;}
        // The isOKToDelete function is called by the contact-resolution logic when...
        // Return value indicates whether or not the contact may be deleted.
        // If TRUE, then the contact may be deleted when it is inactive.
        // If FALSE, then the contact may not be deleted (under any condition).
        virtual bool                isOKToDelete() const { return !isBonded(); }
        // Zero the forces and moments stored in the contact model. This function is called
        // when the contact becomes inactive.
        virtual void                resetForcesAndMoments() { 
            sb_F(DVect(0.0)); 
            dp_F(DVect(0.0)); 
            sb_M(DAVect(0.0));
            if (energies_) {
                energies_->estrain_ = 0.0;
            }
        }
        virtual void     setForce(const DVect &v,IContact *c);
        virtual void     setArea(const double &d) { userArea_ = d; }
        virtual double   getArea() const { return userArea_; }

        // The checkActivity function is called by the contact-resolution logic when...
        // Return value indicates contact activity (TRUE: active, FALSE: inactive).
        virtual bool     checkActivity(const double &gap) { return  gap <= rgap_ || isBonded();}

        // Returns the sliding state (FALSE is returned if not implemented).
        virtual bool     isSliding() const { return sb_S_; }
        // Returns the bonding state (FALSE is returned if not implemented).
        virtual bool     isBonded() const { return bProps_ ? (bProps_->sb_state_ >= 3) : false; }
        virtual void     unbond() { if (bProps_) bProps_->sb_state_ = 0; }

        // Both of these methods are called only for contacts with facets where the wall 
        // resolution scheme is set the full. In such cases one might wish to propagate 
        // contact state information (e.g., shear force) from one active contact to another. 
        // See the Faceted Wall section in the documentation. 
        virtual void     propagateStateInformation(IContactModelMechanical* oldCm,const CAxes &oldSystem=CAxes(),const CAxes &newSystem=CAxes());
        virtual void     setNonForcePropsFrom(IContactModel *oldCM);
           
        /// Return the total force that the contact model holds.
        virtual DVect    getForce(const IContactMechanical *) const;

        /// Return the total moment on 1 that the contact model holds
        virtual DAVect   getMomentOn1(const IContactMechanical *) const;

        /// Return the total moment on 1 that the contact model holds
        virtual DAVect   getMomentOn2(const IContactMechanical *) const;

        // Methods to get and set properties. 
        const double & kn() const {return kn_;}
        void           kn(const double &d) {kn_=d;}
        const double & ks() const {return ks_;}
        void           ks(const double &d) {ks_=d;}
        const double & fric() const {return fric_;}
        void           fric(const double &d) {fric_=d;}
        const double & sb_bmul() const { return   sb_bmul_; }
        void           sb_bmul(const double &d) { sb_bmul_ = d; }
        const double & sb_tmul() const { return   sb_tmul_; }
        void           sb_tmul(const double &d) { sb_tmul_ = d; }
        const DVect &  sb_F() const {return sb_F_;}
        void           sb_F(const DVect &f) { sb_F_=f;}
        const DAVect & sb_M() const { return sb_M_; }
        void           sb_M(const DAVect &f) { sb_M_ = f; }
        bool           sb_S() const {return sb_S_;}
        void           sb_S(bool b) { sb_S_=b;}
        bool           sb_BS() const { return sb_BS_; }
        void           sb_BS(bool b) { sb_BS_ = b; }
        bool           sb_TS() const { return sb_TS_; }
        void           sb_TS(bool b) { sb_TS_ = b; }
        const double & sb_rmul() const { return   sb_rmul_;        }
        void           sb_rmul(const double &d) { sb_rmul_ = d; }
        uint           sb_mode() const {return sb_mode_;}
        void           sb_mode(uint i) { sb_mode_=i;}

        bool           hasBond() const { return bProps_ ? true : false; }
        int            sb_state()   const { return (hasBond() ? bProps_->sb_state_ : 0); }
        void           sb_state(int i) { if (!hasBond()) return; bProps_->sb_state_ = i; }
        double         sb_Ten() const { return (hasBond() ? (bProps_->sb_ten_) : 0.0); }
        void           sb_Ten(const double &d) { if (!hasBond()) return; bProps_->sb_ten_ = d; }
        double         sb_Coh() const { return (hasBond() ? (bProps_->sb_coh_) : 0.0); }
        void           sb_Coh(const double &d) { if (!hasBond()) return; bProps_->sb_coh_ = d; }
        double         sb_FA() const { return (hasBond() ? (bProps_->sb_fa_) : 0.0); }
        void           sb_FA(const double &d) { if (!hasBond()) return; bProps_->sb_fa_ = d; }
        double         sb_MCF() const {return (hasBond() ? (bProps_->sb_mcf_) : 0.0);}
        void           sb_MCF(const double &d) { if(!hasBond()) return; bProps_->sb_mcf_=d;}
        double         sb_soft() const { return (hasBond() ? (bProps_->sb_soft_) : 0.0); }
        void           sb_soft(const double &d) { if (!hasBond()) return; bProps_->sb_soft_ = d; }
        double         sb_cut() const { return (hasBond() ? (bProps_->sb_cut_) : 0.0); }
        void           sb_cut(const double &d) { if (!hasBond()) return; bProps_->sb_cut_ = d; }
        double         sb_maxTen() const { return (hasBond() ? (bProps_->sb_maxTen_) : 0.0); }
        void           sb_maxTen(const double &d) { if (!hasBond()) return; bProps_->sb_maxTen_ = d; }
        double         sb_delu() const { return (hasBond() ? (bProps_->sb_delu_) : 0.0); }
        void           sb_delu(const double &d) { if (!hasBond()) return; bProps_->sb_delu_ = d; }
        Quat           sb_delo() const { return (hasBond() ? (bProps_->sb_delo_) : Quat::identity()); }
        void           sb_delo(const Quat &d) { if (!hasBond()) return; bProps_->sb_delo_ = d; }
        double         sb_maxu() const { return (hasBond() ? (bProps_->sb_maxu_) : 0.0); }
        void           sb_maxu(const double &d) { if (!hasBond()) return; bProps_->sb_maxu_ = d; }
        double         sb_critu() const { return (hasBond() ? (bProps_->sb_critu_) : 0.0); }
        void           sb_critu(const double &d) { if (!hasBond()) return; bProps_->sb_critu_ = d; }

        bool     hasDamping() const {return dpProps_ ? true : false;}
        double   dp_nratio() const {return (hasDamping() ? (dpProps_->dp_nratio_) : 0.0);}
        void     dp_nratio(const double &d) { if(!hasDamping()) return; dpProps_->dp_nratio_=d;}
        double   dp_sratio() const {return hasDamping() ? dpProps_->dp_sratio_: 0.0;}
        void     dp_sratio(const double &d) { if(!hasDamping()) return; dpProps_->dp_sratio_=d;}
        int      dp_mode() const {return hasDamping() ? dpProps_->dp_mode_: -1;}
        void     dp_mode(int i) { if(!hasDamping()) return; dpProps_->dp_mode_=i;}
        DVect    dp_F() const {return hasDamping() ? dpProps_->dp_F_: DVect(0.0);}
        void     dp_F(const DVect &f) { if(!hasDamping()) return; dpProps_->dp_F_=f;}

        bool    hasEnergies() const {return energies_ ? true:false;}
        double  estrain() const {return hasEnergies() ? energies_->estrain_: 0.0;}
        void    estrain(const double &d) { if(!hasEnergies()) return; energies_->estrain_=d;}
        double  eslip() const {return hasEnergies() ? energies_->eslip_: 0.0;}
        void    eslip(const double &d) { if(!hasEnergies()) return; energies_->eslip_=d;}
        double  edashpot() const {return hasEnergies() ? energies_->edashpot_: 0.0;}
        void    edashpot(const double &d) { if(!hasEnergies()) return; energies_->edashpot_=d;}

        uint inheritanceField() const {return inheritanceField_;}
        void inheritanceField(uint i) {inheritanceField_ = i;}

        const DVect2 & effectiveTranslationalStiffness()  const          {return effectiveTranslationalStiffness_;}
        void           effectiveTranslationalStiffness(const DVect2 &v ) {effectiveTranslationalStiffness_=v;}
        const DAVect & effectiveRotationalStiffness()  const             {return effectiveRotationalStiffness_;}
        void           effectiveRotationalStiffness(const DAVect &v )    {effectiveRotationalStiffness_=v;}

    private:
        // Index - used internally by PFC. Should be set to -1 in the cpp file. 
        static int index_;

        bool  FDLawBonded(ContactModelMechanicalState *state, const double &timestep);
        bool  FDLawUnBonded(ContactModelMechanicalState *state, const double &timestep);

        // Structure to compute stiffness
        struct StiffData {
            DVect2 trans_ = DVect2(0.0);
            DAVect ang_   = DAVect(0.0);
            double reff_ = 0.0;
        };

        // Structure to store the energies. 
        struct Energies {
            double estrain_  = 0.0;   // elastic energy  
            double eslip_    = 0.0;   // work dissipated by friction 
            double edashpot_ = 0.0;   // work dissipated by dashpots
        };

        // Structure to store dashpot quantities. 
        struct dpProps {
            double dp_nratio_ = 0.0;         // normal viscous critical damping ratio
            double dp_sratio_ = 0.0;         // shear  viscous critical damping ratio
            int    dp_mode_   = 0;           // for viscous mode (0-4) 0 = dashpots, 1 = tensile limit, 2 = shear limit, 3 = limit both
            DVect  dp_F_      = DVect(0.0);  // Force in the dashpots
        };

        // Structure to store bond-related quantities. 
        struct bProps {
            int     sb_state_    = 0;                // bond mode - 0 (NBNF), 1 (NBFT), 2 (NBFS), 3 (B), 4 (B-Softening), 5 (B-Compression from Softening)
            double  sb_ten_      = 0.0;              // normal strength 
            double  sb_coh_      = 0.0;              // cohesion
            double  sb_fa_       = 0.0;              // friction angle
            double  sb_mcf_      = 1.0;              // moment contribution factor
            double  sb_soft_     = 0.0;              // softening factor 
            double  sb_cut_      = 1.0;              // critical bond length
            double  sb_maxTen_   = 0.0;              // tensile strength one needs to reach for softening
            double  sb_delu_     = 0.0;              // incremental elongation in softening
            Quat    sb_delo_     = Quat::identity(); // incremental orientation in softening
            double  sb_maxu_     = 0.0;              // max elongation for softening
            double  sb_critu_    = 0.0;              // critical elongation for softening
        };


        bool   updateKn(const IContactMechanical *con);
        bool   updateKs(const IContactMechanical *con);
        bool   updateFric(const IContactMechanical *con);

        StiffData computeStiffData(ContactModelMechanicalState *state) const;
        DVect3    computeGeomData(const IContactMechanical *c) const;
        DVect2    SMax(const IContactMechanical *con) const; // Maximum stress (tensile,shear) at bond periphery
        double    shearStrength(const double &pbArea) const;      // Bond shear strength
        double    strainEnergy(double kn, double ks, double kb, double kt) const;

        void      updateStiffness(ContactModelMechanicalState *state);

        // Contact model inheritance fields.
        quint32 inheritanceField_;

        // Effective translational stiffness.
        DVect2  effectiveTranslationalStiffness_;
        DAVect  effectiveRotationalStiffness_;      // (Twisting,Bending,Bending) Rotational stiffness (twisting always 0)

        // linear model properties
        double      kn_;          // Normal stiffness
        double      ks_;          // Shear stiffness
        double      fric_;        // Coulomb friction coefficient
        double      sb_bmul_;     // Bending friction multiplier
        double      sb_tmul_;     // Twisting friction  multiplier
        uint        sb_mode_;     // specifies absolute (0) or incremental (1) behavior for the the normal force 
        DVect       sb_F_;        // Force carried in the model
        DAVect      sb_M_;        // moment (bending + twisting in 3D)         
        bool        sb_S_;        // The current slip state
        bool        sb_BS_;       // The bending  slip state
        bool        sb_TS_;       // The twisting slip state
        double      sb_rmul_;     // Radius multiplier
        double      userArea_;    // Area as specified by the user 
        double      rgap_;        // Reference gap

        dpProps *   dpProps_;   // The viscous properties
        bProps *    bProps_;     // The bond properties

        Energies *   energies_; // The energies

    };
} // namespace cmodelsxd
// EoF

Top

contactmodelsoftbond.cpp

   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
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
// contactmodelsoftbond.cpp
#include "contactmodelsoftbond.h"

#include "module/interface/icontactmechanical.h"
#include "module/interface/icontact.h"
#include "module/interface/ipiecemechanical.h"
#include "module/interface/ipiece.h"
#include "module/interface/ifishcalllist.h"

#include "utility/src/tptr.h"
#include "shared/src/mathutil.h"

#include "kernel/interface/iprogram.h"
#include "module/interface/icontactthermal.h"
#include "contactmodel/src/contactmodelthermal.h"
#include "../version.txt"
#include "fish/src/parameter.h"

#ifdef SOFTBOND_LIB
#ifdef _WIN32
    int __stdcall DllMain(void *,unsigned, void *) {
        return 1;
    }
#endif
    extern "C" EXPORT_TAG const char *getName() {
#if DIM==3
        return "contactmodelmechanical3dsoftbond";
#else
        return "contactmodelmechanical2dsoftbond";
#endif
    }

    extern "C" EXPORT_TAG unsigned getMajorVersion() {
        return MAJOR_VERSION;
    }

    extern "C" EXPORT_TAG unsigned getMinorVersion() {
        return MINOR_VERSION;
    }

    extern "C" EXPORT_TAG void *createInstance() {
        cmodelsxd::ContactModelSoftBond *m = NEWC(cmodelsxd::ContactModelSoftBond());
        return (void *)m;
    }
#endif 

namespace cmodelsxd {
    static const quint32 KnMask      = 0x00000002; // Base 1!
    static const quint32 KsMask      = 0x00000004;
    static const quint32 FricMask    = 0x00000008;

    using namespace itasca;

    int ContactModelSoftBond::index_ = -1;
    UInt ContactModelSoftBond::getMinorVersion() const { return MINOR_VERSION; }

    ContactModelSoftBond::ContactModelSoftBond() : inheritanceField_(KnMask|KsMask|FricMask) 
                                             , effectiveTranslationalStiffness_(DVect2(0.0)) 
                                             , effectiveRotationalStiffness_(DAVect(0.0))
                                             , kn_(0.0)
                                             , ks_(0.0)
                                             , fric_(0.0)
                                             , sb_bmul_(1.0)
                                             , sb_tmul_(1.0)
                                             , sb_mode_(0)
                                             , sb_F_(DVect(0.0))
                                             , sb_M_(DAVect(0.0))
                                             , sb_S_(false)
                                             , sb_BS_(false)
                                             , sb_TS_(false)
                                             , sb_rmul_(1.0)
                                             , userArea_(0.0)
                                             , rgap_(0.0)
                                             , dpProps_(nullptr)
                                             , bProps_(nullptr)
                                             , energies_(nullptr) {
    }

    ContactModelSoftBond::~ContactModelSoftBond() {
        // Make sure to clean up after yourself!
        if (dpProps_)
            delete dpProps_;
        if (bProps_)
            delete bProps_;
        if (energies_)
            delete energies_;
    }

    void ContactModelSoftBond::archive(ArchiveStream &stream) {
        // The stream allows one to archive the values of the contact model
        // so that it can be saved and restored. The minor version can be
        // used here to allow for incremental changes to the contact model too. 
        stream & kn_;
        stream & ks_;
        stream & fric_;
        stream & sb_mode_;
        stream & sb_F_;
        stream & sb_M_;
        stream & sb_S_;
        stream & sb_BS_;
        stream & sb_TS_;
        stream & sb_rmul_;
 
        if (stream.getArchiveState()==ArchiveStream::Save) {
            bool b = false;
            if (dpProps_) {
                b = true;
                stream & b;
                stream & dpProps_->dp_nratio_; 
                stream & dpProps_->dp_sratio_; 
                stream & dpProps_->dp_mode_; 
                stream & dpProps_->dp_F_; 
            }
            else
                stream & b;

            b = false;
            if (energies_) {
                b = true;
                stream & b;
                stream & energies_->estrain_;
                stream & energies_->eslip_;
                stream & energies_->edashpot_;
            }
            else
                stream & b;

            b = false;
            if (bProps_) {
                b = true;
                stream & b;
                stream & bProps_->sb_state_;
                stream & bProps_->sb_ten_;
                stream & bProps_->sb_coh_;
                stream & bProps_->sb_fa_;
                stream & bProps_->sb_mcf_;
                stream & bProps_->sb_soft_;
                stream & bProps_->sb_cut_;
                stream & bProps_->sb_maxTen_;
                stream & bProps_->sb_delu_;
                stream & bProps_->sb_delo_;
                stream & bProps_->sb_maxu_;
                stream & bProps_->sb_critu_;
            }
            else
                stream & b;

        } else {
            bool b(false);
            stream & b;
            if (b) {
                if (!dpProps_)
                    dpProps_ = NEWC(dpProps());
                stream & dpProps_->dp_nratio_; 
                stream & dpProps_->dp_sratio_; 
                stream & dpProps_->dp_mode_; 
                stream & dpProps_->dp_F_; 
            }
            stream & b;
            if (b) {
                if (!energies_)
                    energies_ = NEWC(Energies());
                stream & energies_->estrain_;
                stream & energies_->eslip_;
                stream & energies_->edashpot_;
            }
            stream & b;
            if (b) {
                if (!bProps_)
                    bProps_ = NEWC(bProps());
                stream & bProps_->sb_state_;
                stream & bProps_->sb_ten_;
                stream & bProps_->sb_coh_;
                stream & bProps_->sb_fa_;
                stream & bProps_->sb_mcf_;
                stream & bProps_->sb_soft_;
                stream & bProps_->sb_cut_;
                stream & bProps_->sb_maxTen_;
                stream & bProps_->sb_delu_;
                stream & bProps_->sb_delo_;
                stream & bProps_->sb_maxu_;
                stream & bProps_->sb_critu_;
            }

        }

        stream & inheritanceField_;
        stream & effectiveTranslationalStiffness_;
        stream & effectiveRotationalStiffness_;  

        if (stream.getArchiveState() == ArchiveStream::Save || stream.getRestoreVersion() > 1) {
            stream & sb_bmul_;
            stream & sb_tmul_;
        }

        if (stream.getArchiveState() == ArchiveStream::Save || stream.getRestoreVersion() > 2) 
            stream & userArea_;
        
        if (stream.getArchiveState() == ArchiveStream::Save || stream.getRestoreVersion() > 3) 
            stream & rgap_;

    }

    void ContactModelSoftBond::copy(const ContactModel *cm) {
        // Copy all of the contact model properties. Used in the CMAT 
        // when a new contact is created. 
        ContactModelMechanical::copy(cm);
        const ContactModelSoftBond *in = dynamic_cast<const ContactModelSoftBond*>(cm);
        if (!in) throw std::runtime_error("Internal error: contact model dynamic cast failed.");
        kn(in->kn());
        ks(in->ks());
        fric(in->fric());
        sb_bmul(in->sb_bmul());
        sb_tmul(in->sb_tmul());
        sb_mode(in->sb_mode());
        sb_F(in->sb_F());
        sb_S(in->sb_S());
        sb_BS(in->sb_BS());
        sb_TS(in->sb_TS());
        sb_rmul(in->sb_rmul());
        sb_M(in->sb_M());      

        if (in->hasDamping()) {
            if (!dpProps_)
                dpProps_ = NEWC(dpProps());
            dp_nratio(in->dp_nratio()); 
            dp_sratio(in->dp_sratio()); 
            dp_mode(in->dp_mode()); 
            dp_F(in->dp_F()); 
        }
        if (in->hasEnergies()) {
            if (!energies_)
                energies_ = NEWC(Energies());
            estrain(in->estrain());
            eslip(in->eslip());
            edashpot(in->edashpot());
        }
        if (in->hasBond()) {
            if (!bProps_)
                bProps_ = NEWC(bProps());
            sb_state(in->sb_state());
            sb_Ten(in->sb_Ten());
            sb_Coh(in->sb_Coh());
            sb_FA(in->sb_FA());
            sb_MCF(in->sb_MCF());
            sb_soft(in->sb_soft());
            sb_cut(in->sb_cut());
            sb_maxTen(in->sb_maxTen());
            sb_delu(in->sb_delu());
            sb_delo(in->sb_delo());
            sb_maxu(in->sb_maxu());
            sb_critu(in->sb_critu());
        }
        userArea_ = in->userArea_;
        rgap_ = in->rgap_;
        inheritanceField(in->inheritanceField());
        effectiveTranslationalStiffness(in->effectiveTranslationalStiffness());
        effectiveRotationalStiffness(in->effectiveRotationalStiffness());
    }


    QVariant ContactModelSoftBond::getProperty(uint i,const IContact *con) const {
        // Return the property. The IContact pointer is provided so that 
        // more complicated properties, depending on contact characteristics,
        // can be calcualted. 
        QVariant var;
        switch (i) {
        case kwKn:       return kn_;
        case kwKs:       return ks_;
        case kwFric:     return fric_;
        case kwBMul:     return sb_bmul_;
        case kwTMul:     return sb_tmul_;
        case kwSBMode:   return sb_mode_;
        case kwSBF:      var.setValue(sb_F_); return var;
        case kwSBM:      var.setValue(sb_M_); return var;
        case kwSBS:      return sb_S_;
        case kwSBBS:     return sb_BS_;
        case kwSBTS:     return sb_TS_;
        case kwSBRMul:   return sb_rmul_;
        case kwSBRadius: {
            const IContactMechanical *c(convert_getcast<IContactMechanical>(con));
            if (!c) return 0.0;
            double Cmax1 = c->getEnd1Curvature().y();
            double Cmax2 = c->getEnd2Curvature().y();
            if (!userArea_)
                return sb_rmul_ * 1.0 / std::max(Cmax1, Cmax2);
            else {
#ifdef THREED
                double rad = std::sqrt(userArea_ / dPi);
#else
                double rad = userArea_ / 2.0;
#endif
                return rad;
            }
                
        }
        case kwEmod: {
                const IContactMechanical *c(convert_getcast<IContactMechanical>(con));
                if (!c) return 0.0;
                double rsum(0.0);
                if (c->getEnd1Curvature().y())
                    rsum += 1.0/c->getEnd1Curvature().y();
                if (c->getEnd2Curvature().y())
                    rsum += 1.0/c->getEnd2Curvature().y();
                if (userArea_)
#ifdef THREED
                    rsum = 2.0 * std::sqrt(userArea_ / dPi);
#else
                    rsum = userArea_;
#endif
                return kn_ * rsum;
        }
        case kwKRatio:    return (ks_ == 0.0) ? 0.0 : (kn_/ks_);
        case kwDpNRatio:  return dpProps_ ? dpProps_->dp_nratio_ : 0;
        case kwDpSRatio:  return dpProps_ ? dpProps_->dp_sratio_ : 0;
        case kwDpMode:    return dpProps_ ? dpProps_->dp_mode_ : 0;
        case kwDpF: {
                dpProps_ ? var.setValue(dpProps_->dp_F_) : var.setValue(DVect(0.0));
                return var;
            }
        case kwSBState:     return bProps_ ? bProps_->sb_state_ : 0;
        case kwSBTStr:      return bProps_ ? bProps_->sb_ten_ : 0.0;
        case kwSBSStr: {
            if (!bProps_) return 0.0;
            const IContactMechanical *c(convert_getcast<IContactMechanical>(con));
            double area = computeGeomData(c).x();
            return shearStrength(area);
        }
        case kwSBCoh:       return bProps_ ? bProps_->sb_coh_ : 0;
        case kwSBFa:        return bProps_ ? bProps_->sb_fa_ : 0;
        case kwSBMCF:       return bProps_ ? bProps_->sb_mcf_ : 0;
        case kwSBSig: {
            if (!bProps_ || bProps_->sb_state_ < 3) return 0.0;
            const IContactMechanical *c(convert_getcast<IContactMechanical>(con));
            return SMax(c).x();
        }
        case kwSBTau: {
            if (!bProps_ || bProps_->sb_state_ < 3) return 0.0;
            const IContactMechanical *c(convert_getcast<IContactMechanical>(con));
            return SMax(c).y();
        }
        case kwSBSoft:
            if (!bProps_) return 0.0;
            return bProps_->sb_soft_;
        case kwSBCut:
            if (!bProps_) return 0.0;
            return bProps_->sb_cut_;
        case kwSBArea: {
                if (userArea_) return userArea_;
                //if (!bProps_) return 0.0;
                const IContactMechanical *c(convert_getcast<IContactMechanical>(con));
                if (!c)
                    return 0.0;
                return computeGeomData(c).x();
            }
        case kwUserArea:
            return userArea_;
        case kwRGap:
            return rgap_;
        }
        assert(0);
        return QVariant();
    }

    bool ContactModelSoftBond::getPropertyGlobal(uint i) const {
        // Returns whether or not a property is held in the global axis system (TRUE)
        // or the local system (FALSE). Used by the plotting logic.
        switch (i) {
        case kwSBF:   
        case kwSBM:
        case kwDpF:
            return false;
        }
        return true;
    }

    bool ContactModelSoftBond::setProperty(uint i,const QVariant &v,IContact *) {
        // Set a contact model property. Return value indicates that the timestep
        // should be recalculated. 
        dpProps dp;
        switch (i) {
        case kwKn: {
                if (!v.canConvert<double>())
                    throw Exception("kn must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative kn not allowed.");
                kn_ = val;
                return true;
            }
        case kwKs: {
                if (!v.canConvert<double>())
                    throw Exception("ks must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative ks not allowed.");
                ks_ = val;  
                return true;
            }
        case kwFric: {
                if (!v.canConvert<double>())
                    throw Exception("fric must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative fric not allowed.");
                fric_ = val;
                return false;
            }
        case kwBMul: {
                if (!v.canConvert<double>())
                    throw Exception("sb_bmul must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative sb_bmul not allowed.");
                sb_bmul_ = val;
                return false;
            }
        case kwTMul: {
                if (!v.canConvert<double>())
                    throw Exception("sb_tmul must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative st_bmul not allowed.");
                sb_tmul_ = val;
                return false;
            }
        case kwSBMode: {
                if (!v.canConvert<uint>())
                    throw Exception("sb_mode must be 0 (absolute) or 1 (incremental).");
                double val(v.toUInt());
                if (val>1)
                    throw Exception("sb_mode must be 0 (absolute) or 1 (incremental).");
                sb_mode_ = val;
                return false;
            }
        case kwSBRMul: {
                if (!v.canConvert<double>())
                    throw Exception("rmul must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative rmul not allowed.");
                sb_rmul_ = val;
                return false;
            }
        case kwSBF: {
                if (!v.canConvert<DVect>())
                    throw Exception("sb_force must be a vector.");
                DVect val(v.value<DVect>());
                sb_F_ = val;
                return false;
            }
        case kwSBM: {
                DAVect val(0.0);
#ifdef TWOD               
                if (!v.canConvert<DAVect>() && !v.canConvert<double>())
                    throw Exception("res_moment must be an angular vector.");
                if (v.canConvert<DAVect>())
                    val = DAVect(v.value<DAVect>());
                else
                    val = DAVect(v.toDouble());
#else
                if (!v.canConvert<DAVect>() && !v.canConvert<DVect>())
                    throw Exception("res_moment must be an angular vector.");
                if (v.canConvert<DAVect>())
                    val = DAVect(v.value<DAVect>());
                else
                    val = DAVect(v.value<DVect>());
#endif
                sb_M_ = val;
                return false;
            }
        case kwDpNRatio: {
                if (!v.canConvert<double>())
                    throw Exception("dp_nratio must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative dp_nratio not allowed.");
                if (val == 0.0 && !dpProps_)
                    return false;
                if (!dpProps_)
                    dpProps_ = NEWC(dpProps());
                dpProps_->dp_nratio_ = val; 
                return true;
            }
        case kwDpSRatio: {
                if (!v.canConvert<double>())
                    throw Exception("dp_sratio must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative dp_sratio not allowed.");
                if (val == 0.0 && !dpProps_)
                    return false;
                if (!dpProps_)
                    dpProps_ = NEWC(dpProps());
                dpProps_->dp_sratio_ = val;
                return true;
            }
        case kwDpMode: {
                if (!v.canConvert<int>())
                    throw Exception("The viscous mode dp_mode must be 0, 1, 2, or 3.");
                int val(v.toInt());
                if (val == 0 && !dpProps_)
                    return false;
                if (val < 0 || val > 3)
                    throw Exception("The viscous mode dp_mode must be 0, 1, 2, or 3.");
                if (!dpProps_)
                    dpProps_ = NEWC(dpProps());
                dpProps_->dp_mode_ = val;
                return false;
            }
        case kwDpF: {
                if (!v.canConvert<DVect>())
                    throw Exception("dp_force must be a vector.");
                DVect val(v.value<DVect>());
                if (val.fsum() == 0.0 && !dpProps_)
                    return false;
                if (!dpProps_)
                    dpProps_ = NEWC(dpProps());
                dpProps_->dp_F_ = val;
                return false;
            }
        case kwSBTStr: {
                if (!v.canConvert<double>())
                    throw Exception("sb_ten must be a double.");
                double val(v.toDouble());
                if (val < 0.0)
                    throw Exception("Negative sb_ten not allowed.");
                if (val == 0.0 && !bProps_)
                    return false;
                if (!bProps_)
                    bProps_ = NEWC(bProps());
                bProps_->sb_ten_ = val;
                return false;
            }
        case kwSBCoh: {
                if (!v.canConvert<double>())
                    throw Exception("sb_coh must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative pb_coh not allowed.");
                if (val == 0.0 && !bProps_)
                    return false;
                if (!bProps_)
                    bProps_ = NEWC(bProps());
                bProps_->sb_coh_ = val;
                return false;
            }
        case kwSBFa: {
                if (!v.canConvert<double>())
                    throw Exception("sb_fa must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative sb_fa not allowed.");
                if (val >= 90.0)
                    throw Exception("sb_fa must be lower than 90.0 degrees.");
                if (val == 0.0 && !bProps_)
                    return false;
                if (!bProps_)
                    bProps_ = NEWC(bProps());
                bProps_->sb_fa_ = val;
                return false;
            }
        case kwSBMCF: {
                if (!v.canConvert<double>())
                    throw Exception("sb_mcf must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative sb_mcf not allowed.");
                if (val > 1.0)
                    throw Exception("sb_mcf must be lower or equal to 1.0.");
                if (val == 1.0 && !bProps_)
                    return false;
                if (!bProps_)
                    bProps_ = NEWC(bProps());
                bProps_->sb_mcf_ = val;
                return false;
            }
        case kwSBSoft: {
                if (!v.canConvert<double>())
                    throw Exception("sb_soft must be a double.");
                double val(v.toDouble());
                if (val < 0.0)
                    throw Exception("Negative pb_soft not allowed.");
                if (!bProps_)
                    bProps_ = NEWC(bProps());
                bProps_->sb_soft_ = val;
                return false;
            }
        case kwSBCut: {
                if (!v.canConvert<double>())
                    throw Exception("sb_cut must be a double.");
                double val(v.toDouble());
                if (val < 0.0)
                    throw Exception("Negative sb_cut not allowed.");
                if (!bProps_)
                    bProps_ = NEWC(bProps());
                bProps_->sb_cut_ = val;
                return false;
            }
        case kwSBArea:
        case kwUserArea: {
                if (!v.canConvert<double>())
                    throw Exception("area must be a double.");
                double val(v.toDouble());
                if (val < 0.0)
                    throw Exception("Negative area not allowed.");
                userArea_ = val;
                return true;
            }
        case kwRGap: {
                if (!v.canConvert<double>())
                    throw Exception("Reference gap must be a double.");
                double val(v.toDouble());
                rgap_ = val;  
                return false;
            }
        }
        return false;
    }

    bool ContactModelSoftBond::getPropertyReadOnly(uint i) const {
        // Returns TRUE if a property is read only or FALSE otherwise. 
        switch (i) {
        case kwDpF:
        case kwSBS:
        case kwSBBS:
        case kwSBTS:
        case kwEmod:
        case kwKRatio:
        case kwSBState:
        case kwSBRadius:
        case kwSBSStr:
        case kwSBSig:
        case kwSBTau:
            return true;
        default:
            break;
        }
        return false;
    }

    bool ContactModelSoftBond::supportsInheritance(uint i) const {
        // Returns TRUE if a property supports inheritance or FALSE otherwise. 
        switch (i) {
        case kwKn:
        case kwKs:
        case kwFric:
            return true;
        default:
            break;
        }
        return false;
    }

    QString  ContactModelSoftBond::getMethodArguments(uint i) const {
        // Return a list of contact model method argument names. 
        switch (i) {
        case kwDeformability:
            return "emod,kratio";
        case kwBond:
            return "gap,soft,cut";
        case kwUnbond:
            return "gap";
        case kwArea:
            return QString();
        }
        assert(0);
        return QString();
    }

    bool ContactModelSoftBond::setMethod(uint i,const QVector<QVariant> &vl,IContact *con) {
        // Apply the specified method. 
        IContactMechanical *c(convert_getcast<IContactMechanical>(con));
        switch (i) {
        case kwDeformability: {
                double emod;
                double krat;
                if (vl.at(0).isNull()) 
                    throw Exception("Argument emod must be specified with method deformability in contact model %1.",getName());
                emod = vl.at(0).toDouble();
                if (emod<0.0)
                    throw Exception("Negative emod not allowed in contact model %1.",getName());
                if (vl.at(1).isNull()) 
                    throw Exception("Argument kratio must be specified with method deformability in contact model %1.",getName());
                krat = vl.at(1).toDouble();
                if (krat<0.0)
                    throw Exception("Negative stiffness ratio not allowed in contact model %1.",getName());
                double rsum(0.0);
                if (c->getEnd1Curvature().y())
                    rsum += 1.0 / c->getEnd1Curvature().y();
                if (c->getEnd2Curvature().y())
                    rsum += 1.0 / c->getEnd2Curvature().y();
                if (userArea_) 
#ifdef THREED
                    rsum = 2.0 * std::sqrt(userArea_ / dPi);
#else
                    rsum = userArea_;
#endif
                kn_ = emod / rsum;
                ks_ = (krat == 0.0) ? 0.0 : kn_ / krat;
                setInheritance(1,false);
                setInheritance(2,false);
                return true;
            }
        case kwBond: {
                if (bProps_ && bProps_->sb_state_ >= 3) return false;
                double mingap = -1.0 * limits<double>::max();
                double maxgap = 0;
                if (vl.at(0).canConvert<Double>())
                    maxgap = vl.at(0).toDouble();
                else if (vl.at(0).canConvert<DVect2>()) {
                    DVect2 value = vl.at(0).value<DVect2>();
                    mingap = value.minComp();
                    maxgap = value.maxComp();
                }
                else if (!vl.at(0).isNull())
                    throw Exception("gap value %1 not recognized in method bond in contact model %2.", vl.at(1), getName());
                double soft = bProps_ ? bProps_->sb_soft_ : 0.0;
                if (!vl.at(1).isNull()) {
                    soft = vl.at(1).toDouble();
                if (soft < 0.0)
                    throw Exception("Negative soft not allowed in contact model %1.", getName());            
                }
                double cut = bProps_ ? bProps_->sb_cut_ : 1.0;
                if (!vl.at(2).isNull()) {
                    if (vl.at(2).canConvert<Double>())
                        cut = vl.at(2).toDouble();
                    if (cut < 0.0)
                        throw Exception("cut value %1 is negative, or not recognized in method bond in contact model %2.", vl.at(2), getName());
                    if (cut > 1.0)
                        throw Exception("cut value %1 must be in range [0,1] in method bond in contact model %2.", vl.at(2), getName());
                }
                double gap = c->getGap();
                if (gap >= mingap && gap <= maxgap) {
                    if (!bProps_)
                        bProps_ = NEWC(bProps());
                    bProps_->sb_state_ = 3;
                    bProps_->sb_soft_ = soft;
                    // Update the critical distance
                    if (cut != -1)
                        bProps_->sb_cut_ = cut;
                    // seet to incremental normal force calculation
                    sb_mode_ = 1;
                    return true;
                }
                return false;
            }
        case kwUnbond: {
                if (!bProps_ || bProps_->sb_state_ == 0) return false;
                double mingap = -1.0 * limits<double>::max();
                double maxgap = 0;
                if (vl.at(0).canConvert<double>())
                    maxgap = vl.at(0).toDouble();
                else if (vl.at(0).canConvert<DVect2>()) {
                    DVect2 value = vl.at(0).value<DVect2>();
                    mingap = value.minComp();
                    maxgap = value.maxComp();
                }
                else if (!vl.at(0).isNull())
                    throw Exception("gap value %1 not recognized in method unbond in contact model %2.", vl.at(0), getName());
                double gap = c->getGap();
                if (gap >= mingap && gap <= maxgap) {
                    bProps_->sb_state_ = 0;
                    return true;
                }
                return false;
            }
        case kwArea: {
                if (!userArea_) {
                    double rsq(1./std::max(c->getEnd1Curvature().y(),c->getEnd2Curvature().y()));
#ifdef THREED
                    userArea_ = rsq * rsq * dPi;
#else
                    userArea_ = rsq * 2.0;
#endif                            
                }
                return true;
            }

        }
        return false;
    }

    double ContactModelSoftBond::getEnergy(uint i) const {
        // Return an energy value. 
        double ret(0.0);
        if (!energies_)
            return ret;
        switch (i) {
        case kwEStrain:    return energies_->estrain_;
        case kwESlip:      return energies_->eslip_;
        case kwEDashpot:   return energies_->edashpot_;
        }
        assert(0);
        return ret;
    }

    bool ContactModelSoftBond::getEnergyAccumulate(uint i) const {
        // Returns TRUE if the corresponding energy is accumulated or FALSE otherwise.
        switch (i) {
        case kwEStrain:   return false;
        case kwESlip:     return true;
        case kwEDashpot:  return true;
        }
        assert(0);
        return false;
    }

    void ContactModelSoftBond::setEnergy(uint i,const double &d) {
        // Set an energy value. 
        if (!energies_) return;
        switch (i) {
        case kwEStrain:    energies_->estrain_ = d;   return;  
        case kwESlip:      energies_->eslip_   = d;   return;
        case kwEDashpot:   energies_->edashpot_= d;   return;
        }
        assert(0);
        return;
    }

    bool ContactModelSoftBond::validate(ContactModelMechanicalState *state,const double &) {
        // Validate the / Prepare for entry into ForceDispLaw. The validate function is called when:
        // (1) the contact is created, (2) a property of the contact that returns a true via
        // the setProperty method has been modified and (3) when a set of cycles is executed
        // via the {cycle N} command.
        // Return value indicates contact activity (TRUE: active, FALSE: inactive).
        assert(state);
        const IContactMechanical *c = state->getMechanicalContact(); 
        assert(c);

        if (state->trackEnergy_)
            activateEnergy();

        if (inheritanceField_ & KnMask)
            updateKn(c);
        if (inheritanceField_ & KsMask)
            updateKs(c);
        if (inheritanceField_ & FricMask)
            updateFric(c);

        updateStiffness(state);
        return checkActivity(state->gap_);
    }

    static const QString knstr("kn");
    bool ContactModelSoftBond::updateKn(const IContactMechanical *con) {
        assert(con);
        QVariant v1 = con->getEnd1()->getProperty(knstr);
        QVariant v2 = con->getEnd2()->getProperty(knstr);
        if (!v1.isValid() || !v2.isValid())
            return false;
        double kn1 = v1.toDouble();
        double kn2 = v2.toDouble();
        double val = kn_;
        if (kn1 && kn2)
            kn_ = kn1*kn2/(kn1+kn2);
        else if (kn1)
            kn_ = kn1;
        else if (kn2)
            kn_ = kn2;
        return ( (kn_ != val) );
    }

    static const QString ksstr("ks");
    bool ContactModelSoftBond::updateKs(const IContactMechanical *con) {
        assert(con);
        QVariant v1 = con->getEnd1()->getProperty(ksstr);
        QVariant v2 = con->getEnd2()->getProperty(ksstr);
        if (!v1.isValid() || !v2.isValid())
            return false;
        double ks1 = v1.toDouble();
        double ks2 = v2.toDouble();
        double val = ks_;
        if (ks1 && ks2)
            ks_ = ks1*ks2/(ks1+ks2);
        else if (ks1)
            ks_ = ks1;
        else if (ks2)
            ks_ = ks2;
        return ( (ks_ != val) );
    }

    static const QString fricstr("fric");
    bool ContactModelSoftBond::updateFric(const IContactMechanical *con) {
        assert(con);
        QVariant v1 = con->getEnd1()->getProperty(fricstr);
        QVariant v2 = con->getEnd2()->getProperty(fricstr);
        if (!v1.isValid() || !v2.isValid())
            return false;
        double fric1 = std::max(0.0,v1.toDouble());
        double fric2 = std::max(0.0,v2.toDouble());
        double val = fric_;
        fric_ = std::min(fric1,fric2);
        return ( (fric_ != val) );
    }

    bool ContactModelSoftBond::endPropertyUpdated(const QString &name,const IContactMechanical *c) {
        // The endPropertyUpdated method is called whenever a surface property (with a name
        // that matches an inheritable contact model property name) of one of the contacting
        // pieces is modified. This allows the contact model to update its associated
        // properties. The return value denotes whether or not the update has affected
        // the time step computation (by having modified the translational or rotational
        // tangent stiffnesses). If true is returned, then the time step will be recomputed.  
        assert(c);
        QStringList availableProperties = getProperties().simplified().replace(" ","").split(",",QString::SkipEmptyParts);
        QRegExp rx(name,Qt::CaseInsensitive);
        int idx = availableProperties.indexOf(rx)+1;
        bool ret=false;

        if (idx<=0)
            return ret;
         
        switch(idx) {
        case kwKn:  { //kn
                if (inheritanceField_ & KnMask)
                    ret = updateKn(c);
                break;
            }
        case kwKs:  { //ks
                if (inheritanceField_ & KsMask)
                    ret =updateKs(c);
                break;
            }
        case kwFric:  { //fric
                if (inheritanceField_ & FricMask)
                    updateFric(c);
                break;
            }
        }
        return ret;
    }

    ContactModelSoftBond::StiffData ContactModelSoftBond::computeStiffData(ContactModelMechanicalState *state) const {
        // Update contact data
        double Cmin1 = state->end1Curvature_.x();
        double Cmax1 = state->end1Curvature_.y();
        double Cmax2 = state->end2Curvature_.y();
        double dthick = (Cmin1 == 0.0) ? 1.0 : 0.0;
        double br = sb_rmul_ * 1.0 / std::max(Cmax1, Cmax2);
        if (userArea_)
#ifdef THREED
            br = std::sqrt(userArea_ / dPi);
#else
            br = userArea_ / 2.0;
#endif
        double br2 = br * br;
        double area = dthick <= 0.0 ? dPi * br2 : 2.0*br*dthick;
        double bi = dthick <= 0.0 ? 0.25*area*br2 : 2.0*br*br2*dthick / 3.0;
        StiffData ret;
        ret.reff_ = br;
        ret.trans_ = DVect2(kn_ * area , ks_ * area);
        ret.ang_ = DAVect(kn_ * bi);
#if DIM==3
        ret.ang_.rx() = ks_ * 2.0*bi;
#endif
        return ret;
    }

    void ContactModelSoftBond::updateStiffness(ContactModelMechanicalState *state) {
        // first compute stiffness data
        StiffData stiff = computeStiffData(state);
        // Now calculate effective stiffness
        DVect2 retT = stiff.trans_;
        // correction if viscous damping active
        if (dpProps_) {
            DVect2 correct(1.0);
            if (dpProps_->dp_nratio_)
                correct.rx() = sqrt(1.0+dpProps_->dp_nratio_*dpProps_->dp_nratio_) - dpProps_->dp_nratio_;
            if (dpProps_->dp_sratio_)
                correct.ry() = sqrt(1.0+dpProps_->dp_sratio_*dpProps_->dp_sratio_) - dpProps_->dp_sratio_;
            retT /= (correct*correct);
        }
        effectiveTranslationalStiffness_ = retT;
        // Effective rotational stiffness (bending and twisting)
        effectiveRotationalStiffness_ = stiff.ang_;
    }
     
    bool ContactModelSoftBond::forceDisplacementLaw(ContactModelMechanicalState *state,const double &timestep) {
        assert(state);

        if (state->activated()) {
            // The contact was just activated from an inactive state
            // Trigger the FISH callback if one is hooked up to the 
            // contact_activated event.
            if (cmEvents_[fActivated] >= 0) {
                auto c = state->getContact();
                std::vector<fish::Parameter> arg = { fish::Parameter(c->getIThing()) };
                IFishCallList *fi = const_cast<IFishCallList*>(state->getProgram()->findInterface<IFishCallList>());
                fi->setCMFishCallArguments(c,arg,cmEvents_[fActivated]);
            }
        }
        updateStiffness(state);
        if (isBonded()) return FDLawBonded(state, timestep);
        else return FDLawUnBonded(state, timestep);
            
    }

    bool ContactModelSoftBond::FDLawBonded(ContactModelMechanicalState *state, const double &timestep) {
        // Relative translational/rotational displacement increments
        DVect  trans = state->relativeTranslationalIncrement_;
        DAVect ang = state->relativeAngularIncrement_;

        // Store previous force and moment
        DVect  sb_F_old = sb_F_;
        DAVect sb_M_old = sb_M_;

        // Update stiffness data
        StiffData stiff = computeStiffData(state);
        DVect3 geom = computeGeomData(state->getMechanicalContact());
        double area = geom.x();
        double bi = geom.y();
        double br = geom.z();
        double kn = stiff.trans_.x();
        double ks = stiff.trans_.y();
        double kb = stiff.ang_.z();
#if DIM==3
        double kt = stiff.ang_.x();
#else
        double kt = 0.0;
#endif

        double nsmax0 = -(sb_F_.x() / area) + bProps_->sb_mcf_* sqrt(sb_M_.y()*sb_M_.y() + sb_M_.z()*sb_M_.z()) * br / bi;

        // incremental normal force calculation
        sb_F_.rx() -= trans.x() * kn;

        // shear force calculation
        // dim holds the dimension (e.g., 2 for 2D and 3 for 3D)
        // Loop over the shear components (note: the 0 component is the normal component)
        // and calculate the shear force.
        for (int i = 1; i<dim; ++i)
            sb_F_.rdof(i) -= trans.dof(i) * ks;

        // moment calculation
        sb_M_ -= ang * stiff.ang_;
        double dbend = sqrt(sb_M_.y()*sb_M_.y() + sb_M_.z()*sb_M_.z());

        // maximum tensile stress at bond periphery
        double nsmax = -(sb_F_.x() / area) + bProps_->sb_mcf_* dbend * br / bi;

        bool softened = false;
        // Mode check
        if (state->canFail_) {
            if (bProps_->sb_state_ == 3 || bProps_->sb_state_ == 5) {
                double compVal = bProps_->sb_state_ == 3 ? bProps_->sb_ten_ : bProps_->sb_maxTen_;
                if (nsmax >= compVal ) {
                    // enter softening regime
                    // current bond elongation when softening starts
                    // This is the elongation at the bond periphery
                    double ls = - sb_F_.x() / kn + bProps_->sb_mcf_*dbend* br / kb;
                    bProps_->sb_maxTen_ = compVal;
                    if (bProps_->sb_state_ == 3) 
                        bProps_->sb_critu_ = ls /**(1.0+bProps_->sb_soft_)*/; 
                    bProps_->sb_delu_ = 0.0;
                    bProps_->sb_delo_ = Quat::identity();
                    if (bProps_->sb_state_ == 5 && nsmax < bProps_->sb_maxTen_)
                        softened = true;
                    bProps_->sb_state_ = 4;
                } 
            }
        }

        if (bProps_->sb_state_ == 4 && !softened && !checktol(bProps_->sb_soft_,0.0,1.0,100.0)) {
            double ls = bProps_->sb_critu_;
            double lc = ls * (1.0+bProps_->sb_soft_);
            DVect normal(0.0);
            normal.rx() = 1.0;
            DVect backNormal = (bProps_->sb_delo_.getConj().rotate(normal)).unit();
            double bend = acos(qBound(-1.0,normal|backNormal,1.0));
            double l0 = ls + bProps_->sb_maxu_ + bProps_->sb_delu_ + br*abs(bend);
            bProps_->sb_delu_ += trans.x();
            bProps_->sb_delo_.increment(ang);
            // Take the current contact normal and rotate it in the opposite direction of
            // the orientation - get the angle of bend from there
            backNormal = (bProps_->sb_delo_.getConj().rotate(normal)).unit();
            bend = acos(qBound(-1.0,normal|backNormal,1.0));
            double l = ls + bProps_->sb_maxu_ + bProps_->sb_delu_ + br*abs(bend);
            // target tensile stress
            double ns = bProps_->sb_ten_*(lc-l) / (bProps_->sb_soft_*ls);
            if (ns > 0) {
                if (nsmax >= ns) {
                    double fac = ns / nsmax;
                    sb_F_.rx() = fac*sb_F_.x();
#if DIM==3
                    sb_M_.ry() = fac*sb_M_.y();
#endif
                    sb_M_.rz() = fac*sb_M_.z();
                } else {
                    bProps_->sb_state_ = 5;
                    bProps_->sb_maxTen_ = nsmax0;
                    bProps_->sb_maxu_ = (l0-ls);
                }
            } else {
                sb_F_.rx() = 0.0;
#if DIM==3
                sb_M_.ry() = 0.0;
#endif
                sb_M_.rz() = 0.0;
            }
        } 

        if (state->canFail_) {
            /* check for normal failure */
            bool failed = false;
            if (bProps_->sb_state_ == 4) {
                double dbend = sqrt(sb_M_.y()*sb_M_.y() + sb_M_.z()*sb_M_.z());
                double nsmax = -(sb_F_.x() / area) + bProps_->sb_mcf_*dbend * br / bi;
                if (nsmax <= bProps_->sb_ten_*bProps_->sb_cut_ || checktol(bProps_->sb_soft_,0.0,1.0,100.0)) {
                    // Failed in tension
                    double se = strainEnergy(kn, ks, kb, kt); // bond strain energy at the onset of failure
                    bProps_->sb_state_ = 1;
                    sb_F_.fill(0.0);
                    sb_M_.fill(0.0);
                    failed = true;
                    if (cmEvents_[fBondBreak] >= 0) {
                        auto c = state->getContact();
                        std::vector<fish::Parameter> arg = { fish::Parameter(c->getIThing()),
                                                             fish::Parameter((qint64)bProps_->sb_state_),
                                                             fish::Parameter(nsmax),
                                                             fish::Parameter(se)
                                                           };
                        IFishCallList *fi = const_cast<IFishCallList*>(state->getProgram()->findInterface<IFishCallList>());
                        fi->setCMFishCallArguments(c,arg,cmEvents_[fBondBreak]);
                    }
                }
            }

            if (!failed) {
                /* check for shear failure */
                double dtwist = sb_M_.x();
                DVect bfs(sb_F_);
                bfs.rx() = 0.0;
                double dbfs = bfs.mag();
                double ssmax = dbfs / area + bProps_->sb_mcf_*std::abs(dtwist) * 0.5* br / bi;
                double ss = shearStrength(area);
                if (ss < 0)
                    ss = 0;
                if (ss <= ssmax) {
                    // Failed in shear
                    double se = strainEnergy(kn, ks, kb, kt); // bond strain energy at the onset of failure
                    bProps_->sb_state_ = 2;
                    if (cmEvents_[fBondBreak] >= 0) {
                        auto c = state->getContact();
                        std::vector<fish::Parameter> arg = { fish::Parameter(c->getIThing()),
                                                             fish::Parameter((qint64)bProps_->sb_state_),
                                                             fish::Parameter(ss),
                                                             fish::Parameter(se)
                                                           };
                        IFishCallList *fi = const_cast<IFishCallList*>(state->getProgram()->findInterface<IFishCallList>());
                        fi->setCMFishCallArguments(c,arg,cmEvents_[fBondBreak]);
                    }
                    // Resolve sliding. 
                    double crit = sb_F_.x() * fric_;
                    if (crit < 0)
                        crit = 0;
                    DVect sforce = sb_F_; sforce.rx() = 0.0;
                    // The is the magnitude of the shear force.
                    double sfmag = sforce.mag();
                    // Sliding occurs when the magnitude of the shear force is greater than the 
                    // critical value.
                    if (sfmag > crit) {
                        // Lower the shear force to the critical value for sliding.
                        double rat = crit / sfmag;
                        sforce *= rat;
                        sforce.rx() = sb_F_.x();
                        sb_F_ = sforce;
                        sb_S_ = true;
                    }

                    // Resolve bending
                    crit = sb_bmul_*2.1*0.25*stiff.reff_*std::abs(sb_F_.x()); // Jiang 2015
                    DAVect test = sb_M_;
#if DIM==3
                    test.rx() = 0.0;
#endif
                    double tmag = test.mag();
                    if (tmag > crit) {
                        // Lower the bending moment to the critical value for sliding.
                        double rat = crit / tmag;
                        test *= rat;
                        sb_BS_ = true;
                    }
                    sb_M_.rz() = test.z();
#if DIM==3
                    sb_M_.ry() = test.y();
                    // Resolve twisting
                    crit = sb_tmul_ * 0.65*fric_* stiff.reff_*std::abs(sb_F_.x()) ; // Jiang 2015
                    tmag = std::abs(sb_M_.x());
                    if (tmag > crit) {
                        // Lower the shear force to the critical value for sliding.
                        double rat = crit / tmag;
                        tmag = sb_M_.x() * rat;
                        sb_TS_ = true;
                    } else
                        tmag = sb_M_.x();
                    sb_M_.rx() = tmag;
#endif
                }
            }
        }

        // Account for dashpot forces if the dashpot structure has been defined. 
        if (dpProps_) {
            dpProps_->dp_F_.fill(0.0);
            double vcn(0.0), vcs(0.0);
            // Calculate the damping coefficients. 
            vcn = dpProps_->dp_nratio_ * 2.0 * sqrt((state->inertialMass_*(kn)));
            vcs = dpProps_->dp_sratio_ * 2.0 * sqrt((state->inertialMass_*(ks)));
            // First damp the shear components
            for (int i = 1; i<dim; ++i)
                dpProps_->dp_F_.rdof(i) = trans.dof(i) * (-1.0* vcs) / timestep;
            // Damp the normal component
            dpProps_->dp_F_.rx() -= trans.x() * vcn / timestep;
            // Need to change behavior based on the dp_mode.
            if (bProps_->sb_state_ < 3 && (dpProps_->dp_mode_ == 1 || dpProps_->dp_mode_ == 3)) {
                // Limit in tension if not bonded.
                if (dpProps_->dp_F_.x() + sb_F_.x() < 0)
                    dpProps_->dp_F_.rx() = -sb_F_.rx();
            }
            if (sb_S_ && dpProps_->dp_mode_ > 1) {
                // Limit in shear if sliding.
                double dfn = dpProps_->dp_F_.rx();
                dpProps_->dp_F_.fill(0.0);
                dpProps_->dp_F_.rx() = dfn;
            }
        }

        //Compute energies if energy tracking has been enabled. 
        if (state->trackEnergy_) {
            assert(energies_);
            energies_->estrain_ = 0.0;
            if (kn)
                // Calculate the strain energy. 
                energies_->estrain_ = 0.5*sb_F_.x()*sb_F_.x() / kn;
            if (ks) {
                DVect s = sb_F_;
                s.rx() = 0.0;
                double smag2 = s.mag2();
                // Add the shear component of the strain energy.
                energies_->estrain_ += 0.5*smag2 / ks;

                if (sb_S_) {
                    // If sliding calculate the slip energy and accumulate it.
                    sb_F_old.rx() = 0.0;
                    DVect avg_F_s = (s + sb_F_old)*0.5;
                    DVect u_s_el = (s - sb_F_old) / ks;
                    DVect u_s(0.0);
                    for (int i = 1; i < dim; ++i)
                        u_s.rdof(i) = trans.dof(i);
                    energies_->eslip_ -= std::min(0.0, (avg_F_s | (u_s + u_s_el)));
                }
            }
            // Add the bending/twisting resistance energy contributions.
            if (kb) {
                DAVect tmp = sb_M_;
#ifdef THREED                
                tmp.rx() = 0.0;
#endif
                energies_->estrain_ += 0.5*tmp.mag2() / kb;
                if (sb_BS_) {
                    //  accumulate bending slip energy.
                    DAVect tmp_old = sb_M_old;
#ifdef THREED                
                    tmp_old.rx() = 0.0;
#endif
                    DAVect avg_M = (tmp + tmp_old)*0.5;
                    DAVect t_s_el = (tmp - tmp_old) / kb;
                    energies_->eslip_ -= std::min(0.0, (avg_M | (ang + t_s_el)));
                }
            }
#ifdef THREED                
            if (kt) {
                double mt = std::abs(sb_M_.x());
                energies_->estrain_ += 0.5*mt*mt / kt;
                if (sb_TS_) {
                    //  accumulate twisting slip energy.
                    DAVect tmp(0.0);
                    DAVect tmp_old(0.0);
                    tmp.rx() = sb_M_.x();
                    tmp_old.rx() = sb_M_old.x();
                    DAVect avg_M = (tmp + tmp_old)*0.5;
                    DAVect t_s_el = (tmp - tmp_old) / kt;
                    energies_->eslip_ -= std::min(0.0, (avg_M | (ang + t_s_el)));
                }
            }
#endif                

            if (dpProps_) {
                // Calculate damping energy (accumulated) if the dashpots are active. 
                energies_->edashpot_ -= dpProps_->dp_F_ | trans;
            }
        }

        // This is just a sanity check to ensure, in debug mode, that the force/moment aren't wonky. 
        assert(sb_F_ == sb_F_);
        assert(sb_M_ == sb_M_);
        return true;
    }

    bool ContactModelSoftBond::FDLawUnBonded(ContactModelMechanicalState *state, const double &timestep) {
   
        // Relative translational/rotational displacement increments
        DVect  trans = state->relativeTranslationalIncrement_;
        DAVect ang   = state->relativeAngularIncrement_;
        double overlap = rgap_ - state->gap_;
        double correction = 1.0;
        if (state->activated() && sb_mode_ == 0 && trans.x()) {
                correction = -1.0*overlap / trans.x();
                if (correction < 0)
                    correction = 1.0;
        }

        // Store previous force and moment
        DVect  sb_F_old = sb_F_;
        DAVect sb_M_old = sb_M_;

        // Update stiffness data
        StiffData stiff = computeStiffData(state);
        double kn = stiff.trans_.x();
        double ks = stiff.trans_.y();
        double kb = stiff.ang_.z();
#if DIM==3
        double kt = stiff.ang_.x();
#endif
        // absolute/incremental normal force calculation
        if (sb_mode_==0)
            sb_F_.rx() = overlap * kn;
        else
            sb_F_.rx() -= trans.x() * kn;
        // Normal force can only be positive if unbonded
        sb_F_.rx() = std::max(0.0, sb_F_.x());

        // Calculate the trial shear force.
        DVect sforce(0.0);
        // dim holds the dimension (e.g., 2 for 2D and 3 for 3D)
        // Loop over the shear components (note: the 0 component is the normal component)
        // and calculate the shear force.
        for (int i = 1; i<dim; ++i)
            sforce.rdof(i) = sb_F_.dof(i) - trans.dof(i) * ks;

        // Calculate the trial moment.
        DAVect mom = sb_M_ - ang*stiff.ang_;
        
        // If the SOLVE ELASTIC command is given then the 
        // canFail state is set to FALSE. Otherwise it is always TRUE. 
        if (state->canFail_) {
            bool changed = false;
            // Resolve sliding. This is the normal force multiplied by the coefficient of friction.
            bool slip_changed = false;
            double crit = sb_F_.x() * fric_;
            // The is the magnitude of the shear force.
            double sfmag = sforce.mag();
            // Sliding occurs when the magnitude of the shear force is greater than the 
            // critical value.
            if (sfmag > crit) {
                // Lower the shear force to the critical value for sliding.
                double rat = crit / sfmag;
                sforce *= rat;
                if (!sb_S_) {
                    slip_changed = true;
                    changed = true;
                }
                sb_S_ = true;
            }
            else {
                if (sb_S_) {
                    slip_changed = true;
                    changed = true;
                }
                sb_S_ = false;
            }
            
            // Resolve bending
            bool bslip_changed = false;
            crit = sb_bmul_ * 2.1*0.25*sb_F_.x() * stiff.reff_; // Jiang 2015
            DAVect test = mom; 
#if DIM==3
            test.rx() = 0.0;
#endif
            double tmag = test.mag();
            if (tmag > crit) {
                // Lower the bending moment to the critical value for sliding.
                double rat = crit / tmag;
                test *= rat;
                if (!sb_BS_) {
                    bslip_changed = true;
                    changed = true;
                }
                sb_BS_ = true;
            }
            else {
                if (sb_BS_) {
                    bslip_changed = true;
                    changed = true;
                }
                sb_BS_ = false;
            }
            mom.rz() = test.z();
#if DIM==3
            mom.ry() = test.y();
            // Resolve twisting
            bool tslip_changed = false;
            crit = sb_tmul_ * 0.65*fric_*sb_F_.x() * stiff.reff_; // Jiang 2015
            tmag = std::abs(mom.x());
            if (tmag > crit) {
                // Lower the twisting moment to the critical value for sliding.
                double rat = crit / tmag;
                mom.rx() *= rat;
                if (!sb_TS_) {
                    tslip_changed = true;
                    changed = true;
                }
                sb_TS_ = true;
            } else {
                if (sb_TS_) {
                    tslip_changed = true;
                    changed = true;
                }
                sb_TS_ = false;
            }
#endif
            if (changed && cmEvents_[fSlipChange] >= 0) {
                qint64 code = 0;
                if (slip_changed) {
                    code = 1;
                    if (bslip_changed) {
                        code = 4;
#if DIM==3
                        if (tslip_changed)
                            code = 7;
#endif
                    }
                }
                else if (bslip_changed) {
                    code = 2;
#if DIM==3
                    if (tslip_changed)
                        code = 6;
#endif
                }
#if DIM==3
                else if (tslip_changed) {
                    code = 3;
                    if (slip_changed)
                        code = 5;
                }
#endif
                auto c = state->getContact();
                std::vector<fish::Parameter> arg = { fish::Parameter(c->getIThing()), 
                                                     fish::Parameter(code),
                                                     fish::Parameter(sb_S_),
                                                     fish::Parameter(sb_BS_)
#ifdef THREED
                                                     ,fish::Parameter(sb_TS_)
#endif
                                                   };
                IFishCallList *fi = const_cast<IFishCallList*>(state->getProgram()->findInterface<IFishCallList>());
                fi->setCMFishCallArguments(c,arg,cmEvents_[fSlipChange]);
            }
        }

        // Set the shear components of the total force.
        for (int i = 1; i<dim; ++i)
            sb_F_.rdof(i) = sforce.dof(i);

        // Set the moment.
        sb_M_ = mom;

        // Account for dashpot forces if the dashpot structure has been defined. 
        if (dpProps_) {
            dpProps_->dp_F_.fill(0.0);
            double vcn(0.0), vcs(0.0);
            // Calculate the damping coefficients. 
            vcn = dpProps_->dp_nratio_ * 2.0 * sqrt((state->inertialMass_*(kn)));
            vcs = dpProps_->dp_sratio_ * 2.0 * sqrt((state->inertialMass_*(ks)));
            // First damp the shear components
            for (int i = 1; i<dim; ++i)
                dpProps_->dp_F_.rdof(i) = trans.dof(i) * (-1.0* vcs) / timestep;
            // Damp the normal component
            dpProps_->dp_F_.rx() -= trans.x() * vcn / timestep;
            // Need to change behavior based on the dp_mode.
            if ((dpProps_->dp_mode_ == 1 || dpProps_->dp_mode_ == 3)) {
                // Limit in tension if not bonded.
                if (dpProps_->dp_F_.x() + sb_F_.x() < 0)
                    dpProps_->dp_F_.rx() = -sb_F_.rx();
            }
            if (sb_S_ && dpProps_->dp_mode_ > 1) {
                // Limit in shear if not sliding.
                double dfn = dpProps_->dp_F_.rx();
                dpProps_->dp_F_.fill(0.0);
                dpProps_->dp_F_.rx() = dfn;
            }
        }

        //Compute energies if energy tracking has been enabled. 
        if (state->trackEnergy_) {
            assert(energies_);
            energies_->estrain_ = 0.0;
            if (kn_)
                // Calculate the strain energy. 
                energies_->estrain_ = 0.5*sb_F_.x()*sb_F_.x() / kn;
            if (ks_) {
                DVect s = sb_F_;
                s.rx() = 0.0;
                double smag2 = s.mag2();
                // Add the shear component of the strain energy.
                energies_->estrain_ += 0.5*smag2 / ks;

                if (sb_S_) {
                    // If sliding calculate the slip energy and accumulate it.
                    sb_F_old.rx() = 0.0;
                    DVect avg_F_s = (s + sb_F_old)*0.5;
                    DVect u_s_el = (s - sb_F_old) / ks;
                    DVect u_s(0.0);
                    for (int i = 1; i < dim; ++i)
                        u_s.rdof(i) = trans.dof(i);
                    energies_->eslip_ -= std::min(0.0, (avg_F_s | (u_s + u_s_el)));
                }
            }
            // Add the bending/twisting resistance energy contributions.
            if (kb) {
                DAVect tmp = sb_M_;
#ifdef THREED                
                tmp.rx() = 0.0;
#endif
                energies_->estrain_ += 0.5*tmp.mag2() / kb;
                if (sb_BS_) {
                    //  accumulate bending slip energy.
                    DAVect tmp_old = sb_M_old;
#ifdef THREED                
                    tmp_old.rx() = 0.0;
#endif
                    DAVect avg_M = (tmp + tmp_old)*0.5;
                    DAVect t_s_el = (tmp - tmp_old) / kb;
                    energies_->eslip_ -= std::min(0.0, (avg_M | (ang + t_s_el)));
                }
            }
#ifdef THREED                
            if (kt) {
                double mt = std::abs(sb_M_.x());
                energies_->estrain_ += 0.5*mt*mt / kt;
                if (sb_TS_) {
                    //  accumulate twisting slip energy.
                    DAVect tmp(0.0);
                    DAVect tmp_old(0.0);
                    tmp.rx() = sb_M_.x();
                    tmp_old.rx() = sb_M_old.x();
                    DAVect avg_M = (tmp + tmp_old)*0.5;
                    DAVect t_s_el = (tmp - tmp_old) / kt;
                    energies_->eslip_ -= std::min(0.0, (avg_M | (ang + t_s_el)));
                }
            }
#endif                

            if (dpProps_) {
                // Calculate damping energy (accumulated) if the dashpots are active. 
                energies_->edashpot_ -= dpProps_->dp_F_ | trans;
            }
        }

        // This is just a sanity check to ensure, in debug mode, that the force/moment aren't wonky. 
        assert(sb_F_ == sb_F_);
        assert(sb_M_ == sb_M_);
        return true;
    }
    
    bool ContactModelSoftBond::thermalCoupling(ContactModelMechanicalState*, ContactModelThermalState* ts, IContactThermal*, const double&) {
        // Account for thermal expansion in incremental mode
        if (sb_mode_ == 0 || ts->gapInc_ == 0.0) return false;
        DVect finc(0.0);
        finc.rx() = kn_ * ts->gapInc_;
        sb_F_ -= finc;
        return true;
    }

    void ContactModelSoftBond::setForce(const DVect &v,IContact *c) { 
        sb_F(v); 
        if (v.x() > 0) 
            rgap_ = c->getGap() + v.x() / (kn_ * computeGeomData(convert_getcast<IContactMechanical>(c)).x()); 
    }

    void ContactModelSoftBond::propagateStateInformation(IContactModelMechanical* old,const CAxes &oldSystem,const CAxes &newSystem) {
        // Only called for contacts with wall facets when the wall resolution scheme
        // is set to full!
        // Only do something if the contact model is of the same type
        if (old->getContactModel()->getName().compare("softbond",Qt::CaseInsensitive) == 0 && !isBonded()) {
            ContactModelSoftBond *oldCm = (ContactModelSoftBond *)old;
#ifdef THREED
            // Need to rotate just the shear component from oldSystem to newSystem

            // Step 1 - rotate oldSystem so that the normal is the same as the normal of newSystem
            DVect axis = oldSystem.e1() & newSystem.e1();
            double c, ang, s;
            DVect re2;
            if (!checktol(axis.abs().maxComp(),0.0,1.0,1000)) {
                axis = axis.unit();
                c = oldSystem.e1()|newSystem.e1();
                if (c > 0)
                    c = std::min(c,1.0);
                else
                    c = std::max(c,-1.0);
                ang = acos(c);
                s = sin(ang);
                double t = 1. - c;
                DMatrix<3,3> rm;
                rm.get(0,0) = t*axis.x()*axis.x() + c;
                rm.get(0,1) = t*axis.x()*axis.y() - axis.z()*s;
                rm.get(0,2) = t*axis.x()*axis.z() + axis.y()*s;
                rm.get(1,0) = t*axis.x()*axis.y() + axis.z()*s;
                rm.get(1,1) = t*axis.y()*axis.y() + c;
                rm.get(1,2) = t*axis.y()*axis.z() - axis.x()*s;
                rm.get(2,0) = t*axis.x()*axis.z() - axis.y()*s;
                rm.get(2,1) = t*axis.y()*axis.z() + axis.x()*s;
                rm.get(2,2) = t*axis.z()*axis.z() + c;
                re2 = rm*oldSystem.e2();
            }
            else
                re2 = oldSystem.e2();
            // Step 2 - get the angle between the oldSystem rotated shear and newSystem shear
            axis = re2 & newSystem.e2();
            DVect2 tpf;
            DVect2 tpm;
            DMatrix<2,2> m;
            if (!checktol(axis.abs().maxComp(),0.0,1.0,1000)) {
                axis = axis.unit();
                c = re2|newSystem.e2();
                if (c > 0)
                    c = std::min(c,1.0);
                else
                    c = std::max(c,-1.0);
                ang = acos(c);
                if (!checktol(axis.x(),newSystem.e1().x(),1.0,100))
                    ang *= -1;
                s = sin(ang);
                m.get(0,0) = c;
                m.get(1,0) = s;
                m.get(0,1) = -m.get(1,0);
                m.get(1,1) = m.get(0,0);
                tpf = m*DVect2(oldCm->sb_F_.y(),oldCm->sb_F_.z());
                tpm = m*DVect2(oldCm->sb_M_.y(),oldCm->sb_M_.z());
            } else {
                m.get(0,0) = 1.;
                m.get(0,1) = 0.;
                m.get(1,0) = 0.;
                m.get(1,1) = 1.;
                tpf = DVect2(oldCm->sb_F_.y(),oldCm->sb_F_.z());
                tpm = DVect2(oldCm->sb_M_.y(),oldCm->sb_M_.z());
            }
            DVect pforce = DVect(0,tpf.x(),tpf.y());
            DVect pm     = DVect(0,tpm.x(),tpm.y());
#else
            oldSystem;
            newSystem;
            DVect pforce = DVect(0,oldCm->sb_F_.y());
            DVect pm     = DVect(0,oldCm->sb_M_.y());
#endif
            for (int i=1; i<dim; ++i)
                sb_F_.rdof(i) += pforce.dof(i);
            if (sb_mode_ && oldCm->sb_mode_)
                sb_F_.rx() = oldCm->sb_F_.x();
            oldCm->sb_F_ = DVect(0.0);
            oldCm->sb_M_ = DAVect(0.0);
            if (dpProps_ && oldCm->dpProps_) {
#ifdef THREED
                tpf = m*DVect2(oldCm->dpProps_->dp_F_.y(),oldCm->dpProps_->dp_F_.z());
                pforce = DVect(oldCm->dpProps_->dp_F_.x(),tpf.x(),tpf.y());
#else
                pforce = oldCm->dpProps_->dp_F_;
#endif
                dpProps_->dp_F_ += pforce;
                oldCm->dpProps_->dp_F_ = DVect(0.0);
            }
            if(oldCm->getEnergyActivated()) {
                activateEnergy();
                energies_->estrain_ = oldCm->energies_->estrain_;
                energies_->edashpot_ = oldCm->energies_->edashpot_;
                energies_->eslip_ = oldCm->energies_->eslip_;
                oldCm->energies_->estrain_ = 0.0;
                oldCm->energies_->edashpot_ = 0.0;
                oldCm->energies_->eslip_ = 0.0;
            }
            rgap_ = oldCm->rgap_;
        }
        assert(sb_F_ == sb_F_);
    }

    void ContactModelSoftBond::setNonForcePropsFrom(IContactModel *old) {
        // Only called for contacts with wall facets when the wall resolution scheme
        // is set to full!
        // Only do something if the contact model is of the same type
        if (old->getName().compare("softbond",Qt::CaseInsensitive) == 0 && !isBonded()) {
            ContactModelSoftBond *oldCm = (ContactModelSoftBond *)old;
            kn_       = oldCm->kn_;
            ks_       = oldCm->ks_;
            fric_     = oldCm->fric_;
            sb_bmul_  = oldCm->sb_bmul_;
            sb_tmul_  = oldCm->sb_tmul_;
            sb_mode_  = oldCm->sb_mode_;
            sb_rmul_  = oldCm->sb_rmul_;
            sb_S_     = oldCm->sb_S_;
            sb_BS_    = oldCm->sb_BS_;
            sb_TS_    = oldCm->sb_TS_;
            rgap_     = oldCm->rgap_;
            userArea_ = oldCm->userArea_;

            if (oldCm->dpProps_) {
                if (!dpProps_)
                    dpProps_ = NEWC(dpProps());
                dpProps_->dp_nratio_ = oldCm->dpProps_->dp_nratio_;
                dpProps_->dp_sratio_ = oldCm->dpProps_->dp_sratio_;
                dpProps_->dp_mode_ = oldCm->dpProps_->dp_mode_;
            }
            if (oldCm->bProps_) {
                if (!bProps_)
                    bProps_ = NEWC(bProps());
                bProps_->sb_mcf_    = oldCm->bProps_->sb_mcf_; 
                bProps_->sb_fa_     = oldCm->bProps_->sb_fa_; 
                bProps_->sb_state_  = oldCm->bProps_->sb_state_; 
                bProps_->sb_coh_    = oldCm->bProps_->sb_coh_; 
                bProps_->sb_ten_    = oldCm->bProps_->sb_ten_; 
                bProps_->sb_maxTen_ = oldCm->bProps_->sb_maxTen_; 
                bProps_->sb_cut_    = oldCm->bProps_->sb_cut_; 
                bProps_->sb_delu_   = oldCm->bProps_->sb_delu_; 
                bProps_->sb_delo_   = oldCm->bProps_->sb_delo_; 
                bProps_->sb_maxu_   = oldCm->bProps_->sb_maxu_; 
                bProps_->sb_critu_  = oldCm->bProps_->sb_critu_; 
            }

        }
    }

    DVect ContactModelSoftBond::getForce(const IContactMechanical *) const {
        DVect ret(sb_F_);
        if (dpProps_)
            ret += dpProps_->dp_F_;
        return ret;
    }

    DAVect ContactModelSoftBond::getMomentOn1(const IContactMechanical *c) const {
        DVect force = getForce(c);
        DAVect ret(sb_M_);
        c->updateResultingTorqueOn1Local(force,&ret);
        return ret;
    }

    DAVect ContactModelSoftBond::getMomentOn2(const IContactMechanical *c) const {
        DVect force = getForce(c);
        DAVect ret(sb_M_);
        c->updateResultingTorqueOn2Local(force,&ret);
        return ret;
    }

    DVect3 ContactModelSoftBond::computeGeomData(const IContactMechanical *c) const {
        double Cmax1 = c->getEnd1Curvature().y();
        double Cmax2 = c->getEnd2Curvature().y();
        double br = sb_rmul_ * 1.0 / std::max(Cmax1, Cmax2);
        if (userArea_)
#ifdef THREED
            br = std::sqrt(userArea_ / dPi);
#else
            br = userArea_ / 2.0;
#endif        
        double br2 = br * br;
#ifdef TWOD
        double area = 2.0*br;
        double bi = 2.0*br*br2 / 3.0;
#else
        double area = dPi * br2;
        double bi = 0.25*area*br2;
#endif
        return DVect3(area, bi, br);
    }

    DVect2 ContactModelSoftBond::SMax(const IContactMechanical *c) const {
        DVect3 data = computeGeomData(c);
        double area = data.x();
        double bi = data.y();
        double br = data.z();
        /* maximum stresses */
        double dbend = sqrt(sb_M_.y()*sb_M_.y() + sb_M_.z()*sb_M_.z());
        double dtwist = sb_M_.x();
        DVect bfs(sb_F_);
        bfs.rx() = 0.0;
        double dbfs = bfs.mag();
        double nsmax = -(sb_F_.x() / area) + dbend * br / bi;
        double ssmax = dbfs / area + std::abs(dtwist) * 0.5* br / bi;
        return DVect2(nsmax, ssmax);
    }

    double ContactModelSoftBond::shearStrength(const double &area) const {
        if (!bProps_) return 0.0;
        double sig = -1.0*sb_F_.x() / area;
        double nstr = bProps_->sb_state_ > 2 ? bProps_->sb_ten_ : 0.0;
        return sig <= nstr ? bProps_->sb_coh_ - std::tan(dDegrad*bProps_->sb_fa_)*sig
            : bProps_->sb_coh_ - std::tan(dDegrad*bProps_->sb_fa_)*nstr;
    }


    double ContactModelSoftBond::strainEnergy(double kn,double ks,double kb,double kt) const {
        double ret(0.0);
        if (kn)
            ret = 0.5 * sb_F_.x() * sb_F_.x() / kn;
        if (ks) {
            DVect tmp = sb_F_;
            tmp.rx() = 0.0;
            double smag2 = tmp.mag2();
            ret += 0.5 * smag2 / ks;
        }

        if (kt)
            ret += 0.5 * sb_M_.x() * sb_M_.x() / kt;
        if (kb) {
            DAVect tmp = sb_M_;
#ifdef THREED
            tmp.rx() = 0.0;
            double smag2 = tmp.mag2();
#else
            double smag2 = tmp.z() * tmp.z();
#endif
            ret += 0.5 * smag2 / kb;
        }
        return ret;
    }

} // namespace cmodelsxd
// EoF

Top