• 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.67
/src/timer.cc
1
#include "platform.h"
2
// Implements the Timer class
3
#include "timer.h"
4

5
#include <iostream>
6
#include <string>
7
#if CYCLUS_IS_PARALLEL
8
#include <omp.h>
9
#endif  // CYCLUS_IS_PARALLEL
10

11
#include "agent.h"
12
#include "error.h"
13
#include "logger.h"
14
#include "pyhooks.h"
15
#include "sim_init.h"
16

17
namespace cyclus {
18

19
void Timer::RunSim() {
262✔
20
  CLOG(LEV_INFO1) << "Simulation set to run from start=" << 0
262✔
NEW
21
                  << " to end=" << si_.duration;
×
22
  CLOG(LEV_INFO1) << "Beginning simulation";
262✔
23

24
  ExchangeManager<Material> matl_manager(ctx_);
262✔
25
  ExchangeManager<Product> genrsrc_manager(ctx_);
262✔
26
  while (time_ < si_.duration) {
7,754✔
27
    CLOG(LEV_INFO1) << "Current time: " << time_;
7,497✔
28

29
    if (want_snapshot_) {
7,497✔
30
      want_snapshot_ = false;
28✔
31
      SimInit::Snapshot(ctx_);
28✔
32
    }
33

34
    // run through phases
35
    DoBuild();
7,497✔
36
    CLOG(LEV_INFO2) << "Beginning Tick for time: " << time_;
7,497✔
37
    DoTick();
7,497✔
38
    CLOG(LEV_INFO2) << "Beginning DRE for time: " << time_;
7,497✔
39
    DoResEx(&matl_manager, &genrsrc_manager);
7,497✔
40
    CLOG(LEV_INFO2) << "Beginning Tock for time: " << time_;
7,496✔
41
    DoTock();
7,496✔
42
    CLOG(LEV_INFO2) << "Beginning Decision for time: " << time_;
7,496✔
43
    DoDecision();
7,496✔
44
    DoDecom();
7,496✔
45

46
#ifdef CYCLUS_WITH_PYTHON
47
    EventLoop();
7,496✔
48
#endif
49

50
    time_++;
7,496✔
51

52
    if (want_kill_) {
7,496✔
53
      break;
54
    }
55
  }
56

57
  ctx_->NewDatum("Finish")
261✔
58
      ->AddVal("EarlyTerm", want_kill_)
59
      ->AddVal("EndTime", time_ - 1)
261✔
60
      ->Record();
522✔
61

62
  SimInit::Snapshot(
261✔
63
      ctx_);  // always do a snapshot at the end of every simulation
64
}
261✔
65

66
void Timer::DoBuild() {
7,497✔
67
  // build queued agents
68
  std::vector<std::pair<std::string, Agent*>> build_list = build_queue_[time_];
7,497✔
69
  for (int i = 0; i < build_list.size(); ++i) {
23,320✔
70
    Agent* m = ctx_->CreateAgent<Agent>(build_list[i].first);
47,469✔
71
    Agent* parent = build_list[i].second;
15,823✔
72
    CLOG(LEV_INFO3) << "Building a " << build_list[i].first << " from parent "
15,823✔
NEW
73
                    << build_list[i].second;
×
74
    m->Build(parent);
15,823✔
75
    if (parent != NULL) {
15,823✔
76
      parent->BuildNotify(m);
63✔
77
    } else {
78
      CLOG(LEV_DEBUG1) << "Hey! Listen! Built an Agent without a Parent.";
15,760✔
79
    }
80
  }
81
}
7,497✔
82

83
void Timer::DoTick() {
7,497✔
84
  for (TimeListener* agent : py_tickers_) {
73,784✔
85
    agent->Tick();
66,287✔
86
  }
87

88
#pragma omp parallel for
7,497✔
89
  for (size_t i = 0; i < cpp_tickers_.size(); ++i) {
90
    cpp_tickers_[i]->Tick();
91
  }
92
}
7,497✔
93

94
void Timer::DoResEx(ExchangeManager<Material>* matmgr,
7,497✔
95
                    ExchangeManager<Product>* genmgr) {
96
  matmgr->Execute();
7,497✔
97
  genmgr->Execute();
7,496✔
98
}
7,496✔
99

100
void Timer::DoTock() {
7,496✔
101
  for (TimeListener* agent : py_tickers_) {
73,781✔
102
    agent->Tock();
66,285✔
103
  }
104

105
#pragma omp parallel for
7,496✔
106
  for (size_t i = 0; i < cpp_tickers_.size(); ++i) {
107
    cpp_tickers_[i]->Tock();
108
  }
109

110
  if (si_.explicit_inventory || si_.explicit_inventory_compact) {
7,496✔
111
    std::set<Agent*> ags = ctx_->agent_list_;
170✔
112
    std::vector<Agent*> agent_vec(ags.begin(), ags.end());
170✔
113
#pragma omp parallel for
170✔
114
    for (int i = 0; i < agent_vec.size(); i++) {
115
      Agent* a = agent_vec[i];
116
      if (a->enter_time() != -1) {
117
        RecordInventories(a);
118
      }
119
    }
120
  }
121
}
7,496✔
122

123
void Timer::DoDecision() {
7,496✔
124
  for (std::map<int, TimeListener*>::iterator agent = tickers_.begin();
7,496✔
125
       agent != tickers_.end();
73,721✔
126
       agent++) {
127
    agent->second->Decision();
66,225✔
128
  }
129
}
7,496✔
130

131
void Timer::RecordInventories(Agent* a) {
510✔
132
  Inventories invs = a->SnapshotInv();
510✔
133
  Inventories::iterator it2;
134
  for (it2 = invs.begin(); it2 != invs.end(); ++it2) {
680✔
135
    std::string name = it2->first;
136
    std::vector<Resource::Ptr> mats = it2->second;
170✔
137
    if (mats.empty() || ResCast<Material>(mats[0]) == NULL) {
680✔
138
      continue;  // skip non-material inventories
139
    }
140

141
    Material::Ptr m = ResCast<Material>(mats[0]->Clone());
170✔
142
    for (int i = 1; i < mats.size(); i++) {
935✔
143
      m->Absorb(ResCast<Material>(mats[i]->Clone()));
3,060✔
144
    }
145
    RecordInventory(a, name, m);
510✔
146
  }
170✔
147
}
510✔
148

149
void Timer::RecordInventory(Agent* a, std::string name, Material::Ptr m) {
170✔
150
  if (si_.explicit_inventory) {
170✔
151
    CompMap c = m->comp()->mass();
180✔
152
    compmath::Normalize(&c, m->quantity());
90✔
153
    CompMap::iterator it;
154
    for (it = c.begin(); it != c.end(); ++it) {
270✔
155
      ctx_->NewDatum("ExplicitInventory")
180✔
156
          ->AddVal("AgentId", a->id())
180✔
157
          ->AddVal("Time", time_)
158
          ->AddVal("InventoryName", name)
159
          ->AddVal("NucId", it->first)
160
          ->AddVal("Quantity", it->second)
161
          ->AddVal("Units", m->units())
360✔
162
          ->Record();
1,260✔
163
    }
164
  }
165

166
  if (si_.explicit_inventory_compact) {
170✔
167
    CompMap c = m->comp()->mass();
80✔
168
    compmath::Normalize(&c, 1);
80✔
169
    ctx_->NewDatum("ExplicitInventoryCompact")
80✔
170
        ->AddVal("AgentId", a->id())
80✔
171
        ->AddVal("Time", time_)
172
        ->AddVal("InventoryName", name)
173
        ->AddVal("Quantity", m->quantity())
80✔
174
        ->AddVal("Units", m->units())
160✔
175
        ->AddVal("Composition", c)
176
        ->Record();
720✔
177
  }
178
}
170✔
179

180
void Timer::DoDecom() {
7,496✔
181
  // decommission queued agents
182
  std::vector<Agent*> decom_list = decom_queue_[time_];
7,496✔
183
  for (int i = 0; i < decom_list.size(); ++i) {
19,165✔
184
    Agent* m = decom_list[i];
11,669✔
185
    if (m->parent() != NULL) {
11,669✔
186
      m->parent()->DecomNotify(m);
824✔
187
    }
188
    m->Decommission();
11,669✔
189
  }
190
}
7,496✔
191

192
void Timer::RegisterTimeListener(TimeListener* agent) {
18,876✔
193
  tickers_[agent->id()] = agent;
18,876✔
194
  if (agent->IsShim()) {
18,876✔
195
    py_tickers_.push_back(agent);
18,876✔
196
  } else {
197
    cpp_tickers_.push_back(agent);
×
198
  }
199
}
18,876✔
200

201
void Timer::UnregisterTimeListener(TimeListener* tl) {
11,668✔
202
  tickers_.erase(tl->id());
11,668✔
203
  if (tl->IsShim()) {
11,668✔
204
    py_tickers_.erase(std::remove(py_tickers_.begin(), py_tickers_.end(), tl),
11,668✔
205
                      py_tickers_.end());
206
  } else {
207
    cpp_tickers_.erase(
×
NEW
208
        std::remove(cpp_tickers_.begin(), cpp_tickers_.end(), tl),
×
209
        cpp_tickers_.end());
210
  }
211
}
11,668✔
212

213
void Timer::SchedBuild(Agent* parent, std::string proto_name, int t) {
21,031✔
214
  if (t <= time_) {
21,031✔
215
    throw ValueError("Cannot schedule build for t < [current-time]");
×
216
  }
217
  build_queue_[t].push_back(std::make_pair(proto_name, parent));
21,031✔
218
}
21,031✔
219

220
void Timer::SchedDecom(Agent* m, int t) {
11,858✔
221
  if (t < time_) {
11,858✔
222
    throw ValueError("Cannot schedule decommission for t < [current-time]");
×
223
  }
224

225
  // It is possible that a single agent may be scheduled for decommissioning
226
  // multiple times. If this happens, we cannot just add it to the queue again
227
  // - the duplicate entries will result in a double delete attempt and
228
  // segfaults and otherwise bad things.  Remove previous decommissionings
229
  // before scheduling this new one.
230
  std::map<int, std::vector<Agent*>>::iterator it;
231
  bool done = false;
232
  for (it = decom_queue_.begin(); it != decom_queue_.end(); ++it) {
109,846✔
233
    int t = it->first;
99,652✔
234
    std::vector<Agent*> ags = it->second;
99,652✔
235
    for (int i = 0; i < ags.size(); i++) {
6,828,715✔
236
      if (ags[i] == m) {
6,730,727✔
237
        CLOG(LEV_WARN) << "scheduled over previous decommissioning of "
1,664✔
NEW
238
                       << m->id();
×
239
        decom_queue_[t].erase(decom_queue_[t].begin() + i);
1,664✔
240
        done = true;
241
        break;
242
      }
243
    }
244
    if (done) {
245
      break;
246
    }
247
  }
248

249
  decom_queue_[t].push_back(m);
11,858✔
250
}
11,858✔
251

252
int Timer::time() {
473,506✔
253
  return time_;
473,506✔
254
}
255

256
void Timer::Reset() {
×
257
  tickers_.clear();
258
  cpp_tickers_.clear();
259
  py_tickers_.clear();
260
  build_queue_.clear();
261
  decom_queue_.clear();
262
  si_ = SimInfo(0);
×
263
}
×
264

265
void Timer::Initialize(Context* ctx, SimInfo si) {
568✔
266
  if (si.m0 < 1 || si.m0 > 12) {
568✔
267
    throw ValueError("Invalid month0; must be between 1 and 12 (inclusive).");
×
268
  }
269

270
  want_kill_ = false;
568✔
271
  ctx_ = ctx;
568✔
272
  time_ = 0;
568✔
273
  si_ = si;
568✔
274

275
  if (si.branch_time > -1) {
568✔
276
    time_ = si.branch_time;
4✔
277
  }
278
}
568✔
279

280
int Timer::dur() {
×
281
  return si_.duration;
×
282
}
283

284
Timer::Timer() : time_(0), si_(0), want_snapshot_(false), want_kill_(false) {}
2,138✔
285

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