File: InteractionToStressConverter.cpp

package info (click to toggle)
esys-particle 2.3.4%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,036 kB
  • ctags: 10,805
  • sloc: cpp: 80,009; python: 5,872; makefile: 1,243; sh: 313; perl: 225
file content (541 lines) | stat: -rw-r--r-- 17,562 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
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2014 by The University of Queensland //
// Centre for Geoscience Computing                         //
// http://earth.uq.edu.au/centre-geoscience-computing      //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.apache.org/licenses/LICENSE-2.0          //
//                                                         //
/////////////////////////////////////////////////////////////


#include "Tools/StressCalculator/InteractionToStressConverter.h"
#include "Tools/StressCalculator/VtkStructuredGrid.h"
#include "Tools/StressCalculator/GaussianGridder.h"
#include "Tools/StressCalculator/Raw2InteractionReader.h"
#include "Tools/StressCalculator/ContactCollection.h"
#include "Tools/StressCalculator/EigenvalueCalculator.h"

#include "Geometry/SphereBoxVolCalculator.h"
#include "Geometry/CircleBoxVolCalculator.h"

namespace esys
{
  namespace lsm
  {
    template <typename TmplSphere, typename TmplBox>
    std::string getDetailsString(const TmplSphere &sphere, const TmplBox &box)
    {
      std::stringstream msg;
      msg
        << "box = ("
        << box.getMinPt() << ", " << box.getMaxPt() << ")"
        << ", sphere = (" << sphere.getCentre() << ", " << sphere.getRadius() << ")";
      return msg.str();
    }

    template <typename TmplSphere, typename TmplBox>
    void checkIntersectionVolume(double vol, const TmplSphere &sphere, const TmplBox &box)
    {
      if (isnan(vol))
      {
        std::stringstream msg;
        msg 
          << "nan encountered during volume calculation: "
          << getDetailsString(sphere, box);

        throw std::runtime_error(msg.str());
      }
      else if ((vol < 0.0) && (fabs(vol) > 1.0e-6))
      {
        std::stringstream msg;
        msg 
          << "Negative intersection volume " << vol 
          << ". " << getDetailsString(sphere, box);
        throw std::runtime_error(msg.str());
      }
      else if (vol > (box.getVolume() + (box.getVolume()*1.0e-6)))
      {
        std::stringstream msg;
        msg 
          << "Volume " << vol 
          << " larger than box volume " << box.getVolume()
          << ". " << getDetailsString(sphere, box);
        throw std::runtime_error(msg.str());
      }
      else if (vol > (sphere.getVolume() + (sphere.getVolume()*1.0e-6)))
      {
        std::stringstream msg;
        msg 
          << "Volume " << vol
          << " larger than sphere volume " << sphere.getVolume()
          << ". " << getDetailsString(sphere, box);
        throw std::runtime_error(msg.str());
      }
    }

    class ThreeDIntersectionCalker : public SphereBoxVolCalculator
    {
    public:
      ThreeDIntersectionCalker(const BoundingBox &box)
        : SphereBoxVolCalculator(Box(box.getMinPt(), box.getMaxPt()))
      {
      }

      double getVolume(const Sphere &sphere)
      {
        const double vol = SphereBoxVolCalculator::getVolume(sphere);
        checkIntersectionVolume(vol, sphere, getBox());
        return vol;
      }
    };

    class TwoDIntersectionCalker  : public CircleBoxVolCalculator
    {
    public:
      TwoDIntersectionCalker(const BoundingBox &box)
        :  CircleBoxVolCalculator(Box(box.getMinPt(), box.getMaxPt()))
      {
      }

      double getVolume(const Sphere &sphere)
      {
        const double vol = CircleBoxVolCalculator::getVolume(sphere);
        checkIntersectionVolume(vol, sphere, getBox());
        return vol;
      }
    };

    class PointDataType
      : public
          vtk::DataTypeTuple<
            vtk::Float64Type,
            vtk::Float64Type,
            vtk::Matrix3Type,
            vtk::Float64Type
          >
    {
    public:
      typedef
        vtk::DataTypeTuple<
          vtk::Float64Type,
          vtk::Float64Type,
          vtk::Matrix3Type,
          vtk::Float64Type
        > Inherited;

      PointDataType()
        : Inherited(
            vtk::Float64Type("|sMax-sMin|"),
            vtk::Float64Type("Real(sMax-sMin)"),
            vtk::Matrix3Type("stressTensor"),
            vtk::Float64Type("radius")
          )
      {
      }
    };

