File: GougeConfig.hpp

package info (click to toggle)
esys-particle 2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,284 kB
  • sloc: cpp: 77,304; python: 5,647; makefile: 1,176; sh: 10
file content (1153 lines) | stat: -rw-r--r-- 35,497 bytes parent folder | download
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
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2011 by The University of Queensland //
// Earth Systems Science Computational Centre (ESSCC)      //
// http://www.uq.edu.au/esscc                              //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.opensource.org/licenses/osl-3.0.php          //
//                                                         //
/////////////////////////////////////////////////////////////



#include "Foundation/console.h"
#include "Geometry/GeometryInfo.h"
#include "Geometry/CircularNeighbourTable.h"

#include <stdexcept>
#include <fstream>
#include <sstream>
#include <iomanip>

namespace esys
{
  namespace lsm
  {
    //==========================================================================
    PackingInfo::PackingInfo(
      const BoundingBox &bBox,
      const BoolVector  &periodicDimensions,
      Orientation       orientation,
      double            minRadius,
      double            maxRadius
    ) : m_bBox(bBox),
        m_periodicDimensions(periodicDimensions),
        m_orientation(orientation),
        m_minRadius(minRadius),
        m_maxRadius(maxRadius)
    {
      initialiseFitPlaneVector();
    }
    
    bool PackingInfo::is3d() const
    {
      return (m_bBox.getSizes().Z() > 0.0);
    }
    
    void PackingInfo::initialiseFitPlaneVector()
    {
      m_fitPlaneVector.clear();
      if ((m_orientation != XZ) && (!getPeriodicDimensions()[1])) {
        m_fitPlaneVector.push_back(
          Plane(Vec3(0,  1, 0), getBBox().getMinPt())
        );
        m_fitPlaneVector.push_back(
          Plane(Vec3(0, -1, 0), getBBox().getMaxPt())
        );
      }
      if ((m_orientation != YZ) && (!getPeriodicDimensions()[0])) {
        m_fitPlaneVector.push_back(
          Plane(Vec3( 1, 0, 0), getBBox().getMinPt())
        );
        m_fitPlaneVector.push_back(
          Plane(Vec3(-1, 0, 0), getBBox().getMaxPt())
        );
      }
      if (
          is3d()
          &&
          (m_orientation != XY)
          &&
          (!getPeriodicDimensions()[2])
      ) {
        m_fitPlaneVector.push_back(
          Plane(Vec3(0, 0,  1), getBBox().getMinPt())
        );
        m_fitPlaneVector.push_back(
          Plane(Vec3(0, 0, -1), getBBox().getMaxPt())
        );
      }
    }

    const BoundingBox &PackingInfo::getBBox() const
    {
      return m_bBox;
    }

    const PlaneVector &PackingInfo::getFitPlaneVector() const
    {
      return m_fitPlaneVector;
    }

    double PackingInfo::getMinParticleRadius() const
    {
      return m_minRadius;
    }

    double PackingInfo::getMaxParticleRadius() const
    {
      return m_maxRadius;
    }

    const BoolVector &PackingInfo::getPeriodicDimensions() const
    {
      return m_periodicDimensions;
    }

    //==========================================================================
    
    template <typename TGrainGen>
    GougePackingInfo<TGrainGen>::GougePackingInfo(
      const BoundingBox &bBox,
      const BoolVector  &periodicDimensions,
      Orientation       orientation,
      ParticleGrainGen  &particleGrainGen
    ) : Inherited(
          bBox,
          periodicDimensions,
          orientation,
          particleGrainGen.getMinParticleRadius(),
          particleGrainGen.getMaxParticleRadius()
        ),
        m_pParticleGrainGen(&particleGrainGen)
        
    {
    }

    template <typename TGrainGen>
    typename GougePackingInfo<TGrainGen>::ParticleGrainGen &
    GougePackingInfo<TGrainGen>::getParticleGrainGen() const
    {
      return *m_pParticleGrainGen;
    }

#if 0
    template <typename TGrainGen>
    const typename GougePackingInfo<TGrainGen>::ParticleGrainGen &
    GougePackingInfo<TGrainGen>::getParticleGrainGen() const
    {
      return *m_pParticleGrainGen;
    }
#endif

    template <typename TGrainGen>
    double GougePackingInfo<TGrainGen>::getMinGrainRadius() const
    {
      return getParticleGrainGen().getMinGrainRadius();
    }

    template <typename TGrainGen>
    double GougePackingInfo<TGrainGen>::getMaxGrainRadius() const
    {
      return getParticleGrainGen().getMaxGrainRadius();
    }

