CYCLUS
composition.cc
Go to the documentation of this file.
1 #include "composition.h"
2 
3 #include "comp_math.h"
4 #include "context.h"
5 #include "decayer.h"
6 #include "error.h"
7 #include "recorder.h"
8 #include "pyne_decay.h"
9 
10 namespace cyclus {
11 
12 int Composition::next_id_ = 1;
13 
15  if (!compmath::ValidNucs(v))
16  throw ValueError("invalid nuclide in CompMap");
17 
18  if (!compmath::AllPositive(v))
19  throw ValueError("negative quantity in CompMap");
20 
22  c->atom_ = v;
23  return c;
24 }
25 
27  if (!compmath::ValidNucs(v))
28  throw ValueError("invalid nuclide in CompMap");
29 
30  if (!compmath::AllPositive(v))
31  throw ValueError("negative quantity in CompMap");
32 
34  c->mass_ = v;
35  return c;
36 }
37 
39  return id_;
40 }
41 
43  if (atom_.size() == 0) {
44  CompMap::iterator it;
45  for (it = mass_.begin(); it != mass_.end(); ++it) {
46  Nuc nuc = it->first;
47  atom_[nuc] = it->second / pyne::atomic_mass(nuc);
48  }
49  }
50  return atom_;
51 }
52 
54  if (mass_.size() == 0) {
55  CompMap::iterator it;
56  for (it = atom_.begin(); it != atom_.end(); ++it) {
57  Nuc nuc = it->first;
58  mass_[nuc] = it->second * pyne::atomic_mass(nuc);
59  }
60  }
61  return mass_;
62 }
63 
64 Composition::Ptr Composition::Decay(int delta, uint64_t secs_per_timestep) {
65  int tot_decay = prev_decay_ + delta;
66  if (decay_line_->count(tot_decay) == 1) {
67  // decay_line_ has cached, pre-computed result of this decay
68  return (*decay_line_)[tot_decay];
69  }
70 
71  // Calculate a new decayed composition and insert it into the decay chain.
72  // It will automagically appear in the decay chain for all other compositions
73  // that are a part of this decay chain because decay_line_ is a pointer that
74  // all compositions in the chain share.
75  Composition::Ptr decayed = NewDecay(delta, secs_per_timestep);
76  (*decay_line_)[tot_decay] = decayed;
77  return decayed;
78 }
79 
81  return Decay(delta, kDefaultTimeStepDur);
82 }
83 
85  if (recorded_) {
86  return;
87  }
88  recorded_ = true;
89 
90  CompMap::const_iterator it;
91  CompMap cm = mass(); // force lazy evaluation now
92  compmath::Normalize(&cm, 1);
93  for (it = cm.begin(); it != cm.end(); ++it) {
94  ctx->NewDatum("Compositions")
95  ->AddVal("QualId", id())
96  ->AddVal("NucId", it->first)
97  ->AddVal("MassFrac", it->second)
98  ->Record();
99  }
100 }
101 
102 Composition::Composition() : prev_decay_(0), recorded_(false) {
103  id_ = next_id_;
104  next_id_++;
105  decay_line_ = ChainPtr(new Chain());
106 }
107 
108 Composition::Composition(int prev_decay, ChainPtr decay_line)
109  : recorded_(false),
110  prev_decay_(prev_decay),
111  decay_line_(decay_line) {
112  id_ = next_id_;
113  next_id_++;
114 }
115 
116 Composition::Ptr Composition::NewDecay(int delta, uint64_t secs_per_timestep) {
117  int tot_decay = prev_decay_ + delta;
118  atom(); // force evaluation of atom-composition if not calculated already
119 
120  // the new composition is a part of this decay chain and so is created with a
121  // pointer to the exact same decay_line_.
122  Composition::Ptr decayed(new Composition(tot_decay, decay_line_));
123 
124  // FIXME this is only here for testing, see issue #761
125  if (atom_.size() == 0)
126  return decayed;
127 
128  decayed->atom_ = pyne::decayers::decay(atom_, static_cast<double>(secs_per_timestep) * delta);
129  return decayed;
130 }
131 
132 } // namespace cyclus
boost::shared_ptr< Composition > Ptr
Definition: composition.h:43
bool AllPositive(const CompMap &v)
Returns true if all nuclides in v have quantities greater than or equal to zero.
Definition: comp_math.cc:83
For values that are too big, too small, etc.
Definition: error.h:41
int id()
Returns a unique id associated with this composition.
Definition: composition.cc:38
bool ValidNucs(const CompMap &v)
Returns true if all nuclide keys in v are valid.
Definition: comp_math.cc:73
ChainPtr decay_line_
Definition: composition.h:90
Ptr Decay(int delta)
Returns a decayed version of this composition (decayed delta timesteps) assuming a time step is 1/12 ...
Definition: composition.cc:80
std::map< Nuc, double > CompMap
a raw definition of nuclides and corresponding (dimensionless quantities).
Definition: composition.h:17
std::map< int, Composition::Ptr > Chain
a chain containing compositions that are a result of decay from a common ancestor composition...
Definition: composition.h:84
const uint64_t kDefaultTimeStepDur
Definition: context.h:21
void Record(Context *ctx)
Records the composition in output database Compositions table (if not done previously).
Definition: composition.cc:84
Datum * AddVal(const char *field, boost::spirit::hold_any val, std::vector< int > *shape=NULL)
Add an arbitrary field-value pair to the datum.
Definition: datum.cc:12
comp_map decay(comp_map, double)
A simulation context provides access to necessary simulation-global functions and state...
Definition: context.h:128
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
const CompMap & mass()
Returns the unnormalized mass composition.
Definition: composition.cc:53
void Record()
Record this datum to its Recorder.
Definition: datum.cc:24
double atomic_mass(int nuc)
Returns the atomic mass of a nuclide nuc.
Definition: pyne.cc:10440
static Ptr CreateFromAtom(CompMap v)
Creates a new composition from v with its components having appropriate atom-based ratios...
Definition: composition.cc:14
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
Definition: context.cc:237
void Normalize(CompMap *v, double val)
The sum of quantities of all nuclides of v is normalized to val.
Definition: comp_math.cc:63
boost::shared_ptr< Chain > ChainPtr
Definition: composition.h:86
const CompMap & atom()
Returns the unnormalized atom composition.
Definition: composition.cc:42
static Ptr CreateFromMass(CompMap v)
Creates a new composition from v with its components having appropriate mass-based ratios...
Definition: composition.cc:26
int Nuc
Definition: composition.h:12