KiCad PCB EDA Suite
Loading...
Searching...
No Matches
spice_settings.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2021 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Wayne Stambaugh <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 3
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#include "spice_settings.h"
24#include <settings/parameters.h>
25
26#include <fast_float/fast_float.h>
27
28#include <cctype>
29#include <climits>
30
31
33
34
35SPICE_SETTINGS::SPICE_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
36 NESTED_SETTINGS( "simulator", spiceSettingsSchemaVersion, aParent, aPath ),
37 m_fixIncludePaths( true )
38{
39 m_params.emplace_back( new PARAM<wxString>( "workbook_filename", &m_workbookFilename, "" ) );
40 m_params.emplace_back( new PARAM<bool>( "fix_include_paths", &m_fixIncludePaths, true ) );
41}
42
43
49
50
61
62
64{
65 const NGSPICE_SETTINGS* settings = dynamic_cast<const NGSPICE_SETTINGS*>( &aRhs );
66
67 wxCHECK( settings, false );
68
70 && m_options == settings->m_options;
71}
72
73
74wxString NGSPICE_SETTINGS::GetOption( const std::string& aName ) const
75{
76 auto it = m_options.find( aName );
77
78 return it != m_options.end() ? it->second : wxString();
79}
80
81
82void NGSPICE_SETTINGS::SetOption( const std::string& aName, const wxString& aValue )
83{
84 if( aValue.Strip( wxString::both ).IsEmpty() )
85 m_options.erase( aName );
86 else
87 m_options[aName] = aValue;
88}
89
90
91std::optional<wxString> NGSPICE_SETTINGS::NormalizeOption( const std::string& aName, const wxString& aValue )
92{
93 wxString value = aValue.Strip( wxString::both );
94
95 if( value.IsEmpty() )
96 return wxString();
97
98 if( aName == "method" )
99 {
100 value.MakeLower();
101
102 if( value == wxS( "trap" ) || value == wxS( "gear" ) )
103 return value;
104
105 return std::nullopt;
106 }
107
108 if( aName == "itl1" || aName == "itl4" )
109 {
110 long limit = 0;
111
112 if( !value.IsNumber() || !value.ToLong( &limit ) || limit < 1 || limit > INT_MAX )
113 return std::nullopt;
114
115 return wxString::Format( wxS( "%ld" ), limit );
116 }
117
118 // ngspice reads only a point as the decimal separator
119 value.Replace( wxS( "," ), wxS( "." ) );
120
121 std::string str( value.ToUTF8() );
122 const char* begin = str.data();
123 const char* end = begin + str.size();
124 const char* mantissa = ( *begin == '+' || *begin == '-' ) ? begin + 1 : begin;
125
126 // from_chars also takes inf and nan, which ngspice does not
127 if( mantissa == end || !( std::isdigit( (unsigned char) *mantissa ) || *mantissa == '.' ) )
128 return std::nullopt;
129
130 double parsed = 0.0;
131 auto result = fast_float::from_chars( mantissa, end, parsed );
132
133 if( result.ec != std::errc() )
134 return std::nullopt;
135
136 // Letters after the number are a scale factor and units, which ngspice accepts
137 for( const char* c = result.ptr; c != end; ++c )
138 {
139 if( !std::isalpha( (unsigned char) *c ) )
140 return std::nullopt;
141 }
142
143 return value;
144}
145
146
148{
149 wxString options;
150
151 for( const std::string& name : SupportedOptions() )
152 {
153 std::optional<wxString> value = NormalizeOption( name, GetOption( name ) );
154
155 if( value && !value->IsEmpty() )
156 options += wxString::Format( wxS( " %s=%s" ), wxString::FromUTF8( name ), *value );
157 }
158
159 if( options.IsEmpty() )
160 return wxEmptyString;
161
162 return wxS( ".options" ) + options;
163}
164
165
166const std::vector<std::string>& NGSPICE_SETTINGS::SupportedOptions()
167{
168 static const std::vector<std::string> options = { "method", "reltol", "abstol", "vntol", "chgtol",
169 "trtol", "gmin", "gshunt", "rshunt", "itl1",
170 "itl4", "temp", "tnom" };
171
172 return options;
173}
const char * name
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
NESTED_SETTINGS(const std::string &aName, int aSchemaVersion, JSON_SETTINGS *aParent, const std::string &aPath, bool aLoadFromFile=true)
wxString GetOptionsDirective() const
Build a ".options ..." directive from the valid options that have a value set, or an empty string if ...
std::map< std::string, wxString > m_options
static std::optional< wxString > NormalizeOption(const std::string &aName, const wxString &aValue)
Return aValue in the form ngspice reads for option aName.
NGSPICE_SETTINGS(JSON_SETTINGS *aParent, const std::string &aPath)
bool operator==(const SPICE_SETTINGS &aRhs) const override
void SetOption(const std::string &aName, const wxString &aValue)
Set the value for a simulator option. A blank value clears it.
NGSPICE_COMPATIBILITY_MODE m_compatibilityMode
static const std::vector< std::string > & SupportedOptions()
The ngspice option names exposed through the settings UI.
wxString GetOption(const std::string &aName) const
Return the value set for a simulator option, or an empty string if it is unset.
Stores an enum as an integer.
Definition parameters.h:232
Represents a map of <std::string, Value>.
Definition parameters.h:752
wxString m_workbookFilename
SPICE_SETTINGS(JSON_SETTINGS *aParent, const std::string &aPath)
virtual bool operator==(const SPICE_SETTINGS &aRhs) const =0
const int spiceSettingsSchemaVersion
NGSPICE_COMPATIBILITY_MODE
Ngspice simulator compatibility modes.
VECTOR2I end
wxString result
Test unit parsing edge cases and error handling.