CYCLUS
Loading...
Searching...
No Matches
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
9extern "C" {
10#include "cram.hpp"
11}
12
13namespace cyclus {
14
15int Composition::next_id_ = 1;
16
18 if (!compmath::ValidNucs(v)) throw ValueError("invalid nuclide in CompMap");
19
21 throw ValueError("negative quantity in CompMap");
22
24 c->atom_ = v;
25 return c;
26}
27
29 if (!compmath::ValidNucs(v)) throw ValueError("invalid nuclide in CompMap");
30
32 throw ValueError("negative quantity in CompMap");
33
35 c->mass_ = v;
36 return c;
37}
38
40 return id_;
41}
42
44 if (atom_.size() == 0) {
45 CompMap::iterator it;
46 for (it = mass_.begin(); it != mass_.end(); ++it) {
47 Nuc nuc = it->first;
48 atom_[nuc] = it->second / pyne::atomic_mass(nuc);
49 }
50 }
51 return atom_;
52}
53
55 if (mass_.size() == 0) {
56 CompMap::iterator it;
57 for (it = atom_.begin(); it != atom_.end(); ++it) {
58 Nuc nuc = it->first;
59 mass_[nuc] = it->second * pyne::atomic_mass(nuc);
60 }
61 }
62 return mass_;
63}
64
65Composition::Ptr Composition::Decay(int delta, uint64_t secs_per_timestep) {
66 int tot_decay = prev_decay_ + delta;
67 if (decay_line_->count(tot_decay) == 1) {
68 // decay_line_ has cached, pre-computed result of this decay
69 return (*decay_line_)[tot_decay];
70 }
71
72 // Calculate a new decayed composition and insert it into the decay chain.
73 // It will automagically appear in the decay chain for all other compositions
74 // that are a part of this decay chain because decay_line_ is a pointer that
75 // all compositions in the chain share.
76 Composition::Ptr decayed = NewDecay(delta, secs_per_timestep);
77 (*decay_line_)[tot_decay] = decayed;
78 return decayed;
79}
80
84
86 if (recorded_) {
87 return;
88 }
89 recorded_ = true;
90
91 CompMap::const_iterator it;
92 CompMap cm = mass(); // force lazy evaluation now
93 compmath::Normalize(&cm, 1);
94 for (it = cm.begin(); it != cm.end(); ++it) {
95 ctx->NewDatum("Compositions")
96 ->AddVal("QualId", id())
97 ->AddVal("NucId", it->first)
98 ->AddVal("MassFrac", it->second)
99 ->Record();
100 }
101}
102
103Composition::Composition() : prev_decay_(0), recorded_(false) {
104 id_ = next_id_;
105 next_id_++;
106 decay_line_ = ChainPtr(new Chain());
107}
108
109Composition::Composition(int prev_decay, ChainPtr decay_line)
110 : recorded_(false), prev_decay_(prev_decay), decay_line_(decay_line) {
111 id_ = next_id_;
112 next_id_++;
113}
114
116 std::string comp = "";
117 for (cyclus::CompMap::const_iterator it = v.begin(); it != v.end(); ++it) {
118 comp += std::to_string(it->first) + std::string(": ") +
119 std::to_string(it->second) + std::string("\n");
120 }
121
122 return comp;
123}
124
125Composition::Ptr Composition::NewDecay(int delta, uint64_t secs_per_timestep) {
126 int tot_decay = prev_decay_ + delta;
127 atom(); // force evaluation of atom-composition if not calculated already
128
129 // the new composition is a part of this decay chain and so is created with a
130 // pointer to the exact same decay_line_.
131 Composition::Ptr decayed(new Composition(tot_decay, decay_line_));
132
133 // FIXME this is only here for testing, see issue #761
134 if (atom_.size() == 0) return decayed;
135
136 // Get intial condition vector
137 std::vector<double> n0(pyne_cram_transmute_info.n, 0.0);
138 CompMap::const_iterator it;
139 int i = -1;
140 for (it = atom_.begin(); it != atom_.end(); ++it) {
141 i = pyne_cram_transmute_nucid_to_i(it->first);
142 if (i < 0) {
143 continue;
144 }
145 n0[i] = it->second;
146 }
147
148 // get decay matrix
149 double t = static_cast<double>(secs_per_timestep) * delta;
150 std::vector<double> decay_matrix(pyne_cram_transmute_info.nnz);
151 for (i = 0; i < pyne_cram_transmute_info.nnz; ++i) {
152 decay_matrix[i] = -pyne_cram_transmute_info.decay_matrix[i] * t;
153 }
154
155 // perform decay
156 std::vector<double> n1(pyne_cram_transmute_info.n);
157 pyne_cram_expm_multiply14(decay_matrix.data(), n0.data(), n1.data());
158
159 // convert back to map
160 CompMap cm;
161 for (i = 0; i < pyne_cram_transmute_info.n; ++i) {
162 if (n1[i] > 0.0) {
163 cm[(pyne_cram_transmute_info.nucids)[i]] = n1[i];
164 }
165 }
166 decayed->atom_ = cm;
167 return decayed;
168}
169
170} // namespace cyclus
static Ptr CreateFromMass(CompMap v)
Creates a new composition from v with its components having appropriate mass-based ratios.
const CompMap & mass()
Returns the unnormalized mass composition.
const CompMap & atom()
Returns the unnormalized atom composition.
static std::string ToString(CompMap v)
Transforms a composition into a printable string, primarily for debugging and logging.
boost::shared_ptr< Composition > Ptr
Definition composition.h:43
static Ptr CreateFromAtom(CompMap v)
Creates a new composition from v with its components having appropriate atom-based ratios.
void Record(Context *ctx)
Records the composition in output database Compositions table (if not done previously).
std::map< int, Composition::Ptr > Chain
a chain containing compositions that are a result of decay from a common ancestor composition.
Definition composition.h:89
Ptr Decay(int delta)
Returns a decayed version of this composition (decayed delta timesteps) assuming a time step is 1/12 ...
boost::shared_ptr< Chain > ChainPtr
Definition composition.h:91
int id()
Returns a unique id associated with this composition.
A simulation context provides access to necessary simulation-global functions and state.
Definition context.h:146
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
Definition context.cc:351
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:21
void Record()
Record this datum to its Recorder.
Definition datum.cc:34
For values that are too big, too small, etc.
Definition error.h:37
const uint64_t kDefaultTimeStepDur
Definition context.h:27
int pyne_cram_transmute_nucid_to_i(int nucid)
Definition cram.c:30168
pyne_cram_transmute_info_t pyne_cram_transmute_info
Definition cram.c:26
void pyne_cram_expm_multiply14(double *A, double *b, double *x)
Definition cram.c:242901
bool ValidNucs(const CompMap &v)
Returns true if all nuclide keys in v are valid.
Definition comp_math.cc:73
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
void Normalize(CompMap *v, double val)
The sum of quantities of all nuclides of v is normalized to val.
Definition comp_math.cc:63
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
std::map< Nuc, double > CompMap
a raw definition of nuclides and corresponding (dimensionless quantities).
Definition composition.h:17
int Nuc
Definition composition.h:14
int CapacityConstraint< T >::next_id_
double atomic_mass(int nuc)
Returns the atomic mass of a nuclide nuc.
Definition pyne.cc:8101