Netlist text: a SPICE subset for linear circuits #
A reader (Cslib.Electrical.Text.parse) for the netlists of Cslib.Electrical.Netlist, in a
subset of the SPICE netlist format, with one directive of its own (.probe, below). The format
follows the ngspice manual [VAWN26] where the two overlap (§2.1.1 for the file structure,
§2.1.3 for fields, numbers and node names, §2.4 for the title, .end and comment lines, §4.1 for
the independent sources); the tests use node names of the form a schematic editor writes
(/out, Net-_R1-Pad2_), but no exporter's actual output has been checked against this
reader. An exported netlist is accepted only after its analysis and output lines (.tran,
.ac, .save, .control blocks, …) are replaced by .probe lines.
The grammar #
The text is a list of lines, separated by \n; a line's words are separated by spaces, tabs or
carriage returns, so leading and trailing blanks and \r\n line ends are allowed.
The first line is the title and is ignored, whatever it contains, as in SPICE (§2.1.1, §2.4.1). A
.titleline elsewhere is rejected.Blank lines and comment lines, whose first word starts with
*(§2.4.3), are ignored. End-of-line comments ($,;,//, §2.4.4) are not supported: these characters are not name characters, or (for//) not allowed in a name, so a line containing one is rejected.The netlist ends with a line consisting of the word
.end(in either case) alone (§2.4.2). A text without such a line is rejected, and so is a.endline with more words. Only blank lines may follow it; any other line after it, including a comment, is rejected, so no input is silently dropped. (§2.1.1 asks for a newline after.end; this reader does not.)An element line starts with the element name; its first letter, in either case, gives the element:
line element Rname n+ n- valueresistor, valueohmsCname n+ n- valuecapacitor, valuefaradsLname n+ n- valueinductor, valuehenriesVname n+ n- [[dc] value] [ac [1]]voltage source v(n+) - v(n-) = valueIname n+ n- [[dc] value] [ac [1]]current source, valueA fromn+through it ton-Gname n+ n- nc+ nc- valueVCCS: current value · v(nc+, nc-)(S) fromn+ton-Ename n+ n- nc+ nc- valueVCVS: v(n+) - v(n-) = value · v(nc+, nc-)These are the element laws and the sign conventions of
Cslib.Electrical.Element, which are SPICE's (§4.1 for the sources). A source's value defaults to0, anddcmust be followed by a value.Input ports (an interpretation of this library, not SPICE's). In SPICE,
ac magmarks a source as the stimulus of small-signal AC analysis and does not change its DC or transient value (§4.1). Here a source markedac(with magnitude1, written or implied; §4.1's general form leaves the magnitude optional, and a phase is rejected) is an input port of unit gain: its value isvalue + u_kfor an arbitrary input signalu_k, with the input ports numbered0, 1, …in the order of their lines. For a circuit of the elements above, the transfer function fromu_kto a probe is then what ngspice's AC analysis computes when that source is the only one markedac.An output line selects a probe:
.probe v n1 [n2]is the voltagev(n1) - v(n2)(withn2 = 0if omitted) and.probe i namethe current of the elementname, in the direction of its element law; probes are numbered in the order of their lines. This syntax is this library's own: ngspice's.probedirective (§11.6.5) has another syntax (.probe v(node),.probe I(device), …) and another meaning (it saves vectors in a simulation), and its forms are rejected here.
Nodes. The node 0 is the ground. Every other node name is numbered 1, 2, … in the order
of first appearance on the element lines (positive, negative, then control nodes). Names are
compared exactly, and a text with two distinct names that differ only in case is rejected, since
ngspice does not distinguish them (§2.1.3.4). The name gnd (in any case) is rejected: ngspice
reads it as the ground (§2.1.3.5). Node and element names consist of letters, digits and
_ - / + . #, without //; two elements with the same name (up to case) are rejected.
Values are exact rationals: an optional sign, decimal digits, an optional fractional part
.digits with at least one digit, an optional exponent e[sign]digits and an optional scale
suffix, in either case, t (10¹²), g (10⁹), meg (10⁶), k (10³), m (10⁻³), u
(10⁻⁶), n (10⁻⁹), p (10⁻¹²) or f (10⁻¹⁵) (§2.1.3.2). Thus 1.e2, 1. and .5
are rejected, and so are the suffixes mil and a. Nothing may follow the suffix: ngspice
ignores trailing letters (§2.1.3.3), so that 1F is 10⁻¹⁵ there, and a unit written after a
value is rejected rather than guessed.
Rejected. Every other line is rejected with a message, and nothing is silently omitted:
other elements (diodes, transistors, subcircuit instances X, mutual inductances K,
current-controlled sources F, H, behavioural sources B), every other directive
(.include, .lib, .model, .subckt, .param, .options, analyses such as .tran or
.ac), continuation lines (+), parameter expressions and malformed values. A netlist that
parse accepts may still fail Cslib.Electrical.Netlist.check (a nonpositive R, C or L),
and then no certificate is accepted for it.
Main definitions #
Cslib.Electrical.Text.ParsedNetlist: a netlist with its probes and the names of its nodes and elements.Cslib.Electrical.Text.parse: the reader.
References #
- [VAWN26]: H. Vogt, G. Atkinson, D. Warning, P. Nenzi, Ngspice User's Manual, Version 46, March 31st, 2026; the sections cited above were read in that version.
Lines and words #
Whether c separates words: a space, a tab or a carriage return.
Values #
Cards #
A line of the text that describes part of the circuit, with node names not yet resolved.
Whether w is a well-formed name: nonempty, of name characters, without // (which ngspice
reads as the start of an end-of-line comment).
Whether w is a well-formed node name: a name other than gnd.
The number of nodes of an element with first letter c (lower case): 2 for R, C, L,
V, I, 4 for G, E, and 0 (unsupported) otherwise.
Whether the element with first letter c (lower case) is an independent source.
Resolving names #
A netlist read from text: the circuit, its probes, and the names of its nodes 1, 2, … and
of its branches, in order.
- netlist : Netlist
The circuit.
The output probes.
The names of the nodes
1, …, netlist.nodes.The names of the branches, in order.
The number of input ports among the cards.
Resolving the names of the cards.
The reader: the netlist described by a text, or the reason it is rejected. The first line is the title and is skipped.