#include <iostream>
#include "flame/config.h"
#include "flame/base.h"
# define C0 2.99792458e8
namespace {
{
double x,
xv;
{
const State1D& ST = static_cast<const State1D&>(other);
x = ST.x;
xv= ST.xv;
}
virtual void show(std::ostream &strm)
const
{
strm<<pos<<" ["<<x<<", "<<xv<<"]\n";
}
virtual bool getArray(
unsigned idx, ArrayInfo& Info)
{
unsigned I=0;
if(idx==I++) {
Info.name = "x";
Info.ptr = &x;
} else if(idx==I++) {
Info.name = "xv";
Info.ptr = &xv;
} else {
}
return true;
}
return new State1D(*this, clone_tag());
}
virtual ~State1D() {}
,x(c.get<double>("x", 0.0))
,xv(c.get<double>("xv", 0.0))
{}
State1D(const State1D& O, clone_tag t)
,x(O.x)
,xv(O.xv)
{}
};
{
State1D initial;
Element1DSource(
const Config& c)
,initial(c)
{}
{
}
{
const Element1DSource *O = static_cast<const Element1DSource*>(other);
initial.assign(O->initial);
}
virtual const char*
type_name()
const {
return "source"; }
};
{
double xa,
tt;
Element1DGeneric(
const Config& c)
,xa(c.get<double>("A", 0.0))
{
tt = length/C0;
}
{
State1D &ST = static_cast<State1D&>(s);
ST.xv += xa*tt;
ST.x += xa*tt*tt;
}
{
const Element1DGeneric *O = static_cast<const Element1DGeneric*>(other);
xa = O->xa;
tt = O->tt;
}
virtual const char*
type_name()
const {
return "generic"; }
};
}
void register1D()
{
Machine::registerState<State1D>("Simple1D");
Machine::registerElement<Element1DSource>("Simple1D", "source");
Machine::registerElement<Element1DGeneric>("Simple1D", "generic");
}
static const char lattice[] = ""
"sim_type = \"Simple1D\";\n"
"src : source, x = 1e-5, xv = 1e-6;\n"
"elem1 : generic, A = 1, L = 10;\n"
"elem2 : generic, A = -2, L = 10;\n"
"example : LINE = (src, elem1, elem2);\n"
;
int main(int argc, char *argv[])
{
try {
register1D();
std::auto_ptr<Config> conf;
{
conf.reset(parser.
parse_byte(lattice,
sizeof(lattice)-1));
}
Machine mymachine(*conf);
mymachine.set_trace(&std::cout);
std::auto_ptr<StateBase> thestate(mymachine.allocState());
mymachine.propagate(thestate.get(), 0, 1);
std::cout<<"Source state "<<*thestate<<"\n";
mymachine.propagate(thestate.get(), 1);
std::cout<<"Final state "<<*thestate<<"\n";
return 0;
} catch(std::exception& e) {
std::cerr<<"Error: "<<e.what()<<"\n";
return 1;
}
}