CYCLUS
symbolic_function_factories.cc
Go to the documentation of this file.
1 #include "logger.h"
2 #include "error.h"
3 
5 
6 namespace cyclus {
7 namespace 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;
27  ss >> constant >> exponent >> intercept;
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 
35  return SymFunction::Ptr(new ExponentialFunction(constant, exponent,
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 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77 std::map<std::string, BasicFunctionFactory::FunctionType>
78 BasicFunctionFactory::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: {
103  LinFunctionFactory lff;
104  return lff.GetFunctionPtr(params);
105  }
106  break;
107  case EXP: {
108  ExpFunctionFactory eff;
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
void AddFunction(SymFunction::Ptr function, double starting_coord=0.0, bool continuous=true)
Add a function to the piecewise function being constructed.
A concrete factory for exponential functions.
A generic mechanism to manually manage exceptions.
Definition: error.h:12
Linear functions f(x) = slope_ * x + intercept_.
Piecewise function f(x) for all x in [lhs,rhs] 0 otherwise.
BasicFunctionFactory()
Constructor sets up the enum names map.
debugging information
Definition: logger.h:59
A concrete factory for linear 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_.
double slope(double x2, double y2, double x1, double y1)
Finds the slope of a line from the points (x1, y1) and (x2, y2).
Definition: pyne.cc:353
virtual SymFunction::Ptr GetFunctionPtr(std::string params="")
Return a function pointer to a piecewise function.
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
virtual SymFunction::Ptr GetFunctionPtr(std::string params)
Return a function pointer to a linear function.
#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
boost::shared_ptr< SymFunction > Ptr
SymFunction::Ptr GetFunctionPtr(std::string type, std::string params)
Return a function pointer to a registered function type.