2016-08-11 12:41:07 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2018-03-23 10:14:33 +00:00
|
|
|
* Copyright (C) 2016-2018 CERN
|
2022-03-09 01:40:59 +00:00
|
|
|
* Copyright (C) 2018-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2018-06-04 17:48:17 +00:00
|
|
|
*
|
2016-08-11 12:41:07 +00:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2016-08-11 12:41:43 +00:00
|
|
|
* as published by the Free Software Foundation; either version 3
|
2016-08-11 12:41:07 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
2016-08-11 12:41:43 +00:00
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
2016-08-11 12:41:07 +00:00
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2020-01-22 08:46:16 +00:00
|
|
|
#include <config.h> // Needed for MSW compilation
|
2020-10-18 11:31:07 +00:00
|
|
|
#include <wx/log.h>
|
2020-01-21 18:52:59 +00:00
|
|
|
|
2022-03-05 23:49:25 +00:00
|
|
|
#include "ngspice_helpers.h"
|
2016-08-11 12:41:07 +00:00
|
|
|
#include "ngspice.h"
|
2016-08-11 12:41:19 +00:00
|
|
|
#include "spice_reporter.h"
|
2021-03-19 13:04:28 +00:00
|
|
|
#include "spice_settings.h"
|
2021-03-18 19:31:02 +00:00
|
|
|
|
2020-01-21 18:52:59 +00:00
|
|
|
#include <common.h>
|
2020-10-24 01:38:50 +00:00
|
|
|
#include <locale_io.h>
|
2020-01-21 18:52:59 +00:00
|
|
|
|
2021-02-17 04:26:46 +00:00
|
|
|
#include <paths.h>
|
|
|
|
|
2016-10-13 11:25:06 +00:00
|
|
|
#include <wx/stdpaths.h>
|
|
|
|
#include <wx/dir.h>
|
2020-01-21 18:52:59 +00:00
|
|
|
|
2018-03-21 09:42:39 +00:00
|
|
|
#include <stdexcept>
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2020-05-08 14:34:28 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2016-08-11 12:41:07 +00:00
|
|
|
using namespace std;
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2021-03-18 19:31:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag to enable debug output of Ngspice simulator.
|
|
|
|
*
|
|
|
|
* Use "KICAD_NGSPICE" to enable Ngspice simulator tracing.
|
|
|
|
*
|
|
|
|
* @ingroup trace_env_vars
|
|
|
|
*/
|
2018-03-23 10:14:33 +00:00
|
|
|
static const wxChar* const traceNgspice = wxT( "KICAD_NGSPICE" );
|
|
|
|
|
2021-03-18 19:31:02 +00:00
|
|
|
|
|
|
|
NGSPICE::NGSPICE() :
|
|
|
|
m_ngSpice_Init( nullptr ),
|
|
|
|
m_ngSpice_Circ( nullptr ),
|
|
|
|
m_ngSpice_Command( nullptr ),
|
|
|
|
m_ngGet_Vec_Info( nullptr ),
|
|
|
|
m_ngSpice_CurPlot( nullptr ),
|
|
|
|
m_ngSpice_AllPlots( nullptr ),
|
|
|
|
m_ngSpice_AllVecs( nullptr ),
|
|
|
|
m_ngSpice_Running( nullptr ),
|
|
|
|
m_error( false )
|
2016-08-11 12:41:01 +00:00
|
|
|
{
|
2018-03-21 09:42:39 +00:00
|
|
|
init_dll();
|
2016-08-11 12:41:07 +00:00
|
|
|
}
|
2016-08-11 12:41:01 +00:00
|
|
|
|
|
|
|
|
2022-08-15 17:47:23 +00:00
|
|
|
NGSPICE::~NGSPICE() = default;
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2016-08-11 12:41:07 +00:00
|
|
|
|
2021-03-18 19:31:02 +00:00
|
|
|
void NGSPICE::Init( const SPICE_SIMULATOR_SETTINGS* aSettings )
|
2016-08-11 12:41:01 +00:00
|
|
|
{
|
2016-08-11 12:42:18 +00:00
|
|
|
Command( "reset" );
|
2021-03-18 19:31:02 +00:00
|
|
|
|
|
|
|
for( const std::string& command : GetSettingCommands() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceNgspice, "Sending Ngspice configuration command '%s'.", command );
|
|
|
|
Command( command );
|
|
|
|
}
|
2016-08-11 12:41:01 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 12:41:07 +00:00
|
|
|
|
2021-01-25 21:29:13 +00:00
|
|
|
vector<string> NGSPICE::AllPlots() const
|
2020-04-15 01:51:58 +00:00
|
|
|
{
|
|
|
|
LOCALE_IO c_locale; // ngspice works correctly only with C locale
|
|
|
|
char* currentPlot = m_ngSpice_CurPlot();
|
|
|
|
char** allPlots = m_ngSpice_AllVecs( currentPlot );
|
|
|
|
int noOfPlots = 0;
|
|
|
|
|
2021-02-16 23:37:57 +00:00
|
|
|
vector<string> retVal;
|
2021-03-18 19:31:02 +00:00
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
if( allPlots != nullptr )
|
2021-02-16 23:37:57 +00:00
|
|
|
{
|
2020-04-15 01:51:58 +00:00
|
|
|
for( char** plot = allPlots; *plot != nullptr; plot++ )
|
|
|
|
noOfPlots++;
|
|
|
|
|
2021-02-16 23:37:57 +00:00
|
|
|
retVal.reserve( noOfPlots );
|
2021-03-08 18:49:46 +00:00
|
|
|
|
2021-02-16 23:37:57 +00:00
|
|
|
for( int i = 0; i < noOfPlots; i++, allPlots++ )
|
|
|
|
{
|
|
|
|
string vec = *allPlots;
|
2021-03-08 18:49:46 +00:00
|
|
|
retVal.push_back( vec );
|
2021-02-16 23:37:57 +00:00
|
|
|
}
|
2020-04-15 01:51:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 23:37:57 +00:00
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:31 +00:00
|
|
|
vector<COMPLEX> NGSPICE::GetPlot( const string& aName, int aMaxLen )
|
|
|
|
{
|
2016-08-15 13:52:07 +00:00
|
|
|
LOCALE_IO c_locale; // ngspice works correctly only with C locale
|
2016-08-11 12:41:31 +00:00
|
|
|
vector<COMPLEX> data;
|
2018-03-21 09:42:39 +00:00
|
|
|
vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() );
|
2016-08-11 12:41:31 +00:00
|
|
|
|
|
|
|
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++ )
|
2019-12-05 15:41:21 +00:00
|
|
|
data.emplace_back( vi->v_realdata[i], 0.0 );
|
2016-08-11 12:41:31 +00:00
|
|
|
}
|
|
|
|
else if( vi->v_compdata )
|
|
|
|
{
|
|
|
|
for( int i = 0; i < length; i++ )
|
2019-12-05 15:41:21 +00:00
|
|
|
data.emplace_back( vi->v_compdata[i].cx_real, vi->v_compdata[i].cx_imag );
|
2016-08-11 12:41:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<double> NGSPICE::GetRealPlot( const string& aName, int aMaxLen )
|
2016-08-11 12:41:01 +00:00
|
|
|
{
|
2016-08-15 13:52:07 +00:00
|
|
|
LOCALE_IO c_locale; // ngspice works correctly only with C locale
|
2016-08-11 12:41:01 +00:00
|
|
|
vector<double> data;
|
2018-03-21 09:42:39 +00:00
|
|
|
vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() );
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2016-08-11 12:41:31 +00:00
|
|
|
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++ )
|
|
|
|
{
|
2021-04-14 19:01:39 +00:00
|
|
|
wxASSERT( vi->v_compdata[i].cx_imag == 0.0 );
|
2016-08-11 12:41:31 +00:00
|
|
|
data.push_back( vi->v_compdata[i].cx_real );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<double> NGSPICE::GetImagPlot( const string& aName, int aMaxLen )
|
|
|
|
{
|
2016-08-15 13:52:07 +00:00
|
|
|
LOCALE_IO c_locale; // ngspice works correctly only with C locale
|
2016-08-11 12:41:31 +00:00
|
|
|
vector<double> data;
|
2018-03-21 09:42:39 +00:00
|
|
|
vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() );
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2016-08-11 12:41:31 +00:00
|
|
|
if( vi )
|
2016-08-11 12:41:07 +00:00
|
|
|
{
|
2016-08-11 12:41:31 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-11 12:41:11 +00:00
|
|
|
|
2016-08-11 12:41:31 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<double> NGSPICE::GetMagPlot( const string& aName, int aMaxLen )
|
|
|
|
{
|
2016-08-15 13:52:07 +00:00
|
|
|
LOCALE_IO c_locale; // ngspice works correctly only with C locale
|
2016-08-11 12:41:31 +00:00
|
|
|
vector<double> data;
|
2018-03-21 09:42:39 +00:00
|
|
|
vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() );
|
2016-08-11 12:41:31 +00:00
|
|
|
|
|
|
|
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<double> NGSPICE::GetPhasePlot( const string& aName, int aMaxLen )
|
|
|
|
{
|
2016-08-15 13:52:07 +00:00
|
|
|
LOCALE_IO c_locale; // ngspice works correctly only with C locale
|
2016-08-11 12:41:31 +00:00
|
|
|
vector<double> data;
|
2018-03-21 09:42:39 +00:00
|
|
|
vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() );
|
2016-08-11 12:41:31 +00:00
|
|
|
|
|
|
|
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 ) );
|
|
|
|
}
|
2016-08-11 12:41:07 +00:00
|
|
|
}
|
2016-08-11 12:41:01 +00:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-05 23:49:25 +00:00
|
|
|
bool NGSPICE::Attach( const std::shared_ptr<SIMULATION_MODEL>& aModel )
|
|
|
|
{
|
|
|
|
NGSPICE_CIRCUIT_MODEL* model = dynamic_cast<NGSPICE_CIRCUIT_MODEL*>( aModel.get() );
|
|
|
|
STRING_FORMATTER formatter;
|
|
|
|
|
2022-04-02 12:25:39 +00:00
|
|
|
if( model && model->GetNetlist( &formatter ) )
|
2022-03-05 23:49:25 +00:00
|
|
|
{
|
|
|
|
SIMULATOR::Attach( aModel );
|
|
|
|
|
|
|
|
LoadNetlist( formatter.GetString() );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SIMULATOR::Attach( nullptr );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:07 +00:00
|
|
|
bool NGSPICE::LoadNetlist( const string& aNetlist )
|
2016-08-11 12:41:01 +00:00
|
|
|
{
|
2016-08-15 13:52:07 +00:00
|
|
|
LOCALE_IO c_locale; // ngspice works correctly only with C locale
|
2016-08-11 12:42:17 +00:00
|
|
|
vector<char*> lines;
|
2016-08-11 12:41:07 +00:00
|
|
|
stringstream ss( aNetlist );
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2018-03-03 16:36:26 +00:00
|
|
|
m_netlist = "";
|
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
while( !ss.eof() )
|
2016-08-11 12:41:01 +00:00
|
|
|
{
|
|
|
|
char line[1024];
|
2016-08-11 12:42:17 +00:00
|
|
|
ss.getline( line, sizeof( line ) );
|
|
|
|
lines.push_back( strdup( line ) );
|
2018-03-03 16:36:26 +00:00
|
|
|
m_netlist += std::string( line ) + std::string( "\n" );
|
2016-08-11 12:41:01 +00:00
|
|
|
}
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2018-03-22 10:58:55 +00:00
|
|
|
lines.push_back( nullptr ); // sentinel, as requested in ngSpice_Circ description
|
2022-03-05 23:49:25 +00:00
|
|
|
|
|
|
|
Command( "remcirc" );
|
2022-08-10 15:53:13 +00:00
|
|
|
bool success = !m_ngSpice_Circ( lines.data() );
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
for( auto line : lines )
|
2017-03-01 12:56:24 +00:00
|
|
|
free( line );
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2022-08-10 15:53:13 +00:00
|
|
|
return success;
|
2016-08-11 12:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:07 +00:00
|
|
|
bool NGSPICE::Run()
|
|
|
|
{
|
2022-06-12 03:39:13 +00:00
|
|
|
LOCALE_IO toggle; // ngspice works correctly only with C locale
|
2021-05-05 18:26:56 +00:00
|
|
|
bool success = Command( "bg_run" ); // bg_* commands execute in a separate thread
|
|
|
|
|
|
|
|
if( success )
|
|
|
|
{
|
|
|
|
// wait for end of simulation.
|
|
|
|
// calling wxYield() allows printing activity, and stopping ngspice from GUI
|
|
|
|
// Also note: do not user wxSafeYield, because when using it we cannot stop
|
|
|
|
// ngspice from the GUI
|
|
|
|
do
|
|
|
|
{
|
|
|
|
wxMilliSleep( 50 );
|
|
|
|
wxYield();
|
|
|
|
} while( m_ngSpice_Running() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2016-08-11 12:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool NGSPICE::Stop()
|
|
|
|
{
|
2016-08-15 13:52:07 +00:00
|
|
|
LOCALE_IO c_locale; // ngspice works correctly only with C locale
|
|
|
|
return Command( "bg_halt" ); // bg_* commands execute in a separate thread
|
2016-08-11 12:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool NGSPICE::IsRunning()
|
|
|
|
{
|
2021-05-05 18:26:56 +00:00
|
|
|
// No need to use C locale here
|
2018-03-21 09:42:39 +00:00
|
|
|
return m_ngSpice_Running();
|
2016-08-11 12:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:07 +00:00
|
|
|
bool NGSPICE::Command( const string& aCmd )
|
2016-08-11 12:41:01 +00:00
|
|
|
{
|
2016-08-15 13:52:07 +00:00
|
|
|
LOCALE_IO c_locale; // ngspice works correctly only with C locale
|
2018-03-21 16:22:56 +00:00
|
|
|
validate();
|
2022-08-10 15:53:13 +00:00
|
|
|
return !m_ngSpice_Command( (char*) aCmd.c_str() );
|
2016-08-11 12:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:51 +00:00
|
|
|
string NGSPICE::GetXAxis( SIM_TYPE aType ) const
|
|
|
|
{
|
|
|
|
switch( aType )
|
|
|
|
{
|
2021-03-18 19:31:02 +00:00
|
|
|
case ST_AC:
|
|
|
|
case ST_NOISE:
|
|
|
|
return string( "frequency" );
|
|
|
|
|
|
|
|
case ST_DC:
|
|
|
|
// find plot, which ends with "-sweep"
|
|
|
|
for( auto& plot : AllPlots() )
|
|
|
|
{
|
|
|
|
const string sweepEnding = "-sweep";
|
|
|
|
unsigned int len = sweepEnding.length();
|
2016-08-11 12:41:51 +00:00
|
|
|
|
2021-03-18 19:31:02 +00:00
|
|
|
if( plot.length() > len
|
|
|
|
&& plot.substr( plot.length() - len, len ).compare( sweepEnding ) == 0 )
|
2021-01-25 21:29:13 +00:00
|
|
|
{
|
2021-03-18 19:31:02 +00:00
|
|
|
return string( plot );
|
2021-01-25 21:29:13 +00:00
|
|
|
}
|
2021-03-18 19:31:02 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-08-11 12:41:51 +00:00
|
|
|
|
2021-03-18 19:31:02 +00:00
|
|
|
case ST_TRANSIENT:
|
|
|
|
return string( "time" );
|
2016-08-11 12:41:51 +00:00
|
|
|
|
2021-03-18 19:31:02 +00:00
|
|
|
default:
|
|
|
|
break;
|
2016-08-11 12:41:51 +00:00
|
|
|
}
|
2021-03-18 19:31:02 +00:00
|
|
|
|
2016-08-11 12:41:51 +00:00
|
|
|
return string( "" );
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2021-03-18 19:31:02 +00:00
|
|
|
std::vector<std::string> NGSPICE::GetSettingCommands() const
|
|
|
|
{
|
|
|
|
const NGSPICE_SIMULATOR_SETTINGS* settings =
|
|
|
|
dynamic_cast<const NGSPICE_SIMULATOR_SETTINGS*>( Settings().get() );
|
|
|
|
|
|
|
|
std::vector<std::string> commands;
|
|
|
|
|
|
|
|
wxCHECK( settings, commands );
|
|
|
|
|
|
|
|
switch( settings->GetModelMode() )
|
|
|
|
{
|
|
|
|
case NGSPICE_MODEL_MODE::USER_CONFIG:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NGSPICE_MODEL_MODE::NGSPICE:
|
|
|
|
commands.emplace_back( "unset ngbehavior" );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NGSPICE_MODEL_MODE::PSPICE:
|
|
|
|
commands.emplace_back( "set ngbehavior=ps" );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NGSPICE_MODEL_MODE::LTSPICE:
|
|
|
|
commands.emplace_back( "set ngbehavior=lt" );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NGSPICE_MODEL_MODE::LT_PSPICE:
|
|
|
|
commands.emplace_back( "set ngbehavior=ltps" );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NGSPICE_MODEL_MODE::HSPICE:
|
|
|
|
commands.emplace_back( "set ngbehavior=hs" );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wxFAIL_MSG( wxString::Format( "Undefined NGSPICE_MODEL_MODE %d.",
|
|
|
|
settings->GetModelMode() ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return commands;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const std::string NGSPICE::GetNetlist() const
|
|
|
|
{
|
|
|
|
return m_netlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-21 09:42:39 +00:00
|
|
|
void NGSPICE::init_dll()
|
2016-08-11 12:41:01 +00:00
|
|
|
{
|
2016-08-18 16:10:00 +00:00
|
|
|
if( m_initialized )
|
|
|
|
return;
|
2016-08-11 12:42:18 +00:00
|
|
|
|
2016-08-15 13:52:07 +00:00
|
|
|
LOCALE_IO c_locale; // ngspice works correctly only with C locale
|
2018-03-23 10:14:33 +00:00
|
|
|
const wxStandardPaths& stdPaths = wxStandardPaths::Get();
|
2018-03-21 09:42:39 +00:00
|
|
|
|
2018-03-23 10:12:21 +00:00
|
|
|
if( m_dll.IsLoaded() ) // enable force reload
|
|
|
|
m_dll.Unload();
|
|
|
|
|
2021-03-18 19:31:02 +00:00
|
|
|
// Extra effort to find libngspice
|
|
|
|
// @todo Shouldn't we be using the normal KiCad path searching mechanism here?
|
2018-10-29 13:23:03 +00:00
|
|
|
wxFileName dllFile( "", NGSPICE_DLL_FILE );
|
2019-04-14 17:59:27 +00:00
|
|
|
#if defined(__WINDOWS__)
|
2021-03-18 19:31:02 +00:00
|
|
|
#if defined( _MSC_VER )
|
2021-02-14 01:08:07 +00:00
|
|
|
const vector<string> dllPaths = { "" };
|
2021-03-18 19:31:02 +00:00
|
|
|
#else
|
2018-03-23 10:14:33 +00:00
|
|
|
const vector<string> dllPaths = { "", "/mingw64/bin", "/mingw32/bin" };
|
2021-03-18 19:31:02 +00:00
|
|
|
#endif
|
2022-01-31 23:57:56 +00:00
|
|
|
#elif defined(__WXMAC__)
|
2018-03-23 10:14:33 +00:00
|
|
|
const vector<string> dllPaths = {
|
2021-02-17 04:26:46 +00:00
|
|
|
PATHS::GetOSXKicadUserDataDir().ToStdString() + "/PlugIns/ngspice",
|
|
|
|
PATHS::GetOSXKicadMachineDataDir().ToStdString() + "/PlugIns/ngspice",
|
2021-03-18 19:31:02 +00:00
|
|
|
|
2018-03-23 10:14:33 +00:00
|
|
|
// when running kicad.app
|
2020-07-08 18:29:16 +00:00
|
|
|
stdPaths.GetPluginsDir().ToStdString() + "/sim",
|
2021-03-18 19:31:02 +00:00
|
|
|
|
2018-03-23 10:14:33 +00:00
|
|
|
// when running eeschema.app
|
2021-03-18 19:31:02 +00:00
|
|
|
wxFileName( stdPaths.GetExecutablePath() ).GetPath().ToStdString() +
|
|
|
|
"/../../../../../Contents/PlugIns/sim"
|
2018-03-23 10:14:33 +00:00
|
|
|
};
|
2019-04-14 17:59:27 +00:00
|
|
|
#else // Unix systems
|
|
|
|
const vector<string> dllPaths = { "/usr/local/lib" };
|
|
|
|
#endif
|
2018-03-23 10:14:33 +00:00
|
|
|
|
2019-04-14 17:59:27 +00:00
|
|
|
#if defined(__WINDOWS__) || (__WXMAC__)
|
2018-03-23 10:14:33 +00:00
|
|
|
for( const auto& path : dllPaths )
|
|
|
|
{
|
|
|
|
dllFile.SetPath( path );
|
|
|
|
wxLogTrace( traceNgspice, "libngspice search path: %s", dllFile.GetFullPath() );
|
|
|
|
m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
|
|
|
|
|
|
|
|
if( m_dll.IsLoaded() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceNgspice, "libngspice path found in: %s", dllFile.GetFullPath() );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-04-19 12:48:31 +00:00
|
|
|
|
|
|
|
if( !m_dll.IsLoaded() ) // try also the system libraries
|
|
|
|
m_dll.Load( wxDynamicLibrary::CanonicalizeName( "ngspice" ) );
|
2021-03-18 19:31:02 +00:00
|
|
|
#else
|
2019-04-14 17:59:27 +00:00
|
|
|
// First, try the system libraries
|
|
|
|
m_dll.Load( NGSPICE_DLL_FILE, wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
|
|
|
|
|
|
|
|
// If failed, try some other paths:
|
|
|
|
if( !m_dll.IsLoaded() )
|
|
|
|
{
|
|
|
|
for( const auto& path : dllPaths )
|
|
|
|
{
|
|
|
|
dllFile.SetPath( path );
|
|
|
|
wxLogTrace( traceNgspice, "libngspice search path: %s", dllFile.GetFullPath() );
|
|
|
|
m_dll.Load( dllFile.GetFullPath(), wxDL_VERBATIM | wxDL_QUIET | wxDL_NOW );
|
|
|
|
|
|
|
|
if( m_dll.IsLoaded() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceNgspice, "libngspice path found in: %s", dllFile.GetFullPath() );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2018-03-21 09:42:39 +00:00
|
|
|
|
2018-03-23 10:12:21 +00:00
|
|
|
if( !m_dll.IsLoaded() )
|
2018-03-21 09:42:39 +00:00
|
|
|
throw std::runtime_error( "Missing ngspice shared library" );
|
|
|
|
|
|
|
|
m_error = false;
|
|
|
|
|
|
|
|
// Obtain function pointers
|
2018-03-23 10:12:21 +00:00
|
|
|
m_ngSpice_Init = (ngSpice_Init) m_dll.GetSymbol( "ngSpice_Init" );
|
|
|
|
m_ngSpice_Circ = (ngSpice_Circ) m_dll.GetSymbol( "ngSpice_Circ" );
|
|
|
|
m_ngSpice_Command = (ngSpice_Command) m_dll.GetSymbol( "ngSpice_Command" );
|
|
|
|
m_ngGet_Vec_Info = (ngGet_Vec_Info) m_dll.GetSymbol( "ngGet_Vec_Info" );
|
2020-04-15 01:51:58 +00:00
|
|
|
m_ngSpice_CurPlot = (ngSpice_CurPlot) m_dll.GetSymbol( "ngSpice_CurPlot" );
|
2018-03-23 10:12:21 +00:00
|
|
|
m_ngSpice_AllPlots = (ngSpice_AllPlots) m_dll.GetSymbol( "ngSpice_AllPlots" );
|
|
|
|
m_ngSpice_AllVecs = (ngSpice_AllVecs) m_dll.GetSymbol( "ngSpice_AllVecs" );
|
|
|
|
m_ngSpice_Running = (ngSpice_Running) m_dll.GetSymbol( "ngSpice_running" ); // it is not a typo
|
2018-03-21 09:42:39 +00:00
|
|
|
|
2021-03-18 19:31:02 +00:00
|
|
|
m_ngSpice_Init( &cbSendChar, &cbSendStat, &cbControlledExit, NULL, NULL,
|
|
|
|
&cbBGThreadRunning, this );
|
2016-08-15 11:52:13 +00:00
|
|
|
|
2016-10-13 11:25:06 +00:00
|
|
|
// Load a custom spinit file, to fix the problem with loading .cm files
|
|
|
|
// Switch to the executable directory, so the relative paths are correct
|
|
|
|
wxString cwd( wxGetCwd() );
|
2018-03-23 10:14:33 +00:00
|
|
|
wxFileName exeDir( stdPaths.GetExecutablePath() );
|
2016-10-13 11:25:06 +00:00
|
|
|
wxSetWorkingDirectory( exeDir.GetPath() );
|
|
|
|
|
|
|
|
// Find *.cm files
|
|
|
|
string cmPath = findCmPath();
|
|
|
|
|
|
|
|
// __CMPATH is used in custom spinit file to point to the codemodels directory
|
|
|
|
if( !cmPath.empty() )
|
|
|
|
Command( "set __CMPATH=\"" + cmPath + "\"" );
|
|
|
|
|
|
|
|
// Possible relative locations for spinit file
|
|
|
|
const vector<string> spiceinitPaths =
|
|
|
|
{
|
|
|
|
".",
|
2018-03-23 10:14:33 +00:00
|
|
|
#ifdef __WXMAC__
|
2020-07-08 18:29:16 +00:00
|
|
|
stdPaths.GetPluginsDir().ToStdString() + "/sim/ngspice/scripts",
|
2021-03-18 19:31:02 +00:00
|
|
|
wxFileName( stdPaths.GetExecutablePath() ).GetPath().ToStdString() +
|
|
|
|
"/../../../../../Contents/PlugIns/sim/ngspice/scripts"
|
|
|
|
#endif
|
2016-10-13 11:25:06 +00:00
|
|
|
"../share/kicad",
|
|
|
|
"../share",
|
|
|
|
"../../share/kicad",
|
|
|
|
"../../share"
|
|
|
|
};
|
|
|
|
|
|
|
|
bool foundSpiceinit = false;
|
|
|
|
|
|
|
|
for( const auto& path : spiceinitPaths )
|
|
|
|
{
|
2018-03-23 10:14:33 +00:00
|
|
|
wxLogTrace( traceNgspice, "ngspice init script search path: %s", path );
|
|
|
|
|
2016-10-13 11:25:06 +00:00
|
|
|
if( loadSpinit( path + "/spiceinit" ) )
|
|
|
|
{
|
2018-03-23 10:14:33 +00:00
|
|
|
wxLogTrace( traceNgspice, "ngspice path found in: %s", path );
|
2016-10-13 11:25:06 +00:00
|
|
|
foundSpiceinit = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last chance to load codemodel files, we have not found
|
|
|
|
// spiceinit file, but we know the path to *.cm files
|
|
|
|
if( !foundSpiceinit && !cmPath.empty() )
|
|
|
|
loadCodemodels( cmPath );
|
|
|
|
|
|
|
|
// Restore the working directory
|
|
|
|
wxSetWorkingDirectory( cwd );
|
|
|
|
|
2018-03-21 09:42:39 +00:00
|
|
|
// Workarounds to avoid hang ups on certain errors
|
|
|
|
// These commands have to be called, no matter what is in the spinit file
|
2016-08-11 12:42:18 +00:00
|
|
|
Command( "unset interactive" );
|
2016-10-13 11:25:06 +00:00
|
|
|
Command( "set noaskquit" );
|
|
|
|
Command( "set nomoremode" );
|
2016-08-18 16:10:00 +00:00
|
|
|
|
|
|
|
m_initialized = true;
|
2016-08-11 12:41:01 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2016-10-13 11:25:06 +00:00
|
|
|
bool NGSPICE::loadSpinit( const string& aFileName )
|
|
|
|
{
|
|
|
|
if( !wxFileName::FileExists( aFileName ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wxTextFile file;
|
|
|
|
|
|
|
|
if( !file.Open( aFileName ) )
|
|
|
|
return false;
|
|
|
|
|
2022-08-15 17:47:23 +00:00
|
|
|
for( wxString& cmd = file.GetFirstLine(); !file.Eof(); cmd = file.GetNextLine() )
|
2016-10-13 11:25:06 +00:00
|
|
|
Command( cmd.ToStdString() );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string NGSPICE::findCmPath() const
|
|
|
|
{
|
|
|
|
const vector<string> cmPaths =
|
|
|
|
{
|
2018-03-23 10:14:33 +00:00
|
|
|
#ifdef __WXMAC__
|
2016-10-13 11:25:06 +00:00
|
|
|
"/Applications/ngspice/lib/ngspice",
|
2018-03-23 10:14:33 +00:00
|
|
|
"Contents/Frameworks",
|
2020-07-08 18:29:16 +00:00
|
|
|
wxStandardPaths::Get().GetPluginsDir().ToStdString() + "/sim/ngspice",
|
2021-03-18 19:31:02 +00:00
|
|
|
wxFileName( wxStandardPaths::Get().GetExecutablePath() ).GetPath().ToStdString() +
|
|
|
|
"/../../../../../Contents/PlugIns/sim/ngspice",
|
2019-10-04 01:13:04 +00:00
|
|
|
"../Plugins/sim/ngspice",
|
2021-03-18 19:31:02 +00:00
|
|
|
#endif
|
2016-10-13 11:25:06 +00:00
|
|
|
"../lib/ngspice",
|
2019-10-04 01:13:04 +00:00
|
|
|
"../../lib/ngspice",
|
2016-10-13 11:25:06 +00:00
|
|
|
"lib/ngspice",
|
|
|
|
"ngspice"
|
|
|
|
};
|
|
|
|
|
|
|
|
for( const auto& path : cmPaths )
|
|
|
|
{
|
2018-03-23 10:14:33 +00:00
|
|
|
wxLogTrace( traceNgspice, "ngspice code models search path: %s", path );
|
|
|
|
|
|
|
|
if( wxFileName::FileExists( path + "/spice2poly.cm" ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceNgspice, "ngspice code models found in: %s", path );
|
2016-10-13 11:25:06 +00:00
|
|
|
return path;
|
2018-03-23 10:14:33 +00:00
|
|
|
}
|
2016-10-13 11:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool NGSPICE::loadCodemodels( const string& aPath )
|
|
|
|
{
|
|
|
|
wxArrayString cmFiles;
|
|
|
|
size_t count = wxDir::GetAllFiles( aPath, &cmFiles );
|
|
|
|
|
|
|
|
for( const auto& cm : cmFiles )
|
|
|
|
Command( "codemodel " + cm.ToStdString() );
|
|
|
|
|
|
|
|
return count != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-05 17:49:40 +00:00
|
|
|
int NGSPICE::cbSendChar( char* aWhat, int aId, void* aUser )
|
2016-08-11 12:41:01 +00:00
|
|
|
{
|
2021-05-05 17:49:40 +00:00
|
|
|
NGSPICE* sim = reinterpret_cast<NGSPICE*>( aUser );
|
2016-08-11 12:41:07 +00:00
|
|
|
|
2016-08-11 12:41:19 +00:00
|
|
|
if( sim->m_reporter )
|
2016-08-11 12:41:35 +00:00
|
|
|
{
|
|
|
|
// strip stdout/stderr from the line
|
2021-05-05 17:49:40 +00:00
|
|
|
if( ( strncasecmp( aWhat, "stdout ", 7 ) == 0 )
|
|
|
|
|| ( strncasecmp( aWhat, "stderr ", 7 ) == 0 ) )
|
|
|
|
aWhat += 7;
|
2016-08-11 12:41:35 +00:00
|
|
|
|
2021-05-05 17:49:40 +00:00
|
|
|
sim->m_reporter->Report( aWhat );
|
2016-08-11 12:41:35 +00:00
|
|
|
}
|
2016-08-11 12:41:07 +00:00
|
|
|
|
|
|
|
return 0;
|
2016-08-11 12:41:01 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 12:41:07 +00:00
|
|
|
|
2021-05-05 17:49:40 +00:00
|
|
|
int NGSPICE::cbSendStat( char *aWhat, int aId, void* aUser )
|
2016-08-11 12:41:01 +00:00
|
|
|
{
|
2016-08-11 12:41:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-05 17:49:40 +00:00
|
|
|
int NGSPICE::cbBGThreadRunning( NG_BOOL aFinished, int aId, void* aUser )
|
2016-08-11 12:41:19 +00:00
|
|
|
{
|
2021-05-05 17:49:40 +00:00
|
|
|
NGSPICE* sim = reinterpret_cast<NGSPICE*>( aUser );
|
2016-08-11 12:41:19 +00:00
|
|
|
|
|
|
|
if( sim->m_reporter )
|
2021-05-05 17:49:40 +00:00
|
|
|
sim->m_reporter->OnSimStateChange( sim, aFinished ? SIM_IDLE : SIM_RUNNING );
|
2016-08-11 12:41:19 +00:00
|
|
|
|
2016-08-11 12:41:07 +00:00
|
|
|
return 0;
|
2016-08-11 12:41:01 +00:00
|
|
|
}
|
2016-08-11 12:42:18 +00:00
|
|
|
|
|
|
|
|
2021-05-05 17:49:40 +00:00
|
|
|
int NGSPICE::cbControlledExit( int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId, void* aUser )
|
2016-08-11 12:42:18 +00:00
|
|
|
{
|
|
|
|
// Something went wrong, reload the dll
|
2021-05-05 17:49:40 +00:00
|
|
|
NGSPICE* sim = reinterpret_cast<NGSPICE*>( aUser );
|
2018-03-21 16:22:56 +00:00
|
|
|
sim->m_error = true;
|
2016-08-11 12:42:18 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-08-18 16:10:00 +00:00
|
|
|
|
2018-03-21 16:22:56 +00:00
|
|
|
|
|
|
|
void NGSPICE::validate()
|
|
|
|
{
|
|
|
|
if( m_error )
|
|
|
|
{
|
|
|
|
m_initialized = false;
|
|
|
|
init_dll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-28 20:40:24 +00:00
|
|
|
void NGSPICE::Clean()
|
|
|
|
{
|
|
|
|
Command( "destroy all" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-18 16:10:00 +00:00
|
|
|
bool NGSPICE::m_initialized = false;
|