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

openmc-dev / openmc / 43538648659

14 Nov 2026 01:07AM UTC coverage: 82.356% (+0.05%) from 82.304%
43538648659

push

github

web-flow
Specify temperature from a field (structured mesh only) (#3734)

Co-authored-by: Paul Romano <paul.k.romano@gmail.com>

20953 of 29717 branches covered (70.51%)

Branch coverage included in aggregate %.

320 of 342 new or added lines in 15 files covered. (93.57%)

64452 of 73985 relevant lines covered (87.11%)

42544112.91 hits per line

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

83.44
/src/field.cpp
1
#include "openmc/field.h"
2
#include "openmc/capi.h"
3
#include "openmc/cell.h"
4
#include "openmc/constants.h"
5
#include "openmc/mesh.h"
6
#include "openmc/nuclide.h"
7
#include "openmc/settings.h"
8
#include "openmc/simulation.h"
9
#include "openmc/vector.h"
10

11
#include <cmath>
12
#include <stdexcept>
13

14
namespace openmc {
15

16
ScalarField::ScalarField(
119 ✔
17
  Mesh* mesh_ptr, vector<double> values, const std::string& field_type)
119 ✔
18
{
19
  this->field_type_ = field_type;
119 ✔
20

21
  if (mesh_ptr != nullptr) {
119 !
22
    this->mesh_ptr_ = mesh_ptr;
119 ✔
23
  } else {
NEW
24
    fatal_error(fmt::format("No mesh found for {}!", field_type));
×
25
  }
26

27
  if (this->mesh_ptr_->n_bins() != values.size()) {
119 !
NEW
28
    fatal_error(fmt::format(
×
29
      "The number of elements in the mesh is not consistent with the "
30
      "number of values declared in {}!",
31
      field_type));
32
  }
33

34
  for (double v : values) {
919 ✔
35
    this->values_.push_back(v);
800 ✔
36
  }
37
}
119 ✔
38

39
MeshCrossing ScalarField::next_mesh_crossing(
5,031,014 ✔
40
  int current_bin, const Position& r, const Direction& u)
41
{
42
  return this->mesh_ptr()->next_mesh_crossing(current_bin, r, u);
5,031,014 ✔
43
}
44

45
void TemperatureField::validate_temperature(double temperature)
800 ✔
46
{
47
  if (!std::isfinite(temperature) || temperature < 0.0) {
800 !
48
    throw std::runtime_error {fmt::format(
16 ✔
49
      "Temperature of {} K is not a finite, non-negative value.", temperature)};
32 ✔
50
  }
51

52
  // data::temperature_min/max only bound anything once cross sections have
53
  // been read. Until then they hold their sentinel values of INFTY and 0.0,
54
  // and there is no range to check against.
55
  if (data::temperature_min > data::temperature_max)
784 ✔
56
    return;
57

58
  if (temperature < data::temperature_min - settings::temperature_tolerance) {
24 ✔
59
    throw std::runtime_error {
8 ✔
60
      fmt::format("Temperature of {} K is below minimum temperature at "
8 ✔
61
                  "which data is available of {} K.",
62
        temperature, data::temperature_min)};
16 ✔
63
  }
64
  if (temperature > data::temperature_max + settings::temperature_tolerance) {
16 ✔
65
    throw std::runtime_error {
8 ✔
66
      fmt::format("Temperature of {} K is above maximum temperature at "
8 ✔
67
                  "which data is available of {} K.",
68
        temperature, data::temperature_max)};
16 ✔
69
  }
70
}
71

72
TemperatureField::TemperatureField(Mesh* mesh_ptr, vector<double> values)
119 ✔
73
  : ScalarField(mesh_ptr, values, "TemperatureField")
238 ✔
74
{
75
  for (int i = 0; i < this->values().size(); ++i) {
871 ✔
76
    try {
760 ✔
77
      validate_temperature(this->value(i));
760 ✔
78
    } catch (const std::runtime_error& e) {
8 !
79
      throw std::runtime_error {
9 ✔
80
        fmt::format("Element {} of the temperature field: {}", i, e.what())};
16 ✔
81
    }
8 ✔
82
  }
83
}
119 ✔
84

85
double TemperatureField::get_temperature(int bin)
3,093,045 ✔
86
{
87
  if (bin >= 0 && bin < values().size()) {
3,093,045 !
88
    return this->value(bin);
3,093,029 ✔
89
  }
90
  return -1.0;
91
}
92

93
double TemperatureField::get_sqrtkT(int bin)
3,092,949 ✔
94
{
95
  double temperature = get_temperature(bin);
3,092,949 ✔
96
  if (temperature >= 0) {
3,092,949 !
97
    return sqrt(temperature * K_BOLTZMANN);
3,092,949 ✔
98
  }
99
  return -1.0;
100
}
101

102
int TemperatureField::get_bin(const Position& r)
1,060,685 ✔
103
{
104
  int bin = mesh_ptr()->get_bin(r);
1,060,685 ✔
105
  if (bin >= 0 && bin < values().size()) {
1,060,685 !
106
    return bin;
107
  } else {
108
    return C_NONE;
284,856 ✔
109
  }
110
}
111

112
//==============================================================================
113
// C API
114
//==============================================================================
115

116
extern "C" int openmc_temperature_field_set_temperature(
48 ✔
117
  int32_t index, double temperature)
118
{
119
  if (index < 0 || index >= simulation::temperature_field.values().size()) {
48 !
120
    set_errmsg("Index in temperature field is out of bounds.");
8 ✔
121
    return OPENMC_E_OUT_OF_BOUNDS;
8 ✔
122
  }
123

124
  try {
40 ✔
125
    TemperatureField::validate_temperature(temperature);
40 ✔
126
  } catch (const std::exception& e) {
24 !
127
    set_errmsg(e.what());
24 ✔
128
    return OPENMC_E_INVALID_ARGUMENT;
24 ✔
129
  }
24 ✔
130

131
  simulation::temperature_field.value(index) = temperature;
16 ✔
132
  return 0;
16 ✔
133
}
134

135
extern "C" int openmc_temperature_field_get_mesh(int32_t* index)
224 ✔
136
{
137
  if (!simulation::temperature_field.has_mesh()) {
224 ✔
138
    *index = C_NONE;
16 ✔
139
    return 0;
16 ✔
140
  }
141

142
  const auto* mesh = simulation::temperature_field.mesh_ptr();
208 ✔
143
  auto it = model::mesh_map.find(mesh->id());
208 !
144
  if (it == model::mesh_map.end()) {
208 !
NEW
145
    set_errmsg(
×
146
      "Temperature field mesh could not be found in the meshes array.");
NEW
147
    return OPENMC_E_INVALID_ID;
×
148
  }
149

150
  *index = it->second;
208 ✔
151
  return 0;
208 ✔
152
}
153

154
extern "C" int openmc_temperature_field_get_temperature(
120 ✔
155
  int32_t index, double* temperature)
156
{
157
  if (index < 0 || index >= simulation::temperature_field.values().size()) {
120 !
NEW
158
    set_errmsg("Index in temperature field is out of bounds.");
×
NEW
159
    return OPENMC_E_OUT_OF_BOUNDS;
×
160
  }
161

162
  *temperature = simulation::temperature_field.value(index);
120 ✔
163
  return 0;
120 ✔
164
}
165

NEW
166
extern "C" size_t openmc_temperature_field_size()
×
167
{
NEW
168
  return simulation::temperature_field.values().size();
×
169
}
170

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

© 2026 Coveralls, Inc