Linear Parallel Bond Model Implementation

See this page for the documentation of this contact model.

contactmodellinearpbond.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
#pragma once
// contactmodellinearpbond.h

#include "contactmodel/src/contactmodelmechanical.h"

#ifdef LINEARPBOND_LIB
#  define LINEARPBOND_EXPORT EXPORT_TAG
#elif defined(NO_MODEL_IMPORT)
#  define LINEARPBOND_EXPORT
#else
#  define LINEARPBOND_EXPORT IMPORT_TAG
#endif

namespace cmodelsxd {
    using namespace itasca;
 
    class ContactModelLinearPBond : public ContactModelMechanical {
    public:
        LINEARPBOND_EXPORT ContactModelLinearPBond();
        LINEARPBOND_EXPORT virtual ~ContactModelLinearPBond();
        virtual void                     copy(const ContactModel *c);
        virtual void                     archive(ArchiveStream &); 
        virtual QString  getName() const { return "linearpbond"; }
        virtual void     setIndex(int i) { index_=i;}
        virtual int      getIndex() const {return index_;}

        enum PropertyKeys { 
              kwLinKn=1
            , kwLinKs                            
            , kwLinFric   
            , kwLinF
            , kwLinS 
            , kwLinMode
            , kwRGap
            , kwEmod
            , kwKRatio
            , kwDpNRatio 
            , kwDpSRatio
            , kwDpMode 
            , kwDpF
            , kwPbState
            , kwPbRMul                        
            , kwPbKn  
            , kwPbKs
            , kwPbMcf
            , kwPbTStrength 
            , kwPbSStrength 
            , kwPbCoh
            , kwPbFa 
            , kwPbSig
            , kwPbTau 
            , kwPbF
            , kwPbM 
            , kwPbRadius 
            , kwPbEmod
            , kwPbKRatio
            , kwUserArea
        };
         
        virtual QString  getProperties() const { 
            return "kn"
                   ",ks"
                   ",fric"
                   ",lin_force"
                   ",lin_slip"
                   ",lin_mode"
                   ",rgap"
                   ",emod"
                   ",kratio"
                   ",dp_nratio"
                   ",dp_sratio"
                   ",dp_mode"
                   ",dp_force"
                   ",pb_state"
                   ",pb_rmul"
                   ",pb_kn"
                   ",pb_ks"
                   ",pb_mcf"
                   ",pb_ten"
                   ",pb_shear"
                   ",pb_coh"
                   ",pb_fa"
                   ",pb_sigma"
                   ",pb_tau"
                   ",pb_force"
                   ",pb_moment"
                   ",pb_radius"
                   ",pb_emod"
                   ",pb_kratio"
                   ",user_area";
        }

        enum EnergyKeys { kwEStrain=1,kwESlip,kwEDashpot,kwEPbStrain};
        virtual QString  getEnergies() const { return "energy-strain,energy-slip,energy-dashpot,energy-pbstrain";}
        virtual double   getEnergy(uint i) const;  // Base 1
        virtual bool     getEnergyAccumulate(uint i) const; // Base 1
        virtual void     setEnergy(uint i,const double &d); // Base 1
        virtual void     activateEnergy() { if (energies_) return; energies_ = NEWC(Energies());}
        virtual bool     getEnergyActivated() const {return (energies_ !=0);}

        enum FishCallEvents {fActivated=0,fBondBreak,fSlipChange};
        virtual QString  getFishCallEvents() const { return "contact_activated,bond_break,slip_change"; }
        virtual QVariant getProperty(uint i,const IContact *con=0) const;
        virtual bool     getPropertyGlobal(uint i) const;
        virtual bool     setProperty(uint i,const QVariant &v,IContact *con=0);
        virtual bool     getPropertyReadOnly(uint i) const;

        virtual bool     supportsInheritance(uint i) const; 
        virtual bool     getInheritance(uint i) const { assert(i<32); quint32 mask = to<quint32>(1 << i);  return (inheritanceField_ & mask) ? true : false; }
        virtual void     setInheritance(uint i,bool b) { assert(i<32); quint32 mask = to<quint32>(1 << i);  if (b) inheritanceField_ |= mask;  else inheritanceField_ &= ~mask; }

        enum MethodKeys { kwDeformability=1
                        , kwPbDeformability
                        , kwPbBond 
                        , kwPbUnbond 
                        , kwArea
        };

        virtual QString  getMethods() const {
            return "deformability"
                   ",pb_deformability"
                   ",bond"
                   ",unbond"
                   ",area";
        }

        virtual QString  getMethodArguments(uint i) const; 

        virtual bool     setMethod(uint i,const QVector<QVariant> &vl,IContact *con=0); // Base 1 - returns true if timestep contributions need to be updated

        virtual uint     getMinorVersion() const;

        virtual bool    validate(ContactModelMechanicalState *state,const double &timestep);
        virtual bool    endPropertyUpdated(const QString &name,const IContactMechanical *c);
        virtual bool    forceDisplacementLaw(ContactModelMechanicalState *state,const double &timestep);
        virtual DVect2  getEffectiveTranslationalStiffness() const { DVect2 ret = effectiveTranslationalStiffness_; if(pbProps_) ret+= pbProps_->pbTransStiff_ ;return ret;}
        virtual DAVect  getEffectiveRotationalStiffness() const {if (!pbProps_) return DAVect(0.0); return pbProps_->pbAngStiff_;}

        virtual bool thermalCoupling(ContactModelMechanicalState *, ContactModelThermalState * , IContactThermal *,const double &);

        virtual ContactModelLinearPBond *clone() const { return NEWC(ContactModelLinearPBond()); }
        virtual double              getActivityDistance() const {return rgap_;}
        virtual bool                isOKToDelete() const { return !isBonded(); }
        virtual void                resetForcesAndMoments() { lin_F(DVect(0.0)); dp_F(DVect(0.0)); pbF(DVect(0.0)); pbM(DAVect(0.0));  if (energies_) { energies_->estrain_ = 0.0;  if (energies_) energies_->epbstrain_ = 0.0;}}
        virtual void                setForce(const DVect &v,IContact *c);
        virtual void                setArea(const double &d) { userArea_ = d; }

        virtual bool     checkActivity(const double &gap) { return (gap <= rgap_ || isBonded()); }

        virtual bool     isSliding() const { return lin_S_; }
        virtual bool     isBonded() const { return pbProps_ ? (pbProps_->pb_state_==3) : false; }
        virtual void     propagateStateInformation(IContactModelMechanical* oldCm,const CAxes &oldSystem=CAxes(),const CAxes &newSystem=CAxes());
        virtual void     setNonForcePropsFrom(IContactModel *oldCM);

        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 DVect &  lin_F() const {return lin_F_;}
        void           lin_F(const DVect &f) { lin_F_=f;}
        bool           lin_S() const {return lin_S_;}
        void           lin_S(bool b) { lin_S_=b;}
        uint           lin_mode() const {return lin_mode_;}
        void           lin_mode(uint i) { lin_mode_=i;}
        const double & rgap() const {return rgap_;}
        void           rgap(const double &d) {rgap_=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;}
        double  epbstrain() const {return hasEnergies() ? energies_->epbstrain_: 0.0;}
        void    epbstrain(const double &d) { if(!hasEnergies()) return; energies_->epbstrain_=d;}

        bool     hasPBond() const {return pbProps_ ? true:false;}
        int      pbState()   const {return hasPBond() ? pbProps_->pb_state_: 0;}   
        void     pbState(int i) { if(!hasPBond()) return; pbProps_->pb_state_=i;}
        double   pbRmul() const {return (hasPBond() ? (pbProps_->pb_rmul_) : 0.0);}
        void     pbRmul(const double &d) { if(!hasPBond()) return; pbProps_->pb_rmul_=d;}
        double   pbKn() const {return (hasPBond() ? (pbProps_->pb_kn_) : 0.0);}
        void     pbKn(const double &d) { if(!hasPBond()) return; pbProps_->pb_kn_=d;}
        double   pbKs() const {return (hasPBond() ? (pbProps_->pb_ks_) : 0.0);}
        void     pbKs(const double &d) { if(!hasPBond()) return; pbProps_->pb_ks_=d;}
        double   pbMCF() const {return (hasPBond() ? (pbProps_->pb_mcf_) : 0.0);}
        void     pbMCF(const double &d) { if(!hasPBond()) return; pbProps_->pb_mcf_=d;}
        double   pbTen() const {return (hasPBond() ? (pbProps_->pb_ten_) : 0.0);}
        void     pbTen(const double &d) { if(!hasPBond()) return; pbProps_->pb_ten_=d;}
        double   pbCoh() const {return (hasPBond() ? (pbProps_->pb_coh_) : 0.0);}
        void     pbCoh(const double &d) { if(!hasPBond()) return; pbProps_->pb_coh_=d;}
        double   pbFA() const {return (hasPBond() ? (pbProps_->pb_fa_) : 0.0);}
        void     pbFA(const double &d) { if(!hasPBond()) return; pbProps_->pb_fa_=d;}
        DVect    pbF() const {return hasPBond() ? pbProps_->pb_F_: DVect(0.0);}
        void     pbF(const DVect &f) { if(!hasPBond()) return; pbProps_->pb_F_=f;}
        DAVect   pbM() const {return hasPBond() ? pbProps_->pb_M_: DAVect(0.0);}
        void     pbM(const DAVect &m) { if(!hasPBond()) return; pbProps_->pb_M_=m;}
        DVect2   pbTransStiff() const {return hasPBond() ? pbProps_->pbTransStiff_: DVect2(0.0);}
        void     pbTransStiff(const DVect2 &f) { if(!hasPBond()) return; pbProps_->pbTransStiff_=f;}
        DAVect   pbAngStiff() const {return hasPBond() ? pbProps_->pbAngStiff_: DAVect(0.0);}
        void     pbAngStiff(const DAVect &m) { if(!hasPBond()) return; pbProps_->pbAngStiff_=m;}

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

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

        /// 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;

    private:
        static int index_;

        struct Energies {
            Energies() : estrain_(0.0), eslip_(0.0), edashpot_(0.0), epbstrain_(0.0) {}
            double estrain_;  // elastic energy stored in contact 
            double eslip_;    // work dissipated by friction 
            double edashpot_;    // work dissipated by dashpots
            double epbstrain_; // parallel bond strain energy
        };

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

        struct pbProps {
            pbProps() : pb_state_(0), pb_rmul_(1.0), pb_kn_(0.0), pb_ks_(0.0), 
                        pb_mcf_(1.0), pb_ten_(0.0), pb_coh_(0.0), pb_fa_(0.0), pb_F_(DVect(0.0)), pb_M_(DAVect(0.0)),
                        pbTransStiff_(0.0), pbAngStiff_(0.0){}
            // parallel bond
            int     pb_state_;         // Bond mode - 0 (NBNF), 1 (NBFT), 2 (NBFS), 3 (B)
            double  pb_rmul_;         // Radius multiplier
            double  pb_kn_;           // normal stiffness
            double  pb_ks_;           // shear stiffness
            double  pb_mcf_;          // Moment contribution factor 
            double  pb_ten_;          // normal strength 
            double  pb_coh_;          // cohesion
            double  pb_fa_;           // friction angle
            DVect   pb_F_;            // Force in parallel bond
            DAVect  pb_M_;            // moment in parallel bond
            DVect2  pbTransStiff_;    // (Normal,Shear) Translational stiffness of the parallel bond
            DAVect  pbAngStiff_;      // (Normal,Shear) Rotational stiffness of the parallel bond
        };

        bool   updateKn(const IContactMechanical *con);
        bool   updateKs(const IContactMechanical *con);
        bool   updateFric(const IContactMechanical *con);
        double pbStrainEnergy() const;                     // Compute  bond strain energy 

        void   updateEffectiveStiffness(ContactModelMechanicalState *state);

        DVect3 pbData(const IContactMechanical *con) const; // Bond area and inertia
        DVect2 pbSMax(const IContactMechanical *con) const; // Maximum stress (tensile,shear) at bond periphery
        double pbShearStrength(const double &pbArea) const;      // Bond shear strength
        void   setDampCoefficients(const double &mass,double *vcn,double *vcs);

        // inheritance fields
        quint32 inheritanceField_;

        // linear model
        double      kn_;        // normal stiffness
        double      ks_;        // shear stiffness
        double      fric_;      // Coulomb friction coefficient
        DVect       lin_F_;     // Force carried in the linear model
        bool        lin_S_;     // the current sliding state
        uint        lin_mode_;  // specifies absolute (0) or incremental (1) behavior for the the linear part 
        double      rgap_;      // reference gap for the linear part

        dpProps *   dpProps_;     // The viscous properties
        pbProps *   pbProps_;     // The parallel bond properties

        double      userArea_;    // User specified area 

        Energies *   energies_;   // energies

        DVect2  effectiveTranslationalStiffness_;
         
    };
} // namespace itascaxd
// EoF

Top

contactmodellinearpbond.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
// contactmodellinear.cpp
#include "contactmodellinearpbond.h"

#include "module/interface/icontactmechanical.h"
#include "module/interface/icontact.h"

#include "module/interface/ipiecemechanical.h"
#include "module/interface/ipiece.h"
#include "../version.txt"
#include "base/src/basetoqt.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"

