20#ifndef _PIECEWISE_DEFINED_FUNCTIONS_H_
21#define _PIECEWISE_DEFINED_FUNCTIONS_H_
23#include "mbsim/functions/function.h"
24#include "mbsim/utils/utils.h"
30 template <
typename Ret,
typename Arg>
39 template<
typename Ret,
typename Arg>
41 using B = fmatvec::Function<Ret(Arg)>;
45 for(
unsigned int i=0; i<function.size(); i++)
48 void addLimitedFunction(
const LimitedFunction<Ret(Arg)> &limitedFunction) {
49 function.push_back(limitedFunction.function);
50 limitedFunction.function->setParent(
this);
51 a.push_back(limitedFunction.limit);
53 int getArgSize()
const override {
return 1; }
54 std::pair<int, int> getRetSize()
const override {
return function.size()?function[0]->getRetSize():std::make_pair(0,1); }
55 Ret operator()(
const Arg &x)
override {
58 y0.resize(a.size(),0.0*(*function[0])(Arg(1)));
60 for(
unsigned int i=1; i<a.size(); i++)
64 for(
unsigned int i=0; i<function.size(); i++) {
68 this->throwError(
"(PiecewiseDefinedFunction::operator()): x out of range! x= "+fmatvec::toString(x)+
", upper bound= "+fmatvec::toString(a[function.size()]));
70 typename B::DRetDArg parDer(
const Arg &x)
override {
71 for(
unsigned int i=0; i<function.size(); i++)
74 this->throwError(
"(PiecewiseDefinedFunction::parDer): x out of range! x= "+fmatvec::toString(x)+
", upper bound= "+fmatvec::toString(a[function.size()]));
76 typename B::DRetDArg parDerDirDer(
const Arg &xDir,
const Arg &x)
override {
77 for(
unsigned int i=0; i<function.size(); i++)
80 this->throwError(
"(PiecewiseDefinedFunction::parDerDirDer): x out of range! x= "+fmatvec::toString(x)+
", upper bound= "+fmatvec::toString(a[function.size()]));
83 void initializeUsingXML(xercesc::DOMElement *element)
override {
84 xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%
"limitedFunctions");
85 xercesc::DOMElement *ee=e->getFirstElementChild();
86 while(ee && MBXMLUtils::E(ee)->getTagName()==MBSIM%
"LimitedFunction") {
87 addLimitedFunction(
LimitedFunction<Ret(Arg)>(
ObjectFactory::createAndInit<
Function<Ret(Arg)>>(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%
"function")->getFirstElementChild()),MBXMLUtils::E(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%
"limit"))->getText<double>()));
88 ee=ee->getNextElementSibling();
90 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%
"shiftAbscissa");
91 if(e) shiftAbscissa=MBXMLUtils::E(e)->getText<
bool>();
92 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%
"shiftOrdinate");
93 if(e) shiftOrdinate=MBXMLUtils::E(e)->getText<
bool>();
96 Function<Ret(Arg)>::init(stage, config);
97 for(
auto it=function.begin(); it!=function.end(); it++)
98 (*it)->init(stage, config);
101 for(
unsigned int i=1; i<a.size(); i++)
110 std::vector<
Function<Ret(Arg)> *> function;
111 std::vector<double> a, x0;
113 bool shiftAbscissa{
false}, shiftOrdinate{
false}, firstCall{
true};
InitStage
The stages of the initialization.
Definition: element.h:62
@ preInit
Definition: element.h:64
Definition: function.h:53
Definition: piecewise_defined_function.h:28
static ContainerType * createAndInit(const xercesc::DOMElement *element)
Definition: objectfactory.h:103
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: piecewise_defined_function.h:95
Definition: piecewise_defined_function.h:37
namespace MBSim
Definition: bilateral_constraint.cc:30