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

cyclus / cyclus / 16384544356

19 Jul 2025 03:38AM UTC coverage: 36.53% (-4.7%) from 41.231%
16384544356

push

github

web-flow
Merge pull request #1881 from dean-krueger/clang-format

Clang Format Cyclus src

1517 of 26176 new or added lines in 94 files covered. (5.8%)

309 existing lines in 19 files now uncovered.

51830 of 141883 relevant lines covered (36.53%)

14764.59 hits per line

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

96.48
/src/material.cc
1
#include "material.h"
2

3
#include <math.h>
4

5
#include "comp_math.h"
6
#include "context.h"
7
#include "decayer.h"
8
#include "error.h"
9
#include "logger.h"
10

11
namespace cyclus {
12

13
const ResourceType Material::kType = "Material";
14

15
Material::~Material() {}
236,104✔
16

17
Material::Ptr Material::Create(Agent* creator, double quantity,
7,534✔
18
                               Composition::Ptr c, std::string package_name) {
19
  Material::Ptr m(new Material(creator->context(), quantity, c, package_name));
22,602✔
20
  m->tracker_.Create(creator);
7,534✔
21
  return m;
7,534✔
22
}
23

24
Material::Ptr Material::CreateUntracked(double quantity, Composition::Ptr c) {
15,907✔
25
  Material::Ptr m(new Material(NULL, quantity, c, Package::unpackaged_name()));
31,814✔
26
  return m;
15,907✔
27
}
28

29
int Material::qual_id() const {
207,625✔
30
  return comp_->id();
207,625✔
31
}
32

33
const ResourceType Material::type() const {
207,580✔
34
  return Material::kType;
207,580✔
35
}
36

37
Resource::Ptr Material::Clone() const {
936✔
38
  Material* m = new Material(*this);
936✔
39
  Resource::Ptr c(m);
936✔
40
  m->tracker_.DontTrack();
936✔
41
  return c;
936✔
42
}
43

44
void Material::Record(Context* ctx) const {
207,577✔
45
  // Note that no time field is needed because the resource ID changes
46
  // every time the resource changes - state_id by itself is already unique.
47
  ctx_->NewDatum("MaterialInfo")
207,577✔
48
      ->AddVal("ResourceId", state_id())
49
      ->AddVal("PrevDecayTime", prev_decay_time_)
50
      ->Record();
415,154✔
51

52
  comp_->Record(ctx);
207,577✔
53
}
207,577✔
54

55
std::string Material::units() const {
207,840✔
56
  return "kg";
207,840✔
57
}
58

59
double Material::quantity() const {
324,896✔
60
  return qty_;
324,896✔
61
}
62

63
Resource::Ptr Material::ExtractRes(double qty) {
8✔
64
  return boost::static_pointer_cast<Resource>(ExtractQty(qty));
8✔
65
}
66

67
Material::Ptr Material::ExtractQty(double qty) {
14✔
68
  return ExtractComp(qty, comp_);
27✔
69
}
70

71
Material::Ptr Material::ExtractComp(double qty, Composition::Ptr c,
23✔
72
                                    double threshold) {
73
  if (qty_ < qty) {
23✔
74
    throw ValueError("mass extraction causes negative quantity");
6✔
75
  }
76

77
  // TODO: decide if ExtractComp should force lazy-decay by calling comp()
78
  if (comp_ != c) {
20✔
79
    CompMap v(comp_->mass());
3✔
80
    compmath::Normalize(&v, qty_);
3✔
81
    CompMap otherv(c->mass());
3✔
82
    compmath::Normalize(&otherv, qty);
3✔
83
    CompMap newv = compmath::Sub(v, otherv);
3✔
84
    compmath::ApplyThreshold(&newv, threshold);
3✔
85
    comp_ = Composition::CreateFromMass(newv);
6✔
86
  }
87

88
  qty_ -= qty;
19✔
89
  Material::Ptr other(new Material(ctx_, qty, c, Package::unpackaged_name()));
38✔
90

91
  // Decay called on the extracted material should have the same dt as for
92
  // this material regardless of composition.
93
  other->prev_decay_time_ = prev_decay_time_;
19✔
94

95
  tracker_.Extract(&other->tracker_);
19✔
96

97
  return other;
19✔
98
}
99

100
void Material::Absorb(Material::Ptr mat) {
785✔
101
  // these calls force lazy evaluation if in lazy decay mode
102
  Composition::Ptr c0 = comp();
785✔
103
  Composition::Ptr c1 = mat->comp();
785✔
104

105
  if (c0 != c1) {
785✔
106
    CompMap v(c0->mass());
10✔
107
    compmath::Normalize(&v, qty_);
10✔
108
    CompMap otherv(c1->mass());
10✔
109
    compmath::Normalize(&otherv, mat->qty_);
10✔
110
    comp_ = Composition::CreateFromMass(compmath::Add(v, otherv));
20✔
111
  }
112

113
  // Set the decay time to the value of the material that had the larger
114
  // quantity.  This helps avoid inheriting erroneous prev decay times if, for
115
  // example, you absorb a material into a zero-quantity material that had a
116
  // prev decay time prior to the current simulation time step.
117
  if (qty_ < mat->qty_) {
785✔
118
    prev_decay_time_ = mat->prev_decay_time_;
8✔
119
  }
120

121
  qty_ += mat->qty_;
785✔
122
  mat->qty_ = 0;
785✔
123
  tracker_.Absorb(&mat->tracker_);
785✔
124
}
785✔
125

126
void Material::Transmute(Composition::Ptr c) {
8✔
127
  comp_ = c;
8✔
128
  tracker_.Modify();
8✔
129

130
  // Presumably the user has chosen the new composition to be accurate for
131
  // the current simulation time.  The next call to decay should not include
132
  // accumulated decay delta t from a composition that no longer exists in
133
  // this material.  This ---------+ condition allows testing to work.
134
  //                               |
135
  //                               |
136
  //                               V
137
  if (ctx_ != NULL && ctx_->time() > prev_decay_time_) {
8✔
138
    prev_decay_time_ = ctx_->time();
1✔
139
  }
140
}
8✔
141

142
Resource::Ptr Material::PackageExtract(double qty,
100,008✔
143
                                       std::string new_package_name) {
144
  if ((qty - qty_) > eps_rsrc()) {
100,008✔
145
    throw ValueError("Attempted to extract more quantity than exists.");
×
146
  }
147

148
  qty_ -= qty;
100,008✔
149
  Material::Ptr other(new Material(ctx_, qty, comp_, new_package_name));
200,016✔
150

151
  // Decay called on the extracted material should have the same dt as for
152
  // this material regardless of composition.
153
  other->prev_decay_time_ = prev_decay_time_;
100,008✔
154

155
  // this call to res_tracker must come first before the parent resource
156
  // state id gets modified
157
  other->tracker_.Package(&tracker_);
100,008✔
158
  if (qty_ > eps_rsrc()) {
100,008✔
159
    tracker_.Modify();
100,000✔
160
  }
161
  return boost::static_pointer_cast<Resource>(other);
100,008✔
162
}
163

164
void Material::ChangePackage(std::string new_package_name) {
14,722✔
165
  if (ctx_ == NULL) {
14,722✔
166
    // no change needed
167
    return;
14,716✔
168
  } else if (new_package_name == Package::unpackaged_name()) {
14,700✔
169
    // unpackaged has functionally no restrictions
170
    package_name_ = new_package_name;
14,694✔
171
    return;
14,694✔
172
  }
173

174
  Package::Ptr p = ctx_->GetPackage(package_name_);
12✔
175
  double min = p->fill_min();
176
  double max = p->fill_max();
177
  if (qty_ >= min && qty_ <= max) {
6✔
178
    package_name_ = new_package_name;
6✔
179
  } else {
180
    throw ValueError("Material quantity is outside of package fill limits.");
×
181
  }
182
  tracker_.Package();
6✔
183
}
184

185
void Material::Decay(int curr_time) {
16✔
186
  if (ctx_ != NULL && ctx_->sim_info().decay == "never") {
30✔
187
    return;
9✔
188
  } else if (curr_time < 0 && ctx_ == NULL) {
15✔
189
    throw ValueError("decay cannot use default time with NULL context");
2✔
190
  }
191

192
  if (curr_time < 0) {
14✔
193
    curr_time = ctx_->time();
8✔
194
  }
195

196
  int dt = curr_time - prev_decay_time_;
14✔
197
  if (dt == 0) {
14✔
198
    return;
199
  }
200

201
  double eps = 1e-3;
202
  const CompMap c = comp_->atom();
7✔
203

204
  // If composition has too many nuclides (i.e. > 100), it is cheaper to
205
  // just do the decay rather than check all the decay constants.
206
  bool decay = c.size() > 100;
7✔
207

208
  uint64_t secs_per_timestep = kDefaultTimeStepDur;
209
  if (ctx_ != NULL) {
7✔
210
    secs_per_timestep = ctx_->sim_info().dt;
6✔
211
  }
212

213
  if (!decay) {
7✔
214
    // Only do the decay calc if one of the nuclides would change in number
215
    // density more than fraction eps.
216
    // i.e. decay if   (1 - eps) > exp(-lambda*dt)
217
    CompMap::const_reverse_iterator it;
218
    for (it = c.rbegin(); it != c.rend(); ++it) {
11✔
219
      int nuc = it->first;
10✔
220
      double lambda_timesteps =
221
          pyne::decay_const(nuc) * static_cast<double>(secs_per_timestep);
10✔
222
      double change =
223
          1.0 - std::exp(-lambda_timesteps * static_cast<double>(dt));
10✔
224
      if (change >= eps) {
10✔
225
        decay = true;
226
        break;
227
      }
228
    }
229
    if (!decay) {
7✔
230
      return;
231
    }
232
  }
233

234
  prev_decay_time_ = curr_time;  // this must go before Transmute call
6✔
235
  Composition::Ptr decayed = comp_->Decay(dt, secs_per_timestep);
6✔
236
  Transmute(decayed);
12✔
237
}
238

239
double Material::DecayHeat() {
1✔
240
  double decay_heat = 0.;
241
  // Pyne decay heat operates with grams, cyclus generally in kilograms.
242
  pyne::Material p_map = pyne::Material(comp_->mass(), qty_ * 1000);
3✔
243
  std::map<int, double> dec_heat = p_map.decay_heat();
1✔
244
  for (auto nuc : dec_heat) {
3✔
245
    if (!std::isnan(nuc.second)) {
2✔
246
      decay_heat += nuc.second;
2✔
247
    }
248
  }
249
  return decay_heat;
1✔
250
}
1✔
251

252
Composition::Ptr Material::comp() const {
×
NEW
253
  throw Error(
×
254
      "comp() const is deprecated - use non-const comp() function."
NEW
255
      " Recompilation should fix the problem.");
×
256
}
257

258
Composition::Ptr Material::comp() {
2,060✔
259
  if (ctx_ != NULL && ctx_->sim_info().decay == "lazy") {
3,813✔
260
    Decay(-1);
7✔
261
  }
262
  return comp_;
2,060✔
263
}
264

265
Material::Material(Context* ctx, double quantity, Composition::Ptr c,
123,468✔
266
                   std::string package_name)
123,468✔
267
    : qty_(quantity),
123,468✔
268
      comp_(c),
269
      tracker_(ctx, this),
123,468✔
270
      ctx_(ctx),
123,468✔
271
      prev_decay_time_(0),
123,468✔
272
      package_name_(package_name) {
246,936✔
273
  if (ctx != NULL) {
123,468✔
274
    prev_decay_time_ = ctx->time();
107,551✔
275
  } else {
276
    tracker_.DontTrack();
15,917✔
277
  }
278
}
123,468✔
279

280
Material::Ptr NewBlankMaterial(double quantity) {
7,610✔
281
  Composition::Ptr comp = Composition::CreateFromMass(CompMap());
15,220✔
282
  return Material::CreateUntracked(quantity, comp);
22,830✔
283
}
284

285
std::string Material::package_name() {
307,609✔
286
  return package_name_;
307,609✔
287
}
288

289
}  // namespace cyclus
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