    //==========================================================================

    ParticleRndPackPrms::ParticleRndPackPrms()
      : m_size(0.0),
        m_minParticleRadius(0.0),
        m_maxParticleRadius(0.0)
    {
    }

    ParticleRndPackPrms::ParticleRndPackPrms(
      double size,
      double minRadius,
      double maxRadius
    ) : m_size(size),
        m_minParticleRadius(minRadius),
        m_maxParticleRadius(maxRadius)
    {
    }

    ParticleRndPackPrms::~ParticleRndPackPrms()
    {
    }

    double ParticleRndPackPrms::getSize() const
    {
      return m_size;
    }

    double ParticleRndPackPrms::getMinParticleRadius() const
    {
      return m_minParticleRadius;
    }

    double ParticleRndPackPrms::getMaxParticleRadius() const
    {
      return m_maxParticleRadius;
    }

    //==========================================================================
    
    template <typename TPGrainGen>
    GrainRndPackPrms<TPGrainGen>::GrainRndPackPrms()
      :
        Inherited(),
        m_pParticleGrainGen(NULL)
    {
    }

    template <typename TPGrainGen>
    GrainRndPackPrms<TPGrainGen>::GrainRndPackPrms(
      double size,
      ParticleGrainGen &particleGrainGen,
      int connectionTag
    ) : Inherited(
          size,
          particleGrainGen.getMinParticleRadius(),
          particleGrainGen.getMaxParticleRadius()
        ),
        m_pParticleGrainGen(&particleGrainGen),
        m_connectionTag(connectionTag)
    {
    }

    template <typename TPGrainGen>
    typename GrainRndPackPrms<TPGrainGen>::ParticleGrainGen &
    GrainRndPackPrms<TPGrainGen>::getParticleGrainGen() const
    {
      return *m_pParticleGrainGen;
    }

    template <typename TPGrainGen>
    int
    GrainRndPackPrms<TPGrainGen>::getConnectionTag() const
    {
      return m_connectionTag;
    }

#if 0
    template <typename TPGrainGen>
    const typename GrainRndPackPrms<TPGrainGen>::ParticleGrainGen &
    GrainRndPackPrms<TPGrainGen>::getParticleGrainGen() const
    {
      return *m_pParticleGrainGen;
    }
#endif

    template <typename TPGrainGen>
    double GrainRndPackPrms<TPGrainGen>::getMinGrainRadius()
    {
      return getParticleGrainGen().getMinGrainRadius();
    }

    template <typename TPGrainGen>
    double GrainRndPackPrms<TPGrainGen>::getMaxGrainRadius()
    {
      return getParticleGrainGen().getMaxGrainRadius();
    }

    //==========================================================================

    template <typename TPGrainGen>
    GougeConfigPrms<TPGrainGen>::GougeConfigPrms()
      : m_bBox(Vec3::ZERO, Vec3::ZERO),
        m_padRadius(0.0),
        m_orientation(XZ),
        m_faultPrms(),
        m_gougePrms(),
        m_periodicDimensions(3, false),
        m_maxInsertionFailures(50),
        m_tolerance(DBL_EPSILON*128),
        m_connectionTolerance(DBL_EPSILON*128*10),
        m_blockConnectionTag(0)
    {
    }

    template <typename TPGrainGen>
    GougeConfigPrms<TPGrainGen>::GougeConfigPrms(
      const BoundingBox         &bBox,
      double                    padRadius,
      Orientation               orientation,
      const ParticleRndPackPrms &faultRegionPrms,
      const GrainRPackPrms    &gougeRegionPrms,
      const BoolVector          &periodicDimensions,
      int                       maxInsertionFailures,
      double                    tolerance ,
      double                    connectionTolerance,
      int                       blockConnectionTag
    ) : m_bBox(bBox),
        m_padRadius(padRadius),
        m_orientation(orientation),
        m_faultPrms(faultRegionPrms),
        m_gougePrms(gougeRegionPrms),
        m_periodicDimensions(periodicDimensions),
        m_maxInsertionFailures(maxInsertionFailures),
        m_tolerance(tolerance),
        m_connectionTolerance(connectionTolerance),
        m_blockConnectionTag(blockConnectionTag)
    {
      m_bBox = GridIterator(bBox, getMaxRadius()).getSphereBBox();
    }

    template <typename TPGrainGen>
    GougeConfigPrms<TPGrainGen>::~GougeConfigPrms()
    {
    }

    template <typename TPGrainGen>
    const BoundingBox &GougeConfigPrms<TPGrainGen>::getBBox() const
    {
      return m_bBox;
    }

