2016-08-11 12:41:30 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-12-05 20:01:36 +00:00
|
|
|
* Copyright (C) 2016-2022 CERN
|
2023-01-12 12:46:52 +00:00
|
|
|
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.TXT for contributors.
|
2022-12-05 20:01:36 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
2021-04-14 07:54:29 +00:00
|
|
|
*
|
2016-08-11 12:41:30 +00:00
|
|
|
* 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:30 +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:30 +00:00
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2023-07-14 09:21:58 +00:00
|
|
|
#include "spice_circuit_model.h"
|
2023-09-09 04:10:57 +00:00
|
|
|
#include <string_utils.h>
|
2020-04-15 01:51:58 +00:00
|
|
|
#include <wx/regex.h>
|
2019-05-27 22:13:00 +00:00
|
|
|
#include <wx/tokenzr.h>
|
2022-04-12 14:37:06 +00:00
|
|
|
#include <locale_io.h>
|
2016-08-11 12:41:52 +00:00
|
|
|
|
|
|
|
|
2023-07-14 09:21:58 +00:00
|
|
|
SIM_TRACE_TYPE SPICE_CIRCUIT_MODEL::VectorToSignal( const std::string& aVector,
|
|
|
|
wxString& aSignal ) const
|
2020-04-15 01:51:58 +00:00
|
|
|
{
|
2023-02-05 20:47:27 +00:00
|
|
|
static wxString BRANCH( wxS( "#branch" ) );
|
2023-09-19 16:37:20 +00:00
|
|
|
static wxString POWER( wxS( ":power" ) );
|
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
// See ngspice manual chapt. 31.1 "Accessing internal device parameters"
|
2023-02-05 20:47:27 +00:00
|
|
|
static wxRegEx internalDevParameter( wxS( "^@(\\w*[\\.\\w+]*)\\[(\\w*)\\]$" ), wxRE_ADVANCED );
|
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
wxString vector( aVector );
|
|
|
|
|
|
|
|
if( !internalDevParameter.Matches( vector ) )
|
|
|
|
{
|
2023-02-05 20:47:27 +00:00
|
|
|
if( vector.EndsWith( BRANCH ) )
|
|
|
|
{
|
|
|
|
aSignal = wxT( "I(" ) + vector.Left( vector.Length() - BRANCH.Length() ) + wxT( ")" );
|
|
|
|
return SPT_CURRENT;
|
|
|
|
}
|
2023-09-19 16:37:20 +00:00
|
|
|
else if( vector.EndsWith( POWER ) )
|
|
|
|
{
|
|
|
|
aSignal = wxT( "P(" ) + vector.Left( vector.Length() - POWER.Length() ) + wxT( ")" );
|
|
|
|
return SPT_POWER;
|
|
|
|
}
|
2023-02-05 20:47:27 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
aSignal = wxT( "V(" ) + vector + wxT( ")" );
|
|
|
|
return SPT_VOLTAGE;
|
|
|
|
}
|
2020-04-15 01:51:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxString paramType = internalDevParameter.GetMatch( vector, 2 );
|
|
|
|
|
|
|
|
if( paramType.Lower()[0] == 'i' )
|
|
|
|
{
|
|
|
|
// this is a branch current
|
|
|
|
paramType[0] = 'I';
|
2022-10-25 23:31:48 +00:00
|
|
|
aSignal = paramType + wxT( "(" );
|
|
|
|
aSignal += internalDevParameter.GetMatch( vector, 1 ).Upper() + wxT( ")" );
|
2020-04-15 01:51:58 +00:00
|
|
|
return SPT_CURRENT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return SPT_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-14 09:21:58 +00:00
|
|
|
wxString SPICE_CIRCUIT_MODEL::GetSchTextSimCommand()
|
2016-08-11 12:41:30 +00:00
|
|
|
{
|
2022-10-25 23:31:48 +00:00
|
|
|
wxString simCmd;
|
2016-08-11 12:41:30 +00:00
|
|
|
|
2023-01-12 12:46:52 +00:00
|
|
|
ReadDirectives( 0 );
|
2016-08-11 12:41:30 +00:00
|
|
|
|
2023-01-14 00:17:33 +00:00
|
|
|
for( const wxString& directive : GetDirectives() )
|
2016-08-11 12:41:30 +00:00
|
|
|
{
|
2022-04-12 14:37:06 +00:00
|
|
|
if( IsSimCommand( directive ) )
|
2022-10-25 23:31:48 +00:00
|
|
|
simCmd += wxString::Format( wxT( "%s\r\n" ), directive );
|
2016-08-11 12:41:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 17:25:52 +00:00
|
|
|
return simCmd.Trim();
|
2016-08-11 12:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-14 09:21:58 +00:00
|
|
|
SIM_TYPE SPICE_CIRCUIT_MODEL::CommandToSimType( const wxString& aCmd )
|
2016-08-11 12:41:34 +00:00
|
|
|
{
|
2023-02-22 23:28:35 +00:00
|
|
|
wxString cmd = aCmd.Lower().Trim();
|
2023-02-22 13:52:52 +00:00
|
|
|
|
2023-07-05 17:25:52 +00:00
|
|
|
if( cmd == wxT( ".op" ) ) return ST_OP;
|
|
|
|
else if( cmd.StartsWith( wxT( ".ac" ) ) ) return ST_AC;
|
|
|
|
else if( cmd.StartsWith( wxT( ".dc" ) ) ) return ST_DC;
|
|
|
|
else if( cmd.StartsWith( wxT( ".tran" ) ) ) return ST_TRAN;
|
|
|
|
else if( cmd.StartsWith( wxT( ".disto" ) ) ) return ST_DISTO;
|
|
|
|
else if( cmd.StartsWith( wxT( ".noise" ) ) ) return ST_NOISE;
|
|
|
|
else if( cmd.StartsWith( wxT( ".pz" ) ) ) return ST_PZ;
|
|
|
|
else if( cmd.StartsWith( wxT( ".sens" ) ) ) return ST_SENS;
|
|
|
|
else if( cmd.StartsWith( wxT( ".sp" ) ) ) return ST_SP;
|
|
|
|
else if( cmd.StartsWith( wxT( ".tf" ) ) ) return ST_TF;
|
2023-11-04 15:19:05 +00:00
|
|
|
|
|
|
|
else if( cmd.StartsWith( wxT( "fft" ) ) || cmd.Contains( wxT( "\nfft" ) ) )
|
|
|
|
return ST_FFT;
|
|
|
|
else
|
|
|
|
return ST_UNKNOWN;
|
2016-08-11 12:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-14 09:21:58 +00:00
|
|
|
bool SPICE_CIRCUIT_MODEL::ParseDCCommand( const wxString& aCmd, SPICE_DC_PARAMS* aSource1,
|
|
|
|
SPICE_DC_PARAMS* aSource2 )
|
2019-05-27 22:13:00 +00:00
|
|
|
{
|
|
|
|
if( !aCmd.Lower().StartsWith( ".dc" ) )
|
|
|
|
return false;
|
|
|
|
|
2023-04-22 20:17:08 +00:00
|
|
|
wxString cmd = aCmd.Mid( 3 );
|
|
|
|
wxStringTokenizer tokens( cmd, wxS( " \t" ), wxTOKEN_STRTOK );
|
2019-05-27 22:13:00 +00:00
|
|
|
|
|
|
|
aSource1->m_source = tokens.GetNextToken();
|
|
|
|
aSource1->m_vstart = SPICE_VALUE( tokens.GetNextToken() );
|
|
|
|
aSource1->m_vend = SPICE_VALUE( tokens.GetNextToken() );
|
|
|
|
aSource1->m_vincrement = SPICE_VALUE( tokens.GetNextToken() );
|
|
|
|
|
2023-07-03 14:45:58 +00:00
|
|
|
aSource2->m_source = tokens.GetNextToken();
|
|
|
|
aSource2->m_vstart = SPICE_VALUE( tokens.GetNextToken() );
|
|
|
|
aSource2->m_vend = SPICE_VALUE( tokens.GetNextToken() );
|
|
|
|
aSource2->m_vincrement = SPICE_VALUE( tokens.GetNextToken() );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-14 09:21:58 +00:00
|
|
|
bool SPICE_CIRCUIT_MODEL::ParsePZCommand( const wxString& aCmd, wxString* transferFunction,
|
|
|
|
wxString* input, wxString* inputRef, wxString* output,
|
|
|
|
wxString* outputRef, SPICE_PZ_ANALYSES* analyses )
|
2023-07-13 15:04:06 +00:00
|
|
|
{
|
|
|
|
if( !aCmd.Lower().StartsWith( wxS( ".pz" ) ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*transferFunction = "vol";
|
|
|
|
analyses->m_Poles = true;
|
|
|
|
analyses->m_Zeros = true;
|
|
|
|
|
|
|
|
wxStringTokenizer tokens( aCmd.Mid( 3 ), wxS( " \t" ), wxTOKEN_STRTOK );
|
|
|
|
|
|
|
|
if( tokens.HasMoreTokens() )
|
|
|
|
*input = tokens.GetNextToken();
|
|
|
|
|
|
|
|
if( tokens.HasMoreTokens() )
|
|
|
|
*inputRef = tokens.GetNextToken();
|
|
|
|
|
|
|
|
if( tokens.HasMoreTokens() )
|
|
|
|
*output = tokens.GetNextToken();
|
|
|
|
|
|
|
|
if( tokens.HasMoreTokens() )
|
|
|
|
*outputRef = tokens.GetNextToken();
|
|
|
|
|
|
|
|
if( tokens.HasMoreTokens() )
|
|
|
|
*transferFunction = tokens.GetNextToken();
|
|
|
|
|
|
|
|
if( tokens.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
wxString token = tokens.GetNextToken().Lower();
|
|
|
|
|
|
|
|
if( token == wxS( "pol" ) )
|
|
|
|
analyses->m_Zeros = false;
|
|
|
|
else if( token == wxS( "zer" ) )
|
|
|
|
analyses->m_Poles = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-14 09:21:58 +00:00
|
|
|
bool SPICE_CIRCUIT_MODEL::ParseNoiseCommand( const wxString& aCmd, wxString* aOutput,
|
|
|
|
wxString* aRef, wxString* aSource, wxString* aScale,
|
|
|
|
SPICE_VALUE* aPts, SPICE_VALUE* aFStart,
|
|
|
|
SPICE_VALUE* aFStop, bool* aSaveAll )
|
2023-07-03 14:45:58 +00:00
|
|
|
{
|
|
|
|
if( !aCmd.Lower().StartsWith( wxS( ".noise" ) ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wxString cmd = aCmd.Mid( 6 );
|
|
|
|
|
|
|
|
cmd.Trim( false );
|
|
|
|
|
|
|
|
if( !cmd.Lower().StartsWith( wxS( "v(" ) ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
cmd = cmd.Mid( 2 );
|
|
|
|
|
|
|
|
wxString function = cmd.Before( ')' );
|
|
|
|
wxString params = cmd.After( ')' );
|
|
|
|
|
|
|
|
wxStringTokenizer func_tokens( function, wxS( " ,\t" ), wxTOKEN_STRTOK );
|
|
|
|
|
|
|
|
*aOutput = func_tokens.GetNextToken();
|
|
|
|
*aRef = func_tokens.GetNextToken();
|
|
|
|
|
|
|
|
wxStringTokenizer tokens( params, wxS( " \t" ), wxTOKEN_STRTOK );
|
|
|
|
wxString token = tokens.GetNextToken();
|
|
|
|
|
|
|
|
if( !token.IsEmpty() )
|
|
|
|
{
|
|
|
|
*aSource = token;
|
|
|
|
token = tokens.GetNextToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( token.Lower() == "dec" || token.Lower() == "oct" || token.Lower() == "lin" )
|
2019-05-27 22:13:00 +00:00
|
|
|
{
|
2023-07-03 14:45:58 +00:00
|
|
|
*aScale = token;
|
|
|
|
token = tokens.GetNextToken();
|
2019-05-27 22:13:00 +00:00
|
|
|
}
|
|
|
|
|
2023-07-03 14:45:58 +00:00
|
|
|
if( !token.IsEmpty() )
|
|
|
|
{
|
|
|
|
*aPts = token;
|
|
|
|
token = tokens.GetNextToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !token.IsEmpty() )
|
|
|
|
{
|
|
|
|
*aFStart = SPICE_VALUE( token );
|
|
|
|
token = tokens.GetNextToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !token.IsEmpty() )
|
2023-07-04 10:04:20 +00:00
|
|
|
{
|
2023-07-03 14:45:58 +00:00
|
|
|
*aFStop = SPICE_VALUE( token );
|
2023-07-04 10:04:20 +00:00
|
|
|
token = tokens.GetNextToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !token.IsEmpty() )
|
|
|
|
*aSaveAll = true;
|
2023-07-03 14:45:58 +00:00
|
|
|
|
2019-05-27 22:13:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-05-31 02:55:48 +00:00
|
|
|
|
|
|
|
|
2023-07-14 09:21:58 +00:00
|
|
|
void SPICE_CIRCUIT_MODEL::WriteDirectives( const wxString& aSimCommand, unsigned aSimOptions,
|
|
|
|
OUTPUTFORMATTER& aFormatter ) const
|
2022-05-31 02:55:48 +00:00
|
|
|
{
|
2023-07-05 17:25:52 +00:00
|
|
|
if( aSimCommand.IsEmpty() )
|
|
|
|
aSimOptions |= OPTION_SIM_COMMAND;
|
2022-12-07 00:23:18 +00:00
|
|
|
|
2023-07-05 17:25:52 +00:00
|
|
|
NETLIST_EXPORTER_SPICE::WriteDirectives( aSimCommand, aSimOptions, aFormatter );
|
2022-06-12 03:39:13 +00:00
|
|
|
|
2023-07-05 17:25:52 +00:00
|
|
|
if( !aSimCommand.IsEmpty() )
|
|
|
|
aFormatter.Print( 0, "%s\n", TO_UTF8( aSimCommand ) );
|
2022-05-31 02:55:48 +00:00
|
|
|
}
|