CYCLUS
Loading...
Searching...
No Matches
material.cc
Go to the documentation of this file.
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
11namespace cyclus {
12
13const ResourceType Material::kType = "Material";
14
16
18 Composition::Ptr c, std::string package_name) {
20 m->tracker_.Create(creator);
21 return m;
22}
23
29
30int Material::qual_id() const {
31 return comp_->id();
32}
33
35 return Material::kType;
36}
37
39 Material* m = new Material(*this);
41 m->tracker_.DontTrack();
42 return c;
43}
44
45void Material::Record(Context* ctx) const {
46 // Note that no time field is needed because the resource ID changes
47 // every time the resource changes - state_id by itself is already unique.
48 ctx_->NewDatum("MaterialInfo")
49 ->AddVal("ResourceId", state_id())
50 ->AddVal("PrevDecayTime", prev_decay_time_)
51 ->Record();
52
53 comp_->Record(ctx);
54}
55
56std::string Material::units() const {
57 return "kg";
58}
59
60double Material::quantity() const {
61 return qty_;
62}
63
65 return boost::static_pointer_cast<Resource>(ExtractQty(qty));
66}
67
69 return ExtractComp(qty, comp_);
70}
71
73 double threshold) {
74 if (qty_ < qty) {
75 throw ValueError("mass extraction causes negative quantity");
76 }
77
78 // TODO: decide if ExtractComp should force lazy-decay by calling comp()
79 if (comp_ != c) {
80 CompMap v(comp_->mass());
81 compmath::Normalize(&v, qty_);
82 CompMap otherv(c->mass());
87 }
88
89 qty_ -= qty;
91
92 // Decay called on the extracted material should have the same dt as for
93 // this material regardless of composition.
94 other->prev_decay_time_ = prev_decay_time_;
95
96 tracker_.Extract(&other->tracker_);
97
98 return other;
99}
100
102 // these calls force lazy evaluation if in lazy decay mode
104 Composition::Ptr c1 = mat->comp();
105
106 if (c0 != c1) {
107 CompMap v(c0->mass());
108 compmath::Normalize(&v, qty_);
109 CompMap otherv(c1->mass());
112 }
113
114 // Set the decay time to the value of the material that had the larger
115 // quantity. This helps avoid inheriting erroneous prev decay times if, for
116 // example, you absorb a material into a zero-quantity material that had a
117 // prev decay time prior to the current simulation time step.
118 if (qty_ < mat->qty_) {
119 prev_decay_time_ = mat->prev_decay_time_;
120 }
121
122 qty_ += mat->qty_;
123 mat->qty_ = 0;
124 tracker_.Absorb(&mat->tracker_);
125}
126
128 comp_ = c;
129 tracker_.Modify();
130
131 // Presumably the user has chosen the new composition to be accurate for
132 // the current simulation time. The next call to decay should not include
133 // accumulated decay delta t from a composition that no longer exists in
134 // this material. This ---------+ condition allows testing to work.
135 // |
136 // |
137 // V
138 if (ctx_ != NULL && ctx_->time() > prev_decay_time_) {
139 prev_decay_time_ = ctx_->time();
140 }
141}
142
144 if ((qty - qty_) > eps_rsrc()) {
145 throw ValueError("Attempted to extract more quantity than exists.");
146 }
147
148 qty_ -= qty;
149 Material::Ptr other(new Material(ctx_, qty, comp_, new_package_name));
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_;
154
155 // this call to res_tracker must come first before the parent resource
156 // state id gets modified
157 other->tracker_.Package(&tracker_);
158 if (qty_ > eps_rsrc()) {
159 tracker_.Modify();
160 }
161 return boost::static_pointer_cast<Resource>(other);
162}
163
165 if (ctx_ == NULL) {
166 // no change needed
167 return;
168 }
170 // unpackaged has functionally no restrictions
171 package_name_ = new_package_name;
172 return;
173 }
174
175 Package::Ptr p = ctx_->GetPackage(package_name_);
176 double min = p->fill_min();
177 double max = p->fill_max();
178 if (qty_ >= min && qty_ <= max) {
179 package_name_ = new_package_name;
180 } else {
181 throw ValueError("Material quantity is outside of package fill limits.");
182 }
183 tracker_.Package();
184}
185
187 if (ctx_ != NULL && ctx_->sim_info().decay == "never") {
188 return;
189 } else if (curr_time < 0 && ctx_ == NULL) {
190 throw ValueError("decay cannot use default time with NULL context");
191 }
192
193 if (curr_time < 0) {
194 curr_time = ctx_->time();
195 }
196
197 int dt = curr_time - prev_decay_time_;
198 if (dt == 0) {
199 return;
200 }
201
202 double eps = 1e-3;
203 const CompMap c = comp_->atom();
204
205 // If composition has too many nuclides (i.e. > 100), it is cheaper to
206 // just do the decay rather than check all the decay constants.
207 bool decay = c.size() > 100;
208
210 if (ctx_ != NULL) {
211 secs_per_timestep = ctx_->sim_info().dt;
212 }
213
214 if (!decay) {
215 // Only do the decay calc if one of the nuclides would change in number
216 // density more than fraction eps.
217 // i.e. decay if (1 - eps) > exp(-lambda*dt)
218 CompMap::const_reverse_iterator it;
219 for (it = c.rbegin(); it != c.rend(); ++it) {
220 int nuc = it->first;
221 double lambda_timesteps = pyne::decay_const(nuc) * static_cast<double>(secs_per_timestep);
222 double change = 1.0 - std::exp(-lambda_timesteps * static_cast<double>(dt));
223 if (change >= eps) {
224 decay = true;
225 break;
226 }
227 }
228 if (!decay) {
229 return;
230 }
231 }
232
233 prev_decay_time_ = curr_time; // this must go before Transmute call
234 Composition::Ptr decayed = comp_->Decay(dt, secs_per_timestep);
236}
237
239 double decay_heat = 0.;
240 // Pyne decay heat operates with grams, cyclus generally in kilograms.
241 pyne::Material p_map = pyne::Material(comp_->mass(), qty_ * 1000);
242 std::map<int, double> dec_heat = p_map.decay_heat();
243 for (auto nuc : dec_heat) {
244 if (!std::isnan(nuc.second)) {
245 decay_heat += nuc.second;
246 }
247 }
248 return decay_heat;
249}
250
252 throw Error("comp() const is deprecated - use non-const comp() function."
253 " Recompilation should fix the problem.");
254}
255
257 if (ctx_ != NULL && ctx_->sim_info().decay == "lazy") {
258 Decay(-1);
259 }
260 return comp_;
261}
262
263Material::Material(Context* ctx, double quantity, Composition::Ptr c, std::string package_name)
264 : qty_(quantity),
265 comp_(c),
266 tracker_(ctx, this),
267 ctx_(ctx),
268 prev_decay_time_(0),
269 package_name_(package_name) {
270 if (ctx != NULL) {
271 prev_decay_time_ = ctx->time();
272 } else {
273 tracker_.DontTrack();
274 }
275}
276
279 return Material::CreateUntracked(quantity, comp);
280}
281
283 return package_name_;
284}
285
286} // namespace cyclus
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:49
static Ptr CreateFromMass(CompMap v)
Creates a new composition from v with its components having appropriate mass-based ratios.
boost::shared_ptr< Composition > Ptr
Definition composition.h:43
A simulation context provides access to necessary simulation-global functions and state.
Definition context.h:145
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
Definition context.cc:351
SimInfo sim_info() const
Return static simulation info.
Definition context.h:313
Package::Ptr GetPackage(std::string name)
Retrieve a registered package.
Definition context.cc:214
virtual int time()
Returns the current simulation timestep.
Definition context.cc:313
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:22
void Record()
Record this datum to its Recorder.
Definition datum.cc:35
A generic mechanism to manually manage exceptions.
Definition error.h:12
The material class is primarily responsible for enabling basic material manipulation while helping en...
Definition material.h:71
virtual std::string package_name()
Returns the package id.
Definition material.cc:282
virtual Resource::Ptr Clone() const
Creates an untracked copy of this material object.
Definition material.cc:38
virtual void Decay(int curr_time=-1)
Updates the material's composition by performing a decay calculation.
Definition material.cc:186
Material(Context *ctx, double quantity, Composition::Ptr c, std::string package_name=Package::unpackaged_name())
Definition material.cc:263
Ptr ExtractQty(double qty)
Same as ExtractComp with c = this->comp().
Definition material.cc:68
static const ResourceType kType
Definition material.h:76
double DecayHeat()
Returns a double with the decay heat of the material in units of W/kg.
Definition material.cc:238
virtual void Record(Context *ctx) const
Records the internal nuclide composition of this resource.
Definition material.cc:45
virtual Resource::Ptr ExtractRes(double qty)
Splits the resource and returns the extracted portion as a new resource object.
Definition material.cc:64
Composition::Ptr comp()
Returns the nuclide composition of this material.
Definition material.cc:256
virtual int qual_id() const
Returns the id of the material's internal nuclide composition.
Definition material.cc:30
virtual std::string units() const
Returns "kg".
Definition material.cc:56
virtual void ChangePackage(std::string new_package_name=Package::unpackaged_name())
Changes the package id.
Definition material.cc:164
void Transmute(Composition::Ptr c)
Changes the material's composition to c without changing its mass.
Definition material.cc:127
virtual double quantity() const
Returns the mass of this material in kg.
Definition material.cc:60
virtual Resource::Ptr PackageExtract(double qty, std::string new_package_name=Package::unpackaged_name())
Definition material.cc:143
boost::shared_ptr< Material > Ptr
Definition material.h:75
virtual ~Material()
Definition material.cc:15
virtual void Absorb(Ptr mat)
Combines material mat with this one. mat's quantity becomes zero.
Definition material.cc:101
static Ptr Create(Agent *creator, double quantity, Composition::Ptr c, std::string package_name=Package::unpackaged_name())
Creates a new material resource that is "live" and tracked.
Definition material.cc:17
Ptr ExtractComp(double qty, Composition::Ptr c, double threshold=eps_rsrc())
Creates a new material by extracting from this one.
Definition material.cc:72
virtual const ResourceType type() const
Returns Material::kType.
Definition material.cc:34
static Ptr CreateUntracked(double quantity, Composition::Ptr c)
Creates a new material resource that does not actually exist as part of the simulation and is untrack...
Definition material.cc:24
boost::shared_ptr< Package > Ptr
Definition package.h:21
static std::string unpackaged_name()
Definition package.h:68
void Modify()
Should be called when the state of a resource changes (e.g.
void Extract(ResTracker *removed)
Should be called when a resource has some quantity removed from it (e.g.
void Package(ResTracker *parent=NULL)
Should be called when a resource's package gets modified.
void Absorb(ResTracker *absorbed)
Should be called when a resource is combined with another.
void DontTrack()
Prevent a resource's heritage from being tracked and recorded.
const int state_id() const
Returns the unique id corresponding to this resource and its current state.
Definition resource.h:42
boost::shared_ptr< Resource > Ptr
Definition resource.h:27
uint64_t dt
Duration in seconds of a single time step in the simulation.
Definition context.h:107
std::string decay
"manual" if use of the decay function is allowed, "never" otherwise
Definition context.h:85
For values that are too big, too small, etc.
Definition error.h:41
Material composed of nuclides.
Definition pyne.h:4801
const uint64_t kDefaultTimeStepDur
Definition context.h:23
Code providing rudimentary logging capability for the Cyclus core.
CompMap Sub(const CompMap &v1, const CompMap &v2)
Does component-wise subtraction of the nuclide quantities of v1 and v2 and returns the result.
Definition comp_math.cc:27
void ApplyThreshold(CompMap *v, double threshold)
All nuclides with quantities below threshold will have their quantity set to zero.
Definition comp_math.cc:45
CompMap Add(const CompMap &v1, const CompMap &v2)
Does component-wise subtraction of the nuclide quantities of v1 and v2 and returns the result.
Definition comp_math.cc:18
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
double eps_rsrc()
an epsilon value to be used by resources
Definition cyc_limits.h:19
std::map< Nuc, double > CompMap
a raw definition of nuclides and corresponding (dimensionless quantities).
Definition composition.h:17
double eps()
a generic epsilon value
Definition cyc_limits.h:12
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
std::string ResourceType
Definition resource.h:17
Material::Ptr NewBlankMaterial(double quantity)
Creates and returns a new material with the specified quantity and a default, meaningless composition...
Definition material.cc:277
double decay_const(int nuc)
Returns the decay constant for a nuclide nuc.
Definition pyne.cc:11914