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);
26 double constant, exponent, intercept = 0.0;
27 ss >> constant >> exponent;
28 if (!ss.eof()) {
29 ss >> intercept;
30 }
31
32 LOG(LEV_DEBUG2, "Funct") <<
33 "Exponential function created in the form y = a*exp(b*x) + c, with";
34 LOG(LEV_DEBUG2, "Funct") << " * a: " << constant;
35 LOG(LEV_DEBUG2, "Funct") << " * b: " << exponent;
36 LOG(LEV_DEBUG2, "Funct") << " * c: " << intercept;
37
38 return SymFunction::Ptr(new ExponentialFunction(constant, exponent,
39 intercept));
40}
41
42// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44 function_ = boost::shared_ptr<PiecewiseFunction>(new PiecewiseFunction());
45}
46
47// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49 if (!params.empty()) {
50 throw Error("Piecewise Functions cannot be created with a list of parameters");
51 }
52
53 LOG(LEV_DEBUG2, "Funct") << "A piecewise function has been created: "
54 << function_->Print();
55 return function_;
56}
57
58// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
60 double starting_coord,
61 bool continuous) {
62 if (!function_->functions_.empty()) {
63 const PiecewiseFunction::PiecewiseFunctionInfo& last =
64 function_->functions_.back();
65 if (starting_coord <= last.xoffset) {
66 throw Error("Cannot append a function before the last registered function");
67 }
68 }
69
70 double yoffset = 0;
71 if (continuous) {
72 yoffset = function_->value(starting_coord) - function->value(0);
73 }
74
75 function_->functions_.push_back(PiecewiseFunction::PiecewiseFunctionInfo(
76 function, starting_coord, yoffset));
77}
78
79// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
80std::map<std::string, BasicFunctionFactory::FunctionType>
81BasicFunctionFactory::enum_names_ =
82 std::map<std::string, BasicFunctionFactory::FunctionType>();
83
84// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
86 if (enum_names_.empty()) {
87 enum_names_["lin"] = LIN;
88 enum_names_["linear"] = LIN;
89 enum_names_["exp"] = EXP;
90 enum_names_["exponential"] = EXP;
91 }
92}
93
94// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
96 std::string params) {
97 if (enum_names_.count(type) == 0) {
98 std::stringstream err("");
99 err << type << " is not a registered function type"
100 << " of the basic function factory.";
101 throw Error(err.str());
102 }
103
104 switch (enum_names_.at(type)) {
105 case LIN: {
107 return lff.GetFunctionPtr(params);
108 }
109 break;
110 case EXP: {
112 return eff.GetFunctionPtr(params);
113 }
114 break;
115 default:
116 throw Error("Function type " + type + " not yet supported.");
117 break;
118 }
119}
120
121} // namespace toolkit
122} // 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