Skip to content

Commit

Permalink
setting the good command line arguments for simulating with ngspice
Browse files Browse the repository at this point in the history
  • Loading branch information
nunobrum committed Apr 26, 2023
1 parent ff08d6a commit a542221
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
21 changes: 11 additions & 10 deletions PyLTSpice/sim/ngspice_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
# Licence: refer to the LICENSE file
# -------------------------------------------------------------------------------

import logging
import sys
import os

from pathlib import Path
from typing import Union

from .simulator import Simulator, run_function

Expand All @@ -37,8 +32,8 @@ class NGspiceSimulator(Simulator):
'--autorun' : ['--autorun'], # run the loaded netlist
'-b' : ['-b'],
'--batch' : ['--batch'], # process FILE in batch mode
# '-c' : ['-c', '<FILE>'], #
# '--circuitfile' : ['--circuitfile', '<FILE>'], # set the circuitfile
'-c' : ['-c', '<FILE>'], #
'--circuitfile' : ['--circuitfile', '<FILE>'], # set the circuitfile
'-D' : ['-D', 'var_value'], #
'--define' : ['--define', 'var_value'], # define variable to true/[value]
'-i' : ['-i'], #
Expand All @@ -58,11 +53,12 @@ class NGspiceSimulator(Simulator):
'--server' : ['--server'], # run spice as a server process
'-t' : ['-t', '<TERM>'], #
'--term' : ['--term', '<TERM>'], # set the terminal type
# '-h' : ['-h'], #
# '--help' : ['--help'], # display this help and exit
'-h' : ['-h'], #
'--help' : ['--help'], # display this help and exit
'-v' : ['-v'], #
'--version' : ['--version'], # output version information and exit
}
default_run_switches = ['-b', '-o', '-r', '-a']

@classmethod
def valid_switch(cls, switch, parameter='') -> list:
Expand All @@ -79,6 +75,9 @@ def valid_switch(cls, switch, parameter='') -> list:
"""
ret = [] # This is an empty switch
if switch in cls.ngspice_args:
if switch in cls.default_run_switches:
print(f"Switch {switch} is already in the default switches")
return ret
switch_list = cls.ngspice_args[switch]
if len(switch_list) == 2:
param_token = switch_list[1]
Expand All @@ -96,7 +95,9 @@ def valid_switch(cls, switch, parameter='') -> list:

@classmethod
def run(cls, netlist_file, cmd_line_switches, timeout):
cmd_run = cls.spice_exe + cmd_line_switches + ['-c'] + [netlist_file]
logfile = Path(netlist_file).with_suffix('.log').as_posix()
rawfile = Path(netlist_file).with_suffix('.raw').as_posix()
cmd_run = cls.spice_exe + cmd_line_switches + ['-b'] + ['-o'] + [logfile] + ['-r'] + [rawfile] + [netlist_file]
# start execution
return run_function(cmd_run, timeout=timeout)

Expand Down
25 changes: 12 additions & 13 deletions PyLTSpice/sim/spice_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,19 +952,18 @@ def write_netlist(self, run_netlist_file: 'Path') -> None:
:type run_netlist_file: Path
:returns: Nothing
"""
f = run_netlist_file.open('w', encoding=self.encoding)
lines = iter(self.netlist)
for line in lines:
if isinstance(line, SpiceCircuit):
line._write_lines(f)
else:
# Writes the modified subcircuits at the end just before the .END clause
if line.upper().startswith(".END"):
# write here the modified subcircuits
for sub in self.modified_subcircuits.values():
sub._write_lines(f)
f.write(line)
f.close()
with run_netlist_file.open('w', encoding=self.encoding) as f:
lines = iter(self.netlist)
for line in lines:
if isinstance(line, SpiceCircuit):
line._write_lines(f)
else:
# Writes the modified subcircuits at the end just before the .END clause
if line.upper().startswith(".END"):
# write here the modified subcircuits
for sub in self.modified_subcircuits.values():
sub._write_lines(f)
f.write(line)

def reset_netlist(self) -> None:
"""
Expand Down
5 changes: 3 additions & 2 deletions tests/ngspice_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ def processing_data(raw_file, log_file):
LTC = SimRunner(output_folder='./temp', simulator=NGspiceSimulator.create_from('C:/Apps/NGSpice64/bin/ngspice.exe'))
netlist = SpiceEditor('testfile_ngspice.net')
# set default arguments
netlist.set_parameters(res=0, cap=100e-6)
netlist.set_component_value('R1', '4k')
netlist.set_element_model('V1', "SINE(0 1 3k 0 0 0)") # Modifying the
netlist.remove_instruction('.op')
netlist.add_instruction(".tran 1n 3m")
netlist.add_instruction(".plot V(out)")
netlist.add_instruction(".save all")

# .step dec param cap 1p 10u 1
for cap in sweep_log(1e-12, 10e-6, 10):
netlist.set_parameter('cap', cap)
netlist.set_component_value('C1', cap)
LTC.run(netlist, callback=processing_data)

LTC.wait_completion()
Expand Down
13 changes: 6 additions & 7 deletions tests/testfile_ngspice.net
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
* C:\sandbox\PySpice\tests\testfile.asc
V1 in 0 PULSE(-1 1 1u 1n 1n 2m 1m 1)
R1 out in {res}
C1 out 0 {cap}
C2 out in {cap}
.tran 3m
.param cap=1n res=1k
* C:\sandbox\PyLTSpice_GitHub_nunobrum\tests\rc_example.asc
V1 in 0 10V
R1 out in 1k
C1 out 0 1u
.op
.print op V(out)
.end

0 comments on commit a542221

Please sign in to comment.