9 #include <boost/filesystem.hpp> 10 #include <libxml++/libxml++.h> 28 namespace fs = boost::filesystem;
31 std::ifstream file_stream(file.c_str());
33 throw IOError(
"The file '" + file +
"' could not be loaded.");
36 stream << file_stream.rdbuf();
42 if (format ==
"none") {
44 inext = fs::path(file).extension().string();
48 if (inext ==
".json" || format ==
"json") {
51 }
else if (inext ==
".py" || format ==
"py") {
58 std::stringstream input;
64 std::stringstream input;
70 std::vector<AgentSpec> specs;
71 std::set<std::string> unique;
75 for (
int i = 0; i < n; ++i) {
77 if (unique.count(spec.str()) == 0) {
78 specs.push_back(spec);
79 unique.insert(spec.str());
83 if (specs.size() == 0) {
84 throw ValidationError(
"failed to parse archetype specs from input file");
95 std::stringstream schema(
"");
99 std::vector<AgentSpec> specs =
ParseSpecs(infile, format);
101 std::map<std::string, std::string> subschemas;
104 subschemas[
"region"] =
"";
105 subschemas[
"inst"] =
"";
106 subschemas[
"facility"] =
"";
108 for (
int i = 0; i < specs.size(); ++i) {
110 subschemas[m->
kind()] +=
"<element name=\"" + specs[i].alias() +
"\">\n";
112 subschemas[m->
kind()] +=
"</element>\n";
117 std::map<std::string, std::string>::iterator it;
118 for (it = subschemas.begin(); it != subschemas.end(); ++it) {
120 size_t pos = master.find(search_str);
121 if (pos != std::string::npos) {
122 master.replace(pos, search_str.size(), it->second);
132 if (basis_str ==
"atom") {
134 }
else if (basis_str ==
"mass") {
137 throw IOError(basis_str +
" basis is not 'mass' or 'atom'.");
145 for (
int i = 0; i < nnucs; i++) {
148 value = strtod(nuclide->
GetString(
"comp").c_str(), NULL);
164 const std::string format,
bool ms_print) : b_(b), rec_(r) {
170 std::stringstream input;
175 std::stringstream ss;
176 parser_->Document()->write_to_stream_formatted(ss);
211 std::map<std::string, double> commod_priority;
214 int num_commods = xqe.
NMatches(query);
215 for (
int i = 0; i < num_commods; i++) {
218 priority = OptionalQuery<double>(qe,
"solution_priority", -1);
219 commod_priority[
name] = priority;
223 std::map<std::string, double>::iterator it;
224 for (it = commod_priority.begin(); it != commod_priority.end(); ++it) {
226 ->
AddVal(
"Commodity", it->first)
227 ->
AddVal(
"SolutionPriority", it->second)
232 string config =
"config";
233 string greedy =
"greedy";
234 string coinor =
"coin-or";
235 string solver_name = greedy;
237 if (xqe.
NMatches(
"/*/control/solver") == 1) {
238 qe = xqe.
SubTree(
"/*/control/solver");
242 exclusive = cyclus::OptionalQuery<bool>(qe,
"allow_exclusive_orders",
247 if (qe->NMatches(
std::string(
"exclusive_orders_only")) != 0) {
248 std::stringstream ss;
249 ss <<
"Use of 'exclusive_orders_only' is deprecated." 250 <<
" Please see http://fuelcycle.org/user/input_specs/control.html";
251 Warn<DEPRECATION_WARNING>(ss.str());
256 std::stringstream ss;
257 ss <<
"You have set allow_exclusive_orders to False." 258 <<
" Many archetypes (e.g., :cycamore:Reactor will not work" 259 <<
" as intended with this feature turned off.";
260 Warn<VALUE_WARNING>(ss.str());
264 ->
AddVal(
"Solver", solver_name)
265 ->
AddVal(
"ExclusiveOrders", exclusive)
269 if (solver_name == greedy) {
270 query =
string(
"/*/control/solver/config/greedy/preconditioner");
271 string precon_name = cyclus::OptionalQuery<string>(&xqe, query, greedy);
273 ->
AddVal(
"Preconditioner", precon_name)
275 }
else if (solver_name == coinor) {
276 query =
string(
"/*/control/solver/config/coin-or/timeout");
277 double timeout = cyclus::OptionalQuery<double>(&xqe, query, -1);
278 query =
string(
"/*/control/solver/config/coin-or/verbose");
279 bool verbose = cyclus::OptionalQuery<bool>(&xqe, query,
false);
280 query =
string(
"/*/control/solver/config/coin-or/mps");
281 bool mps = cyclus::OptionalQuery<bool>(&xqe, query,
false);
283 ->
AddVal(
"Timeout", timeout)
284 ->
AddVal(
"Verbose", verbose)
288 throw ValueError(
"unknown solver name: " + solver_name);
293 std::map<std::string, double>* commod_priority) {
294 double max = std::max_element(
295 commod_priority->begin(),
296 commod_priority->end(),
302 std::map<std::string, double>::iterator it;
303 for (it = commod_priority->begin();
304 it != commod_priority->end();
306 if (it->second < 1) {
307 it->second = max + 1;
310 <<
" is " << it->second;
318 int num_recipes = xqe.
NMatches(query);
319 for (
int i = 0; i < num_recipes; i++) {
331 for (
int i = 0; i < specs.size(); ++i) {
332 specs_[specs[i].alias()] = specs[i];
337 std::map<std::string, std::string> schema_paths;
338 schema_paths[
"Region"] =
"/*/region";
339 schema_paths[
"Inst"] =
"/*/region/institution";
340 schema_paths[
"Facility"] =
"/*/facility";
346 std::map<std::string, std::string>::iterator it;
347 for (it = schema_paths.begin(); it != schema_paths.end(); it++) {
348 int num_agents = xqe.
NMatches(it->second);
349 for (
int i = 0; i < num_agents; i++) {
359 agent->Agent::InfileToDb(qe,
DbInit(agent,
true));
364 std::vector<Cond> conds;
366 conds.push_back(
Cond(
"SimTime",
"==", static_cast<int>(0)));
367 conds.push_back(
Cond(
"AgentId",
"==", agent->
id()));
372 agent->Agent::InitFrom(&pi);
381 int nregions = xqe.
NMatches(schema_paths[
"Region"]);
382 for (
int i = 0; i < nregions; ++i) {
387 int ninsts = qe->
NMatches(
"institution");
388 for (
int j = 0; j < ninsts; ++j) {
393 int nfac = qe2->
NMatches(
"initialfacilitylist/entry");
394 for (
int k = 0; k < nfac; ++k) {
398 int number = atoi(qe3->
GetString(
"number").c_str());
399 for (
int z = 0; z < number; ++z) {
410 if (parent != NULL) {
422 if (qe->
NMatches(
"simhandle") > 0) {
428 int dur = strtol(dur_str.c_str(), NULL, 10);
431 int m0 = strtol(m0_str.c_str(), NULL, 10);
434 int y0 = strtol(y0_str.c_str(), NULL, 10);
436 std::string d = OptionalQuery<std::string>(qe,
"decay",
"manual");
438 SimInfo si(dur, y0, m0, handle, d);
440 si.explicit_inventory = OptionalQuery<bool>(qe,
"explicit_inventory",
false);
441 si.explicit_inventory_compact = OptionalQuery<bool>(qe,
"explicit_inventory_compact",
false);
447 double eps_ = OptionalQuery<double>(qe,
"tolerance_generic", 1e-6);
451 double eps_rsrc_ = OptionalQuery<double>(qe,
"tolerance_resource", 1e-6);
XMLFileLoader(Recorder *r, QueryableBackend *b, std::string schema_file, const std::string input_file="", const std::string format="none", bool ms_print=false)
Create a new loader reading from the xml simulation input file and writing to and initializing the ba...
virtual std::string master_schema()
std::string BuildMasterSchema(std::string schema_path, std::string infile, std::string format)
Builds and returns a master cyclus input xml schema that includes the sub-schemas defined by all inst...
boost::shared_ptr< XMLParser > parser_
the parser
boost::uuids::uuid sim_id()
returns the unique id associated with this cyclus simulation.
boost::shared_ptr< Composition > Ptr
std::string schema_path_
filepath to the schema
double b(int nuc)
Computes the scattering length [cm] from the coherent and incoherent components.
std::string file_
the input file name
void AddRecipe(std::string name, Composition::Ptr c)
Adds a composition recipe to a simulation-wide accessible list.
void LoadRawStringstreamFromFile(std::stringstream &stream, std::string file)
Reads the given file path into the passed stream without modification.
void LoadRecipes()
Method to load recipes from either the primary input file or a recipeBook catalog.
virtual int NMatches(std::string query)
investigates the current status and returns the number of elements matching a query ...
virtual void InfileToDb(InfileTree *qe, DbInit di)
Translates info for the agent from an input file to the database by reading parameters from the passe...
virtual void LoadInitialAgents()
Creates all initial agent instances from the input file.
double cy_eps
generic epsilon values
Agent * BuildAgent(std::string proto, Agent *parent)
Creates and builds an agent, notifying its parent.
a less-than comparison for pairs
For values that are too big, too small, etc.
A class for extracting information from a given XML parser.
std::string format_
the input file format
For validating files received via IO.
static const bool kDefaultExclusive
default value to allow exclusive orders or not
virtual const int id() const
The agent instance's unique ID within a simulation.
std::string name(int nuc)
void LoadControlParams()
Method to load the simulation control parameters.
virtual std::string GetElementName(int index=0)
investigates the current status and returns a string representing the name of a given index ...
virtual void BuildNotify(Agent *m)
Called when a new child of this agent has just been built.
std::map< Nuc, double > CompMap
a raw definition of nuclides and corresponding (dimensionless quantities).
A helper class to hold xml file data and provide automatic validation.
virtual std::string schema()
Returns an agent's xml rng schema for initializing from input files.
virtual void Build(Agent *parent)
Called when the agent enters the smiulation as an active participant and is only ever called once...
virtual std::string GetString(std::string query, int index=0)
investigates the current status and returns a string representing the content of a query at a given i...
void InitSim(SimInfo si)
Initializes the simulation time parameters.
const uint64_t kDefaultTimeStepDur
for failed reading/writing to files, network connections, etc..
const std::string kind() const
Returns a string that describes the agent subclass (e.g.
int parent(int nuc, unsigned int rx, std::string z="n")
Wrapper class for QueryableBackends that injects a set of Cond's into every query before being execut...
std::map< std::string, AgentSpec > specs_
Information helpful for simulation users and developers alike - least verbose.
T * CreateAgent(std::string proto_name)
Create a new agent by cloning the named prototype.
Datum * AddVal(const char *field, boost::spirit::hold_any val, std::vector< int > *shape=NULL)
Add an arbitrary field-value pair to the datum.
Composition::Ptr ReadRecipe(InfileTree *qe)
Creates a composition from the recipe in the query engine.
void AddPrototype(std::string name, Agent *m)
Adds a prototype to a simulation-wide accessible list, a prototype can not be added more than once...
DbInit provides an interface for agents to record data to the output db that automatically injects th...
void Flush()
Flushes all buffered Datum objects and flushes all registered backends.
bool ms_print_
flag to indicate printing master schema
static void Snapshot(Context *ctx)
Records a snapshot of the current state of the simulation being managed by ctx into the simulation's ...
Collects and manages output data generation for the cyclus core and agents during a simulation...
double cy_eps_rsrc
epsilon values to be used by resources
Wrapper class for QueryableBackends that injects prefix in front of the title/table for every query b...
InfileTree * SubTree(std::string query, int index=0)
populates a child infile based on a query and index
Container for a static simulation-global parameters that both describe the simulation and affect its ...
Code providing rudimentary logging capability for the Cyclus core.
A simulation context provides access to necessary simulation-global functions and state...
The abstract base class used by all types of agents that live and interact in a simulation.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
A type to represent variable-length array of bytes for dumping to a cyclus output database...
void LoadSolver()
Method to load the simulation exchange solver.
const double pi
pi = 3.14159265359
std::vector< AgentSpec > ParseSpecs(std::string infile, std::string format)
Returns a list of the full module+agent spec for all agents in the given input file.
void ProcessCommodities(std::map< std::string, double > *commodity_priority)
Processes commodity priorities, such that any without a defined priority (i.e., are nonpositive)...
virtual void InitFrom(QueryableBackend *b)
Intializes an agent's internal state from the database.
Represents a condition used to filter rows returned by a query.
void LoadStringstreamFromFile(std::stringstream &stream, std::string file, std::string format)
Reads the given file path as XML into the passed stream.
virtual void LoadSim()
Load an entire simulation from the inputfile.
void Record()
Record this datum to its Recorder.
std::string LoadStringFromFile(std::string file, std::string format)
Reads the given file path and returns an XML string.
void LoadSpecs()
Load agent specs from the input file to a map by alias.
static Agent * Make(Context *ctx, AgentSpec spec)
Returns a newly constructed agent for the given module spec.
Interface implemented by backends that support rudimentary querying.
void Init(const std::stringstream &input)
initializes a parser with an xml snippet
static Ptr CreateFromAtom(CompMap v)
Creates a new composition from v with its components having appropriate atom-based ratios...
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
static Ptr CreateFromMass(CompMap v)
Creates a new composition from v with its components having appropriate mass-based ratios...
void DelAgent(Agent *m)
Destructs and cleans up m (and it's children recursively).
Controls simulation timestepping and inter-timestep phases.