diff --git a/eeschema/sim/ngspice.cpp b/eeschema/sim/ngspice.cpp index a918ca737d..6529b60bb6 100644 --- a/eeschema/sim/ngspice.cpp +++ b/eeschema/sim/ngspice.cpp @@ -62,18 +62,132 @@ void NGSPICE::Init() } -const vector NGSPICE::GetPlot( const string& aName, int aMaxLen ) +vector NGSPICE::GetPlot( const string& aName, int aMaxLen ) { - vector data; - + vector data; vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ); - if( vi && vi->v_realdata ) + if( vi ) { - data.reserve( vi->v_length ); + int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length ); + data.reserve( length ); - for( int i = 0; i < vi->v_length; i++ ) - data.push_back( vi->v_realdata[i] ); + if( vi->v_realdata ) + { + for( int i = 0; i < length; i++ ) + data.push_back( COMPLEX( vi->v_realdata[i], 0.0 ) ); + } + else if( vi->v_compdata ) + { + for( int i = 0; i < length; i++ ) + data.push_back( COMPLEX( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag ) ); + } + } + + return data; +} + + +vector NGSPICE::GetRealPlot( const string& aName, int aMaxLen ) +{ + vector data; + vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ); + + if( vi ) + { + int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length ); + data.reserve( length ); + + if( vi->v_realdata ) + { + for( int i = 0; i < length; i++ ) + { + data.push_back( vi->v_realdata[i] ); + } + } + else if( vi->v_compdata ) + { + for( int i = 0; i < length; i++ ) + { + assert( vi->v_compdata[i].cx_imag == 0.0 ); + data.push_back( vi->v_compdata[i].cx_real ); + } + } + } + + return data; +} + + +vector NGSPICE::GetImagPlot( const string& aName, int aMaxLen ) +{ + vector data; + vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ); + + if( vi ) + { + int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length ); + data.reserve( length ); + + if( vi->v_compdata ) + { + for( int i = 0; i < length; i++ ) + { + data.push_back( vi->v_compdata[i].cx_imag ); + } + } + } + + return data; +} + + +vector NGSPICE::GetMagPlot( const string& aName, int aMaxLen ) +{ + vector data; + vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ); + + if( vi ) + { + int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length ); + data.reserve( length ); + + if( vi->v_realdata ) + { + for( int i = 0; i < length; i++ ) + data.push_back( vi->v_realdata[i] ); + } + else if( vi->v_compdata ) + { + for( int i = 0; i < length; i++ ) + data.push_back( hypot( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag ) ); + } + } + + return data; +} + + +vector NGSPICE::GetPhasePlot( const string& aName, int aMaxLen ) +{ + vector data; + vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ); + + if( vi ) + { + int length = aMaxLen < 0 ? vi->v_length : std::min( aMaxLen, vi->v_length ); + data.reserve( length ); + + if( vi->v_realdata ) + { + for( int i = 0; i < length; i++ ) + data.push_back( 0.0 ); // well, that's life + } + else if( vi->v_compdata ) + { + for( int i = 0; i < length; i++ ) + data.push_back( atan2( vi->v_compdata[i].cx_imag, vi->v_compdata[i].cx_real ) ); + } } return data; diff --git a/eeschema/sim/ngspice.h b/eeschema/sim/ngspice.h index 7decad324e..2460537aba 100644 --- a/eeschema/sim/ngspice.h +++ b/eeschema/sim/ngspice.h @@ -42,7 +42,12 @@ public: bool Stop() override; bool IsRunning() override; bool Command( const std::string& aCmd ) override; - const std::vector GetPlot( const std::string& aName, int aMaxLen = -1 ) override; + + std::vector GetPlot( const std::string& aName, int aMaxLen = -1 ) override; + std::vector GetRealPlot( const std::string& aName, int aMaxLen = -1 ) override; + std::vector GetImagPlot( const std::string& aName, int aMaxLen = -1 ) override; + std::vector GetMagPlot( const std::string& aName, int aMaxLen = -1 ) override; + std::vector GetPhasePlot( const std::string& aName, int aMaxLen = -1 ) override; private: typedef void (*ngSpice_Init)( SendChar*, SendStat*, ControlledExit*, diff --git a/eeschema/sim/spice_simulator.h b/eeschema/sim/spice_simulator.h index ead779e3c3..7b12163bd7 100644 --- a/eeschema/sim/spice_simulator.h +++ b/eeschema/sim/spice_simulator.h @@ -27,17 +27,11 @@ #include #include +#include class SPICE_REPORTER; -enum SIM_TRACE_TYPE -{ - SIM_AC_MAG = 0x1, - SIM_AC_PHASE = 0x2, - SIM_TR_VOLTAGE = 0x4, - SIM_TR_CURRENT = 0x8, - SIM_TR_FFT = 0x10 -}; +typedef std::complex COMPLEX; class SPICE_SIMULATOR { @@ -59,7 +53,12 @@ public: m_reporter = aReporter; } - virtual const std::vector GetPlot( const std::string& aName, int aMaxLen = -1 ) = 0; + virtual std::vector GetPlot( const std::string& aName, int aMaxLen = -1 ) = 0; + virtual std::vector GetRealPlot( const std::string& aName, int aMaxLen = -1 ) = 0; + virtual std::vector GetImagPlot( const std::string& aName, int aMaxLen = -1 ) = 0; + virtual std::vector GetMagPlot( const std::string& aName, int aMaxLen = -1 ) = 0; + virtual std::vector GetPhasePlot( const std::string& aName, int aMaxLen = -1 ) = 0; + protected: SPICE_REPORTER* m_reporter;