Skip to content

Commit

Permalink
Modified RawFile reader to access directly with numpy to the file to …
Browse files Browse the repository at this point in the history
…read the raw data, and also accept unfinished simulations.
  • Loading branch information
jmgc committed Jun 7, 2021
1 parent 363b461 commit aa4989a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
46 changes: 42 additions & 4 deletions PySpice/Spice/Xyce/RawFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
####################################################################################################

import os
import numpy as np

from ..RawFile import VariableAbc, RawFileAbc

Expand Down Expand Up @@ -113,10 +114,21 @@ class RawFile(RawFileAbc):

##############################################

def __init__(self, output):

raw_data = self._read_header(output)
self._read_variable_data(raw_data)
def __init__(self, output=None, filename=None):
if filename:
binary_line = b'Binary:\n'
header = b""
with open(filename, 'rb') as ifile:
for line in ifile:
header += line
if line == binary_line:
break
idx = ifile.tell()
self._read_header(header)
self._read_file_variable_data(filename, idx)
else:
raw_data = self._read_header(output)
self._read_variable_data(raw_data)
# self._to_analysis()

self._simulation = None
Expand Down Expand Up @@ -154,6 +166,32 @@ def _read_header(self, output):

return raw_data

def _read_file_variable_data(self, filename, idx):
""" Read the raw data and set the variable values. """

if self.flags == 'real':
number_of_columns = self.number_of_variables
elif self.flags == 'complex':
number_of_columns = 2 * self.number_of_variables
else:
raise NotImplementedError

input_data = np.fromfile(filename,
count=self.number_of_points*self.number_of_variables,
dtype='f8',
offset=idx)

number_of_rows = input_data.shape[0] // number_of_columns
input_data = input_data[:number_of_rows * number_of_columns]
input_data = input_data.reshape((-1, number_of_columns)).transpose()
if self.flags == 'complex':
raw_data = input_data
input_data = np.array(raw_data[0::2], dtype='complex64')
input_data.imag = raw_data[1::2]
for variable in self.variables.values():
variable.data = input_data[variable.index]
return input_data

##############################################

def fix_case(self):
Expand Down
3 changes: 3 additions & 0 deletions unit-test/Spice/test_SpiceParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ def test_library(self):
simulator.transient(1e-9, 1e-3)

def test_working_dir(self):
from PySpice.Spice.Xyce.RawFile import RawFile
circuit = Circuit('Test working directory')
circuit.PulseVoltageSource('hlp',
1,
Expand All @@ -449,6 +450,8 @@ def test_working_dir(self):
result = simulator.transient(1e-2, 10)
self.assertTrue(os.path.exists('input.cir') and os.path.isfile('input.cir'))
self.assertTrue(os.path.exists('output.raw') and os.path.isfile('output.raw'))
data = RawFile(filename="output.raw")
print(data.nodes())

def test_transient(self):
transient = SpiceParser.parse(source="""
Expand Down

0 comments on commit aa4989a

Please sign in to comment.