forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckedGeoTrackView.hh
277 lines (248 loc) · 8.58 KB
/
CheckedGeoTrackView.hh
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
//----------------------------------*-C++-*----------------------------------//
// Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/geo/CheckedGeoTrackView.hh
//---------------------------------------------------------------------------//
#pragma once
#include <iomanip>
#include "corecel/Types.hh"
#include "corecel/cont/ArrayIO.hh"
#include "corecel/io/Logger.hh"
#include "orange/Types.hh"
namespace celeritas
{
namespace test
{
//---------------------------------------------------------------------------//
/*!
* Check validity of safety and volume crossings while navigating on CPU.
*
* Also count the number of calls to "find distance" and "find safety".
*/
template<class GTV>
class CheckedGeoTrackView : public GTV
{
public:
//!@{
//! \name Type aliases
using GeoTrackViewT = GTV;
using Initializer_t = typename GTV::Initializer_t;
using DetailedInitializer = typename GTV::DetailedInitializer;
//!@}
public:
//! Forward construction arguments to the original track view
template<class... Args>
CheckedGeoTrackView(Args&&... args) : GTV(std::forward<Args>(args)...)
{
}
// Initialize the state
inline CELER_FUNCTION CheckedGeoTrackView&
operator=(GeoTrackInitializer const& init)
{
GTV::operator=(init);
CELER_ENSURE(!this->is_outside());
return *this;
}
// Initialize the state from a parent state and new direction
inline CELER_FUNCTION CheckedGeoTrackView&
operator=(DetailedInitializer const& init)
{
GTV::operator=(init);
CELER_ENSURE(!this->is_outside());
return *this;
}
// Check volume consistency this far from the boundary
static constexpr real_type safety_tol()
{
return 1e-4 * units::centimeter;
}
// Calculate or return the safety up to an infinite distance
inline real_type find_safety();
// Calculate or return the safety up to the given distance
inline real_type find_safety(real_type max_safety);
// Change the direction
inline void set_dir(Real3 const&);
// Find the distance to the next boundary
inline Propagation find_next_step();
// Find the distance to the next boundary
inline Propagation find_next_step(real_type max_distance);
// Move a linear step fraction
inline void move_internal(real_type);
// Move within the safety distance to a specific point
inline void move_internal(Real3 const& pos);
// Move to the boundary in preparation for crossing it
inline void move_to_boundary();
// Cross from one side of the current surface to the other
inline void cross_boundary();
//! Number of calls fo find_next_step
size_type intersect_count() const { return num_intersect_; }
//! Number of calls fo find_safety
size_type safety_count() const { return num_safety_; }
//! Reset the stepscounter
void reset_count() { num_intersect_ = num_safety_ = 0; }
private:
bool checked_internal_{false};
size_type num_intersect_{0};
size_type num_safety_{0};
};
//---------------------------------------------------------------------------//
// DEDUCTION GUIDES
//---------------------------------------------------------------------------//
template<class GTV>
CheckedGeoTrackView(GTV&&) -> CheckedGeoTrackView<GTV>;
//---------------------------------------------------------------------------//
// INLINE DEFINITIONS
//---------------------------------------------------------------------------//
/*!
* Calculate the safety distance.
*/
template<class GTV>
real_type CheckedGeoTrackView<GTV>::find_safety()
{
++num_safety_;
return GTV::find_safety();
}
//---------------------------------------------------------------------------//
/*!
* Calculate the safety distance up to a given length.
*/
template<class GTV>
real_type CheckedGeoTrackView<GTV>::find_safety(real_type max_safety)
{
++num_safety_;
real_type result = GTV::find_safety(max_safety);
return result;
}
//---------------------------------------------------------------------------//
/*!
* Set the direction.
*/
template<class GTV>
void CheckedGeoTrackView<GTV>::set_dir(Real3 const& newdir)
{
CELER_EXPECT(!this->is_outside());
GTV::set_dir(newdir);
}
//---------------------------------------------------------------------------//
/*!
* Find the distance to the next boundary.
*/
template<class GTV>
Propagation CheckedGeoTrackView<GTV>::find_next_step()
{
CELER_EXPECT(!this->is_outside());
++num_intersect_;
return GTV::find_next_step();
}
//---------------------------------------------------------------------------//
/*!
* Find the distance to the next boundary.
*/
template<class GTV>
Propagation CheckedGeoTrackView<GTV>::find_next_step(real_type distance)
{
CELER_EXPECT(distance > 0);
CELER_EXPECT(!this->is_outside());
++num_intersect_;
auto result = GTV::find_next_step(distance);
if (result.boundary && result.distance > this->safety_tol()
&& !this->is_on_boundary())
{
real_type safety = GTV::find_safety(distance);
CELER_VALIDATE(safety <= result.distance,
<< std::setprecision(16) << "safety " << safety
<< " exceeds actual distance " << result.distance
<< " to boundary at " << this->pos() << " in "
<< this->volume_id().get());
}
return result;
}
//---------------------------------------------------------------------------//
/*!
* Move within the volume along the current direction.
*/
template<class GTV>
void CheckedGeoTrackView<GTV>::move_internal(real_type step)
{
CELER_EXPECT(!this->is_outside());
GTV::move_internal(step);
CELER_VALIDATE(!this->is_on_boundary() && !this->is_outside()
&& GTV::find_safety() > 0,
<< std::setprecision(16)
<< "zero safety distance after moving " << step << " to "
<< this->pos());
}
//---------------------------------------------------------------------------//
/*!
* Move within the volume to a nearby position.
*/
template<class GTV>
void CheckedGeoTrackView<GTV>::move_internal(Real3 const& pos)
{
CELER_EXPECT(!this->is_outside());
real_type orig_safety = (this->is_on_boundary() ? 0 : GTV::find_safety());
auto orig_pos = this->pos();
GTV::move_internal(pos);
CELER_ASSERT(!this->is_on_boundary());
if (!checked_internal_ && orig_safety > this->safety_tol())
{
VolumeId expected = this->volume_id();
Initializer_t here{this->pos(), this->dir()};
*this = here;
CELER_VALIDATE(!this->is_outside(),
<< std::setprecision(16)
<< "internal move ends up 'outside' at " << this->pos());
CELER_VALIDATE(this->volume_id() == expected,
<< std::setprecision(16)
<< "volume ID changed during internal move at "
<< this->pos() << ": was " << expected.get() << ", now "
<< this->volume_id().get());
checked_internal_ = true;
}
if (orig_safety == 0 && !this->is_on_boundary())
{
real_type new_safety = GTV::find_safety();
if (!(new_safety > 0))
{
CELER_LOG_LOCAL(warning)
<< "Moved internally from boundary but safety didn't "
"increase: volume "
<< this->volume_id().get() << " from " << orig_pos << " to "
<< this->pos() << " (distance: " << distance(orig_pos, pos)
<< ")";
}
}
}
//---------------------------------------------------------------------------//
/*!
* Move to the next boundary.
*/
template<class GTV>
void CheckedGeoTrackView<GTV>::move_to_boundary()
{
// Move to boundary
GTV::move_to_boundary();
checked_internal_ = false;
}
//---------------------------------------------------------------------------//
/*!
* Move to the next boundary.
*/
template<class GTV>
void CheckedGeoTrackView<GTV>::cross_boundary()
{
// Cross boundary
VolumeId prev = this->volume_id();
GTV::cross_boundary();
if (!this->is_outside() && prev == this->volume_id())
{
CELER_LOG_LOCAL(warning)
<< "Volume did not change from " << prev.get()
<< " when crossing boundary at " << this->pos();
}
}
//---------------------------------------------------------------------------//
} // namespace test
} // namespace celeritas