#ifdef LINEARPBOND_LIB
  int __stdcall DllMain(void *,unsigned, void *)
  {
    return 1;
  }

  extern "C" EXPORT_TAG const char *getName() 
  {
#if DIM==3
    return "contactmodelmechanical3dlinearpbond";
#else
    return "contactmodelmechanical2dlinearpbond";
#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::ContactModelLinearPBond *m = NEWC(cmodelsxd::ContactModelLinearPBond());
    return (void *)m;
  }
#endif // LINEARPBOND_EXPORTS

namespace cmodelsxd {
    static const quint32 linKnMask      = 0x00002; // Base 1!
    static const quint32 linKsMask      = 0x00004;
    static const quint32 linFricMask    = 0x00008;

    using namespace itasca;

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

    ContactModelLinearPBond::ContactModelLinearPBond() : inheritanceField_(linKnMask|linKsMask|linFricMask) 
                                                       , kn_(0.0)
                                                       , ks_(0.0)
                                                       , fric_(0.0)
                                                       , lin_F_(DVect(0.0))
                                                       , lin_S_(false)
                                                       , lin_mode_(0)
                                                       , rgap_(0.0)
                                                       , dpProps_(0)
                                                       , pbProps_(0)
                                                       , userArea_(0)
                                                       , energies_(0)
                                                       , effectiveTranslationalStiffness_(DVect2(0.0)) {
//    setFromParent(ContactModelMechanicalList::instance()->find(getName()));
    }

    ContactModelLinearPBond::~ContactModelLinearPBond() {
        if (dpProps_)
            delete dpProps_;
        if (pbProps_)
            delete pbProps_;
        if (energies_)
            delete energies_;
    }

    void ContactModelLinearPBond::archive(ArchiveStream &stream) {
        stream & kn_;
        stream & ks_;
        stream & fric_;
        stream & lin_F_;
        stream & lin_S_;
        stream & lin_mode_;
        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_;
                stream & energies_->epbstrain_;
            }
            else
                stream & b;

