Post date: Mar 19, 2016 12:23:43 AM
NGSPICE's waveform output is termed a "raw file". This is essentially a set of vectors, the first of which describes time or whatever other 'x-axis' quantity for different kinds of analysis. The subsequent ones describe the voltage or current at one point in the circuit at that time.
By default, NGSPICE uses a binary format that the documentation describes as being an "internal format", with the only official way to read it being NGSPICE itself. However using the SPICE_ASCIIRAWFILE environment variable it is possible to force NGSPICE to output in an ASCII format. While still technically "internal" the structure can be spotted by eye.
Essentially the structure is a bunch of Metadata presented key-colon-value lines, followed by a line that says 'Values:' then one double in ASCII format per line. The order of the doubles is that the time is printed first, then the value of each vector at that time follows, then the next time. The lines corresponding to time are prefixed with a index number and a colon before the double.
Having worked this out, an ad-hoc parser was written to load this into Java. The format loaded to is:
public class SpiceResults {
public int num_vars=-1;
public int num_points=-1;
class Series {
public String name;
public String units;
public double[] values;
}
public Series [] series;
....
}
This class also as load(BufferedReader) member to load from a file.
Since loading is expected to be slow for larger simulations an AsyncTask to do the loading in, called ResultsLoaderAsyncTask was written. SpiceRunner was modified to, on the callback for a successful completion of a SPICE run, then invoke this task rather than directly calling back to the activity, and on the completion of the load task then callback the activity.
This code was tested by writing a SpiceResults.toString() method that puts the data back as text but in a different format from the original rawfile. This was then shown in a TextView in the app.
Two follow-up components remain: to implement graphing, and to implement remote running.
P.S. Subsequent examination of a binary raw files in a hex editor showed that it gave the header data in a the same ASCII format, then instead of "Values:" a "Binary:" line is given, and immediately after this line the file transitions to a the simplest possible binary representations, a big block of double in the same order as before, but no index ints. Parsing this would not be too much additional effort but was not implemented since it did not give rise to additional functionality.
In a production version of the app, this would be desirable for efficiency.