Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrectly handles stderr outputs that aren't errors from libngspice version 43 #379

Open
mbacvanski opened this issue Oct 18, 2024 · 2 comments · May be fixed by #381
Open

Incorrectly handles stderr outputs that aren't errors from libngspice version 43 #379

mbacvanski opened this issue Oct 18, 2024 · 2 comments · May be fixed by #381

Comments

@mbacvanski
Copy link

Environment (OS, Python version, PySpice version, simulator)

  • MacOS 15.0
  • Python 3.10.15
  • PySpice version 1.5
  • libngspice version 43

Issue

I installed libngspice via brew install libngspice, which gave libngspice version 43. I am trying to run this code:

from PySpice.Spice.Netlist import Circuit
from PySpice.Unit import *

circuit = Circuit('RC Low-Pass Filter')

# Define components and their connections
circuit.V(1, 'input', circuit.gnd, 10@u_V)  # Voltage source
circuit.R(1, 'input', 'output', 1@u_kΩ)      # Resistor
circuit.C(1, 'output', circuit.gnd, 1@u_uF)  # Capacitor

simulator = circuit.simulator(temperature=25, nominal_temperature=25)
analysis = simulator.transient(step_time=1@u_us, end_time=10@u_ms)

On running this, I get the error:

Warning: can't find the initialization file spinit.
Unsupported Ngspice version 43
Using SPARSE 1.3 as Direct Linear Solver

---------------------------------------------------------------------------
NgSpiceCommandError                       Traceback (most recent call last)
Cell In[2], line 14
     11 circuit.C(1, 'output', circuit.gnd, 1@u_uF)  # Capacitor
     13 simulator = circuit.simulator(temperature=25, nominal_temperature=25)
---> 14 analysis = simulator.transient(step_time=1@u_us, end_time=10@u_ms)

File ~/mambaforge/envs/am/lib/python3.10/site-packages/PySpice/Spice/Simulation.py:1214, in CircuitSimulator.transient(self, *args, **kwargs)
   1213 def transient(self, *args, **kwargs):
-> 1214     return self._run('transient', *args, **kwargs)

File ~/mambaforge/envs/am/lib/python3.10/site-packages/PySpice/Spice/NgSpice/Simulation.py:119, in NgSpiceSharedCircuitSimulator._run(self, analysis_method, *args, **kwargs)
    116 # load circuit and simulation
    117 # Fixme: Error: circuit not parsed.
    118 self._ngspice_shared.load_circuit(str(self))
--> 119 self._ngspice_shared.run()
    120 self._logger.debug(str(self._ngspice_shared.plot_names))
    121 self.reset_analysis()

File ~/mambaforge/envs/am/lib/python3.10/site-packages/PySpice/Spice/NgSpice/Shared.py:1196, in NgSpiceShared.run(self, background)
   1193 #  in the background thread and wait until the simulation is done
   1195 command = 'bg_run' if background else 'run'
-> 1196 self.exec_command(command)
   1198 if background:
   1199     self._is_running = True

File ~/mambaforge/envs/am/lib/python3.10/site-packages/PySpice/Spice/NgSpice/Shared.py:855, in NgSpiceShared.exec_command(self, command, join_lines)
--> 855     raise NgSpiceCommandError("Command '{}' failed".format(command))
    857 if join_lines:
    858     return self.stdout

NgSpiceCommandError: Command 'run' failed

It seems that the error is due to the handling of stderr in Spice/NgSpice/Shared.py (around line 625). When anything appears in stderr that does not begin with 'Warning:', self._error_in_stderr is set to True, causing an NgSpiceCommandError to be raised here. It appears that the latest version of libngspice can output Using SPARSE 1.3 as Direct Linear Solver to stderr, which leads to simulator crashing despite no actual error appearing in stderr.

If I comment out the line 625 setting self._error_in_stderr = True, then the simulation completes with no issues.

@Kreijstal
Copy link

Kreijstal commented Oct 28, 2024

Kreijstal added a commit to Kreijstal/PySpice that referenced this issue Oct 28, 2024
…spice version 43

**Fixes PySpice-org#379**

This pull request addresses an issue where PySpice incorrectly handles certain stderr outputs from libngspice version 43, specifically messages like "Using SPARSE 1.3 as Direct Linear Solver". These messages, while appearing on stderr, are informational and not indicative of errors.

**Changes:**

- Modified the `_send_char` callback in `Spice/NgSpice/Shared.py` to specifically handle messages starting with "Using".
- These messages are now treated as debug output instead of errors, preventing the `self._error_in_stderr` flag from being set and avoiding the `NgSpiceCommandError`.
@mbacvanski
Copy link
Author

I agree it's odd that they print that info statement to stderr. However it looks that PySpice doesn't (want to?) support libngspice versions above 34 (according to the last update here), so I'm not sure if this quick fix is a long-term solution.

I got around the issue by compiling libngspice version 34 from scratch for my system. It would be nice if this note was included in the documentation :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants