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(
39 new ExponentialFunction(constant, exponent, intercept));
40}
41
42// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44 function_ = boost::shared_ptr<PiecewiseFunction>(new PiecewiseFunction());
45}
46
47// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49 if (!params.empty()) {
50 throw Error(
51 "Piecewise Functions cannot be created with a list of parameters");
52 }
53
54 LOG(LEV_DEBUG2, "Funct") << "A piecewise function has been created: "
55 << function_->Print();
56 return function_;
57}
58
59// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
61 double starting_coord,
62 bool continuous) {
63 if (!function_->functions_.empty()) {
64 const PiecewiseFunction::PiecewiseFunctionInfo& last =
65 function_->functions_.back();
66 if (starting_coord <= last.xoffset) {
67 throw Error(
68 "Cannot append a function before the last registered function");
69 }
70 }
71
72 double yoffset = 0;
73 if (continuous) {
74 yoffset = function_->value(starting_coord) - function->value(0);
75 }
76
77 function_->functions_.push_back(PiecewiseFunction::PiecewiseFunctionInfo(
78 function, starting_coord, yoffset));
79}
80
81// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
82std::map<std::string, BasicFunctionFactory::FunctionType>
83 BasicFunctionFactory::enum_names_ =
84 std::map<std::string, BasicFunctionFactory::FunctionType>();
85
86// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88 if (enum_names_.empty()) {
89 enum_names_["lin"] = LIN;
90 enum_names_["linear"] = LIN;
91 enum_names_["exp"] = EXP;
92 enum_names_["exponential"] = EXP;
93 }
94}
95
96// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98 std::string params) {
99 if (enum_names_.count(type) == 0) {
100 std::stringstream err("");
101 err << type << " is not a registered function type"
102 << " of the basic function factory.";
103 throw Error(err.str());
104 }
105
106 switch (enum_names_.at(type)) {
107 case LIN: {
109 return lff.GetFunctionPtr(params);
110 } break;
111 case EXP: {
113 return eff.GetFunctionPtr(params);
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:34
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:71