    class PointDataTypeForGrid
      : public
          vtk::DataTypeTuple<
            vtk::Float64Type,
            vtk::Float64Type,
            vtk::Matrix3Type
          >
    {
    public:
      typedef
        vtk::DataTypeTuple<
          vtk::Float64Type,
          vtk::Float64Type,
          vtk::Matrix3Type
        > Inherited;

      PointDataTypeForGrid()
        : Inherited(
            vtk::Float64Type("|sMax-sMin|"),
            vtk::Float64Type("Real(sMax-sMin)"),
            vtk::Matrix3Type("stressTensor")
          )
      {
      }


    };

    typedef vtk::Vec3Type                                    PointType;
    typedef vtk::UnstructuredPiece<PointType, PointDataType> Piece;
    typedef vtk::UnstructuredPiece<PointType, PointDataTypeForGrid> PieceForGrid;
    typedef EigenvalueCalculator::ComplexRealImagComparer    RealImagComparer;
    typedef EigenvalueCalculator::ComplexAbsRealImagComparer AbsRealImagComparer;
    typedef EigenvalueCalculator::ComplexNormComparer        NormComparer;

    InteractionToStressConverter::InteractionToStressConverter(const BoundingBox &bBox, double gridSpacing)
      : m_gridSpacing(gridSpacing),
        m_bBox(bBox),
        m_stressCalculator(),
        m_stressTensorCollection(m_stressCalculator),
        m_regTensorGrid(bBox, gridSpacing),
        m_regDevStressGrid(bBox, gridSpacing),
        m_irrStressTensorGrid(bBox, gridSpacing)
    {
    }

    double InteractionToStressConverter::getRealDevStress(const Tensor &stressTensor) const
    {
      StressTensor::ComplexVector eigenvals = stressTensor.getEigenvalues();
      double devStress = 0.0;

      if (is3d())
      {
        std::sort(eigenvals.begin(), eigenvals.end(), RealImagComparer());
        devStress = (eigenvals[2].real() - eigenvals[0].real());
      }
      else
      {
        //
        // In 2d, make sure we only ever subtract \sigma_1 and \sigma_2,
        // so we sort by absolute value. This ensures that the zero-eigenvalue
        // (corresponding to the z-dimension) will be sorted into the
        // eigenvals[0] element.
        //
        std::sort(eigenvals.begin(), eigenvals.end(), AbsRealImagComparer());
        devStress = fabs(eigenvals[2].real() - eigenvals[1].real());
      }

      return devStress;
    }

    double InteractionToStressConverter::getNormDevStress(const Tensor &stressTensor) const
    {
      StressTensor::ComplexVector eigenvals = stressTensor.getEigenvalues();
      std::sort(eigenvals.begin(), eigenvals.end(), NormComparer());
      
      return
        (
          is3d()
          ?
          std::norm(eigenvals[2] - eigenvals[0])
          :
          std::norm(eigenvals[2] - eigenvals[1])
        );
    }

    bool InteractionToStressConverter::is3d() const
    {
      return ParticleData::is3d();
    }

    void InteractionToStressConverter::addRaw2Interactions(std::istream &iStream)
    {
      Raw2InteractionReader reader(iStream);
      ContactCollection contacts;
      contacts.addInteractions(reader);

      m_stressTensorCollection.addContactIterators(contacts.getContactIteratorIterator());
    }

    void InteractionToStressConverter::writeVtkUnstructuredXml(std::ostream &oStream)
    {
      StressTensCollection::StressTensorIterator it = m_stressTensorCollection.getIterator();
      Piece piece(PointType("points"), PointDataType());
      while (it.hasNext()) {
        StressTensCollection::StressTensorIterator::reference stressTensor = it.next();

        const double realDevStress = getRealDevStress(stressTensor);
        const double normDevStress = getNormDevStress(stressTensor);

        piece.setPoint(
          stressTensor.getPos(),
          PointDataType::DataValueTuple(
            normDevStress,
            realDevStress,
            stressTensor.getTensor(),
            stressTensor.getRad()
          )
        );
      }
      // Write the xml document header
      oStream << "<?xml version=\"1.0\"?>" << std::endl;
      vtk::UnstructuredGrid grid;
      grid.addPiece(piece);
      grid.writeXml(oStream);      
    }

