// var.cpp // // last-edit-by: J.A. de Jong // // Description: // ////////////////////////////////////////////////////////////////////// #include "tasmet_variable.h" #include "tasmet_exception.h" #define Ns (_gc->Ns()) #define Nf (_gc->Nf()) #define fDFT (_gc->fDFT) #define iDFT (_gc->iDFT) #define DDTfd (_gc->DDTfd) //******************************************************************************** Operators Variable operator*(const double& scalar,const Variable& other){ // Pre-multiplication with scalar TRACE(0,"Variable::operator*(scalar,Variable)"); return Variable(other._gc,scalar*other._adata); } Variable Variable::operator/(const Variable& Variable2) const { TRACE(0,"Variable::operator/()"); return Variable(this->_gc,this->_tdata/Variable2._tdata,false); } Variable Variable::operator/(d val) const { TRACE(0,"Variable::operator/(double)"); return Variable(this->_gc,this->tdata()/val,false); } Variable Variable::operator-(const Variable& other) const{ TRACE(0,"Variable::operator-(Variable)"); return Variable(this->_gc,_adata-other._adata); } Variable Variable::operator*(d scalar) const { TRACE(0,"Variable::operator*(scalar)"); // Post-multiplication with scalar return Variable(this->_gc,scalar*_adata); } Variable Variable::operator+(const Variable& other) const{ TRACE(0,"Variable::operator+()"); return Variable(this->_gc,_adata+other._adata); } Variable Variable::operator*(const Variable& Variable2) const { // Multiply two TRACE(0,"Variable::operator*(const Variable& Variable2) const"); return Variable(this->_gc,_tdata%Variable2._tdata,false); } //***************************************** The Variable class Variable::Variable(const gc_ptr& gc,double initval): _gc(gc) { TRACE(0,"Variable::Variable(us ndofs, double initval)"); _tdata=initval*ones(Ns); _adata=zeros(Ns); _adata(0)=initval; } Variable::Variable(const gc_ptr& gc,const vd& data,bool adata): _gc(gc) { // Create a tasystem and fill it with time data. TRACE(0,"Variable::Variable(gc,_tdata)"); if(adata){ this->_adata=data; _tdata=iDFT*_adata; } else{ this->_tdata=data; _adata=fDFT*_tdata; } } Variable::Variable(const gc_ptr& gc,const vc& data) :Variable(gc) { // Create a tasystem and fill it with time data. if(data.size()!=Nf+1) throw TaSMETError("Wrong size of amplitude vector given. Does the" "vector size correspond to Ns?"); setadata(data); } void Variable::setGc(const gc_ptr& gc){ TRACE(10,"Variable::setGc()"); this->_gc = gc; updateNf(); } void Variable::resetHarmonics(){ TRACE(10,"Variable::resetHarmonics()"); if(Nf>1){ for(us i=3;i_gc=other._gc; this->_tdata=other._tdata; this->_adata=other._adata; } Variable& Variable::operator=(const Variable& other){ // THIS WOULD COUPLE TO THE WRONG GLOBALCONF when setRes is used // between ducts!!!!! if(this!=&other){ if(!this->_gc) { this->_gc=other._gc; } this->_tdata=other._tdata; this->_adata=other._adata; updateNf(); } return *this; } void Variable::updateNf(){ TRACE(0,"Variable::updateNf()"); us oldsize=_adata.size(); assert(oldsize==_tdata.size()); assert(_gc); us newsize=Ns; if(oldsize!=newsize){ _adata.resize(newsize); _tdata=iDFT*_adata; } } // Get methods (which require implementation) d Variable::operator()(us i) const {//Extract result at specific frequency TRACE(-2,"Variable::operator()("<=Ns) throw TaSMETError("Invalid frequency number!"); TRACE(-1,"_adata: "<<_adata); return _adata(i); } vc Variable::getcRes() const { TRACE(-2,"Variable::getcRes()"); // TRACE(0,"_adata:" << _adata); vc cadata(Nf+1); cadata(0)=_adata(0); for(us i=1;i=Ns) throw TaSMETError("Tried to change a value beyond length of the vector"); _adata[freqnr]=val; _tdata=iDFT*_adata; } void Variable::setadata(const vc& res) { TRACE(0,"Variable::setadata(const vc& res)"); assert(res.size()==Nf+1); _adata(0)=res(0).real(); for(us i=1;igetomg(); for(us i=0;igetfreq(); return linspace(0,nperiod*T,ninst); } dmat Variable::freqMultiplyMat() const{ TRACE(0,"Variable::freqMultiplyMat()"); dmat result(Ns,Ns,fillwith::zeros); result(0,0)=_adata(0); if(Nf>0){ for(us j=1;j0) return result; } //Get a tasystem which is the time derivative of the current one Variable Variable::ddt() const { vd newadata=DDTfd*_adata; return Variable(_gc,newadata); } // The product //***************************************** End of the Variable class