    template <typename TPGrainGen>
    int GougeConfigPrms<TPGrainGen>::getGougeConnectionTag() const
    {
      return m_gougePrms.getConnectionTag();
    }

    template <typename TPGrainGen>
    int GougeConfigPrms<TPGrainGen>::getBlockConnectionTag() const
    {
      return m_blockConnectionTag;
    }

    template <typename TPGrainGen>
    int GougeConfigPrms<TPGrainGen>::getMaxInsertionFailures() const
    {
      return m_maxInsertionFailures;
    }

    template <typename TPGrainGen>
    const BoolVector &GougeConfigPrms<TPGrainGen>::getPeriodicDimensions() const
    {
      return m_periodicDimensions;
    }

    template <typename TPGrainGen>
    Orientation GougeConfigPrms<TPGrainGen>::getOrientation() const
    {
      return m_orientation;
    }

    template <typename TPGrainGen>
    double GougeConfigPrms<TPGrainGen>::getTolerance() const
    {
      return m_tolerance;
    }

    template <typename TPGrainGen>
    double GougeConfigPrms<TPGrainGen>::getConnectionTolerance() const
    {
      return m_connectionTolerance;
    }

    template <typename TPGrainGen>
    int GougeConfigPrms<TPGrainGen>::getOrientationIndex() const
    {
      int idx = 0;
      switch (m_orientation) {
        case XZ:
        {
          idx = 1;
          break;
        }
        case YZ:
        {
          idx = 0;
          break;
        }
        case XY:
        {
          idx = 2;
          break;
        }
        default:
        {
          std::stringstream msg;
          msg << "Invalid orientation: " << m_orientation;
          throw std::runtime_error(msg.str());
        }
      }
      return idx;
    }

    template <typename TPGrainGen>
    BoundingBox GougeConfigPrms<TPGrainGen>::cutFromCentre(double d1, double d2) const
    {
      const int idx = getOrientationIndex();
      const BoundingBox bBox = getBBox(); 

      const double cmp1 = d1 + (bBox.getMaxPt()[idx] + bBox.getMinPt()[idx])/2.0;
      const double cmp2 = d2 + (bBox.getMaxPt()[idx] + bBox.getMinPt()[idx])/2.0;
      Vec3 minPt = bBox.getMinPt();
      Vec3 maxPt = bBox.getMaxPt();

      minPt[idx] = std::min(cmp1, cmp2);
      maxPt[idx] = std::max(cmp1, cmp2);

      Vec3 tmpPt = maxPt;
      tmpPt[idx] = minPt[idx];

      if ((tmpPt - bBox.getMinPt())[idx] > getTolerance()) {
        const BoundingBox minBBox = GridIterator(BoundingBox(bBox.getMinPt(), tmpPt), getMaxRadius()).getSphereBBox();
        tmpPt = minPt;
        tmpPt[idx] = minBBox.getMaxPt()[idx];
      }
      else {
        tmpPt = bBox.getMinPt();
      }
      const BoundingBox maxBBox = GridIterator(BoundingBox(bBox.getMinPt(), maxPt), getMaxRadius()).getSphereBBox();
      BoundingBox returnBBox = BoundingBox(tmpPt, maxBBox.getMaxPt());

      return returnBBox;
    }

    template <typename TPGrainGen>
    double GougeConfigPrms<TPGrainGen>::getRegularBlockRadius() const
    {
      return m_padRadius;
    }

    template <typename TPGrainGen>
    double GougeConfigPrms<TPGrainGen>::getFaultMinRadius() const
    {
      return m_faultPrms.getMinParticleRadius();
    }

    template <typename TPGrainGen>
    double GougeConfigPrms<TPGrainGen>::getFaultMaxRadius() const
    {
      return m_faultPrms.getMaxParticleRadius();
    }

    template <typename TPGrainGen>
    double GougeConfigPrms<TPGrainGen>::getGougeMinRadius() const
    {
      return m_gougePrms.getMinParticleRadius();
    }

    template <typename TPGrainGen>
    double GougeConfigPrms<TPGrainGen>::getGougeMaxRadius() const
    {
      return m_gougePrms.getMaxParticleRadius();
    }

    template <typename TPGrainGen>
    double GougeConfigPrms<TPGrainGen>::getOrientationSize() const
    {
      return 
        (
          getBBox().getMaxPt()[getOrientationIndex()]
          -
          getBBox().getMinPt()[getOrientationIndex()]
        );
    }