    void InteractionToStressConverter::writeVtkUnstructuredXmlGridInformation(std::ostream &oStream)
    {
      TensorGrid &regularGrid = getTensorRegularGrid();
      TensorGrid::CellIterator cellIt = regularGrid.getCellIterator();
      PieceForGrid piece(PointType("points"), PointDataTypeForGrid());

      while (cellIt.hasNext())
      {
        TensorGrid::Cell::Iterator posValIt = cellIt.next().getIterator();
        while (posValIt.hasNext())
        {
          const TensorGrid::Cell::PosValuePair &pair = posValIt.next();
          const double realDevStress = getRealDevStress(pair.getValue());
          const double normDevStress = getNormDevStress(pair.getValue());

          piece.setPoint(
            pair.getPos(),
            PointDataTypeForGrid::DataValueTuple(
              normDevStress,
              realDevStress,
              pair.getValue().getTensor()
            )
          );
        }
      }
      // Write the xml document header
      oStream << "<?xml version=\"1.0\"?>" << std::endl;
      vtk::UnstructuredGrid grid;
      grid.addPiece(piece);
      grid.writeXml(oStream);
    }

    void InteractionToStressConverter::writeUnstructuredDx(std::ostream &oStream)
    {
      oStream << "points = " << m_stressTensorCollection.size() << std::endl;
      oStream << "format = ascii" << std::endl;
      oStream << "dependency = positions, positions" << std::endl;
      oStream << "interleaving = field" << std::endl;
      oStream << "field = locations, principleStressDiff" << std::endl;
      oStream << "structure = 3-vector, scalar" << std::endl;
      oStream << "type = float, float" << std::endl;
      oStream << "header = marker \"Start\\n\"" << std::endl << std::endl;
      oStream << "end" << std::endl;
      oStream << "Start" << std::endl;
      StressTensCollection::StressTensorIterator it = m_stressTensorCollection.getIterator();
      while (it.hasNext()) {
        StressTensCollection::StressTensorIterator::reference stressTensor = it.next();
        const double val = getRealDevStress(stressTensor);
        oStream << stressTensor.getPos() << " " << val << "\n";
      }
    }

    void InteractionToStressConverter::writeFlatUnstructured(std::ostream &oStream)
    {
      StressTensCollection::StressTensorIterator it = m_stressTensorCollection.getIterator();
      while (it.hasNext()) {
        StressTensCollection::StressTensorIterator::reference stressTensor = it.next();
        const double val = getRealDevStress(stressTensor);
        oStream
          << stressTensor.getPos() << " "
          << stressTensor.getRad() << " "
          << val << "\n";
      }
    }

    class StrctPointDataType
      : public
          vtk::DataTypeTuple<
            vtk::Float64Type
          >
    {
    public:
      typedef
        vtk::DataTypeTuple<
          vtk::Float64Type
        > Inherited;

      StrctPointDataType()
        : Inherited(
            vtk::Float64Type("sMax-sMin")
          )
      {
      }
    };

    typedef vtk::Vec3Type                                            StrctPointType;
    typedef vtk::StructuredPiece<StrctPointType, StrctPointDataType> StrctPiece;

    StressTensorPtrGrid &InteractionToStressConverter::getTensorIrregularGrid()
    {
      if (m_irrStressTensorGrid.size() <= 0)
      {
        calcTensorIrregularGrid();
      }
      return m_irrStressTensorGrid;
    }

    double InteractionToStressConverter::getMaxRadius()
    {
      StressTensorPtrGrid::ValueIterator it = getTensorIrregularGrid().getValueIterator();
      double maxRadius = -1.0;

      while (it.hasNext())
      {
        StressTensorPtrGrid::const_reference stressTensor = it.next();
        if (stressTensor->getRad() > maxRadius)
        {
          maxRadius = stressTensor->getRad();
        }
      }
      return maxRadius;
    }

    void InteractionToStressConverter::calcTensorIrregularGrid()
    {
      m_irrStressTensorGrid = StressTensorPtrGrid(m_bBox, m_gridSpacing);
      StressTensCollection::StressTensorIterator it = m_stressTensorCollection.getIterator();

      while (it.hasNext()) {
        StressTensCollection::StressTensorIterator::reference stressTensor = it.next();
        m_irrStressTensorGrid.insert(stressTensor.getPos(), &stressTensor);
      }
    }

    TensorGrid &InteractionToStressConverter::getTensorRegularGrid()
    {
      if (m_regTensorGrid.size() <= 0)
      {
        calcTensorRegularGrid();
      }
      return m_regTensorGrid;
    }

