/******************************************************* * * DESCRIPTION: QBG resistor class (effort input) * * AUTHOR: Mariana C. D'Abreu * * EMAIL: mdabreu@dc.uba.ar * ********************************************************/ #include "qbg_resistance_ei.h" #include "ini.h" #include "message.h" #include "mainsimu.h" #include "defaults.h" const RealValue QBGResistanceEffortIn::DefaultResistance = 1; /******************************************************************* * Function Name: QBGResistanceEffortIn * Description: constructor ********************************************************************/ QBGResistanceEffortIn::QBGResistanceEffortIn( const string &name ) : Atomic( name ) , e1p( addInputPort( "e1p" ) ) , f1p( addOutputPort( "f1p" ) ) { try { try { // resistance string resistance( MainSimulator::Instance().getParameter( description(), "R" ) ); R = atof( resistance.data() ); } catch ( MException &exc ) { R = DefaultResistance; } if ( R == 0 ) { throw MException( "Invalid Resistance value : " + Value2StrReal( R ) ); } // out file try { outFileName = MainSimulator::Instance().getParameter( description(), "outputFile" ); } catch ( MException &e ) { outFileName = DEFAULT_QBG_OUTFILENAME; } } catch ( MException &e ) { e.print( cerr ); MTHROW( e ) ; } } /******************************************************************* * Function Name: ~QBGResistanceEffortIn * Destructor ********************************************************************/ QBGResistanceEffortIn::~QBGResistanceEffortIn() { outFile.close(); } /******************************************************************* * Function Name: initFunction ********************************************************************/ Model &QBGResistanceEffortIn::initFunction() { effort = 0; flow = 0; flowSent = 0; // log file outFile.open( outFileName.c_str() ); outFile << "\nTime,Flow,Effort"; passivate(); return *this ; } /******************************************************************* * Function Name: externalFunction ********************************************************************/ Model &QBGResistanceEffortIn::externalFunction( const ExternalMessage &msg ) { effort = msg.value(); calculateFlow(); holdIn( active, Time::Zero ); return *this; } /******************************************************************* * Function Name: internalFunction ********************************************************************/ Model &QBGResistanceEffortIn::internalFunction( const InternalMessage & ) { passivate(); return *this ; } /******************************************************************* * Function Name: outputFunction ********************************************************************/ Model &QBGResistanceEffortIn::outputFunction( const InternalMessage &msg ) { if( flow != flowSent ) { sendOutput( msg.time(), f1p, flow ); flowSent = flow; outFile << "\n" << msg.time().asMsecs() << "," << flow << "," << effort; } return *this ; } /******************************************************************* * Function Name: calculateEffort ********************************************************************/ QBGResistanceEffortIn &QBGResistanceEffortIn::calculateFlow() { MASSERT( R != 0 ); // e = f * R flow = effort / R; return *this; }