• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmc-dev / openmc / 10394876935

14 Aug 2024 08:56PM UTC coverage: 84.732% (-0.2%) from 84.9%
10394876935

Pull #3112

github

web-flow
Merge a35fa5359 into 9483cce0b
Pull Request #3112: Revamp CI with dependency and Python caching for efficient installs

49440 of 58349 relevant lines covered (84.73%)

31868645.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/include/openmc/surface.h
1
#ifndef OPENMC_SURFACE_H
2
#define OPENMC_SURFACE_H
3

4
#include <limits> // For numeric_limits
5
#include <string>
6
#include <unordered_map>
7

8
#include "hdf5.h"
9
#include "pugixml.hpp"
10

11
#include "openmc/boundary_condition.h"
12
#include "openmc/constants.h"
13
#include "openmc/memory.h" // for unique_ptr
14
#include "openmc/particle.h"
15
#include "openmc/position.h"
16
#include "openmc/vector.h"
17

18
namespace openmc {
19

20
//==============================================================================
21
// Global variables
22
//==============================================================================
23

24
class Surface;
25

26
namespace model {
27
extern std::unordered_map<int, int> surface_map;
28
extern vector<unique_ptr<Surface>> surfaces;
29
} // namespace model
30

31
//==============================================================================
32
//! Coordinates for an axis-aligned cuboid that bounds a geometric object.
33
//==============================================================================
34

35
struct BoundingBox {
36
  double xmin = -INFTY;
37
  double xmax = INFTY;
38
  double ymin = -INFTY;
39
  double ymax = INFTY;
40
  double zmin = -INFTY;
41
  double zmax = INFTY;
42

43
  inline BoundingBox operator&(const BoundingBox& other)
44
  {
45
    BoundingBox result = *this;
46
    return result &= other;
47
  }
48

49
  inline BoundingBox operator|(const BoundingBox& other)
50
  {
51
    BoundingBox result = *this;
52
    return result |= other;
53
  }
54

55
  // intersect operator
56
  inline BoundingBox& operator&=(const BoundingBox& other)
57
  {
58
    xmin = std::max(xmin, other.xmin);
59
    xmax = std::min(xmax, other.xmax);
60
    ymin = std::max(ymin, other.ymin);
61
    ymax = std::min(ymax, other.ymax);
62
    zmin = std::max(zmin, other.zmin);
63
    zmax = std::min(zmax, other.zmax);
64
    return *this;
65
  }
66

67
  // union operator
68
  inline BoundingBox& operator|=(const BoundingBox& other)
69
  {
70
    xmin = std::min(xmin, other.xmin);
×
71
    xmax = std::max(xmax, other.xmax);
×
72
    ymin = std::min(ymin, other.ymin);
×
73
    ymax = std::max(ymax, other.ymax);
×
74
    zmin = std::min(zmin, other.zmin);
×
75
    zmax = std::max(zmax, other.zmax);
×
76
    return *this;
×
77
  }
78
};
79

80
//==============================================================================
81
//! A geometry primitive used to define regions of 3D space.
82
//==============================================================================
83

84
class Surface {
85
public:
86
  int id_;                           //!< Unique ID
87
  std::string name_;                 //!< User-defined name
88
  unique_ptr<BoundaryCondition> bc_; //!< Boundary condition
89
  GeometryType geom_type_;           //!< Geometry type indicator (CSG or DAGMC)
90
  bool surf_source_ {false}; //!< Activate source banking for the surface?
91

92
  explicit Surface(pugi::xml_node surf_node);
93
  Surface();
94

95
  virtual ~Surface() {}
96

97
  //! Determine which side of a surface a point lies on.
98
  //! \param r The 3D Cartesian coordinate of a point.
99
  //! \param u A direction used to "break ties" and pick a sense when the
100
  //!   point is very close to the surface.
101
  //! \return true if the point is on the "positive" side of the surface and
102
  //!   false otherwise.
103
  bool sense(Position r, Direction u) const;
104

105
  //! Determine the direction of a ray reflected from the surface.
106
  //! \param[in] r The point at which the ray is incident.
107
  //! \param[in] u Incident direction of the ray
108
  //! \param[inout] p Pointer to the particle. Only DAGMC uses this.
109
  //! \return Outgoing direction of the ray
110
  virtual Direction reflect(
111
    Position r, Direction u, GeometryState* p = nullptr) const;
112

113
  virtual Direction diffuse_reflect(
114
    Position r, Direction u, uint64_t* seed, GeometryState* p = nullptr) const;
115

116
  //! Evaluate the equation describing the surface.
117
  //!
118
  //! Surfaces can be described by some function f(x, y, z) = 0.  This member
119
  //! function evaluates that mathematical function.
120
  //! \param r A 3D Cartesian coordinate.
121
  virtual double evaluate(Position r) const = 0;
122

123
  //! Compute the distance between a point and the surface along a ray.
124
  //! \param r A 3D Cartesian coordinate.
125
  //! \param u The direction of the ray.
126
  //! \param coincident A hint to the code that the given point should lie
127
  //!   exactly on the surface.
128
  virtual double distance(Position r, Direction u, bool coincident) const = 0;
129

130
  //! Compute the local outward normal direction of the surface.
131
  //! \param r A 3D Cartesian coordinate.
132
  //! \return Normal direction
133
  virtual Direction normal(Position r) const = 0;
134

135
  //! Write all information needed to reconstruct the surface to an HDF5 group.
136
  //! \param group_id An HDF5 group id.
137
  void to_hdf5(hid_t group_id) const;
138

139
  //! Get the BoundingBox for this surface.
140
  virtual BoundingBox bounding_box(bool /*pos_side*/) const { return {}; }
141

142
protected:
143
  virtual void to_hdf5_inner(hid_t group_id) const = 0;
144
};
145

146
class CSGSurface : public Surface {
147
public:
148
  explicit CSGSurface(pugi::xml_node surf_node);
149
  CSGSurface();
150
};
151

152
//==============================================================================
153
//! A plane perpendicular to the x-axis.
154
//
155
//! The plane is described by the equation \f$x - x_0 = 0\f$
156
//==============================================================================
157

158
class SurfaceXPlane : public CSGSurface {
159
public:
160
  explicit SurfaceXPlane(pugi::xml_node surf_node);
161
  double evaluate(Position r) const override;
162
  double distance(Position r, Direction u, bool coincident) const override;
163
  Direction normal(Position r) const override;
164
  void to_hdf5_inner(hid_t group_id) const override;
165
  BoundingBox bounding_box(bool pos_side) const override;
166

167
  double x0_;
168
};
169

170
//==============================================================================
171
//! A plane perpendicular to the y-axis.
172
//
173
//! The plane is described by the equation \f$y - y_0 = 0\f$
174
//==============================================================================
175

176
class SurfaceYPlane : public CSGSurface {
177
public:
178
  explicit SurfaceYPlane(pugi::xml_node surf_node);
179
  double evaluate(Position r) const override;
180
  double distance(Position r, Direction u, bool coincident) const override;
181
  Direction normal(Position r) const override;
182
  void to_hdf5_inner(hid_t group_id) const override;
183
  BoundingBox bounding_box(bool pos_side) const override;
184

185
  double y0_;
186
};
187

188
//==============================================================================
189
//! A plane perpendicular to the z-axis.
190
//
191
//! The plane is described by the equation \f$z - z_0 = 0\f$
192
//==============================================================================
193

194
class SurfaceZPlane : public CSGSurface {
195
public:
196
  explicit SurfaceZPlane(pugi::xml_node surf_node);
197
  double evaluate(Position r) const override;
198
  double distance(Position r, Direction u, bool coincident) const override;
199
  Direction normal(Position r) const override;
200
  void to_hdf5_inner(hid_t group_id) const override;
201
  BoundingBox bounding_box(bool pos_side) const override;
202

203
  double z0_;
204
};
205

206
//==============================================================================
207
//! A general plane.
208
//
209
//! The plane is described by the equation \f$A x + B y + C z - D = 0\f$
210
//==============================================================================
211

212
class SurfacePlane : public CSGSurface {
213
public:
214
  explicit SurfacePlane(pugi::xml_node surf_node);
215
  double evaluate(Position r) const override;
216
  double distance(Position r, Direction u, bool coincident) const override;
217
  Direction normal(Position r) const override;
218
  void to_hdf5_inner(hid_t group_id) const override;
219

220
  double A_, B_, C_, D_;
221
};
222

223
//==============================================================================
224
//! A cylinder aligned along the x-axis.
225
//
226
//! The cylinder is described by the equation
227
//! \f$(y - y_0)^2 + (z - z_0)^2 - R^2 = 0\f$
228
//==============================================================================
229

230
class SurfaceXCylinder : public CSGSurface {
231
public:
232
  explicit SurfaceXCylinder(pugi::xml_node surf_node);
233
  double evaluate(Position r) const override;
234
  double distance(Position r, Direction u, bool coincident) const override;
235
  Direction normal(Position r) const override;
236
  void to_hdf5_inner(hid_t group_id) const override;
237
  BoundingBox bounding_box(bool pos_side) const override;
238

239
  double y0_, z0_, radius_;
240
};
241

242
//==============================================================================
243
//! A cylinder aligned along the y-axis.
244
//
245
//! The cylinder is described by the equation
246
//! \f$(x - x_0)^2 + (z - z_0)^2 - R^2 = 0\f$
247
//==============================================================================
248

249
class SurfaceYCylinder : public CSGSurface {
250
public:
251
  explicit SurfaceYCylinder(pugi::xml_node surf_node);
252
  double evaluate(Position r) const override;
253
  double distance(Position r, Direction u, bool coincident) const override;
254
  Direction normal(Position r) const override;
255
  void to_hdf5_inner(hid_t group_id) const override;
256
  BoundingBox bounding_box(bool pos_side) const override;
257

258
  double x0_, z0_, radius_;
259
};
260

261
//==============================================================================
262
//! A cylinder aligned along the z-axis.
263
//
264
//! The cylinder is described by the equation
265
//! \f$(x - x_0)^2 + (y - y_0)^2 - R^2 = 0\f$
266
//==============================================================================
267

268
class SurfaceZCylinder : public CSGSurface {
269
public:
270
  explicit SurfaceZCylinder(pugi::xml_node surf_node);
271
  double evaluate(Position r) const override;
272
  double distance(Position r, Direction u, bool coincident) const override;
273
  Direction normal(Position r) const override;
274
  void to_hdf5_inner(hid_t group_id) const override;
275
  BoundingBox bounding_box(bool pos_side) const override;
276

277
  double x0_, y0_, radius_;
278
};
279

280
//==============================================================================
281
//! A sphere.
282
//
283
//! The cylinder is described by the equation
284
//! \f$(x - x_0)^2 + (y - y_0)^2 + (z - z_0)^2 - R^2 = 0\f$
285
//==============================================================================
286

287
class SurfaceSphere : public CSGSurface {
288
public:
289
  explicit SurfaceSphere(pugi::xml_node surf_node);
290
  double evaluate(Position r) const override;
291
  double distance(Position r, Direction u, bool coincident) const override;
292
  Direction normal(Position r) const override;
293
  void to_hdf5_inner(hid_t group_id) const override;
294
  BoundingBox bounding_box(bool pos_side) const override;
295

296
  double x0_, y0_, z0_, radius_;
297
};
298

299
//==============================================================================
300
//! A cone aligned along the x-axis.
301
//
302
//! The cylinder is described by the equation
303
//! \f$(y - y_0)^2 + (z - z_0)^2 - R^2 (x - x_0)^2 = 0\f$
304
//==============================================================================
305

306
class SurfaceXCone : public CSGSurface {
307
public:
308
  explicit SurfaceXCone(pugi::xml_node surf_node);
309
  double evaluate(Position r) const override;
310
  double distance(Position r, Direction u, bool coincident) const override;
311
  Direction normal(Position r) const override;
312
  void to_hdf5_inner(hid_t group_id) const override;
313

314
  double x0_, y0_, z0_, radius_sq_;
315
};
316

317
//==============================================================================
318
//! A cone aligned along the y-axis.
319
//
320
//! The cylinder is described by the equation
321
//! \f$(x - x_0)^2 + (z - z_0)^2 - R^2 (y - y_0)^2 = 0\f$
322
//==============================================================================
323

324
class SurfaceYCone : public CSGSurface {
325
public:
326
  explicit SurfaceYCone(pugi::xml_node surf_node);
327
  double evaluate(Position r) const override;
328
  double distance(Position r, Direction u, bool coincident) const override;
329
  Direction normal(Position r) const override;
330
  void to_hdf5_inner(hid_t group_id) const override;
331

332
  double x0_, y0_, z0_, radius_sq_;
333
};
334

335
//==============================================================================
336
//! A cone aligned along the z-axis.
337
//
338
//! The cylinder is described by the equation
339
//! \f$(x - x_0)^2 + (y - y_0)^2 - R^2 (z - z_0)^2 = 0\f$
340
//==============================================================================
341

342
class SurfaceZCone : public CSGSurface {
343
public:
344
  explicit SurfaceZCone(pugi::xml_node surf_node);
345
  double evaluate(Position r) const override;
346
  double distance(Position r, Direction u, bool coincident) const override;
347
  Direction normal(Position r) const override;
348
  void to_hdf5_inner(hid_t group_id) const override;
349

350
  double x0_, y0_, z0_, radius_sq_;
351
};
352

353
//==============================================================================
354
//! A general surface described by a quadratic equation.
355
//
356
//! \f$A x^2 + B y^2 + C z^2 + D x y + E y z + F x z + G x + H y + J z + K =
357
//! 0\f$
358
//==============================================================================
359

360
class SurfaceQuadric : public CSGSurface {
361
public:
362
  explicit SurfaceQuadric(pugi::xml_node surf_node);
363
  double evaluate(Position r) const override;
364
  double distance(Position r, Direction u, bool coincident) const override;
365
  Direction normal(Position r) const override;
366
  void to_hdf5_inner(hid_t group_id) const override;
367

368
  // Ax^2 + By^2 + Cz^2 + Dxy + Eyz + Fxz + Gx + Hy + Jz + K = 0
369
  double A_, B_, C_, D_, E_, F_, G_, H_, J_, K_;
370
};
371

372
//==============================================================================
373
//! A toroidal surface described by the quartic torus lies in the x direction
374
//
375
//! \f$(x-x_0)^2/B^2 + (\sqrt{(y-y_0)^2 + (z-z_0)^2} - A)^2/C^2 -1 \f$
376
//==============================================================================
377

378
class SurfaceXTorus : public CSGSurface {
379
public:
380
  explicit SurfaceXTorus(pugi::xml_node surf_node);
381
  double evaluate(Position r) const override;
382
  double distance(Position r, Direction u, bool coincident) const override;
383
  Direction normal(Position r) const override;
384
  void to_hdf5_inner(hid_t group_id) const override;
385

386
  double x0_, y0_, z0_, A_, B_, C_;
387
};
388

389
//==============================================================================
390
//! A toroidal surface described by the quartic torus lies in the y direction
391
//
392
//! \f$(y-y_0)^2/B^2 + (\sqrt{(x-x_0)^2 + (z-z_0)^2} - A)^2/C^2 -1 \f$
393
//==============================================================================
394

395
class SurfaceYTorus : public CSGSurface {
396
public:
397
  explicit SurfaceYTorus(pugi::xml_node surf_node);
398
  double evaluate(Position r) const override;
399
  double distance(Position r, Direction u, bool coincident) const override;
400
  Direction normal(Position r) const override;
401
  void to_hdf5_inner(hid_t group_id) const override;
402

403
  double x0_, y0_, z0_, A_, B_, C_;
404
};
405

406
//==============================================================================
407
//! A toroidal surface described by the quartic torus lies in the z direction
408
//
409
//! \f$(z-z_0)^2/B^2 + (\sqrt{(x-x_0)^2 + (y-y_0)^2} - A)^2/C^2 -1 \f$
410
//==============================================================================
411

412
class SurfaceZTorus : public CSGSurface {
413
public:
414
  explicit SurfaceZTorus(pugi::xml_node surf_node);
415
  double evaluate(Position r) const override;
416
  double distance(Position r, Direction u, bool coincident) const override;
417
  Direction normal(Position r) const override;
418
  void to_hdf5_inner(hid_t group_id) const override;
419

420
  double x0_, y0_, z0_, A_, B_, C_;
421
};
422

423
//==============================================================================
424
// Non-member functions
425
//==============================================================================
426

427
void read_surfaces(pugi::xml_node node);
428

429
void free_memory_surfaces();
430

431
} // namespace openmc
432
#endif // OPENMC_SURFACE_H
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc