File: UnplacedTorus2.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 (302 lines) | stat: -rw-r--r-- 10,924 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
/// \file UnplacedTorus2.cpp

#include "VecGeom/volumes/UnplacedTorus2.h"
#include "VecGeom/volumes/SpecializedTorus2.h"

#include "VecGeom/volumes/utilities/VolumeUtilities.h"
#include "VecGeom/management/VolumeFactory.h"

namespace vecgeom {
inline namespace VECGEOM_IMPL_NAMESPACE {

VECCORE_ATT_HOST_DEVICE
void UnplacedTorus2::Print() const
{
  printf("UnplacedTorus2 {%.2f, %.2f, %.2f, %.2f, %.2f}", rmin(), rmax(), rtor(), sphi(), dphi());
}

void UnplacedTorus2::Print(std::ostream &os) const
{
  os << "UnplacedTorus2 {" << rmin() << ", " << rmax() << ", " << rtor() << ", " << sphi() << ", " << dphi();
}

#ifndef VECCORE_CUDA
SolidMesh *UnplacedTorus2::CreateMesh3D(Transformation3D const &trans, size_t nSegments) const
{
  typedef Vector3D<Precision> Vec_t;
  bool isFull   = dphi() == 2 * kPi;
  SolidMesh *sm = new SolidMesh();

  sm->ResetMesh(2 * (nSegments + 1) * (nSegments + 1), (nSegments * nSegments) + 2);
  Vec_t *vertices = new Vec_t[2 * (nSegments + 1) * (nSegments + 1)];

  Precision phi_step   = dphi() / nSegments;
  Precision theta_step = 2 * M_PI / nSegments;
  Precision phi        = sphi();
  Precision theta      = 0.;
  Precision intermediate1, intermediate2;

  for (size_t p = 0, i = 0; i <= nSegments; ++i, theta += theta_step, phi = sphi()) {
    intermediate1 = (rtor() + rmax() * std::cos(theta));
    intermediate2 = (rtor() + rmin() * std::cos(theta));
    for (size_t j = 0; j <= nSegments; ++j, p++, phi += phi_step) {
      vertices[p] = Vec_t(intermediate1 * std::cos(phi), intermediate1 * std::sin(phi), rmax() * std::sin(theta));
      vertices[p + (nSegments + 1) * (nSegments + 1)] =
          Vec_t(intermediate2 * std::cos(phi), intermediate2 * std::sin(phi), rmin() * std::sin(theta));
    }
  }

  sm->SetVertices(vertices, 2 * (nSegments + 1) * (nSegments + 1));
  delete[] vertices;
  sm->TransformVertices(trans);

  // outer surface
  for (size_t j = 0, k = 0; j < nSegments; j++, k++) {
    for (size_t i = 0, l = k + nSegments + 1; i < nSegments; i++, k++, l++) {
      sm->AddPolygon(4, {l + 1, l, k, k + 1}, true);
    }
  }
  // inner surface
  for (size_t j = 0, k = (nSegments + 1) * (nSegments + 1); j < nSegments; j++, k++) {
    for (size_t i = 0, l = k + nSegments + 1; i < nSegments; i++, k++, l++) {
      sm->AddPolygon(4, {k + 1, k, l, l + 1}, true);
    }
  }

  // surfaces due to torus not being full
  if (!isFull) {

    for (size_t i = 0, j = 0; i < nSegments; i++, j += nSegments + 1) {
      sm->AddPolygon(4,
                     {j, j + nSegments + 1, j + nSegments + 1 + (nSegments + 1) * (nSegments + 1),
                      j + (nSegments + 1) * (nSegments + 1)},
                     true);
    }

    for (size_t i = 0, j = nSegments; i < nSegments; i++, j += nSegments + 1) {
      sm->AddPolygon(4,
                     {j + (nSegments + 1) * (nSegments + 1), j + (nSegments + 1) * (nSegments + 1) + nSegments + 1,
                      j + nSegments + 1, j},
                     true);
    }
  }

  return sm;
}
#endif

#ifndef VECCORE_CUDA
VPlacedVolume *UnplacedTorus2::SpecializedVolume(LogicalVolume const *const volume,
                                                 Transformation3D const *const transformation,
                                                 const TranslationCode trans_code, const RotationCode rot_code,
                                                 VPlacedVolume *const placement) const
{
  return VolumeFactory::CreateByTransformation<UnplacedTorus2>(volume, transformation, trans_code, rot_code, placement);
}
#else
__device__ VPlacedVolume *UnplacedTorus2::SpecializedVolume(LogicalVolume const *const volume,
                                                            Transformation3D const *const transformation,
                                                            const TranslationCode trans_code,
                                                            const RotationCode rot_code, const int id,
                                                            const int copy_no, const int child_id,
                                                            VPlacedVolume *const placement) const
{
  return VolumeFactory::CreateByTransformation<UnplacedTorus2>(volume, transformation, trans_code, rot_code, id,
                                                               copy_no, child_id, placement);
}
#endif

Vector3D<Precision> UnplacedTorus2::SamplePointOnSurface() const
{
  // taken from Geant4
  Precision cosu, sinu, cosv, sinv, aOut, aIn, aSide, chose, phi, theta, rRand;

  phi   = RNG::Instance().uniform(fTorus.fSphi, fTorus.fSphi + fTorus.fDphi);
  theta = RNG::Instance().uniform(0., vecgeom::kTwoPi);

  cosu = std::cos(phi);
  sinu = std::sin(phi);
  cosv = std::cos(theta);
  sinv = std::sin(theta);

  // compute the areas

  aOut  = (fTorus.fDphi) * vecgeom::kTwoPi * fTorus.fRtor * fTorus.fRmax;
  aIn   = (fTorus.fDphi) * vecgeom::kTwoPi * fTorus.fRtor * fTorus.fRmin;
  aSide = vecgeom::kPi * (fTorus.fRmax * fTorus.fRmax - fTorus.fRmin * fTorus.fRmin);

  if ((fTorus.fSphi == 0.) && (fTorus.fDphi == vecgeom::kTwoPi)) {
    aSide = 0;
  }
  chose = RNG::Instance().uniform(0., aOut + aIn + 2. * aSide);

  if (chose < aOut) {
    return Vector3D<Precision>((fTorus.fRtor + fTorus.fRmax * cosv) * cosu, (fTorus.fRtor + fTorus.fRmax * cosv) * sinu,
                               fTorus.fRmax * sinv);
  } else if ((chose >= aOut) && (chose < aOut + aIn)) {
    return Vector3D<Precision>((fTorus.fRtor + fTorus.fRmin * cosv) * cosu, (fTorus.fRtor + fTorus.fRmin * cosv) * sinu,
                               fTorus.fRmin * sinv);
  } else if ((chose >= aOut + aIn) && (chose < aOut + aIn + aSide)) {
    rRand = volumeUtilities::GetRadiusInRing(fTorus.fRmin, fTorus.fRmax);
    return Vector3D<Precision>((fTorus.fRtor + rRand * cosv) * std::cos(fTorus.fSphi),
                               (fTorus.fRtor + rRand * cosv) * std::sin(fTorus.fSphi), rRand * sinv);
  } else {
    rRand = volumeUtilities::GetRadiusInRing(fTorus.fRmin, fTorus.fRmax);
    return Vector3D<Precision>((fTorus.fRtor + rRand * cosv) * std::cos(fTorus.fSphi + fTorus.fDphi),
                               (fTorus.fRtor + rRand * cosv) * std::sin(fTorus.fSphi + fTorus.fDphi), rRand * sinv);
  }
}

VECCORE_ATT_HOST_DEVICE
void UnplacedTorus2::DetectConvexity()
{
  // Default safe convexity value
  fGlobalConvexity = false;

  // Logic to calculate the convexity
  if (fTorus.fRtor == 0.) {   // This will turn Torus2 to Spherical Shell
    if (fTorus.fRmin == 0.) { // This will turn the Spherical shell to Orb
      if (fTorus.fDphi <= kPi || fTorus.fDphi == kTwoPi) fGlobalConvexity = true;
    }
  }
}

template <TranslationCode transCodeT, RotationCode rotCodeT>
VECCORE_ATT_DEVICE
VPlacedVolume *UnplacedTorus2::Create(LogicalVolume const *const logical_volume,
                                      Transformation3D const *const transformation,
#ifdef VECCORE_CUDA
                                      const int id, const int copy_no, const int child_id,
#endif
                                      VPlacedVolume *const placement)
{
  (void)placement;
  return new SimpleTorus2(logical_volume, transformation
#ifdef VECCORE_CUDA
                          ,
                          id, copy_no, child_id
#endif
  );
}

#ifdef VECGEOM_CUDA_INTERFACE

DevicePtr<cuda::VUnplacedVolume> UnplacedTorus2::CopyToGpu(DevicePtr<cuda::VUnplacedVolume> const in_gpu_ptr) const
{
  return CopyToGpuImpl<UnplacedTorus2>(in_gpu_ptr, fTorus.fRmin, fTorus.fRmax, fTorus.fRtor, fTorus.fSphi,
                                       fTorus.fDphi);
}

DevicePtr<cuda::VUnplacedVolume> UnplacedTorus2::CopyToGpu() const
{
  return CopyToGpuImpl<UnplacedTorus2>();
}

#endif // VECGEOM_CUDA_INTERFACE

// Return unit normal of surface closest to p
// - note if point on z axis, ignore phi divided sides
// - unsafe if point close to z axis a rmin=0 - no explicit checks
VECCORE_ATT_HOST_DEVICE
bool UnplacedTorus2::Normal(Vector3D<Precision> const &point, Vector3D<Precision> &norm) const
{

  int noSurfaces = 0;
  bool valid     = true;

  Precision rho2, rho, pt2, pt, pPhi;
  Precision distRMin = kInfLength;
  Precision distSPhi = kInfLength, distEPhi = kInfLength;

  // To cope with precision loss
  //
  const Precision delta  = Max(10.0 * kTolerance, 1.0e-8 * (fTorus.fRtor + fTorus.fRmax));
  const Precision dAngle = 10.0 * kTolerance;

  Vector3D<Precision> nR, nPs, nPe;
  Vector3D<Precision> sumnorm(0., 0., 0.);

  rho2 = point.x() * point.x() + point.y() * point.y();
  rho  = Sqrt(rho2);
  pt2  = rho2 + point.z() * point.z() + fTorus.fRtor * (fTorus.fRtor - 2 * rho);
  pt2  = Max(pt2, Precision(0.)); // std::fabs(pt2);
  pt   = Sqrt(pt2);

  Precision distRMax = Abs(pt - fTorus.fRmax);
  if (fTorus.fRmin) distRMin = Abs(pt - fTorus.fRmin);

  if (rho > delta && pt != Precision(0.)) {
    Precision redFactor = (rho - fTorus.fRtor) / rho;
    nR                  = Vector3D<Precision>(point.x() * redFactor, // p.x()*(1.-fRtor/rho),
                             point.y() * redFactor, // p.y()*(1.-fRtor/rho),
                             point.z());
    nR *= Precision(1.) / pt;
  }

  if (fTorus.fDphi < kTwoPi) // && rho ) // old limitation against (0,0,z)
  {
    if (rho) {
      pPhi = std::atan2(point.y(), point.x());

      if (pPhi < fTorus.fSphi - delta) {
        pPhi += kTwoPi;
      } else if (pPhi > fTorus.fSphi + fTorus.fDphi + delta) {
        pPhi -= kTwoPi;
      }

      distSPhi = Abs(pPhi - fTorus.fSphi);
      distEPhi = Abs(pPhi - fTorus.fSphi - fTorus.fDphi);
    }
    nPs = Vector3D<Precision>(sin(fTorus.fSphi), -cos(fTorus.fSphi), 0);
    nPe = Vector3D<Precision>(-sin(fTorus.fSphi + fTorus.fDphi), cos(fTorus.fSphi + fTorus.fDphi), 0);
  }
  if (distRMax <= delta) {
    noSurfaces++;
    sumnorm += nR;
  } else if (fTorus.fRmin && (distRMin <= delta)) // Must not be on both Outer and Inner
  {
    noSurfaces++;
    sumnorm -= nR;
  }

  //  To be on one of the 'phi' surfaces,
  //  it must be within the 'tube' - with tolerance

  if ((fTorus.fDphi < kTwoPi) && (fTorus.fRmin - delta <= pt) && (pt <= (fTorus.fRmax + delta))) {
    if (distSPhi <= dAngle) {
      noSurfaces++;
      sumnorm += nPs;
    }
    if (distEPhi <= dAngle) {
      noSurfaces++;
      sumnorm += nPe;
    }
  }
  if (noSurfaces == 0) {

    valid = false;
  } else if (noSurfaces == 1) {
    norm = sumnorm;
  } else {
    norm = sumnorm.Unit();
  }

  return valid;
}

} // namespace VECGEOM_IMPL_NAMESPACE

#ifdef VECCORE_CUDA

namespace cxx {

template size_t DevicePtr<cuda::UnplacedTorus2>::SizeOf();
template void DevicePtr<cuda::UnplacedTorus2>::Construct(const Precision rmin, const Precision rmax,
                                                         const Precision rtor, const Precision sphi,
                                                         const Precision dphi) const;

} // namespace cxx

#endif

} // namespace vecgeom