kicad-developers team mailing list archive
-
kicad-developers team
-
Mailing list archive
-
Message #26647
Re: Spice simulation on windows.
Finally, I could get back to the issue. If we really focus about the
consequences, the real problem is not that spinit is not processed, but
the codemodel libraries are not loaded.
Looking at the default spinit file:
- most of the lines are comments
- one causes problems ('set interactive', has to be unset later)
- three others have no special meaning in shared library mode (aliases
and 'set x11lineararcs')
- the only meaningful lines are the ones that load codemodel libraries
To fix the problem, there is a patch which:
- Allows ngspice to load the default spinit/.spiceinit files (no changes
here).
- Looks for codemodel files in a few paths relative to eeschema
executable. If a valid path is found, then an ngspice variable __CMPATH
is set.
- After the default initialization, looks for spiceinit (note it is
spiceinit not .spiceinit) file in a few paths relative to eeschema
executable. If one is found, then it is executed. If we decide to
provide our spiceinit file (see the attachment), then thanks to __CMPATH
variable we can point to the right codemodel directory.
- If no spiceinit was found, but we know the correct path to codemodels,
then they are simply loaded.
- Unsets a few variables which may cause simulator hangups.
Once the patch is committed, codemodels should work out of the box for
the common msys2 builds and nightly Windows installers, even without a
custom spiceinit. If OSX bundles provide codemodel libraries, then there
is a chance it will work for them as well, otherwise we can add another
search path.
I know it may look like an ugly hack, but sincerely I have no better
idea at the moment. I am going to leave it here for comments for a few
days, if there are no objections, I will commit the changes.
Regards,
Orson
On 10/06/2016 05:53 PM, Wayne Stambaugh wrote:
> I have some additional information that may prove useful:
>
> 1) Using relative paths in the spinit file does not work on windows.
>
> 2) Placing a spinit file in the path where the ngspice and libngspice
> binaries reside works with no need to set any environment variables.
>
> Option 2 could be used by the installer. The installer itself would
> have to create the spinit file by substituting the install path for the
> path of the .cm files. I'm not sure if this would work on osx. Maybe
> one of our osx devs could test this. If it does, than that would
> resolve the simulation init issues.
>
> I've attached a simple circuit that demonstrates the issue. When the
> .cm files are not located, the simulation will run with the following
> warnings and cause the output of the op-amp to be an impossibly high 260V:
>
> Error on line 0 :
> a$poly$e.xu1.eos %vd [ xu1.53 xu1.98 ] %vd ( xu1.3 net-_u1-pad1_ )
> a$poly$e.xu1.eos
> MIF-ERROR - unable to find definition of model a$poly$e.xu1.eos
> Warning: Model issue on line 0 :
> .model a$poly$e.xu1.eos spice2poly coef = [ 1.7e-3 1 ] ...
> Unknown model type spice2poly - ignored
> Error on line 0 :
> a$poly$e.xu1.eref1 %vd [ vdd 0 0 0 ] %vd ( xu1.98 0 ) a$poly$e.xu1.eref1
> MIF-ERROR - unable to find definition of model a$poly$e.xu1.eref1
> Warning: Model issue on line 0 :
> .model a$poly$e.xu1.eref1 spice2poly coef = [ 0 0.5 0.5 ] ...
> Unknown model type spice2poly - ignored
> Error on line 0 :
> a$poly$e.xu1.eref2 %vd [ net-_u1-pad1_ 0 /out 0 ] %vd ( xu1.97 0 )
> a$poly$e.xu1.eref2
> MIF-ERROR - unable to find definition of model a$poly$e.xu1.eref2
> Warning: Model issue on line 0 :
> .model a$poly$e.xu1.eref2 spice2poly coef = [ 0 0.5 0.5 ] ...
> Unknown model type spice2poly - ignored
> Error on line 0 :
> a$poly$e.xu1.eo3 %vd [ xu1.98 xu1.30 ] %vd ( vdd xu1.42 ) a$poly$e.xu1.eo3
> MIF-ERROR - unable to find definition of model a$poly$e.xu1.eo3
> Warning: Model issue on line 0 :
> .model a$poly$e.xu1.eo3 spice2poly coef = [ 0.7175 0.5 ] ...
> Unknown model type spice2poly - ignored
> Error on line 0 :
> a$poly$e.xu1.eo4 %vd [ xu1.30 xu1.98 ] %vd ( xu1.44 0 ) a$poly$e.xu1.eo4
> MIF-ERROR - unable to find definition of model a$poly$e.xu1.eo4
> Warning: Model issue on line 0 :
> .model a$poly$e.xu1.eo4 spice2poly coef = [ 0.7355 0.5 ] ...
> Unknown model type spice2poly - ignored
> Reducing trtol to 1 for xspice 'A' devices
> Doing analysis at TEMP = 27.000000 and TNOM = 27.000000
> Warning: vv3: no DC value, transient time 0 value used
>
> Let me know if you have any other questions or comments.
>
> Cheers,
>
> Wayne
>
> On 10/6/2016 10:56 AM, Nick Østergaard wrote:
>> Hi Maciej
>>
>> In the latest nightlies they are now stored in lib/ngspice/
>>
>> I guess that should equate to a relative path to the executables to
>> ../lib/ngspice/*.cm, given that the exe's are in the bin folder on the
>> same level as lib.
>>
>> So feel free to submit your fix. Also, are there any demos that make
>> use of those cm libs such that it can be tested?
>>
>> Nick
>>
>> 2016-10-05 23:16 GMT+02:00 Maciej Sumiński <maciej.suminski@xxxxxxx>:
>>> Hi Nick,
>>>
>>> Are the .cm files included in the Windows nightlies installer? If so,
>>> could you tell me what is the relative path to the directory storing
>>> them? The easiest way to fix the problem is to send a few commands to
>>> ngspice before a simulation starts.
>>>
>>> Regards,
>>> Orson
>>>
>>> On 10/05/2016 10:23 PM, Nick Østergaard wrote:
>>>> Is this really needed? What exactly does the .cm files provide?
>>>>
>>>> When I run the latest nightly I can run the allen key demo without
>>>> problems as far as I can see. Maybe some other simulation modes do
>>>> not work properly?
>>>>
>>>> 2016-09-30 14:37 GMT+02:00 Wayne Stambaugh <stambaughw@xxxxxxxxx>:
>>>>> That would work as a long term solution as well. I was trying to at
>>>>> least prove that it can be done without make changes to the current
>>>>> code. Until a full solution is implemented, users (me) will have an
>>>>> interim solution if they want to use the spice simulator.
>>>>>
>>>>> Cheers,
>>>>>
>>>>> Wayne
>>>>>
>>>>> On 9/30/2016 3:40 AM, Maciej Sumiński wrote:
>>>>>> We have also discussed on IRC another possibility, which is loading the
>>>>>> extensions manually instead of having ngspice process its initialization
>>>>>> file (spinit). This way we can adjust the paths during runtime.
>>>>>>
>>>>>> Regards,
>>>>>> Orson
>>>>>>
>>>>>> On 09/29/2016 08:51 PM, Wayne Stambaugh wrote:
>>>>>>> After much cursing and many config attempts, I finally have a working
>>>>>>> spice simulation solution on windows. I'm guessing similar parallels
>>>>>>> can be applied to osx as well.
>>>>>>>
>>>>>>>
>>>>>>> Option A: running from a mingw32 or mingw64 terminal.
>>>>>>>
>>>>>>> 1) copy the installed spinit file (by default will be in
>>>>>>> ${MINGW-PACKAGE-PREFIX}/share/ngspice/scripts) to ~/.spiceinit.
>>>>>>> 2) change the msys2 paths (/mingw##) in ~/.spiceinit to absolute windows
>>>>>>> paths with / not \ (in my case C:/msys64/mingw##).
>>>>>>> 3) Launch kicad.exe from the terminal.
>>>>>>>
>>>>>>> I realize this is not very elegant and will only work with either the 64
>>>>>>> or 32 bit mingw (not both without editing .spiceinit) but it works and
>>>>>>> is handy for mingw users.
>>>>>>>
>>>>>>>
>>>>>>> Option B: configuring windows and run kicad from a shortcut.
>>>>>>>
>>>>>>> 1) locate the installed spinit file
>>>>>>> ($INSTALL_PATH/share/ngspice/scripts) and change the msys2 paths
>>>>>>> (/mingw##) to absolute windows paths with / not \ (in my case
>>>>>>> C:/msys64/mingw##).
>>>>>>> 2) Run kicad, open the config paths dialog, and add an environment
>>>>>>> variable SPICE_LIB_DIR with path to the spinit file minus the last
>>>>>>> "scripts" path ($INSTALL_PATH/share/ngspice).
>>>>>>>
>>>>>>> I also tried copying the .spiceinit file from option A to %USERPROFILE%
>>>>>>> but that did not work when launching kicad from a shortcut.
>>>>>>>
>>>>>>> Option B a cleaner solution but still requires some configuration by the
>>>>>>> user. This is going to be an interesting problem to solve for our
>>>>>>> package devs. We need to figure out a way to generate or modify the
>>>>>>> spinit file base on where it gets installed by the installer on
>>>>>>> platforms where this is relevant. We will also either have to set an
>>>>>>> the SPICE_LIB_DIR environment variable or teach ngspice how to find the
>>>>>>> correct spinit file at run time.
>>>>>>>
>>>>>>> At least now windows users have a way to have the same functional spice
>>>>>>> simulation as linux users.
>>>>>>>
>>>>>>> Cheers,
>>>>>>>
>>>>>>> Wayne
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> Mailing list: https://launchpad.net/~kicad-developers
>>>>>>> Post to : kicad-developers@xxxxxxxxxxxxxxxxxxx
>>>>>>> Unsubscribe : https://launchpad.net/~kicad-developers
>>>>>>> More help : https://help.launchpad.net/ListHelp
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> Mailing list: https://launchpad.net/~kicad-developers
>>>>> Post to : kicad-developers@xxxxxxxxxxxxxxxxxxx
>>>>> Unsubscribe : https://launchpad.net/~kicad-developers
>>>>> More help : https://help.launchpad.net/ListHelp
>>>
From ba0c5822c47ef9bb02dcbb8a0dc34ceab55e34a0 Mon Sep 17 00:00:00 2001
From: Maciej Suminski <maciej.suminski@xxxxxxx>
Date: Thu, 13 Oct 2016 13:25:06 +0200
Subject: [PATCH] Solve codemodel libraries loading problem in ngspice
---
eeschema/sim/ngspice.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++++++-
eeschema/sim/ngspice.h | 10 +++++
2 files changed, 109 insertions(+), 2 deletions(-)
diff --git a/eeschema/sim/ngspice.cpp b/eeschema/sim/ngspice.cpp
index db7ea55..4007d28 100644
--- a/eeschema/sim/ngspice.cpp
+++ b/eeschema/sim/ngspice.cpp
@@ -26,7 +26,9 @@
#include "ngspice.h"
#include "spice_reporter.h"
-#include <common.h>
+#include <common.h> // LOCALE_IO
+#include <wx/stdpaths.h>
+#include <wx/dir.h>
#include <sstream>
@@ -273,13 +275,108 @@ void NGSPICE::init()
LOCALE_IO c_locale; // ngspice works correctly only with C locale
ngSpice_Init( &cbSendChar, &cbSendStat, &cbControlledExit, NULL, NULL, &cbBGThreadRunning, this );
- // Workaround to avoid hang ups on certain errors
+ // Load a custom spinit file, to fix the problem with loading .cm files
+ // Switch to the executable directory, so the relative paths are correct
+ const wxStandardPaths& paths = wxStandardPaths::Get();
+ wxString cwd( wxGetCwd() );
+ wxFileName exeDir( paths.GetExecutablePath() );
+ 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 =
+ {
+ ".",
+ "../share/kicad",
+ "../share",
+ "../../share/kicad",
+ "../../share"
+ };
+
+ bool foundSpiceinit = false;
+
+ for( const auto& path : spiceinitPaths )
+ {
+ if( loadSpinit( path + "/spiceinit" ) )
+ {
+ 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 );
+
+ // Workarounds to avoid hang ups on certain errors,
+ // they have to be called, no matter what is in the spinit file
Command( "unset interactive" );
+ Command( "set noaskquit" );
+ Command( "set nomoremode" );
m_initialized = true;
}
+bool NGSPICE::loadSpinit( const string& aFileName )
+{
+ if( !wxFileName::FileExists( aFileName ) )
+ return false;
+
+ wxTextFile file;
+
+ if( !file.Open( aFileName ) )
+ return false;
+
+ for( auto cmd = file.GetFirstLine(); !file.Eof(); cmd = file.GetNextLine() )
+ Command( cmd.ToStdString() );
+
+ return true;
+}
+
+
+string NGSPICE::findCmPath() const
+{
+ const vector<string> cmPaths =
+ {
+ "../lib/ngspice",
+ "../../lib/ngspice"
+ "lib/ngspice",
+ "ngspice"
+ };
+
+ for( const auto& path : cmPaths )
+ {
+ if( wxFileName::DirExists( path ) )
+ return path;
+ }
+
+ 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;
+}
+
+
int NGSPICE::cbSendChar( char* what, int id, void* user )
{
NGSPICE* sim = reinterpret_cast<NGSPICE*>( user );
diff --git a/eeschema/sim/ngspice.h b/eeschema/sim/ngspice.h
index fdba05d..7f0a5e5 100644
--- a/eeschema/sim/ngspice.h
+++ b/eeschema/sim/ngspice.h
@@ -76,6 +76,16 @@ public:
private:
void init();
+ ///> Executes commands from a file
+ bool loadSpinit( const std::string& aFileName );
+
+ ///> Checks a few different locations for codemodel files and returns one
+ ///> if it exists
+ std::string findCmPath() const;
+
+ ///> Loads codemodel files from a directory
+ bool loadCodemodels( const std::string& aPath );
+
// Callback functions
static int cbSendChar( char* what, int id, void* user );
static int cbSendStat( char* what, int id, void* user );
--
2.9.1
* Standard ngspice init file
alias exit quit
alias acct rusage all
set x11lineararcs
*set rndseed=12
** ascii rawfile **
*set filetype=ascii
** frontend debug output **
*set ngdebug
** asking after quit **
*set askquit
** set the number of threads in openmp
** default (if compiled with --enable-openmp) is: 2
*set num_threads=4
set interactive
strcmp __flag $program "ngspice"
if $__flag = 0
* For SPICE2 POLYs, edit the below line to point to the location
* of your codemodel.
codemodel $__CMPATH/spice2poly.cm
codemodel $__CMPATH/analog.cm
codemodel $__CMPATH/digital.cm
codemodel $__CMPATH/xtradev.cm
codemodel $__CMPATH/xtraevt.cm
end
unset __flag
Attachment:
signature.asc
Description: OpenPGP digital signature
Follow ups
References