    template <typename TmplCellIterator, typename TmplIntsectVolCalker>
    Matrix3 getBoxTensor(
      TmplCellIterator cellIt,
      TmplIntsectVolCalker intersectCalker
    )
    {
      Matrix3 tensor;
      while (cellIt.hasNext())
      {
        typename TmplCellIterator::value_type::Iterator pairIt = cellIt.next().getIterator();
        while (pairIt.hasNext())
        {
          const StressTensor *stressTensor = pairIt.next().getValue();
          const typename TmplIntsectVolCalker::Sphere sphere(stressTensor->getPos(), stressTensor->getRad());
          const double intersectVol = intersectCalker.getVolume(sphere);
//          std::cout << "sphere: " << sphere.getCentre() << ", " << sphere.getRadius() << ", vol: " << intersectVol << std::endl;
          tensor += ((stressTensor->getTensor())*intersectVol);
        }
      }
      //
      // Return the average stress over the cell.
      //
      return tensor*(1.0/intersectCalker.getBox().getVolume());
    }

    void InteractionToStressConverter::calcTensorRegularGrid()
    {
      StressTensorPtrGrid &irregularGrid = getTensorIrregularGrid();

      m_regTensorGrid = TensorGrid(m_bBox, m_gridSpacing);

      const double maxRadius = (getMaxRadius() + m_gridSpacing);

      TensorGrid::CellIterator regCellIt = m_regTensorGrid.getCellIterator();
      while (regCellIt.hasNext())
      {
        TensorGrid::Cell &regCell = regCellIt.next();
        StressTensorPtrGrid::CellIterator irrCellIt = irregularGrid.getCellIterator(regCell.getPos(), maxRadius);

        if (is3d())
        {
          m_regTensorGrid.insert(
            regCell.getPos(),
            Tensor(
              regCell.getPos(),
              getBoxTensor(
                irrCellIt,
                ThreeDIntersectionCalker(regCell.getBox())
              )
            )
          );
        }
        else
        {
          m_regTensorGrid.insert(
            regCell.getPos(),
            Tensor(
              regCell.getPos(),
              getBoxTensor(
                irrCellIt,
                TwoDIntersectionCalker(regCell.getBox())
              )
            )
          );
        }
      }
    }

    DoubleGrid &InteractionToStressConverter::getDevRegularGrid()
    {
      if (m_regDevStressGrid.size() <= 0)
      {
        calcDevRegularGrid();
      }
      return m_regDevStressGrid;
    }

    void InteractionToStressConverter::calcDevRegularGrid()
    {
      const TensorGrid &regularTensorGrid = getTensorRegularGrid();
      m_regDevStressGrid = DoubleGrid(m_bBox, m_gridSpacing);

      TensorGrid::CellConstIterator cellIt = regularTensorGrid.getCellIterator();
      while (cellIt.hasNext())
      {
        TensorGrid::Cell::ConstIterator pairIt = cellIt.next().getIterator();
        while (pairIt.hasNext())
        {
          const Tensor &tensor = pairIt.next().getValue();
          m_regDevStressGrid.insert(
            tensor.getPos(),
            getRealDevStress(tensor)
          );
        }
      }
    }

    void InteractionToStressConverter::writeVtkStructuredXml(std::ostream &oStream)
    {
      TensorGrid &regularGrid = getTensorRegularGrid();
      StrctPiece piece(StrctPointType("points"), StrctPointDataType());
      piece.setExtent(regularGrid.getMinVecIndex(), regularGrid.getMaxVecIndex());
      TensorGrid::CellIterator cellIt = regularGrid.getCellIterator();
      while (cellIt.hasNext())
      {
        TensorGrid::Cell::Iterator posValIt = cellIt.next().getIterator();
        while (posValIt.hasNext())
        {
          const TensorGrid::Cell::PosValuePair &pair = posValIt.next();
          piece.setPoint(
            pair.getPos(),
            StrctPiece::PointData(
              getRealDevStress(pair.getValue())
              //pair.getValue().getTensor()
            )
          );
        }
      }
      // Write the xml document header
      oStream << "<?xml version=\"1.0\"?>" << std::endl;
      vtk::StructuredGrid grid;
      grid.setExtent(piece.getMinExtent(), piece.getMaxExtent());      
      grid.addPiece(piece);
      grid.writeXml(oStream);
    }
    
    void InteractionToStressConverter::writeFlatStructured(std::ostream &oStream)
    {
      DoubleGrid regular = getDevRegularGrid();
      DoubleGrid::CellIterator cellIt = regular.getCellIterator();
      while (cellIt.hasNext())
      {
        DoubleGrid::Cell::Iterator posValIt = cellIt.next().getIterator();
        while (posValIt.hasNext())
        {
          const DoubleGrid::Cell::PosValuePair &pair = posValIt.next();
          oStream << pair.getPos() << " " << pair.getValue() << "\n";
        }
      }
    }
  }
}