diff options
| author | Tanner Robison <[email protected]> | 2026-07-07 20:48:28 -0700 |
|---|---|---|
| committer | Your Name <[email protected]> | 2026-07-10 19:51:37 +0000 |
| commit | 4f36a1654ff6560c915a39e1f9999e0efc4a9680 (patch) | |
| tree | 984b234215ee1972a396d1491e5dc3fd07da5d4c | |
| parent | e2b30976cdaccf9d2b9820fefa22ced03c82711f (diff) | |
SPICE model in the loop testing
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | MIL_testing/integration_test.py | 14 | ||||
| -rw-r--r-- | MIL_testing/ng_test1.cir | 25 | ||||
| -rw-r--r-- | MIL_testing/spice_backend.py | 70 | ||||
| -rw-r--r-- | references/kaevin_memristor.py | 2 | ||||
| m--------- | spires | 0 | ||||
| -rw-r--r-- | src/README.md | 2 | ||||
| -rw-r--r-- | src/spice_memristor.py | 7 |
8 files changed, 113 insertions, 11 deletions
@@ -13,8 +13,6 @@ neurobench_testing/.venv/ neurobench_testing/ -pyspice_testing/ - #memTorch will need to be cloned & installed locally **/MemTorch/ **/memtorch/ @@ -33,3 +31,5 @@ pyspice_testing/ *.csv *.h5 *.hdf5 +#AI code assistant +.aider* diff --git a/MIL_testing/integration_test.py b/MIL_testing/integration_test.py new file mode 100644 index 0000000..7c8e2fa --- /dev/null +++ b/MIL_testing/integration_test.py @@ -0,0 +1,14 @@ +import subprocess + +result = subprocess.run( + ["ngspice", "-b", "ng_test1.cir"], + capture_output=True, + text=True +) + +print("Return code:", result.returncode) +print(result.stdout) + +if result.stderr: + print("Errors:") + print(result.stderr) diff --git a/MIL_testing/ng_test1.cir b/MIL_testing/ng_test1.cir new file mode 100644 index 0000000..892a284 --- /dev/null +++ b/MIL_testing/ng_test1.cir @@ -0,0 +1,25 @@ +* RC Charging Circuit + +V1 in 0 PULSE(0 5 1u 1u 1u 100m 200m) + +R1 in out 1k + +C1 out 0 1u + +.tran 10u 50m + +.control +run + +set finalindex = $&{length(v(in)) - 1} + +let final_vin = v(in)[$finalindex] +let final_vout = v(out)[$finalindex] +let final_current = v1#branch[$finalindex] + +echo "----- final time analysis -----" +print final_vin final_vout +print final_current + +.endc +.end diff --git a/MIL_testing/spice_backend.py b/MIL_testing/spice_backend.py new file mode 100644 index 0000000..674ae30 --- /dev/null +++ b/MIL_testing/spice_backend.py @@ -0,0 +1,70 @@ +import subprocess +import re + +class SpiceBackend: + """This is what actually does all of the spice stuff""" + def __init__(self, device_file: str): + self.conductance = 0 + self.voltage = 0 + self.current = 0 + self.time = 0 + + def simulate(self, voltage_signal: float, return_current: bool = False) -> float: + """runs the simulation for one time step""" + len_voltage_signal = 1 + try: + len_voltage_signal = len(voltage_signal) + except: + voltage_signal = [voltage_signal] + + if return_current: + current = np.zeros(len_voltage_signal) + + for t in enumerate(len_voltage_signal): + result = self.run_spice(voltage_signal) + + single_current = result.current + self.conductance = result.conductance + + if return_current: + current[t] = single_current + + if return_current: + return current + + + def set_conductance(self, conductance: float): + """Handled by spice now""" + """Could probably still add some safety checks just to be sure later""" + self.conductance = conductance + + def run_spice(self, voltage: float) -> float: + """returns the current after given a voltage signal""" + #need to find a way to pass voltage signal to ngspice here + #also maybe a time series resolution?? + result = subprocess.run( + ["ngspice" "-b" "spicefile.cir"], + capture_output=True, + text=True + ) + if result.returncode != 0: + raise RuntimeError(result.stderr) + + # parse current into float variable + output = result.stdout + #is there a better (less rigid) way of grabbing the output + current_match = re.search("final_current = ([-\deE.+]+)", output) + if current_match is None: + raise RuntimeError("Could not find current in ngspice output") + + current = float(current_match.group(1)) + + if abs(voltage) > 1e-12: + conductance = current / voltage + else: + conductance = self.conductance + + return { + "current": current, + "conductance": conductance, + } diff --git a/references/kaevin_memristor.py b/references/kaevin_memristor.py index f186958..890eb9b 100644 --- a/references/kaevin_memristor.py +++ b/references/kaevin_memristor.py @@ -6,7 +6,7 @@ class TEAMMemristor: def __init__( self, - k_off=1, # switching rate for off-state + k_on=-1, # switching rate for on-state alpha_off=5, # exponent controlling nonlinearity when switching off alpha_on=5, # exponent controlling nonlinearity when switching on diff --git a/spires b/spires deleted file mode 160000 -Subproject c9544bdcd3fe9be7d055a3e1e120110ef4fe258 diff --git a/src/README.md b/src/README.md index 0d58f19..565bc68 100644 --- a/src/README.md +++ b/src/README.md @@ -1,7 +1,7 @@ # Spires reservoir simulated on memristor crossbar using memTorch ## setup ### general -python3 -m .venv venv +python3 -m venv .venv pip install -r requirements.txt diff --git a/src/spice_memristor.py b/src/spice_memristor.py deleted file mode 100644 index 87dcdc3..0000000 --- a/src/spice_memristor.py +++ /dev/null @@ -1,7 +0,0 @@ -class SpiceMemristor: - def __init__(self): - self.build_circuit() - - self.build_simulator() - - self.time = 0 |
