CYCLUS
Loading...
Searching...
No Matches
symbolic_function_factories.cc
Go to the documentation of this file.
1#include "logger.h"
2#include "error.h"
3
5
6namespace cyclus {
7namespace toolkit {
8
9// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
11 std::stringstream ss(params);
12 double slope, intercept;
13 ss >> slope >> intercept;
14
15 LOG(LEV_DEBUG2, "Funct") <<
16 "Linear function created in the form y = m*x + b, with";
17 LOG(LEV_DEBUG2, "Funct") << " * m: " << slope;
18 LOG(LEV_DEBUG2, "Funct") << " * b: " << intercept;
19
20 return SymFunction::Ptr(new LinearFunction(slope, intercept));
21}
22
23// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
25 std::stringstream ss(params);
28
29 LOG(LEV_DEBUG2, "Funct") <<
30 "Exponential function created in the form y = a*exp(b*x) + c, with";
31 LOG(LEV_DEBUG2, "Funct") << " * a: " << constant;
32 LOG(LEV_DEBUG2, "Funct") << " * b: " << exponent;
33 LOG(LEV_DEBUG2, "Funct") << " * c: " << intercept;
34
36 intercept));
37}
38
39// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
41 function_ = boost::shared_ptr<PiecewiseFunction>(new PiecewiseFunction());
42}
43
44// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46 if (!params.empty()) {
47 throw Error("Piecewise Functions cannot be created with a list of parameters");
48 }
49
50 LOG(LEV_DEBUG2, "Funct") << "A piecewise function has been created: "
51 << function_->Print();
52 return function_;
53}
54
55// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57 double starting_coord,
58 bool continuous) {
59 if (!function_->functions_.empty()) {
60 const PiecewiseFunction::PiecewiseFunctionInfo& last =
61 function_->functions_.back();
62 if (starting_coord <= last.xoffset) {
63 throw Error("Cannot append a function before the last registered function");
64 }
65 }
66
67 double yoffset = 0;
68 if (continuous) {
69 yoffset = function_->value(starting_coord) - function->value(0);
70 }
71
72 function_->functions_.push_back(PiecewiseFunction::PiecewiseFunctionInfo(
73 function, starting_coord, yoffset));
74}
75
76// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77std::map<std::string, BasicFunctionFactory::FunctionType>
78BasicFunctionFactory::enum_names_ =
79 std::map<std::string, BasicFunctionFactory::FunctionType>();
80
81// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
83 if (enum_names_.empty()) {
84 enum_names_["lin"] = LIN;
85 enum_names_["linear"] = LIN;
86 enum_names_["exp"] = EXP;
87 enum_names_["exponential"] = EXP;
88 }
89}
90
91// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
93 std::string params) {
94 if (enum_names_.count(type) == 0) {
95 std::stringstream err("");
96 err << type << " is not a registered function type"
97 << " of the basic function factory.";
98 throw Error(err.str());
99 }
100
101 switch (enum_names_.at(type)) {
102 case LIN: {
104 return lff.GetFunctionPtr(params);
105 }
106 break;
107 case EXP: {
109 return eff.GetFunctionPtr(params);
110 }
111 break;
112 default:
113 throw Error("Function type " + type + " not yet supported.");
114 break;
115 }
116}
117
118} // namespace toolkit
119} // namespace cyclus
A generic mechanism to manually manage exceptions.
Definition error.h:12
SymFunction::Ptr GetFunctionPtr(std::string type, std::string params)
Return a function pointer to a registered function type.
@ LIN
See cyclus::toolkit::LinFunctionFactory for a description of function parameters.
@ EXP
See cyclus::toolkit::ExpFunctionFactory for a description of function parameters.
BasicFunctionFactory()
Constructor sets up the enum names map.
A concrete factory for exponential functions.
virtual SymFunction::Ptr GetFunctionPtr(std::string params)
Return a function pointer to a exponential function.
Exponential functions f(x) = constant_ * exp ( exponent_ * x ) + intercept_.
A concrete factory for linear functions.
virtual SymFunction::Ptr GetFunctionPtr(std::string params)
Return a function pointer to a linear function.
Linear functions f(x) = slope_ * x + intercept_.
virtual SymFunction::Ptr GetFunctionPtr(std::string params="")
Return a function pointer to a piecewise function.
void AddFunction(SymFunction::Ptr function, double starting_coord=0.0, bool continuous=true)
Add a function to the piecewise function being constructed.
Piecewise function f(x) for all x in [lhs,rhs] 0 otherwise.
boost::shared_ptr< SymFunction > Ptr
Code providing rudimentary logging capability for the Cyclus core.
#define LOG(level, prefix)
allows easy logging via the streaming operator similar to std::cout; this is the primary way to use t...
Definition logger.h:35
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
@ LEV_DEBUG2
debugging information
Definition logger.h:59
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters