File: UnplacedVolume.cpp

package info (click to toggle)
vecgeom 1.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 23,928 kB
  • sloc: cpp: 88,717; ansic: 6,894; python: 1,035; sh: 582; sql: 538; makefile: 29
file content (331 lines) | stat: -rw-r--r-- 12,482 bytes parent folder | download | duplicates (2)
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
#include "VecGeom/volumes/UnplacedVolume.h"
#include "VecGeom/volumes/PlacedVolume.h"
#include "VecGeom/base/SOA3D.h"
#include "VecGeom/volumes/utilities/VolumeUtilities.h"
#ifdef VECGEOM_ENABLE_CUDA
#include "VecGeom/backend/cuda/Interface.h"
#endif

namespace vecgeom {
inline namespace VECGEOM_IMPL_NAMESPACE {

using Vec3D  = Vector3D<Precision>;
using Real_v = vecgeom::VectorBackend::Real_v;

// generic implementation for SamplePointOnSurface
Vector3D<Precision> VUnplacedVolume::SamplePointOnSurface() const
{
  Vector3D<Precision> surfacepoint;
  SOA3D<Precision> points(1);
  volumeUtilities::FillRandomPoints(*this, points);

  Vector3D<Precision> dir = volumeUtilities::SampleDirection();
  surfacepoint            = points[0] + DistanceToOut(points[0], dir) * dir;

  // assert( Inside(surfacepoint) == vecgeom::kSurface );
  return surfacepoint;
}

// trivial implementations for the interface functions
// (since we are moving to these interfaces only gradually)

// ---------------- DistanceToOut functions -----------------------------------------------------

// the USolid/GEANT4-like interface for DistanceToOut (returning also exiting normal)
VECCORE_ATT_HOST_DEVICE
Precision VUnplacedVolume::DistanceToOut(Vector3D<Precision> const &p, Vector3D<Precision> const &d,
                                         Vector3D<Precision> &normal, bool &convex, Precision step_max) const
{
#ifndef VECCORE_CUDA
  throw std::runtime_error("unimplemented function called");
#endif
  return -1.;
}

// an explicit SIMD interface
VECCORE_ATT_HOST_DEVICE
Real_v VUnplacedVolume::DistanceToOutVec(Vector3D<Real_v> const &p, Vector3D<Real_v> const &d,
                                         Real_v const &step_max) const
{
#ifndef VECCORE_CUDA
  throw std::runtime_error("unimplemented function called");
#endif
  return Real_v(-1.);
}

// the container/basket interface (possibly to be deprecated)
void VUnplacedVolume::DistanceToOut(SOA3D<Precision> const &points, SOA3D<Precision> const &directions,
                                    Precision const *const step_max, Precision *const output) const
{
#ifndef VECCORE_CUDA
  throw std::runtime_error("unimplemented function called");
#endif
}

// ---------------- SafetyToOut functions -----------------------------------------------------
// an explicit SIMD interface
VECCORE_ATT_HOST_DEVICE
Real_v VUnplacedVolume::SafetyToOutVec(Vector3D<Real_v> const &p) const
{
#ifndef VECCORE_CUDA
  throw std::runtime_error("unimplemented function called");
#endif
  return Real_v(-1.);
}

// the container/basket interface (possibly to be deprecated)
void VUnplacedVolume::SafetyToOut(SOA3D<Precision> const &points, Precision *const output) const
{
#ifndef VECCORE_CUDA
  throw std::runtime_error("unimplemented function called");
#endif
}

// ---------------- DistanceToIn functions -----------------------------------------------------
VECCORE_ATT_HOST_DEVICE
Real_v VUnplacedVolume::DistanceToInVec(Vector3D<Real_v> const &position, Vector3D<Real_v> const &direction,
                                        const Real_v &step_max) const
{
#ifndef VECCORE_CUDA
  throw std::runtime_error("unimplemented function called");
#endif
  return Real_v(-1.);
}

// ---------------- SafetyToIn functions -------------------------------------------------------
// explicit SIMD interface
VECCORE_ATT_HOST_DEVICE
Real_v VUnplacedVolume::SafetyToInVec(Vector3D<Real_v> const &p) const
{
#ifndef VECCORE_CUDA
  throw std::runtime_error("unimplemented function called");
#endif
  return Real_v(-1.);
}

// ---------------- Normal ---------------------------------------------------------------------

VECCORE_ATT_HOST_DEVICE
bool VUnplacedVolume::Normal(Vector3D<Precision> const &p, Vector3D<Precision> &normal) const
{
#ifndef VECCORE_CUDA
  throw std::runtime_error("unimplemented function called");
#endif
  return false;
}

// ----------------- Extent --------------------------------------------------------------------
VECCORE_ATT_HOST_DEVICE
void VUnplacedVolume::Extent(Vector3D<Precision> &aMin, Vector3D<Precision> &aMax) const
{
#ifndef VECCORE_CUDA
  throw std::runtime_error("unimplemented function called");
#endif
}

// estimating the surface area by sampling
// based on the six-point method of G4
Precision VUnplacedVolume::EstimateSurfaceArea(int nStat) const
{
  static const Precision s2 = 1. / Sqrt(2.);
  static const Precision s3 = 1. / Sqrt(3.);

  // Predefined directions
  //
  static const Vec3D directions[64] = {
      Vec3D(0, 0, 0),      Vec3D(-1, 0, 0),    // (  ,  ,  ) ( -,  ,  )
      Vec3D(1, 0, 0),      Vec3D(-1, 0, 0),    // ( +,  ,  ) (-+,  ,  )
      Vec3D(0, -1, 0),     Vec3D(-s2, -s2, 0), // (  , -,  ) ( -, -,  )
      Vec3D(s2, -s2, 0),   Vec3D(0, -1, 0),    // ( +, -,  ) (-+, -,  )

      Vec3D(0, 1, 0),      Vec3D(-s2, s2, 0), // (  , +,  ) ( -, +,  )
      Vec3D(s2, s2, 0),    Vec3D(0, 1, 0),    // ( +, +,  ) (-+, +,  )
      Vec3D(0, -1, 0),     Vec3D(-1, 0, 0),   // (  ,-+,  ) ( -,-+,  )
      Vec3D(1, 0, 0),      Vec3D(-1, 0, 0),   // ( +,-+,  ) (-+,-+,  )

      Vec3D(0, 0, -1),     Vec3D(-s2, 0, -s2),   // (  ,  , -) ( -,  , -)
      Vec3D(s2, 0, -s2),   Vec3D(0, 0, -1),      // ( +,  , -) (-+,  , -)
      Vec3D(0, -s2, -s2),  Vec3D(-s3, -s3, -s3), // (  , -, -) ( -, -, -)
      Vec3D(s3, -s3, -s3), Vec3D(0, -s2, -s2),   // ( +, -, -) (-+, -, -)

      Vec3D(0, s2, -s2),   Vec3D(-s3, s3, -s3), // (  , +, -) ( -, +, -)
      Vec3D(s3, s3, -s3),  Vec3D(0, s2, -s2),   // ( +, +, -) (-+, +, -)
      Vec3D(0, 0, -1),     Vec3D(-s2, 0, -s2),  // (  ,-+, -) ( -,-+, -)
      Vec3D(s2, 0, -s2),   Vec3D(0, 0, -1),     // ( +,-+, -) (-+,-+, -)

      Vec3D(0, 0, 1),      Vec3D(-s2, 0, s2),   // (  ,  , +) ( -,  , +)
      Vec3D(s2, 0, s2),    Vec3D(0, 0, 1),      // ( +,  , +) (-+,  , +)
      Vec3D(0, -s2, s2),   Vec3D(-s3, -s3, s3), // (  , -, +) ( -, -, +)
      Vec3D(s3, -s3, s3),  Vec3D(0, -s2, s2),   // ( +, -, +) (-+, -, +)

      Vec3D(0, s2, s2),    Vec3D(-s3, s3, s3), // (  , +, +) ( -, +, +)
      Vec3D(s3, s3, s3),   Vec3D(0, s2, s2),   // ( +, +, +) (-+, +, +)
      Vec3D(0, 0, 1),      Vec3D(-s2, 0, s2),  // (  ,-+, +) ( -,-+, +)
      Vec3D(s2, 0, s2),    Vec3D(0, 0, 1),     // ( +,-+, +) (-+,-+, +)

      Vec3D(0, 0, -1),     Vec3D(-1, 0, 0),    // (  ,  ,-+) ( -,  ,-+)
      Vec3D(1, 0, 0),      Vec3D(-1, 0, 0),    // ( +,  ,-+) (-+,  ,-+)
      Vec3D(0, -1, 0),     Vec3D(-s2, -s2, 0), // (  , -,-+) ( -, -,-+)
      Vec3D(s2, -s2, 0),   Vec3D(0, -1, 0),    // ( +, -,-+) (-+, -,-+)

      Vec3D(0, 1, 0),      Vec3D(-s2, s2, 0), // (  , +,-+) ( -, +,-+)
      Vec3D(s2, s2, 0),    Vec3D(0, 1, 0),    // ( +, +,-+) (-+, +,-+)
      Vec3D(0, -1, 0),     Vec3D(-1, 0, 0),   // (  ,-+,-+) ( -,-+,-+)
      Vec3D(1, 0, 0),      Vec3D(-1, 0, 0),   // ( +,-+,-+) (-+,-+,-+)
  };

  // Get bounding box
  //
  Vec3D bmin, bmax;
  this->Extent(bmin, bmax);
  Vec3D bdim = bmax - bmin;

  // Define statistics and shell thickness
  //
  int npoints      = (nStat < 1000) ? 1000 : nStat;
  Precision coeff  = 0.5 / Cbrt(Precision(npoints));
  Precision eps    = coeff * bdim.Min(); // half thickness
  Precision twoeps = 2. * eps;
  Precision del    = 1.8 * eps; // six-point offset - should be more than sqrt(3.)

  // Enlarge bounding box by eps
  //
  bmin -= Vec3D(eps);
  bdim += Vec3D(twoeps);

  // Calculate surface area
  //
  int icount = 0;
  for (int i = 0; i < npoints; ++i) {
    Precision px = bmin.x() + bdim.x() * RNG::Instance().uniform();
    Precision py = bmin.y() + bdim.y() * RNG::Instance().uniform();
    Precision pz = bmin.z() + bdim.z() * RNG::Instance().uniform();
    Vec3D p(px, py, pz);
    EnumInside in  = this->Inside(p);
    Precision dist = 0;
    if (in == EInside::kInside) {
      if (this->SafetyToOut(p) >= eps) continue;
      int icase = 0;
      if (this->Inside(Vec3D(px - del, py, pz)) != EInside::kInside) icase += 1;
      if (this->Inside(Vec3D(px + del, py, pz)) != EInside::kInside) icase += 2;
      if (this->Inside(Vec3D(px, py - del, pz)) != EInside::kInside) icase += 4;
      if (this->Inside(Vec3D(px, py + del, pz)) != EInside::kInside) icase += 8;
      if (this->Inside(Vec3D(px, py, pz - del)) != EInside::kInside) icase += 16;
      if (this->Inside(Vec3D(px, py, pz + del)) != EInside::kInside) icase += 32;
      if (icase == 0) continue;
      Vec3D v = directions[icase];
      dist    = this->DistanceToOut(p, v);
      Vec3D n;
      this->Normal(p + v * dist, n);
      dist *= v.Dot(n);
    } else if (in == EInside::kOutside) {
      if (this->SafetyToIn(p) >= eps) continue;
      int icase = 0;
      if (this->Inside(Vec3D(px - del, py, pz)) != EInside::kOutside) icase += 1;
      if (this->Inside(Vec3D(px + del, py, pz)) != EInside::kOutside) icase += 2;
      if (this->Inside(Vec3D(px, py - del, pz)) != EInside::kOutside) icase += 4;
      if (this->Inside(Vec3D(px, py + del, pz)) != EInside::kOutside) icase += 8;
      if (this->Inside(Vec3D(px, py, pz - del)) != EInside::kOutside) icase += 16;
      if (this->Inside(Vec3D(px, py, pz + del)) != EInside::kOutside) icase += 32;
      if (icase == 0) continue;
      Vec3D v = directions[icase];
      dist    = this->DistanceToIn(p, v);
      if (dist == kInfLength) continue;
      Vec3D n;
      this->Normal(p + v * dist, n);
      dist *= -(v.Dot(n));
    }
    if (dist < eps) icount++;
  }
  return bdim.x() * bdim.y() * bdim.z() * icount / npoints / twoeps;
}

// estimating the cubic volume by sampling
// based on the method of G4
Precision VUnplacedVolume::EstimateCapacity(int nStat) const
{
  Precision epsilon = 1E-4;

  // limits
  if (nStat < 100) nStat = 100;

  Vector3D<Precision> lower, upper, offset;
  this->Extent(lower, upper);
  offset                        = 0.5 * (upper + lower);
  const Vector3D<Precision> dim = 0.5 * (upper - lower);

  int insidecounter = 0;
  for (int i = 0; i < nStat; i++) {
    auto p = offset + volumeUtilities::SamplePoint(dim);
    if (this->Contains(p)) insidecounter++;
  }
  return 8. * (dim[0] + epsilon) * (dim[1] + epsilon) * (dim[2] + epsilon) * insidecounter / nStat;
}

std::ostream &operator<<(std::ostream &os, VUnplacedVolume const &vol)
{
  vol.Print(os);
  return os;
}

#ifndef VECCORE_CUDA

VPlacedVolume *VUnplacedVolume::PlaceVolume(LogicalVolume const *const volume,
                                            Transformation3D const *const transformation,
                                            VPlacedVolume *const placement) const
{

  const TranslationCode trans_code = transformation->GenerateTranslationCode();
  const RotationCode rot_code      = transformation->GenerateRotationCode();

  return SpecializedVolume(volume, transformation, trans_code, rot_code, placement);
}

VPlacedVolume *VUnplacedVolume::PlaceVolume(char const *const label, LogicalVolume const *const volume,
                                            Transformation3D const *const transformation,
                                            VPlacedVolume *const placement) const
{
  VPlacedVolume *const placed = PlaceVolume(volume, transformation, placement);
  placed->set_label(label);
  return placed;
}

#endif

#ifdef VECGEOM_CUDA_INTERFACE
void VUnplacedVolume::CopyBBoxesToGpu(const std::vector<VUnplacedVolume const *> &volumes,
                                      const std::vector<DevicePtr<cuda::VUnplacedVolume>> &gpu_ptrs)
{
  assert(volumes.size() == gpu_ptrs.size() && "Unequal CPU/GPU vectors for copying bounding boxes.");
  // Copy boxes data in a contiguous array, box icrt starting at index 6*icrt
  std::vector<Precision> boxesData(6 * gpu_ptrs.size());
  int icrt = 0;
  for (auto vol : volumes) {
    Vector3D<Precision> amin, amax;
    vol->GetBBox(amin, amax);
    assert((amax - amin).Mag() > 0);
    for (unsigned int i = 0; i < 3; ++i) {
      boxesData[6 * icrt + i]     = amin[i];
      boxesData[6 * icrt + i + 3] = amax[i];
    }
    icrt++;
  }
  // Dispatch to the GPU interface helper
  CopyBBoxesToGpuImpl<cuda::VUnplacedVolume, DevicePtr<cuda::VUnplacedVolume>>(gpu_ptrs.size(), gpu_ptrs.data(),
                                                                               boxesData.data());
}
#endif

} // namespace VECGEOM_IMPL_NAMESPACE
#ifdef VECCORE_CUDA

namespace cxx {

template void CopyBBoxesToGpuImpl<cuda::VUnplacedVolume, DevicePtr<cuda::VUnplacedVolume>>(
    std::size_t, DevicePtr<cuda::VUnplacedVolume> const *, cuda::Precision *);

} // namespace cxx

#endif // VECCORE_CUDA

} // namespace vecgeom