Skip to content

Commit f81d180

Browse files
committed
Address Patrick's second round of review comments.
1 parent d2b2049 commit f81d180

File tree

5 files changed

+52
-37
lines changed

5 files changed

+52
-37
lines changed

include/openmc/cell.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ class Cell {
223223
//! \return Density multiplier [-]
224224
double density_mult(int32_t instance = -1) const;
225225

226+
//! Get the density of a cell instance in g/cm3
227+
//! \param[in] instance Instance index. If -1 is given, the density
228+
//! for the first instance is returned.
229+
//! \return Density multiplier g/cm3
230+
double density(int32_t instance = -1) const;
231+
226232
//! Set the temperature of a cell instance
227233
//! \param[in] T Temperature in [K]
228234
//! \param[in] instance Instance index. If -1 is given, the temperature for
@@ -233,15 +239,14 @@ class Cell {
233239
void set_temperature(
234240
double T, int32_t instance = -1, bool set_contained = false);
235241

236-
//! Set the density multiplier of a cell instance
237-
//! \param[in] rho Density multiplier [-]
238-
//! \param[in] instance Instance index. If -1 is given, the density multiplier
239-
//! for
240-
//! all instances is set.
242+
//! Set the density of a cell instance
243+
//! \param[in] rho Density [g/cm3]
244+
//! \param[in] instance Instance index. If -1 is given, the density
245+
//! for all instances is set.
241246
//! \param[in] set_contained If this cell is not filled with a material,
242247
//! collect all contained cells with material fills and set their
243-
//! density multipliers.
244-
void set_density_mult(
248+
//! densities.
249+
void set_density(
245250
double rho, int32_t instance = -1, bool set_contained = false);
246251

247252
int32_t n_instances() const;

openmc/cell.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ class Cell(IDManagerMixin):
7474
to give each distributed cell instance a unique temperature.
7575
density : float or iterable of float
7676
Density of the cell in g/cm3. Multiple densities can be given to give
77-
each distributed cell instance a unique density.
77+
each distributed cell instance a unique density. Densities set here will
78+
override the density set on materials used to fill the cell.
7879
translation : Iterable of float
7980
If the cell is filled with a universe, this array specifies a vector
8081
that is used to translate (shift) the universe.

src/cell.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ double Cell::density_mult(int32_t instance) const
112112
}
113113
}
114114

115+
double Cell::density(int32_t instance) const
116+
{
117+
const int32_t mat_index = material(instance);
118+
if (mat_index == MATERIAL_VOID)
119+
return 0.0;
120+
121+
if (instance >= 0) {
122+
double rho =
123+
rho_mult_.size() == 1 ? rho_mult_.at(0) : rho_mult_.at(instance);
124+
return rho * model::materials[mat_index]->density_gpcc();
125+
} else {
126+
return rho_mult_[0];
127+
}
128+
}
129+
115130
void Cell::set_temperature(double T, int32_t instance, bool set_contained)
116131
{
117132
if (settings::temperature_method == TemperatureMethod::INTERPOLATION) {
@@ -162,7 +177,7 @@ void Cell::set_temperature(double T, int32_t instance, bool set_contained)
162177
}
163178
}
164179

165-
void Cell::set_density_mult(double rho, int32_t instance, bool set_contained)
180+
void Cell::set_density(double rho, int32_t instance, bool set_contained)
166181
{
167182
if (type_ != Fill::MATERIAL && !set_contained) {
168183
fatal_error(
@@ -172,17 +187,21 @@ void Cell::set_density_mult(double rho, int32_t instance, bool set_contained)
172187
}
173188

174189
if (type_ == Fill::MATERIAL) {
190+
const int32_t mat_index = material(instance);
191+
if (mat_index == MATERIAL_VOID)
192+
return;
193+
175194
if (instance >= 0) {
176195
// If density multiplier vector is not big enough, resize it first
177196
if (rho_mult_.size() != n_instances())
178197
rho_mult_.resize(n_instances(), rho_mult_[0]);
179198

180199
// Set density multiplier for the corresponding instance
181-
rho_mult_.at(instance) = rho;
200+
rho_mult_.at(instance) = rho / model::materials[mat_index]->density_gpcc();
182201
} else {
183202
// Set density multiplier for all instances
184203
for (auto& Rho_ : rho_mult_) {
185-
Rho_ = rho;
204+
Rho_ = rho / model::materials[mat_index]->density_gpcc();
186205
}
187206
}
188207
} else {
@@ -192,7 +211,7 @@ void Cell::set_density_mult(double rho, int32_t instance, bool set_contained)
192211
assert(cell->type_ == Fill::MATERIAL);
193212
auto& instances = entry.second;
194213
for (auto instance : instances) {
195-
cell->set_density_mult(rho, instance);
214+
cell->set_density(rho, instance);
196215
}
197216
}
198217
}
@@ -1243,18 +1262,8 @@ extern "C" int openmc_cell_set_density(
12431262

12441263
int32_t instance_index = instance ? *instance : -1;
12451264
try {
1246-
if (model::cells[index]->type_ != Fill::MATERIAL) {
1247-
fatal_error(
1248-
fmt::format("Cell {}, instance {} is not filled with a material.",
1249-
model::cells[index]->id_, instance_index));
1250-
}
1251-
1252-
int32_t mat_index = model::cells[index]->material(instance_index);
1253-
if (mat_index != MATERIAL_VOID) {
1254-
model::cells[index]->set_density_mult(
1255-
rho / model::materials[mat_index]->density_gpcc(), instance_index,
1256-
set_contained);
1257-
}
1265+
model::cells[index]->set_density(
1266+
rho, instance_index, set_contained);
12581267
} catch (const std::exception& e) {
12591268
set_errmsg(e.what());
12601269
return OPENMC_E_UNASSIGNED;

tests/regression_tests/cpp_driver/driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ int main(int argc, char** argv)
6565
// model)
6666
model::cells[model::cell_map[4]]->set_temperature(400.0, 3, true);
6767

68-
// set a larger density multiplier for another lattice cell
69-
model::cells[model::cell_map[4]]->set_density_mult(2.0, 2, true);
68+
// set the density of another lattice cell to 2
69+
model::cells[model::cell_map[4]]->set_density(2.0, 2, true);
7070

7171
// the summary file will be used to check that
7272
// temperatures were set correctly so clear
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
k-combined:
2-
1.964706E+00 2.868566E-02
2+
1.953962E+00 1.828426E-02
33
tally 1:
4-
8.017467E+01
5-
7.150161E+02
6-
2.300257E+01
7-
5.904224E+01
8-
7.992670E+01
9-
7.135847E+02
10-
1.831039E+02
11-
3.734941E+03
12-
1.831039E+02
13-
3.734941E+03
4+
9.607953E+01
5+
1.031898E+03
6+
2.853683E+01
7+
9.085469E+01
8+
9.745011E+01
9+
1.058928E+03
10+
2.220665E+02
11+
5.496813E+03
12+
2.220665E+02
13+
5.496813E+03

0 commit comments

Comments
 (0)