    template <typename TPGrainGen>
    BoundingBoxVector GougeConfigPrms<TPGrainGen>::getRegularBBoxVector() const
    {
      BoundingBoxVector bBoxVector;
      if (
        (getOrientationSize() - (m_gougePrms.getSize() + 2*m_faultPrms.getSize()))
        >
        2.0*m_padRadius
      )
      {
        bBoxVector.reserve(2);
        bBoxVector.push_back(
          cutFromCentre(
            -(m_gougePrms.getSize()/2.0 + m_faultPrms.getSize()),
            -getOrientationSize()/2.0
          )
        );
        bBoxVector.push_back(
          cutFromCentre(
            m_gougePrms.getSize()/2.0 + m_faultPrms.getSize(),
            getOrientationSize()/2.0
          )
        );
      }
      return bBoxVector;
    }

    template <typename TPGrainGen>
    typename GougeConfigPrms<TPGrainGen>::GougePackingInfoVector
    GougeConfigPrms<TPGrainGen>::getGougePackingInfoVector() const
    {
      GougePackingInfoVector infoVec;
      if (m_gougePrms.getSize() > 0.0) {
        Vec3 overlap = Vec3::ZERO;
        overlap[getOrientationIndex()] = m_faultPrms.getMaxParticleRadius();
        BoundingBox bBox =
          cutFromCentre(
             m_gougePrms.getSize()/2.0,
            -m_gougePrms.getSize()/2.0
          );
        
        infoVec.push_back(
          GougePackInfo(
            BoundingBox(bBox.getMinPt() - overlap, bBox.getMaxPt() + overlap),
            getPeriodicDimensions(),
            getOrientation(),
            m_gougePrms.getParticleGrainGen()
          )
        );
      }
      return infoVec;
    }

    template <typename TPGrainGen>
    PackingInfoVector GougeConfigPrms<TPGrainGen>::getFaultPackingInfoVector() const
    {
      PackingInfoVector infoVec;
      if (m_faultPrms.getSize() > 0.0)
      {
        if (
            (getOrientationSize() - (m_gougePrms.getSize() + 2.0*m_faultPrms.getSize()))
            >
            0.0
        )
        {
          infoVec.reserve(2);
          const double roughnessSize = m_faultPrms.getSize();
          Vec3 overlap = Vec3::ZERO;
          overlap[getOrientationIndex()] = m_padRadius;
          const BoundingBox bBox1 =
            cutFromCentre(
              -m_gougePrms.getSize()/2.0,
              -(m_gougePrms.getSize()/2.0 + roughnessSize)
            );
          const BoundingBox bBox2 =
            cutFromCentre(
              m_gougePrms.getSize()/2.0,
              m_gougePrms.getSize()/2.0 + roughnessSize
            );
  
          infoVec.push_back(
            PackingInfo(
              BoundingBox(bBox1.getMinPt() - overlap, bBox1.getMaxPt()),
              getPeriodicDimensions(),
              getOrientation(),
              getFaultMinRadius(),
              getFaultMaxRadius()
            )
          );
  
          infoVec.push_back(
            PackingInfo(
              BoundingBox(bBox2.getMinPt(), bBox2.getMaxPt() + overlap),
              getPeriodicDimensions(),
              getOrientation(),
              getFaultMinRadius(),
              getFaultMaxRadius()
            )
          );
        }
        else
        {
          std::stringstream msg;
          msg
            << "Roughness size plus gouge size is greater than block size: "
            << "2*" << m_faultPrms.getSize() << " + " << m_gougePrms.getSize()
            << " > " << getOrientationSize();
          throw std::runtime_error(msg.str().c_str());
        }
      }
      return infoVec;
    }

    template <typename TPGrainGen>
    double GougeConfigPrms<TPGrainGen>::getMaxRadius() const
    {
      return 
        std::max(
          m_padRadius,
          std::max(m_faultPrms.getMaxParticleRadius(), m_gougePrms.getMaxParticleRadius())
        );
    }

    template <typename TPGrainGen>
    double GougeConfigPrms<TPGrainGen>::getMinRadius() const
    {
      return 
        std::min(
          m_padRadius,
          std::min(m_faultPrms.getMinParticleRadius(), m_gougePrms.getMinParticleRadius())
        );
    }

    template <typename TPGrainGen>
    bool GougeConfigPrms<TPGrainGen>::is2d() const
    {
      return (getBBox().getSizes()[2] == 0.0);
    }

    //==========================================================================

    template <typename TGPckr,typename TPPckr,typename TConn>
    int GougeConfig<TGPckr,TPPckr,TConn>::getNumParticles() const
    {
      int numParticles = 0;
      for (
        typename GeneratorPtrVector::const_iterator it = m_genPtrVector.begin();
        it != m_genPtrVector.end();
        it++
      )
      {
        numParticles += (*it)->getNumParticles();
      }
      return numParticles;
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    int GougeConfig<TGPckr,TPPckr,TConn>::getNumGrains() const
    {
      int numGrains = 0;
      for (
        typename GrainRndPackerPtrVector::const_iterator packerIt =
          getGougeGeneratorVector().begin();
        packerIt != getGougeGeneratorVector().end();
        packerIt++
      )
      {
        numGrains += (*packerIt)->getNumGrains();
      }
      return numGrains;
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    int GougeConfig<TGPckr,TPPckr,TConn>::getNumConnections() const
    {
      return m_connectionSet.size();
    }
    
    template <typename TGPckr,typename TPPckr,typename TConn>
    GougeConfig<TGPckr,TPPckr,TConn>::GougeConfig(const GougeConfPrms &prms)
      : m_nTablePtr(),
        m_prms(prms),
        m_connectionSet(),
        m_gougeGenPtrVector(),
        m_genPtrVector(),
        m_particlePoolPtr(new ParticlePool(8*4096)),
        m_grainPoolPtr(new GrainPool(4096)),
        m_regularGenPtrVector(),
        m_faultGenPtrVector()
    {
      /*
       * Adjust the size of the ntable bounding-box to accommodate circular
       * boundary conditions.
       */
      const BoundingBox bBox = m_prms.getBBox();
      Vec3 ntableAdjust = 
        Vec3(
          m_prms.getPeriodicDimensions()[0] ? 1 : 0,
          m_prms.getPeriodicDimensions()[1] ? 1 : 0,
          m_prms.getPeriodicDimensions()[2] ? 1 : 0
        )*m_prms.getMaxRadius();
      if (m_prms.getBBox().getSizes().Z() >= 4*m_prms.getMaxRadius()) {
        ntableAdjust += Vec3(m_prms.getMaxRadius(), 0, 0);
      }
      const BoundingBox nTableBBox(bBox.getMinPt(), bBox.getMaxPt() - ntableAdjust);
      m_nTablePtr =
        NTablePtr(
          new NTable(
            nTableBBox,
            (4.0*m_prms.getMinRadius()), // grid spacing
            m_prms.getPeriodicDimensions(),
            2.1*m_prms.getMaxRadius() // width of border-region in which
                                      // particles are duplicated
                                      // for circular boundry
          )
        );
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    void GougeConfig<TGPckr,TPPckr,TConn>::createRegularBlockGenerators()
    {
      BoundingBoxVector bBoxVector = m_prms.getRegularBBoxVector();
      for (
        BoundingBoxVector::const_iterator it = bBoxVector.begin();
        it != bBoxVector.end();
        it++
      ) {
        console.Debug()
          << "GougeConfig<TGPckr,TPPckr,TConn>::createRegularBlockGenerators:"
          << "Creating RegBoxPacker in box: " << StringUtil::toString(*it)
          << "\n";

        GeneratorPtr genPtr =
          GeneratorPtr(
            new RegBoxPacker(
              RegRadiusGenPtr(new RegRadiusGen(m_prms.getRegularBlockRadius())),
              m_particlePoolPtr,
              m_nTablePtr,
              *it,
              m_prms.getPeriodicDimensions(),
              m_prms.getTolerance(),
              m_prms.getRegularBlockRadius()
            )
          );
        m_genPtrVector.push_back(genPtr);
        m_regularGenPtrVector.push_back(genPtr);
      }
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    void GougeConfig<TGPckr,TPPckr,TConn>::createFaultBlockGenerators()
    {      
      PackingInfoVector infoVec = m_prms.getFaultPackingInfoVector();
      for (
        PackingInfoVector::const_iterator it = infoVec.begin();
        it != infoVec.end();
        it++
      ) {
        console.Debug()
          << "GougeConfig<TGPckr,TPPckr,TConn>::createFaultBlockGenerators:"
          << "Creating RndBoxPacker in box: " << StringUtil::toString(it->getBBox())
          << "\n";
        GeneratorPtr genPtr = 
          GeneratorPtr(
            new RndBoxPacker(
              RndRadiusGenPtr(
                new RndRadiusGen(
                  it->getMinParticleRadius(),
                  it->getMaxParticleRadius()
                )
              ),
              m_particlePoolPtr,
              m_nTablePtr,
              it->getBBox(),
              it->getPeriodicDimensions(),
              m_prms.getTolerance(),
              it->getMaxParticleRadius(),
              m_prms.getMaxInsertionFailures(),
              it->getFitPlaneVector()
            )
          );
        m_genPtrVector.push_back(genPtr);
        m_faultGenPtrVector.push_back(genPtr);
      }
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    void GougeConfig<TGPckr,TPPckr,TConn>::createGougeConfigGenerators()
    {
      GougePackingInfoVector infoVec = m_prms.getGougePackingInfoVector();
      for (
        typename GougePackingInfoVector::const_iterator it = infoVec.begin();
        it != infoVec.end();
        it++
      ) {
        console.Debug()
          << "GougeConfig<TGPckr,TPPckr,TConn>::createGougeConfigGenerators:"
          << "Creating GrainRandomPacker in box: " << StringUtil::toString(it->getBBox())
          << "\n";
        GrainRandomPackerPtr genPtr =
          GrainRandomPackerPtr(
            new GrainRandomPacker(
              typename GrainRandomPacker::ParticleGrainGenPtr(),
              m_particlePoolPtr,
              m_nTablePtr,
              it->getBBox(),
              it->getPeriodicDimensions(),
              m_prms.getTolerance(),
              it->getParticleGrainGen().getMaxGrainRadius(),
              m_prms.getMaxInsertionFailures(),
              it->getFitPlaneVector(),
              m_grainPoolPtr
            )
          );
        genPtr->setParticleGrainGen(it->getParticleGrainGen());
        m_genPtrVector.push_back(genPtr);
        m_gougeGenPtrVector.push_back(genPtr);
      }
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    GougeConfig<TGPckr,TPPckr,TConn>::~GougeConfig()
    {
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    void GougeConfig<TGPckr,TPPckr,TConn>::generate()
    {
      // setup block generators
      createRegularBlockGenerators();
      createFaultBlockGenerators();
      createGougeConfigGenerators();
 
      // use block generators  
      console.Info() << "bbox = " << m_prms.getBBox().getMinPt() << " " << m_prms.getBBox().getMaxPt() << "\n";
      for (
        typename GeneratorPtrVector::iterator it = m_genPtrVector.begin();
        it != m_genPtrVector.end();
        it++
      )
      {
        (*it)->generate();
      }
      createConnectionSet();
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    void GougeConfig<TGPckr,TPPckr,TConn>::writeToFile(const std::string &fileName) const
    {
      std::ofstream fStream(fileName.c_str());
      write(fStream);
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    const typename GougeConfig<TGPckr,TPPckr,TConn>::GrainRndPackerPtrVector &
    GougeConfig<TGPckr,TPPckr,TConn>::getGougeGeneratorVector() const
    {
      return m_gougeGenPtrVector;
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    typename GougeConfig<TGPckr,TPPckr,TConn>::GrainRndPackerPtrVector &
    GougeConfig<TGPckr,TPPckr,TConn>::getGougeGeneratorVector()
    {
      return m_gougeGenPtrVector;
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    const typename GougeConfig<TGPckr,TPPckr,TConn>::GeneratorPtrVector &
    GougeConfig<TGPckr,TPPckr,TConn>::getFaultGeneratorVector() const
    {
      return m_faultGenPtrVector;
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    bool GougeConfig<TGPckr,TPPckr,TConn>::areInDifferentFaultBlocks(
      const Particle &p1,
      const Particle &p2
    ) const
    {
      const GeneratorPtrVector &generators = getFaultGeneratorVector();
      if (generators.size() == 2) {
        return
          (
            (generators[0]->contains(p1) && generators[1]->contains(p2))
            ||
            (generators[0]->contains(p2) && generators[1]->contains(p1))
          );
      }
      else if (generators.size() > 2) {
        throw
          std::runtime_error(
            "GougeConfig<TGPckr,TPPckr,TConn>::areInDifferentFaultBlocks: "
            "More than two fault blocks."
          );
      }
      return false;
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    bool GougeConfig<TGPckr,TPPckr,TConn>::isGougeParticle(const Particle &particle) const
    {
      const GrainRndPackerPtrVector &generators = getGougeGeneratorVector();
      for (
        typename GrainRndPackerPtrVector::const_iterator it = generators.begin();
        it != generators.end();
        it++
      )
      {
        if ((*it)->contains(particle)) {
          return true;
        }
      }
      return false;
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    void GougeConfig<TGPckr,TPPckr,TConn>::createConnectionSet()
    {
      //
      // First created connections in the elastic blocks.
      //
      typename NTable::ParticleIterator particleIt = m_nTablePtr->getParticleIterator();
      ConnectionValidator validator = ConnectionValidator(*this, m_prms.getConnectionTolerance());
      while (particleIt.hasNext()) {
        const typename NTable::Particle *pParticle = particleIt.next();
        const typename NTable::ParticleVector neighbours =
          m_nTablePtr->getNeighbourVector(
            pParticle->getPos(),
            pParticle->getRad() + m_prms.getConnectionTolerance()
          );
        for (
          typename NTable::ParticleVector::const_iterator it = neighbours.begin();
          it != neighbours.end();
          it++
        )
        {
          if (validator.isValid(*pParticle, *(*it))) {
            m_connectionSet.insert(
              typename ConnectionSet::value_type(
                pParticle->getID(),
                (*it)->getID(),
                m_prms.getBlockConnectionTag()
              )
            );
          }
        }
      }
      const int numBlockConns = m_connectionSet.size();
      console.Info()
        << "Created " <<  numBlockConns << " connections in "
        << "bonded blocks.\n";

      //
      // Create connections with grains.
      //
      console.Debug()
        << "Prms BBox: " <<  StringUtil::toString(m_prms.getBBox()) << "\n";
      console.Debug()
        << "NTbl BBox: " <<  StringUtil::toString(m_nTablePtr->getBBox()) << "\n";
      for (
        typename GrainRndPackerPtrVector::iterator packerIt = getGougeGeneratorVector().begin();
        packerIt != getGougeGeneratorVector().end();
        packerIt++
      )
      {
        typename GrainRandomPacker::GrainIterator grainIt = (*packerIt)->getGrainIterator();
        while (grainIt.hasNext())
        {
          ConnectionFinder connFinder =
            ConnectionFinder(
              m_prms.getConnectionTolerance(),
              m_prms.getGougeConnectionTag(),
              m_nTablePtr->getBBox(),
              m_nTablePtr->getPeriodicDimensions()
            );
          Grain &g = grainIt.next();
          connFinder.create(g.getParticleIterator());
          typename ConnectionFinder::Iterator connIt = connFinder.getIterator();
          while (connIt.hasNext())
          {
            m_connectionSet.insert(connIt.next());
          }
          if (connFinder.getNumConnections() == 0)
          {
            console.Info()
              << "Found no connections in grain " << g.getId()
              << ":\n";
            typename Grain::ParticleIterator partIt = g.getParticleIterator();
            while (partIt.hasNext())
            {
              console.Info() << StringUtil::toString(partIt.next()) << "\n";
            }
          }
        }
      }
      console.Info()
        << "Created " <<  m_connectionSet.size()-numBlockConns << " connections in "
        << "gouge region.\n";
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    typename GougeConfig<TGPckr,TPPckr,TConn>::ParticleCollection
    GougeConfig<TGPckr,TPPckr,TConn>::getParticleCollection()
    {
      ParticleCollection pCollection(m_particlePoolPtr);
      for (
        typename GeneratorPtrVector::iterator it = m_genPtrVector.begin();
        it != m_genPtrVector.end();
        it++
      )
      {
        ParticleIterator particleIt = (*it)->getParticleIterator();
        while (particleIt.hasNext()) {
          pCollection.insertRef(particleIt.next());
        }
      }

      return pCollection;
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    typename GougeConfig<TGPckr,TPPckr,TConn>::GrainCollection
    GougeConfig<TGPckr,TPPckr,TConn>::getGrainCollection()
    {
      GrainCollection gCollection(m_particlePoolPtr, m_grainPoolPtr);
      for (
        typename GrainRndPackerPtrVector::iterator packerIt =
          getGougeGeneratorVector().begin();
        packerIt != getGougeGeneratorVector().end();
        packerIt++
      )
      {
        GrainIterator grainIt = (*packerIt)->getGrainIterator();
        while (grainIt.hasNext()) {
          gCollection.insertRef(grainIt.next());
        }
      }

      return gCollection;
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    const typename GougeConfig<TGPckr,TPPckr,TConn>::ConnectionSet &
    GougeConfig<TGPckr,TPPckr,TConn>::getConnectionSet() const
    {
      return m_connectionSet;
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    void GougeConfig<TGPckr,TPPckr,TConn>::write(std::ostream &oStream) const
    {
      Vec3 minPt = m_nTablePtr->getBBox().getMinPt();
      Vec3 maxPt = m_nTablePtr->getBBox().getMaxPt();
      if (fabs(maxPt.Z() - minPt.Z()) < (2*m_prms.getMaxRadius())) {
        minPt.Z() = minPt.Z() - m_prms.getMaxRadius() - m_prms.getTolerance();
        maxPt.Z() = maxPt.Z() + m_prms.getMaxRadius() + m_prms.getTolerance();
      }
      
      const BoundingBox geoBBox(minPt, maxPt + m_prms.getTolerance());

      GeometryInfo info = 
        GeometryInfo(
          1.2,
          geoBBox.getMinPt(),
          geoBBox.getMaxPt(),
          m_prms.getPeriodicDimensions(),
          (m_prms.getBBox().getSizes().Z() <= 0.0)
        );
      info.write(oStream);

      /*
       * Some ugliness to ensure that duplicated particles (because of
       * circular boundary conditions) get the correct tag. First, create
       * a set of particles which have already been tagged.
       */
      typename NTable::ParticleIterator particleIt = m_nTablePtr->getParticleIterator();
      typedef std::set<Particle *, IdCompare> ParticleSet;
      ParticleSet taggedParticleSet;
      while (particleIt.hasNext()) {
        taggedParticleSet.insert(particleIt.next());
      }

      /*
       * Now eliminate any particles which were generated with their centre-point
       * lying outside the geo bounding box. Also set the tag in case the particle
       * was a duplicate, created due to circular boundary.
       */
      particleIt = m_nTablePtr->getParticleIterator();
      ParticleSet particleSet;
      while (particleIt.hasNext()) {
        Particle *pParticle = particleIt.next();
        if (geoBBox.contains(pParticle->getPos())) {
          pParticle->setTag((*(taggedParticleSet.find(pParticle)))->getTag());
          particleSet.insert(pParticle);
        }
      }
      
      /*
       * Write particles to the stream.
       */
      oStream 
        << "\n" 
        << "BeginParticles"
        << "\n"
        << "Simple"
        << "\n"
        << particleSet.size()
        << "\n";
      const int precision = 12;
      GeoParticleWriter particleVisitor(oStream, precision);
      for (
        typename ParticleSet::const_iterator it = particleSet.begin();
        it != particleSet.end();
        it++
      )
      {
        particleVisitor.visitParticle(*(*it));
      }

      oStream << "EndParticles\n" << "BeginConnect\n";
      oStream << getConnectionSet().size() << "\n";
      oStream.flush();

      GeoConnectionWriter connectionVisitor(oStream);
      visitConnections(connectionVisitor);
      oStream << "EndConnect";
      oStream.flush();
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    void GougeConfig<TGPckr,TPPckr,TConn>::tagGougeParticles(int tag)
    {
      for (
        typename GrainRndPackerPtrVector::iterator it = m_gougeGenPtrVector.begin();
        it != m_gougeGenPtrVector.end();
        it++
      )
      {
        typename GrainRandomPacker::ParticleIterator particleIt =
          (*it)->getParticleIterator();
        while (particleIt.hasNext()) {
          particleIt.next().setTag(tag);
        }
      }
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    void GougeConfig<TGPckr,TPPckr,TConn>::tagRndBlockParticles(int tag)
    {
      for (
        typename GeneratorPtrVector::iterator it = m_faultGenPtrVector.begin();
        it != m_faultGenPtrVector.end();
        it++
      )
      {
        ParticleIterator particleIt = (*it)->getParticleIterator();
        while (particleIt.hasNext()) {
          particleIt.next().setTag(tag);
        }
      }
    }

    template <typename TGPckr,typename TPPckr,typename TConn>
    void GougeConfig<TGPckr,TPPckr,TConn>::tagDrivingPlateParticles(
      int lowDrivingTag,
      int highDrivingTag,
      double distanceFromBBoxEdge
    )
    {
      ParticleCollection particleCollection = getParticleCollection();
      const BoundingBox bBox = particleCollection.getParticleBBox();
      
      const int    idx     = this->m_prms.getOrientationIndex();
      const double maxLow  = bBox.getMinPt()[idx] + distanceFromBBoxEdge;
      const double minHigh = bBox.getMaxPt()[idx] - distanceFromBBoxEdge;

      int lowTagCount = 0;
      int highTagCount = 0;
      typename ParticleCollection::ParticleIterator particleIt =
        particleCollection.getParticleIterator();
      while (particleIt.hasNext())
      {
        Particle &particle = particleIt.next();
        const double dimPos = particle.getPos()[idx];
        const double radius = particle.getRad();
        
        if (dimPos - radius <= maxLow) {
          particle.setTag(lowDrivingTag);
          lowTagCount++;
        }
        if (dimPos + radius >= minHigh) {
          particle.setTag(highDrivingTag);
          highTagCount++;
        }
      }
      console.Info() << "Tagged " << lowTagCount << " particles with " << lowDrivingTag << "\n";
      console.Info() << "Tagged " << highTagCount << " particles with " << highDrivingTag << "\n";
    }
  }
}