            b = false;
            if (pbProps_) {
                b = true;
                stream & b;
                stream & pbProps_->pb_state_;
                stream & pbProps_->pb_rmul_;
                stream & pbProps_->pb_kn_;
                stream & pbProps_->pb_ks_;
                stream & pbProps_->pb_mcf_;
                stream & pbProps_->pb_ten_;
                stream & pbProps_->pb_coh_;
                stream & pbProps_->pb_fa_;
                stream & pbProps_->pb_F_;
                stream & pbProps_->pb_M_;
            }
            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 & energies_->epbstrain_;
            }
            stream & b;
            if (b) {
                if (!pbProps_)
                    pbProps_ = NEWC(pbProps());
                stream & pbProps_->pb_state_;
                stream & pbProps_->pb_rmul_;
                stream & pbProps_->pb_kn_;
                stream & pbProps_->pb_ks_;
                stream & pbProps_->pb_mcf_;
                stream & pbProps_->pb_ten_;
                stream & pbProps_->pb_coh_;
                stream & pbProps_->pb_fa_;
                stream & pbProps_->pb_F_;
                stream & pbProps_->pb_M_;
            }
        }

        stream & inheritanceField_;
        stream & effectiveTranslationalStiffness_;

        if (stream.getArchiveState()==ArchiveStream::Save || stream.getRestoreVersion() == getMinorVersion())
            stream & rgap_;

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

    void ContactModelLinearPBond::copy(const ContactModel *cm) {
        ContactModelMechanical::copy(cm);
        const ContactModelLinearPBond *in = dynamic_cast<const ContactModelLinearPBond*>(cm);
        if (!in) throw std::runtime_error("Internal error: contact model dynamic cast failed.");
        kn(in->kn());
        ks(in->ks());
        fric(in->fric());
        lin_F(in->lin_F());
        lin_S(in->lin_S());
        lin_mode(in->lin_mode());
        rgap(in->rgap());
        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());
            epbstrain(in->epbstrain());
        }
        if (in->hasPBond()) {
            if (!pbProps_)
                pbProps_ = NEWC(pbProps());
            pbState(in->pbState());
            pbRmul(in->pbRmul());
            pbKn(in->pbKn());
            pbKs(in->pbKs());
            pbMCF(in->pbMCF());
            pbTen(in->pbTen());
            pbCoh(in->pbCoh());
            pbFA(in->pbFA());
            pbF(in->pbF());
            pbM(in->pbM());
            pbTransStiff(in->pbTransStiff());
            pbAngStiff(in->pbAngStiff());
        }
        userArea_ = in->userArea_;
        inheritanceField(in->inheritanceField());
        effectiveTranslationalStiffness(in->effectiveTranslationalStiffness());
    }

    QVariant ContactModelLinearPBond::getProperty(uint i,const IContact *con) const {
        QVariant var;
        switch (i) {
        case kwLinKn:        return kn_;
        case kwLinKs:        return ks_;
        case kwLinFric:      return fric_;
        case kwLinMode:      return lin_mode_;
        case kwLinF:         var.setValue(lin_F_); return var;
        case kwLinS:   return lin_S_;
        case kwRGap:       return rgap_;
        case kwEmod: {
                        const IContactMechanical *c(convert_getcast<IContactMechanical>(con));
                        if (c ==nullptr) return 0.0;
                        double rsq(std::max(c->getEnd1Curvature().y(),c->getEnd2Curvature().y()));
                        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
                            rsq = std::sqrt(userArea_ / dPi);
#else
                            rsq = userArea_ / 2.0;
#endif        
                            rsum = rsq + rsq;
                            rsq = 1. / rsq;
                        }
#ifdef TWOD             
                        return (kn_ * rsum * rsq / 2.0);
#else                     
                        return (kn_ * rsum * rsq * rsq) / dPi;
#endif                    
                    }
        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 kwUserArea:    return userArea_;
        case kwDpF: {
                dpProps_ ? var.setValue(dpProps_->dp_F_) : var.setValue(DVect(0.0));
                return var;
            }
        case kwPbState:     return pbProps_ ? pbProps_->pb_state_ : 0;
        case kwPbRMul:      return pbProps_ ? pbProps_->pb_rmul_ : 1.0;
        case kwPbKn:        return pbProps_ ? pbProps_->pb_kn_ : 0;
        case kwPbKs:        return pbProps_ ? pbProps_->pb_ks_ : 0;
        case kwPbMcf:       return pbProps_ ? pbProps_->pb_mcf_ : 1.0;
        case kwPbTStrength: return pbProps_ ? pbProps_->pb_ten_ : 0.0;
        case kwPbSStrength: {
                if (!pbProps_) return 0.0;
                const IContactMechanical *c(convert_getcast<IContactMechanical>(con));
                double pbArea = pbData(c).x();
                return pbShearStrength(pbArea);
            }
        case kwPbCoh:       return pbProps_ ? pbProps_->pb_coh_ : 0;
        case kwPbFa:        return pbProps_ ? pbProps_->pb_fa_ : 0;
        case kwPbSig: {
                if (!pbProps_ || pbProps_->pb_state_ < 3) return 0.0;
                const IContactMechanical *c(convert_getcast<IContactMechanical>(con));
                return pbSMax(c).x();
            }
        case kwPbTau: {
                if (!pbProps_ || pbProps_->pb_state_ < 3) return 0.0;
                const IContactMechanical *c(convert_getcast<IContactMechanical>(con));
                return pbSMax(c).y();
            }
        case kwPbF: {
                pbProps_ ? var.setValue(pbProps_->pb_F_) : var.setValue(DVect(0.0));
                return var;
            }
        case kwPbM: {
                pbProps_ ? var.setValue(pbProps_->pb_M_) : var.setValue(DAVect(0.0));
                return var;
            }
        case kwPbRadius: {
                if (!pbProps_) return 0.0;
                const IContactMechanical *c(convert_getcast<IContactMechanical>(con));
                double Cmax1 = c->getEnd1Curvature().y();
                double Cmax2 = c->getEnd2Curvature().y();
                double br = pbProps_->pb_rmul_ * 1.0 / std::max(Cmax1,Cmax2);
                if (userArea_)
#ifdef THREED
                    br = std::sqrt(userArea_ / dPi);
#else
                    br = userArea_ / 2.0;
#endif
                return br;
            }
        case kwPbEmod: {
                if (!pbProps_) return 0.0;
                const IContactMechanical *c(convert_getcast<IContactMechanical>(con));
                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
                    double rad = std::sqrt(userArea_ / dPi);
#else
                    double rad = userArea_ / 2.0;
#endif        
                    rsum = 2 * rad;
                }
                double emod = pbProps_->pb_kn_ * rsum;
                return emod;
            }
        case kwPbKRatio: {
                if (!pbProps_) return 0.0;
                return (pbProps_->pb_ks_ == 0.0) ? 0.0 : (pbProps_->pb_kn_/pbProps_->pb_ks_);
            }
        }
        assert(0);
        return QVariant();
    }

    bool ContactModelLinearPBond::getPropertyGlobal(uint i) const {
        switch (i) {
        case kwLinF:   
        case kwDpF:  
        case kwPbF:
            return false;
        }
        return true;
    }

    bool ContactModelLinearPBond::setProperty(uint i,const QVariant &v,IContact *) {
        pbProps pb;
        dpProps dp;

        switch (i) {
        case kwLinKn: {
                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 kwLinKs: {
                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 kwLinFric: {
                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 kwLinF: {
                if (!v.canConvert<DVect>())
                    throw Exception("lin_force must be a vector.");
                DVect val(v.value<DVect>());
                lin_F_ = val;
                return false;
            }
        case kwLinMode: {
                if (!v.canConvert<uint>())
                    throw Exception("lin_mode must be 0 (absolute) or 1 (incremental).");
                uint val(v.toUInt());
                if (val>1)
                    throw Exception("lin_mode must be 0 (absolute) or 1 (incremental).");
                lin_mode_ = val;
                return false;
            }
        case kwRGap: {
                if (!v.canConvert<double>())
                    throw Exception("Reference gap must be a double.");
                double val(v.toDouble());
                rgap_ = val;  
                return false;
            }
        case kwPbRMul: {
                if (!v.canConvert<double>())
                    throw Exception("pb_rmul must be a double.");
                double val(v.toDouble());
                if (val<=0.0)
                    throw Exception("pb_rmul must be positive.");
                if (val == 1.0 && !pbProps_)
                    return false;
                if (!pbProps_)
                    pbProps_ = NEWC(pbProps());
                pbProps_->pb_rmul_ = val;
                return false;
            }
        case kwPbKn: {
                if (!v.canConvert<double>())
                    throw Exception("pb_kn must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative pb_kn not allowed.");
                if (val == 0.0 && !pbProps_)
                    return false;
                if (!pbProps_)
                    pbProps_ = NEWC(pbProps());
                pbProps_->pb_kn_ = val;
                return true;
            }
        case kwPbKs: {
                if (!v.canConvert<double>())
                    throw Exception("pb_ks must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative pb_ks not allowed.");
                if (val == 0.0 && !pbProps_)
                    return false;
                if (!pbProps_)
                    pbProps_ = NEWC(pbProps());
                pbProps_->pb_ks_ = val;
                return true;
            }
        case kwPbMcf: {
                if (!v.canConvert<double>())
                    throw Exception("pb_mcf must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative pb_mcf not allowed.");
                if (val > 1.0)
                    throw Exception("pb_mcf must be lower or equal to 1.0.");
                if (val == 1.0 && !pbProps_)
                    return false;
                if (!pbProps_)
                    pbProps_ = NEWC(pbProps());
                pbProps_->pb_mcf_ = val;  
                return false;
            }
        case kwPbTStrength: {
                if (!v.canConvert<double>())
                    throw Exception("pb_ten must be a double.");
                double val(v.toDouble());
                if (val < 0.0)
                    throw Exception("Negative pb_ten not allowed.");
                if (val == 0.0 && !pbProps_)
                    return false;
                if (!pbProps_)
                    pbProps_ = NEWC(pbProps());
                pbProps_->pb_ten_ = val;  
                return false;
            }
        case kwPbCoh: {
                if (!v.canConvert<double>())
                    throw Exception("pb_coh must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative pb_coh not allowed.");
                if (val == 0.0 && !pbProps_)
                    return false;
                if (!pbProps_)
                    pbProps_ = NEWC(pbProps());
                pbProps_->pb_coh_ = val;  
                return false;
            }
        case kwPbFa: {
                if (!v.canConvert<double>())
                    throw Exception("pb_fa must be a double.");
                double val(v.toDouble());
                if (val<0.0)
                    throw Exception("Negative pb_fa not allowed.");
                if (val>=90.0)
                    throw Exception("pb_fa must be lower than 90.0 degrees.");
                if (val == 0.0 && !pbProps_)
                    return false;
                if (!pbProps_)
                    pbProps_ = NEWC(pbProps());
                pbProps_->pb_fa_ = val;  
                return false;
            }
        case kwPbF: {
                if (!v.canConvert<DVect>())
                    throw Exception("pb_force must be a vector.");
                DVect val(v.value<DVect>());
                if (val.fsum() == 0.0 && !pbProps_)
                    return false;
                if (!pbProps_)
                    pbProps_ = NEWC(pbProps());
                pbProps_->pb_F_ = val;
                return false;
            }
        case kwPbM: {
                DAVect val(0.0);
#ifdef TWOD               
                if (!v.canConvert<DAVect>() && !v.canConvert<double>())
                    throw Exception("pb_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("pb_moment must be an angular vector.");
                if (v.canConvert<DAVect>())
                    val = DAVect(v.value<DAVect>());
                else
                    val = DAVect(v.value<DVect>());
#endif
                if (val.fsum() == 0.0 && !pbProps_)
                    return false;
                if (!pbProps_)
                    pbProps_ = NEWC(pbProps());
                pbProps_->pb_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 kwUserArea: {
                if (!v.canConvert<double>())
                    throw Exception("user_area must be a double.");
                double val(v.toDouble());
                if (val < 0.0)
                    throw Exception("Negative user_area not allowed.");
                userArea_ = val;
                return true;
            }
        }
//    assert(0);
        return false;
    }

    bool ContactModelLinearPBond::getPropertyReadOnly(uint i) const {
        switch (i) {
        case kwDpF:
        case kwLinS:
        case kwEmod:
        case kwKRatio:
        case kwPbState:
        case kwPbRadius:
        case kwPbSStrength:
        case kwPbSig:
        case kwPbTau:
        case kwPbEmod:
        case kwPbKRatio:
            return true;
        default:
            break;
        }
        return false;
    }

    bool ContactModelLinearPBond::supportsInheritance(uint i) const {
        switch (i) {
        case kwLinKn:
        case kwLinKs:
        case kwLinFric:
            return true;
        default:
            break;
        }
        return false;
    }

    QString  ContactModelLinearPBond::getMethodArguments(uint i) const {
        QString def = QString();
        switch (i) {
        case kwDeformability:
            return "emod,kratio";
        case kwPbDeformability:
            return "emod,kratio";
        case kwPbBond: 
            return "gap";
        case kwPbUnbond: 
            return "gap";
        }
        return def;
    }

    bool ContactModelLinearPBond::setMethod(uint i,const QVector<QVariant> &vl,IContact *con) {
        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 linear stiffness ratio not allowed in contact model %1.",getName());
                double rsq(std::max(c->getEnd1Curvature().y(),c->getEnd2Curvature().y()));
                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
                    rsq = std::sqrt(userArea_ / dPi);
#else
                    rsq = userArea_ / 2.0;
#endif        
                    rsum = rsq + rsq;
                    rsq = 1. / rsq;
                }
#ifdef TWOD
                kn_ = 2.0 * emod / (rsq * rsum);
#else
                kn_ = dPi * emod / (rsq * rsq * rsum);
#endif
                ks_ = (krat == 0.0) ? 0.0 : kn_ / krat;
                setInheritance(1,false);
                setInheritance(2,false);
                return true;
            }
        case kwPbDeformability: {
                //if (!pbProps_ || pbProps_->pb_state_ != 3) return false;
                double emod;
                double krat;
                if (vl.at(0).isNull()) 
                    throw Exception("Argument emod must be specified with method pb_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 pb_deformability in contact model %1.",getName());
                krat = vl.at(1).toDouble();
                if (krat<0.0)
                    throw Exception("Negative parallel bond 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 (!pbProps_)
                    pbProps_ = NEWC(pbProps());
                if (userArea_) 
#ifdef THREED
                    rsum = 2 * std::sqrt(userArea_ / dPi);
#else
                    rsum = 2 * userArea_ / 2.0;
#endif
                pbProps_->pb_kn_ = emod / rsum;
                pbProps_->pb_ks_ = (krat == 0.0) ? 0.0 : pbProps_->pb_kn_ / krat;
                return true;
            }
        case kwPbBond: {
                if (pbProps_ && pbProps_->pb_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(0),getName());
                double gap = c->getGap(); 
                if ( gap >= mingap && gap <= maxgap) {
                    if (pbProps_)
                        pbProps_->pb_state_ = 3;
                    else {
                        pbProps_ = NEWC(pbProps());
                        pbProps_->pb_state_ = 3;
                    }
                    return true;
                }
                return false;
            }
        case kwPbUnbond: {
                if (!pbProps_ || pbProps_->pb_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) {
                    pbProps_->pb_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 ContactModelLinearPBond::getEnergy(uint i) const {
        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_;
        case kwEPbStrain:return energies_->epbstrain_;
        }
        assert(0);
        return ret;
    }

    bool ContactModelLinearPBond::getEnergyAccumulate(uint i) const {
        switch (i) {
        case kwEStrain:   return false;
        case kwESlip:     return true;
        case kwEDashpot:  return true;
        case kwEPbStrain: return false;
        }
        assert(0);
        return false;
    }

    void ContactModelLinearPBond::setEnergy(uint i,const double &d) {
        if (!energies_) return;
        switch (i) {
        case kwEStrain:   energies_->estrain_  = d; return;  
        case kwESlip:     energies_->eslip_    = d; return;
        case kwEDashpot:  energies_->edashpot_ = d; return;
        case kwEPbStrain: energies_->epbstrain_= d; return;
        }
        assert(0);
        return;
    }

    bool ContactModelLinearPBond::validate(ContactModelMechanicalState *state,const double &) {
        assert(state);
        const IContactMechanical *c = state->getMechanicalContact(); 
        assert(c);

        if (state->trackEnergy_)
            activateEnergy();

        if (inheritanceField_ & linKnMask)
            updateKn(c);
        if (inheritanceField_ & linKsMask)
            updateKs(c);
        if (inheritanceField_ & linFricMask)
            updateFric(c);

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

    static const QString knstr("kn");
    bool ContactModelLinearPBond::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 ContactModelLinearPBond::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 ContactModelLinearPBond::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 ContactModelLinearPBond::endPropertyUpdated(const QString &name,const IContactMechanical *c) {
        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 kwLinKn:  { //kn
                if (inheritanceField_ & linKnMask)
                    ret = updateKn(c);
                break;
            }
        case kwLinKs:  { //ks
                if (inheritanceField_ & linKsMask)
                    ret =updateKs(c);
                break;
            }
        case kwLinFric:  { //fric
                if (inheritanceField_ & linFricMask)
                    updateFric(c);
                break;
            }
        }
        return ret;
    }

    void ContactModelLinearPBond::updateEffectiveStiffness(ContactModelMechanicalState *state) {
        DVect2 ret(kn_,ks_);
        // account for viscous damping
        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_;
            ret /= (correct*correct);
        }
        effectiveTranslationalStiffness_ = ret;
        if (isBonded()) {
            double Cmin1 = state->end1Curvature_.x();
            double Cmax1 = state->end1Curvature_.y();
            double Cmax2 = state->end2Curvature_.y();
            double dthick = (Cmin1 == 0.0) ? state->end1Extent_.x() : 0.0;    
            double br = pbProps_->pb_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 pbArea = dthick <= 0.0 ? dPi*br2 : 2.0*br*dthick;
            double bi = dthick <= 0.0 ? 0.25*pbArea*br2 : 2.0*br*br2*dthick/3.0;
            pbProps_->pbTransStiff_.rx() = pbProps_->pb_kn_*pbArea;
            pbProps_->pbTransStiff_.ry() = pbProps_->pb_ks_*pbArea;
#if DIM==3 
            pbProps_->pbAngStiff_.rx() = pbProps_->pb_ks_* 2.0 * bi;
            pbProps_->pbAngStiff_.ry() = pbProps_->pb_kn_* bi;
#endif
            pbProps_->pbAngStiff_.rz() = pbProps_->pb_kn_* bi;
        }
    }
     
    double ContactModelLinearPBond::pbStrainEnergy() const {
        double ret(0.0);
        if (pbProps_->pb_kn_)
            ret = 0.5 * pbProps_->pb_F_.x() * pbProps_->pb_F_.x() / pbProps_->pbTransStiff_.x();
        if (pbProps_->pb_ks_) {
            DVect tmp = pbProps_->pb_F_;
            tmp.rx() = 0.0;
            double smag2 = tmp.mag2();
            ret += 0.5 * smag2 / pbProps_->pbTransStiff_.y();
        }

#ifdef THREED
        if (pbProps_->pbAngStiff_.x())
            ret += 0.5 * pbProps_->pb_M_.x() * pbProps_->pb_M_.x() / pbProps_->pbAngStiff_.x();
#endif
        if (pbProps_->pbAngStiff_.z()) {
            DAVect tmp = pbProps_->pb_M_;
#ifdef THREED
            tmp.rx() = 0.0;
            double smag2 = tmp.mag2();
#else
            double smag2 = tmp.z() * tmp.z();
#endif
            ret += 0.5 * smag2 / pbProps_->pbAngStiff_.z();
        }
        return ret;
    }

    bool ContactModelLinearPBond::forceDisplacementLaw(ContactModelMechanicalState *state,const double &timestep) {
        assert(state);

        double overlap = rgap_ - state->gap_;
        DVect trans = state->relativeTranslationalIncrement_;
        double correction = 1.0;

        if (state->activated()) {
            if (cmEvents_[fActivated] >= 0) {
                FArray<QVariant,2> arg;
                QVariant v;
                IContact * c = const_cast<IContact*>(state->getContact());
                TPtr<IThing> t(c->getIThing());
                v.setValue(t);
                arg.push_back(v);
                IFishCallList *fi = const_cast<IFishCallList*>(state->getProgram()->findInterface<IFishCallList>());
                fi->setCMFishCallArguments(c,arg,cmEvents_[fActivated]);
            }
            if (lin_mode_ == 0 && trans.x()) {
                correction = -1.0*overlap / trans.x();
                if (correction < 0)
                    correction = 1.0;
            }
        }

#ifdef THREED
        DVect norm(trans.x(),0.0,0.0);
#else
        DVect norm(trans.x(),0.0);
#endif
        DAVect ang  = state->relativeAngularIncrement_;
        DVect ss_F_old = lin_F_;

        if (lin_mode_==0)
            lin_F_.rx() = overlap * kn_;
        else
            lin_F_.rx() -= correction * norm.x() * kn_;
        lin_F_.rx() = std::max(0.0,lin_F_.x());

        DVect u_s = trans;
        u_s.rx() = 0.0;
        DVect sforce = lin_F_ - u_s * ks_ * correction;
        sforce.rx() = 0.0;
        // Make sure that the shear force opposses the direction of translation - otherwise you can
        // have strange behavior
        //for (int j=1; j<dim; ++j)
        //{
        //  qDebug()<<sforce.dof(j)<<trans.dof(j);
        //  if (sign<double>(1,sforce.dof(j)) == sign<double>(1,trans.dof(j)))
        //    sforce.rdof(j) = 0.0;
        //}

        // Resolve sliding
        if (state->canFail_) {
            double crit = lin_F_.x() * fric_;
            double sfmag = sforce.mag();
            if (sfmag > crit) {
                double rat = crit / sfmag;
                sforce *= rat;
                if (!lin_S_ && cmEvents_[fSlipChange] >= 0) {
                    FArray<QVariant,3> arg;
                    QVariant p1;
                    IContact * c = const_cast<IContact*>(state->getContact());
                    TPtr<IThing> t(c->getIThing());
                    p1.setValue(t);
                    arg.push_back(p1);
                    p1.setValue(0);
                    arg.push_back(p1);
                    IFishCallList *fi = const_cast<IFishCallList*>(state->getProgram()->findInterface<IFishCallList>());
                    fi->setCMFishCallArguments(c,arg,cmEvents_[fSlipChange]);
                }
                lin_S_ = true;
            } else {
                if (lin_S_) {
                    if (cmEvents_[fSlipChange] >= 0) {
                        FArray<QVariant,3> arg;
                        QVariant p1;
                        IContact * c = const_cast<IContact*>(state->getContact());
                        TPtr<IThing> t(c->getIThing());
                        p1.setValue(t);
                        arg.push_back(p1);
                        p1.setValue(1);
                        arg.push_back(p1);
                        IFishCallList *fi = const_cast<IFishCallList*>(state->getProgram()->findInterface<IFishCallList>());
                        fi->setCMFishCallArguments(c,arg,cmEvents_[fSlipChange]);
                    }
                    lin_S_ = false;
                }
            }
        }

        sforce.rx() = lin_F_.x();
        lin_F_ = sforce;          // total force in linear contact model
         
        // Account for dashpot forces
        if (dpProps_) {
            dpProps_->dp_F_.fill(0.0);
            double vcn(0.0), vcs(0.0);
            setDampCoefficients(state->inertialMass_,&vcn,&vcs);
            // Need to change behavior based on the dp_mode
            if (dpProps_->dp_mode_ == 0)  { // Damp all components
                dpProps_->dp_F_ = u_s * (-1.0* vcs) / timestep; // shear component   
                dpProps_->dp_F_ -= norm * vcn / timestep;       // normal component
            } else if (dpProps_->dp_mode_ == 1)  { // limit the tensile
                dpProps_->dp_F_ -= norm * vcn / timestep;       // normal component
                if (dpProps_->dp_F_.x() + lin_F_.x() < 0)
                    dpProps_->dp_F_.rx() = - lin_F_.rx();
            } else if (dpProps_->dp_mode_ == 2)  { // limit the shear
                if (!lin_S_)
                    dpProps_->dp_F_ = u_s * (-1.0* vcs) / timestep; // shear component 
            } else {
                if (!lin_S_)
                    dpProps_->dp_F_ = u_s * (-1.0* vcs) / timestep; // shear component   
                dpProps_->dp_F_ -= norm * vcn / timestep;       // normal component
                if (dpProps_->dp_F_.x() + lin_F_.x() < 0)
                    dpProps_->dp_F_.rx() = - lin_F_.rx();
            }
        }

        // Account for parallel bonds
        bool doPb = false;
        if (pbProps_ && pbProps_->pb_state_ > 2) {
            doPb = true;
            // Parallel Bond Logic:
            // bond area and inertia
            // minimal curvature of end1
            double Cmin1 = state->end1Curvature_.x();
            double Cmax1 = state->end1Curvature_.y();
            double Cmax2 = state->end2Curvature_.y();
            double dthick = (Cmin1 == 0.0) ? state->end1Extent_.x() : 0.0;    
            double br = pbProps_->pb_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 pbArea = dthick <= 0.0 ? dPi*br2 : 2.0*br*dthick;
            double bi = dthick <= 0.0 ? 0.25*pbArea*br2 : 2.0*br*br2*dthick/3.0;
            pbProps_->pbTransStiff_.rx() = pbProps_->pb_kn_*pbArea;
            pbProps_->pbTransStiff_.ry() = pbProps_->pb_ks_*pbArea;
     
            /* elastic force increments */
            pbProps_->pb_F_ -= norm *(pbProps_->pb_kn_*pbArea) + u_s * (pbProps_->pb_ks_*pbArea);

            /* elastic moment increments */
            //DAVect pbMomentInc(0.0);
#if DIM==3 
            pbProps_->pbAngStiff_.rx() = pbProps_->pb_ks_* 2.0 * bi;
            pbProps_->pbAngStiff_.ry() = pbProps_->pb_kn_* bi;
#endif
            pbProps_->pbAngStiff_.rz() = pbProps_->pb_kn_* bi;

            DAVect pbMomentInc = ang * pbProps_->pbAngStiff_ *(-1.0);
            pbProps_->pb_M_ += pbMomentInc;

            /* check bond failure */
            if (state->canFail_) {
                /* maximum stresses */
                double dbend = sqrt(pbProps_->pb_M_.y()*pbProps_->pb_M_.y() + pbProps_->pb_M_.z()*pbProps_->pb_M_.z());
                double dtwist = pbProps_->pb_M_.x();
                DVect bfs(pbProps_->pb_F_);
                bfs.rx() = 0.0;
                double dbfs = bfs.mag();
                double nsmax = -(pbProps_->pb_F_.x() / pbArea)  +  pbProps_->pb_mcf_ * dbend * br/bi;
                double ssmax = dbfs / pbArea  +  pbProps_->pb_mcf_ * std::abs(dtwist) * 0.5* br/bi;
                double ss ;

                if (nsmax >= pbProps_->pb_ten_) {
                    // Failed in tension
                    double se = pbStrainEnergy(); // bond strain energy at the onset of failure
                    pbProps_->pb_state_ = 1;
                    pbProps_->pb_F_.fill(0.0);
                    pbProps_->pb_M_.fill(0.0);
                    if (cmEvents_[fBondBreak] >= 0) {
                        FArray<QVariant,3> arg;
                        QVariant p1;
                        IContact * c = const_cast<IContact*>(state->getContact());
                        TPtr<IThing> t(c->getIThing());
                        p1.setValue(t);
                        arg.push_back(p1);
                        p1.setValue(pbProps_->pb_state_);
                        arg.push_back(p1);
                        p1.setValue(pbProps_->pb_ten_);
                        arg.push_back(p1);
                        p1.setValue(se);
                        arg.push_back(p1);
                        IFishCallList *fi = const_cast<IFishCallList*>(state->getProgram()->findInterface<IFishCallList>());
                        fi->setCMFishCallArguments(c,arg,cmEvents_[fBondBreak]);
                    }
                } else if ((ss = pbShearStrength(pbArea)) <= ssmax) {
                    // Failed in shear
                    double se = pbStrainEnergy(); // bond strain energy at the onset of failure
                    pbProps_->pb_state_ = 2;
                    pbProps_->pb_F_.fill(0.0);
                    pbProps_->pb_M_.fill(0.0);
                    if (cmEvents_[fBondBreak] >= 0) {
                        FArray<QVariant,3> arg;
                        QVariant p1;
                        IContact * c = const_cast<IContact*>(state->getContact());
                        TPtr<IThing> t(c->getIThing());
                        p1.setValue(t);
                        arg.push_back(p1);
                        p1.setValue(pbProps_->pb_state_);
                        arg.push_back(p1);
                        p1.setValue(ss);
                        arg.push_back(p1);
                        p1.setValue(se);
                        arg.push_back(p1);
                        IFishCallList *fi = const_cast<IFishCallList*>(state->getProgram()->findInterface<IFishCallList>());
                        fi->setCMFishCallArguments(c,arg,cmEvents_[fBondBreak]);
                    }
                }
            }
        }

        // Compute energies
        if (state->trackEnergy_) {
            assert(energies_);
            energies_->estrain_ = 0.0;
            energies_->epbstrain_ = 0.0;
            if (kn_)
                energies_->estrain_ = 0.5 * lin_F_.x()* lin_F_.x() /kn_;
            if (ks_) {
                DVect s = lin_F_;
                s.rx() = 0.0;
                double smag2 = s.mag2();
                energies_->estrain_ += 0.5* smag2 / ks_ ;

                if (lin_S_) {
                    ss_F_old.rx() = 0.0;
                    DVect avg_F_s = (s + ss_F_old)*0.5;
                    DVect u_s_el =  (s - ss_F_old) / ks_;
                    energies_->eslip_ -= std::min(0.0,(avg_F_s | (u_s + u_s_el)));
                }
            }
            if (dpProps_)
                energies_->edashpot_ -= dpProps_->dp_F_ | trans;
            if (doPb)
                energies_->epbstrain_ = pbStrainEnergy();
        }

        assert(lin_F_ == lin_F_);
        return checkActivity(state->gap_);
    }

    bool ContactModelLinearPBond::thermalCoupling(ContactModelMechanicalState *ms,ContactModelThermalState *ts, IContactThermal *ct,const double &) {
        bool ret = false;
        if (!pbProps_) return ret;
        if (pbProps_->pb_state_ < 3) return ret;
        int idx = ct->getModel()->getContactModel()->isProperty("thexp");
        if (idx<=0 ) return ret; 

        double thexp = (ct->getModel()->getContactModel()->getProperty(idx)).toDouble();
        double length = ts->length_;
        double delTemp =ts->tempInc_;
        double delUn = length * thexp * delTemp;
        if (delUn == 0.0) return ret;
         
        double dthick = 0.0;
        double Cmin1 = ms->end1Curvature_.x();
        double Cmax1 = ms->end1Curvature_.y();
        double Cmin2 = ms->end2Curvature_.x();
        double Cmax2 = ms->end2Curvature_.y();

        Cmin2;
        if (Cmin1 == 0.0)
            dthick = ms->end1Extent_.x();    

        double br = pbProps_->pb_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 pbArea = dthick <= 0.0 ? dPi*br2 : 2.0*br*dthick;
        //
        DVect finc(0.0);
        finc.rx() = 1.0;
        finc *= pbProps_->pb_kn_*pbArea*delUn;
        pbProps_->pb_F_ += finc;
         
        //ms->force_ += finc;
         
        // The state force has been updated - update the state with the resulting torques
        //ms->getMechanicalContact()->updateResultingTorquesLocal(ms->force_,&ms->momentOn1_,&ms->momentOn2_);

        ret = true;
        return ret;
    }

    void ContactModelLinearPBond::setForce(const DVect &v,IContact *c) { 
        lin_F(v); 
        if (v.x() > 0) 
            rgap_ = c->getGap() + v.x() / kn_; 
    } 

    void ContactModelLinearPBond::propagateStateInformation(IContactModelMechanical* old,const CAxes &oldSystem,const CAxes &newSystem) {
        // Only do something if the contact model is of the same type
        if (old->getContactModel()->getName().compare("linearpbond",Qt::CaseInsensitive) == 0 && !isBonded()) {
            ContactModelLinearPBond *oldCm = (ContactModelLinearPBond *)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;
            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->lin_F_.y(),oldCm->lin_F_.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->lin_F_.y(),oldCm->lin_F_.z());
            }
            DVect pforce = DVect(0,tpf.x(),tpf.y());
#else
            oldSystem;
            newSystem;
            DVect pforce = DVect(0,oldCm->lin_F_.y());
#endif
            for (int i=1; i<dim; ++i)
                lin_F_.rdof(i) += pforce.dof(i);
            oldCm->lin_F_ = DVect(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_->eslip_ = oldCm->energies_->eslip_;
                energies_->edashpot_ = oldCm->energies_->edashpot_;
                energies_->epbstrain_ = oldCm->energies_->epbstrain_;
                oldCm->energies_->estrain_ = 0.0;
                oldCm->energies_->edashpot_ = 0.0;
                oldCm->energies_->eslip_ = 0.0;
                oldCm->energies_->epbstrain_ = 0.0;
            }
            rgap_ = oldCm->rgap_;
        }
        assert(lin_F_ == lin_F_);
    }

    void ContactModelLinearPBond::setNonForcePropsFrom(IContactModel *old) {
        // Only do something if the contact model is of the same type
        if (old->getName().compare("linearpbond",Qt::CaseInsensitive) == 0 && !isBonded()) {
            ContactModelLinearPBond *oldCm = (ContactModelLinearPBond *)old;
            kn_ = oldCm->kn_;
            ks_ = oldCm->ks_;
            fric_ = oldCm->fric_;
            lin_mode_ = oldCm->lin_mode_;
            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->pbProps_) {
                if (!pbProps_)
                    pbProps_ = NEWC(pbProps());
                pbProps_->pb_rmul_ = oldCm->pbProps_->pb_rmul_;
                pbProps_->pb_kn_ = oldCm->pbProps_->pb_kn_;
                pbProps_->pb_ks_ = oldCm->pbProps_->pb_ks_; 
                pbProps_->pb_mcf_ = oldCm->pbProps_->pb_mcf_; 
                pbProps_->pb_fa_ = oldCm->pbProps_->pb_fa_; 
                pbProps_->pb_state_ = oldCm->pbProps_->pb_state_; 
                pbProps_->pb_coh_ = oldCm->pbProps_->pb_coh_; 
                pbProps_->pb_ten_ = oldCm->pbProps_->pb_ten_; 
                pbProps_->pbTransStiff_ = oldCm->pbProps_->pbTransStiff_; 
                pbProps_->pbAngStiff_ = oldCm->pbProps_->pbAngStiff_; 
            }
        }
    }

    DVect ContactModelLinearPBond::getForce(const IContactMechanical *) const {
        DVect ret(lin_F_);
        if (dpProps_)
            ret += dpProps_->dp_F_;
        if (pbProps_)
            ret += pbProps_->pb_F_;
        return ret;
    }

    DAVect ContactModelLinearPBond::getMomentOn1(const IContactMechanical *c) const {
        DVect force = getForce(c);
        DAVect ret(0.0);
        if (pbProps_)
            ret = pbProps_->pb_M_;
        c->updateResultingTorqueOn1Local(force,&ret);
        return ret;
    }

    DAVect ContactModelLinearPBond::getMomentOn2(const IContactMechanical *c) const {
        DVect force = getForce(c);
        DAVect ret(0.0);
        if (pbProps_)
            ret = pbProps_->pb_M_;
        c->updateResultingTorqueOn2Local(force,&ret);
        return ret;
    }

    DVect3 ContactModelLinearPBond::pbData(const IContactMechanical *c) const {
        double Cmin1 = c->getEnd1Curvature().x();
        double Cmax1 = c->getEnd1Curvature().y();
        double Cmax2 = c->getEnd2Curvature().y();
        double dthick = (Cmin1 == 0.0) ? c->getEnd1Extent().x() : 0.0;    
        double br = pbProps_->pb_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 pbArea = dthick <= 0.0 ? dPi*br2 : 2.0*br*dthick;
        double bi = dthick <= 0.0 ? 0.25*pbArea*br2 : 2.0*br*br2*dthick/3.0;
        return DVect3(pbArea,bi,br);
    }

    DVect2 ContactModelLinearPBond::pbSMax(const IContactMechanical *c) const {
        DVect3 data = pbData(c);
        double pbArea = data.x();
        double bi = data.y();
        double br = data.z();
        /* maximum stresses */
        double dbend = sqrt(pbProps_->pb_M_.y()*pbProps_->pb_M_.y() + pbProps_->pb_M_.z()*pbProps_->pb_M_.z());
        double dtwist = pbProps_->pb_M_.x();
        DVect bfs(pbProps_->pb_F_);
        bfs.rx() = 0.0;
        double dbfs = bfs.mag();
        double nsmax = -(pbProps_->pb_F_.x() / pbArea)  +  pbProps_->pb_mcf_ * dbend * br/bi;
        double ssmax = dbfs / pbArea  +  pbProps_->pb_mcf_ * std::abs(dtwist) * 0.5* br/bi;
        return DVect2(nsmax,ssmax);
    }

    double ContactModelLinearPBond::pbShearStrength(const double &pbArea) const {
        if (!pbProps_) return 0.0;
        double sig = -1.0*pbProps_->pb_F_.x() / pbArea;
        double nstr = pbProps_->pb_state_ > 2 ? pbProps_->pb_ten_ : 0.0;
        return sig <= nstr ? pbProps_->pb_coh_ - std::tan(dDegrad*pbProps_->pb_fa_)*sig
                            : pbProps_->pb_coh_ - std::tan(dDegrad*pbProps_->pb_fa_)*nstr;
    }

    void ContactModelLinearPBond::setDampCoefficients(const double &mass,double *vcn,double *vcs) {
        *vcn = dpProps_->dp_nratio_ * 2.0 * sqrt(mass*(kn_));
        *vcs = dpProps_->dp_sratio_ * 2.0 * sqrt(mass*(ks_));
    }

} // namespace itascaxd
// EoF

Top