UnplacedGenericPolycone.cpp 11.2 KB
Newer Older
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
/// @file UnplacedGenericPolycone.cpp
/// @author Raman Sehgal (raman.sehgal@cern.ch)

#include "volumes/Cone.h"
#include "volumes/UnplacedCoaxialCones.h"
#include "management/GeoManager.h"
#include "volumes/UnplacedGenericPolycone.h"
#include "management/VolumeFactory.h"
#include "volumes/SpecializedGenericPolycone.h"
#include "base/RNG.h"
#include <stdio.h>
#include <cmath>

namespace vecgeom {
inline namespace VECGEOM_IMPL_NAMESPACE {

/*
 * All the required Parametric Constructor
 */

VECCORE_ATT_HOST_DEVICE
UnplacedGenericPolycone::UnplacedGenericPolycone(Precision phiStart, // initial phi starting angle
                                                 Precision phiTotal, // total phi angle
                                                 int numRZ, // number corners in r,z space (must be an even number)
                                                 Precision const *r, // r coordinate of these corners
                                                 Precision const *z)
{
28
29
30
31
  fGlobalConvexity = false;
  fSPhi            = phiStart;
  fDPhi            = phiTotal;
  fNumRZ           = numRZ;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  for (int i = 0; i < numRZ; i++) {
    fR.push_back(r[i]);
    fZ.push_back(z[i]);
  }

  Vector<Vector2D<Precision>> rzVect;
  for (int i = 0; i < numRZ; i++) {
    rzVect.push_back(Vector2D<double>(r[i], z[i]));
  }
  ReducedPolycone rd(rzVect);
  Vector<Precision> zS;
  Vector<Vector<Precision>> vectOfRmin1Vect;
  Vector<Vector<Precision>> vectOfRmax1Vect;
  Vector<Vector<Precision>> vectOfRmin2Vect;
  Vector<Vector<Precision>> vectOfRmax2Vect;
  rd.GetPolyconeParameters(vectOfRmin1Vect, vectOfRmax1Vect, vectOfRmin2Vect, vectOfRmax2Vect, zS, fAMin, fAMax);

  fGenericPolycone.Set(vectOfRmin1Vect, vectOfRmax1Vect, vectOfRmin2Vect, vectOfRmax2Vect, zS, fSPhi, fDPhi);
}

52
VECCORE_ATT_HOST_DEVICE
53
54
void UnplacedGenericPolycone::Extent(Vector3D<Precision> &aMin, Vector3D<Precision> &aMax) const
{
55
  /*  Algo : Extent along Z direction will be Z[0] and Z[max]
56
57
58
59
60
61
62
63
64
   *         Extent along X and Y will be from the Extent of
   *         cone/tube formed with Maximum outer radius
   */
  Precision rmax = 0.;
  for (unsigned int i = 0; i < fR.size(); i++) {
    if (fR[i] > rmax) {
      rmax = fR[i];
    }
  }
65
66
67
68
69
70
  /* Using the simplest extent for the time being, because
   * currently it's not possible to generate a Cone using
   * factory on GPU. Once it becomes possible then the code
   * written below can be used.
   */
  aMin.Set(-rmax, -rmax, fAMin.z());
71
  aMax.Set(rmax, rmax, fAMax.z());
72
  /*auto coneUnplaced = GeoManager::MakeInstance<UnplacedCone>(0., rmax, 0., rmax, 1., fSPhi, fDPhi);
73
74
  coneUnplaced->Extent(aMin, aMax);
  aMin.z() = fAMin.z();
75
  aMax.z() = fAMax.z();*/
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
}

/* Borrowed the definition from Polycone and made the required modifications*/
VECCORE_ATT_HOST_DEVICE
bool UnplacedGenericPolycone::Normal(Vector3D<Precision> const &point, Vector3D<Precision> &norm) const
{
  bool valid = true;
  int index  = fGenericPolycone.GetSectionIndex(point.z() - kTolerance);

  if (index < 0) {
    valid = false;
    if (index == -1) norm = Vector3D<Precision>(0., 0., -1.);
    if (index == -2) norm = Vector3D<Precision>(0., 0., 1.);
    return valid;
  }
  GenericPolyconeSection const &sec = fGenericPolycone.GetSection(index);
  valid                             = sec.fCoaxialCones->Normal(point - Vector3D<Precision>(0, 0, sec.fShift), norm);

  // if point is within tolerance of a Z-plane between 2 sections, get normal from other section too
  if (size_t(index + 1) < fGenericPolycone.fSections.size() &&
      std::abs(point.z() - fGenericPolycone.fZs[index + 1]) < kTolerance) {
    GenericPolyconeSection const &sec2 = fGenericPolycone.GetSection(index + 1);
    bool valid2                        = false;
    Vector3D<Precision> norm2;
    valid2 = sec2.fCoaxialCones->Normal(point - Vector3D<Precision>(0, 0, sec2.fShift), norm2);

    if (!valid && valid2) {
      norm  = norm2;
      valid = valid2;
    }

    // if both valid && valid2 true, norm and norm2 should be added...
    if (valid && valid2) {

      // discover exiting direction by moving point a bit (it would be good to have track direction here)
      // if(sec.fSolid->Contains(point + kTolerance*10*norm - Vector3D<Precision>(0, 0, sec.fShift))){

      //}
      bool c2;
      using CI = CoaxialConesImplementation; // ConeImplementation<ConeTypes::UniversalCone>;
      CI::Contains(*sec2.fCoaxialCones, point + kTolerance * 10 * norm2 - Vector3D<Precision>(0, 0, sec2.fShift), c2);
      if (c2) {
        norm = norm2;
      } else {
        norm = norm + norm2;
        // but we might be in the interior of the polycone, and norm2=(0,0,-1) and norm1=(0,0,1) --> norm=(0,0,0)
        // quick fix:  set a normal pointing to the input point, but setting its z=0 (radial)
        if (norm.Mag2() < kTolerance) norm = Vector3D<Precision>(point.x(), point.y(), 0);
      }
    }
  }
  if (valid) norm /= norm.Mag();
  return valid;
}

Vector3D<Precision> UnplacedGenericPolycone::SamplePointOnSurface() const
{
  /* Algo : First select the section
   *        From the selected section select the cone
   *        Sample the point from selected cone and return the sampled point
   */
  int sectionSelection                             = (int)RNG::Instance().uniform(0., fGenericPolycone.GetNSections());
  const GenericPolyconeSection &section            = fGenericPolycone.GetSection(sectionSelection);
  CoaxialConesStruct<Precision> *coaxialCones      = section.fCoaxialCones;
  Vector<ConeStruct<Precision> *> coneStructVector = coaxialCones->fConeStructVector;
  int coneSelection                                = (int)RNG::Instance().uniform(0., coneStructVector.size());
  ConeStruct<Precision> *coneStruct                = coneStructVector[coneSelection];
  auto coneUnplaced =
      GeoManager::MakeInstance<UnplacedCone>(coneStruct->fRmin1, coneStruct->fRmax1, coneStruct->fRmin2,
                                             coneStruct->fRmax2, coneStruct->fDz, coneStruct->fSPhi, coneStruct->fDPhi);
  return coneUnplaced->SamplePointOnSurface();
}

void UnplacedGenericPolycone::Print() const
{
  // Provided Elliptical Cone Parameters as done for Tube below
  // printf("GenericPolycone {%.2f, %.2f, %.2f}", fGenericPolycone.fDx, fGenericPolycone.fDy, fGenericPolycone.fDz);
153
154
155
156
157
158
159
160
161
162
163
  printf("---------------------------------------------------\n");
  printf(" Solid type: GenericPolycone\n");
  printf(" Parameters: \n");
  printf("    starting phi angle : %f radians\n", fSPhi);
  printf("    ending phi angle : %f radians\n", fDPhi);
  printf("    number of RZ points : %d\n", fNumRZ);
  printf("              RZ values (corners): \n");
  for (int i = 0; i < fNumRZ; i++) {
    printf("          %f, %f\n", fR[i], fZ[i]);
  }
  printf("---------------------------------------------------\n");
164
165
166
167
168
169
170
}

void UnplacedGenericPolycone::Print(std::ostream &os) const
{
  // Provided Elliptical Cone Parameters as done for Tube below
  // os << "GenericPolycone {" << fEllipticalTube.fDx << ", " << fEllipticalTube.fDy << ", " << fEllipticalTube.fDz <<
  // "}";
171
172
173
174
175
176
177
178
179
180
181
182
  os << "---------------------------------------------------\n"
     << " Solid type: GenericPolycone\n"
     << " Parameters: \n"
     << "    starting phi angle : " << fSPhi << " radians \n"
     << "    ending phi angle   : " << fDPhi << " radians \n";

  os << "    number of RZ points: " << fNumRZ << "\n"
     << "              RZ values (corners): \n";
  for (int i = 0; i < fNumRZ; i++) {
    os << "                         " << fR[i] << ", " << fZ[i] << "\n";
  }
  os << "-----------------------------------------------------------\n";
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
}

#ifndef VECCORE_CUDA
template <TranslationCode trans_code, RotationCode rot_code>
VPlacedVolume *UnplacedGenericPolycone::Create(LogicalVolume const *const logical_volume,
                                               Transformation3D const *const transformation,
                                               VPlacedVolume *const placement)
{
  if (placement) {
    new (placement) SpecializedGenericPolycone<trans_code, rot_code>(logical_volume, transformation);
    return placement;
  }
  return new SpecializedGenericPolycone<trans_code, rot_code>(logical_volume, transformation);
}

VPlacedVolume *UnplacedGenericPolycone::SpecializedVolume(LogicalVolume const *const volume,
                                                          Transformation3D const *const transformation,
                                                          const TranslationCode trans_code, const RotationCode rot_code,
                                                          VPlacedVolume *const placement) const
{
  return VolumeFactory::CreateByTransformation<UnplacedGenericPolycone>(volume, transformation, trans_code, rot_code,
                                                                        placement);
}
#else

template <TranslationCode trans_code, RotationCode rot_code>
VECCORE_ATT_DEVICE
VPlacedVolume *UnplacedGenericPolycone::Create(LogicalVolume const *const logical_volume,
                                               Transformation3D const *const transformation, const int id,
                                               VPlacedVolume *const placement)
{
  if (placement) {
    new (placement) SpecializedGenericPolycone<trans_code, rot_code>(logical_volume, transformation, id);
    return placement;
  }
  return new SpecializedGenericPolycone<trans_code, rot_code>(logical_volume, transformation, id);
}

VECCORE_ATT_DEVICE
VPlacedVolume *UnplacedGenericPolycone::SpecializedVolume(LogicalVolume const *const volume,
                                                          Transformation3D const *const transformation,
                                                          const TranslationCode trans_code, const RotationCode rot_code,
                                                          const int id, VPlacedVolume *const placement) const
{
  return VolumeFactory::CreateByTransformation<UnplacedGenericPolycone>(volume, transformation, trans_code, rot_code,
                                                                        id, placement);
}

#endif

#ifdef VECGEOM_CUDA_INTERFACE

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

DevicePtr<cuda::VUnplacedVolume> UnplacedGenericPolycone::CopyToGpu(
    DevicePtr<cuda::VUnplacedVolume> const in_gpu_ptr) const
{
  Precision *z_gpu_ptr = AllocateOnGpu<Precision>(fNumRZ * sizeof(Precision));
  Precision *r_gpu_ptr = AllocateOnGpu<Precision>(fNumRZ * sizeof(Precision));

  vecgeom::CopyToGpu(&fZ[0], z_gpu_ptr, sizeof(Precision) * fNumRZ);
  vecgeom::CopyToGpu(&fR[0], r_gpu_ptr, sizeof(Precision) * fNumRZ);

  DevicePtr<cuda::VUnplacedVolume> gpuGenericPolycone =
      CopyToGpuImpl<UnplacedGenericPolycone>(in_gpu_ptr, fSPhi, fDPhi, fNumRZ, r_gpu_ptr, z_gpu_ptr);
  // remove temporary space from GPU
  FreeFromGpu(z_gpu_ptr);
  FreeFromGpu(r_gpu_ptr);

  return gpuGenericPolycone;
}

#endif // VECGEOM_CUDA_INTERFACE

} // namespace VECGEOM_IMPL_NAMESPACE

#ifdef VECCORE_CUDA

namespace cxx {

template size_t DevicePtr<cuda::UnplacedGenericPolycone>::SizeOf();
template void DevicePtr<cuda::UnplacedGenericPolycone>::Construct(Precision phiStart, Precision phiTotal, int numRZ,
                                                                  Precision *r, Precision *z) const;

} // namespace cxx

#endif
} // namespace vecgeom