• 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

90.38
/src/context.cc
1
#include "platform.h"
2
#include "context.h"
3

4
#include <vector>
5
#include <boost/uuid/uuid_generators.hpp>
6
#if CYCLUS_IS_PARALLEL
7
#include <omp.h>
8
#endif  // CYCLUS_IS_PARALLEL
9

10
#include "error.h"
11
#include "exchange_solver.h"
12
#include "logger.h"
13
#include "pyhooks.h"
14
#include "sim_init.h"
15
#include "timer.h"
16
#include "random_number_generator.h"
17
#include "version.h"
18

19
namespace cyclus {
20

21
double cy_eps = 1e-6;
22
double cy_eps_rsrc = 1e-6;
23

24
SimInfo::SimInfo()
257✔
25
    : duration(0),
257✔
26
      y0(0),
257✔
27
      m0(0),
257✔
28
      dt(kDefaultTimeStepDur),
257✔
29
      decay("manual"),
257✔
30
      branch_time(-1),
257✔
31
      explicit_inventory(false),
257✔
32
      explicit_inventory_compact(false),
257✔
33
      parent_sim(boost::uuids::nil_uuid()),
257✔
34
      parent_type("init"),
257✔
35
      seed(kDefaultSeed),
257✔
36
      stride(kDefaultStride) {}
257✔
37

38
SimInfo::SimInfo(int dur, int y0, int m0, std::string handle)
2,261✔
39
    : duration(dur),
2,261✔
40
      y0(y0),
2,261✔
41
      m0(m0),
2,261✔
42
      dt(kDefaultTimeStepDur),
2,261✔
43
      decay("manual"),
2,261✔
44
      branch_time(-1),
2,261✔
45
      handle(handle),
2,261✔
46
      explicit_inventory(false),
2,261✔
47
      explicit_inventory_compact(false),
2,261✔
48
      parent_sim(boost::uuids::nil_uuid()),
2,261✔
49
      parent_type("init"),
2,261✔
50
      seed(kDefaultSeed),
2,261✔
51
      stride(kDefaultStride) {}
2,261✔
52

53
SimInfo::SimInfo(int dur, int y0, int m0, std::string handle, std::string d)
450✔
54
    : duration(dur),
450✔
55
      y0(y0),
450✔
56
      m0(m0),
450✔
57
      dt(kDefaultTimeStepDur),
450✔
58
      decay(d),
450✔
59
      branch_time(-1),
450✔
60
      handle(handle),
450✔
61
      explicit_inventory(false),
450✔
62
      explicit_inventory_compact(false),
450✔
63
      parent_sim(boost::uuids::nil_uuid()),
450✔
64
      parent_type("init"),
450✔
65
      seed(kDefaultSeed),
450✔
66
      stride(kDefaultStride) {}
450✔
67

NEW
68
SimInfo::SimInfo(int dur, boost::uuids::uuid parent_sim, int branch_time,
×
NEW
69
                 std::string parent_type, std::string handle)
×
70
    : duration(dur),
×
71
      y0(-1),
×
72
      m0(-1),
×
73
      dt(kDefaultTimeStepDur),
×
74
      decay("manual"),
×
75
      parent_sim(parent_sim),
×
76
      parent_type(parent_type),
×
77
      branch_time(branch_time),
×
78
      explicit_inventory(false),
×
79
      explicit_inventory_compact(false),
×
80
      handle(handle),
×
81
      seed(kDefaultSeed),
×
82
      stride(kDefaultStride) {}
×
83

84
Context::Context(Timer* ti, Recorder* rec)
1,078✔
85
    : ti_(ti), rec_(rec), solver_(NULL), trans_id_(0), si_(0) {
1,078✔
86
  rng_ = new RandomNumberGenerator();
1,078✔
87
}
1,078✔
88

89
Context::~Context() {
1,067✔
90
  if (solver_ != NULL) {
1,067✔
91
    delete solver_;
262✔
92
  }
93
  if (rng_ != NULL) {
1,067✔
94
    delete rng_;
1,067✔
95
  }
96
  // initiate deletion of agents that don't have parents.
97
  // dealloc will propagate through hierarchy as agents delete their children
98
  std::vector<Agent*> to_del;
99
  std::set<Agent*>::iterator it;
100
  for (it = agent_list_.begin(); it != agent_list_.end(); ++it) {
10,745✔
101
    if ((*it)->parent() == NULL) {
9,678✔
102
      to_del.push_back(*it);
7,718✔
103
    }
104
  }
105
  for (int i = 0; i < to_del.size(); ++i) {
8,785✔
106
    DelAgent(to_del[i]);
7,718✔
107
  }
108
}
1,067✔
109

110
void Context::DelAgent(Agent* m) {
20,232✔
111
  int n = agent_list_.erase(m);
20,232✔
112
  if (n == 1) {
20,232✔
113
    PyDelAgent(m->id());
20,230✔
114
    delete m;
20,230✔
115
    m = NULL;
116
  }
117
}
20,232✔
118

119
void Context::SchedBuild(Agent* parent, std::string proto_name, int t) {
21,031✔
120
#pragma omp critical
42,062✔
121
  {
122
    if (t == -1) {
21,031✔
123
      t = time() + 1;
20,875✔
124
    }
125
    int pid = (parent != NULL) ? parent->id() : -1;
21,031✔
126
    ti_->SchedBuild(parent, proto_name, t);
21,031✔
127
    NewDatum("BuildSchedule")
128
        ->AddVal("ParentId", pid)
129
        ->AddVal("Prototype", proto_name)
130
        ->AddVal("SchedTime", time())
21,031✔
131
        ->AddVal("BuildTime", t)
132
        ->Record();
84,124✔
133
  }
134
}
21,031✔
135

136
void Context::SchedDecom(Agent* m, int t) {
11,858✔
137
#pragma omp critical
23,716✔
138
  {
139
    if (t == -1) {
11,858✔
140
      t = time();
11,656✔
141
    }
142
    ti_->SchedDecom(m, t);
11,858✔
143
    NewDatum("DecomSchedule")
144
        ->AddVal("AgentId", m->id())
11,858✔
145
        ->AddVal("SchedTime", time())
11,858✔
146
        ->AddVal("DecomTime", t)
147
        ->Record();
47,432✔
148
  }
149
}
11,858✔
150

151
boost::uuids::uuid Context::sim_id() {
125✔
152
  return rec_->sim_id();
125✔
153
}
154

155
void Context::AddPrototype(std::string name, Agent* p) {
1,501✔
156
  AddPrototype(name, p, false);
1,501✔
157
}
1,500✔
158

159
void Context::AddPrototype(std::string name, Agent* p, bool overwrite) {
1,502✔
160
  if (!overwrite && protos_.find(name) != protos_.end()) {
1,502✔
161
    throw KeyError("Prototype name " + name + " has already been added" +
2✔
162
                   " and cannot be overwritten.");
2✔
163
  }
164

165
  protos_[name] = p;
1,501✔
166
  // explicit snapshot required for in situ (non-xml) prototype addition
167
  SimInit::SnapAgent(p);
1,501✔
168
  NewDatum("Prototypes")
169
      ->AddVal("Prototype", name)
170
      ->AddVal("AgentId", p->id())
1,501✔
171
      ->AddVal("Spec", p->spec())
1,501✔
172
      ->Record();
9,006✔
173

174
  std::string spec = p->spec();
175
  if (rec_ver_.count(spec) == 0) {
176
    rec_ver_.insert(spec);
177
    NewDatum("AgentVersions")
178
        ->AddVal("Spec", spec)
179
        ->AddVal("Version", p->version())
2,672✔
180
        ->Record();
6,680✔
181
  }
182
}
1,501✔
183

184
void Context::AddRecipe(std::string name, Composition::Ptr c) {
448✔
185
  recipes_[name] = c;
448✔
186
  NewDatum("Recipes")
187
      ->AddVal("Recipe", name)
188
      ->AddVal("QualId", c->id())
448✔
189
      ->Record();
1,792✔
190
}
448✔
191

192
Composition::Ptr Context::GetRecipe(std::string name) {
13,751✔
193
  if (recipes_.count(name) == 0) {
194
    throw KeyError("Invalid recipe name " + name);
×
195
  }
196
  return recipes_[name];
27,502✔
197
}
198

199
void Context::AddPackage(std::string name, double fill_min, double fill_max,
14✔
200
                         std::string strategy) {
201
  if (packages_.count(name) == 0) {
202
    Package::Ptr pkg = Package::Create(name, fill_min, fill_max, strategy);
26✔
203
    packages_[name] = pkg;
13✔
204
    RecordPackage(pkg);
26✔
205
  } else {
206
    throw KeyError("Package " + name + " already exists!");
2✔
207
  }
208
}
13✔
209

210
void Context::RecordPackage(Package::Ptr pkg) {
392✔
211
  NewDatum("Packages")
212
      ->AddVal("PackageName", pkg->name())
392✔
213
      ->AddVal("FillMin", pkg->fill_min())
214
      ->AddVal("FillMax", pkg->fill_max())
215
      ->AddVal("Strategy", pkg->strategy())
392✔
216
      ->Record();
1,960✔
217
}
392✔
218

219
Package::Ptr Context::GetPackage(std::string name) {
25✔
220
  if (name == Package::unpackaged_name()) {
25✔
221
    return Package::unpackaged();
3✔
222
  }
223
  if (packages_.size() == 0) {
22✔
224
    throw KeyError("No user-created packages exist");
×
225
  }
226
  if (packages_.count(name) == 0) {
227
    throw KeyError("Invalid package name " + name);
×
228
  }
229
  return packages_[name];
22✔
230
}
231

232
void Context::AddTransportUnit(std::string name, int fill_min, int fill_max,
3✔
233
                               std::string strategy) {
234
  if (transport_units_.count(name) == 0) {
235
    TransportUnit::Ptr tu =
236
        TransportUnit::Create(name, fill_min, fill_max, strategy);
4✔
237
    transport_units_[name] = tu;
2✔
238
    RecordTransportUnit(tu);
4✔
239
  } else {
240
    throw KeyError("TransportUnit " + name + " already exists!");
2✔
241
  }
242
}
2✔
243

244
void Context::RecordTransportUnit(TransportUnit::Ptr tu) {
2✔
245
  NewDatum("TransportUnits")
246
      ->AddVal("TransportUnitName", tu->name())
2✔
247
      ->AddVal("FillMin", tu->fill_min())
248
      ->AddVal("FillMax", tu->fill_max())
249
      ->AddVal("Strategy", tu->strategy())
2✔
250
      ->Record();
10✔
251
}
2✔
252

253
/// Retrieve a registered transport unit
254
TransportUnit::Ptr Context::GetTransportUnit(std::string name) {
7✔
255
  if (name == TransportUnit::unrestricted_name()) {
7✔
256
    return TransportUnit::unrestricted();
5✔
257
  }
258
  if (transport_units_.size() == 0) {
2✔
259
    throw KeyError("No user-created transport units exist");
×
260
  }
261
  if (transport_units_.count(name) == 0) {
262
    throw KeyError("Invalid transport unit name " + name);
×
263
  }
264
  return transport_units_[name];
2✔
265
}
266

267
void Context::InitSim(SimInfo si) {
524✔
268
  NewDatum("Info")
269
      ->AddVal("Handle", si.handle)
524✔
270
      ->AddVal("InitialYear", si.y0)
271
      ->AddVal("InitialMonth", si.m0)
272
      ->AddVal("Duration", si.duration)
273
      ->AddVal("Seed", static_cast<int>(si.seed))
524✔
274
      ->AddVal("Stride", static_cast<int>(si.stride))
524✔
275
      ->AddVal("ParentSimId", si.parent_sim)
276
      ->AddVal("ParentType", si.parent_type)
524✔
277
      ->AddVal("BranchTime", si.branch_time)
278
      ->AddVal("CyclusVersion", std::string(version::core()))
1,048✔
279
      ->AddVal("CyclusVersionDescribe", std::string(version::describe()))
1,048✔
280
      ->AddVal("SqliteVersion", std::string(version::sqlite3()))
1,048✔
281
      ->AddVal("Hdf5Version", std::string(version::hdf5()))
1,048✔
282
      ->AddVal("BoostVersion", std::string(version::boost()))
1,048✔
283
      ->AddVal("LibXML2Version", std::string(version::xml2()))
1,048✔
284
      ->AddVal("CoinCBCVersion", std::string(version::coincbc()))
1,048✔
285
      ->Record();
9,956✔
286

287
  NewDatum("DecayMode")->AddVal("Decay", si.decay)->Record();
1,572✔
288

289
  NewDatum("InfoExplicitInv")
290
      ->AddVal("RecordInventory", si.explicit_inventory)
291
      ->AddVal("RecordInventoryCompact", si.explicit_inventory_compact)
292
      ->Record();
1,048✔
293

294
  // TODO: when the backends get uint64_t support, the static_cast here should
295
  // be removed.
296
  NewDatum("TimeStepDur")
297
      ->AddVal("DurationSecs", static_cast<int>(si.dt))
524✔
298
      ->Record();
1,048✔
299

300
  NewDatum("Epsilon")
301
      ->AddVal("GenericEpsilon", si.eps)
302
      ->AddVal("ResourceEpsilon", si.eps_rsrc)
303
      ->Record();
1,048✔
304

305
  NewDatum("XMLPPInfo")
306
      ->AddVal("LibXMLPlusPlusVersion", std::string(version::xmlpp()))
1,048✔
307
      ->Record();
1,572✔
308

309
  si_ = si;
524✔
310
  ti_->Initialize(this, si);
524✔
311
  rng_->Initialize(si);
524✔
312
}
524✔
313

314
int Context::time() {
473,506✔
315
  return ti_->time();
473,506✔
316
}
317

318
int Context::random() {
2✔
319
  return rng_->random();
2✔
320
}
321

322
double Context::random_01() {
1✔
323
  return rng_->random_01();
1✔
324
}
325

326
int Context::random_uniform_int(int low, int high) {
1✔
327
  return rng_->random_uniform_int(low, high);
1✔
328
}
329

330
double Context::random_uniform_real(double low, double high) {
1✔
331
  return rng_->random_uniform_real(low, high);
1✔
332
}
333

334
double Context::random_normal_real(double mean, double std_dev, double low,
1✔
335
                                   double high) {
336
  return rng_->random_normal_real(mean, std_dev, low, high);
1✔
337
}
338

339
int Context::random_normal_int(double mean, double std_dev, int low, int high) {
1✔
340
  return rng_->random_normal_int(mean, std_dev, low, high);
1✔
341
}
342

343
void Context::RegisterTimeListener(TimeListener* tl) {
18,876✔
344
  ti_->RegisterTimeListener(tl);
18,876✔
345
}
18,876✔
346

347
void Context::UnregisterTimeListener(TimeListener* tl) {
11,668✔
348
  ti_->UnregisterTimeListener(tl);
11,668✔
349
}
11,668✔
350

351
Datum* Context::NewDatum(std::string title) {
558,501✔
352
  return rec_->NewDatum(title);
1,117,002✔
353
}
354

355
void Context::Snapshot() {
56✔
356
  ti_->Snapshot();
56✔
357
}
56✔
358

359
void Context::KillSim() {
4✔
360
  ti_->KillSim();
4✔
361
}
4✔
362

363
}  // 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