|
| 1 | +# SpiceSharpParser.CustomComponents |
| 2 | + |
| 3 | +[](https://www.nuget.org/packages/SpiceSharpParser.CustomComponents) |
| 4 | +[](https://github.com/SpiceSharp/SpiceSharpParser/blob/main/LICENSE) |
| 5 | + |
| 6 | +`SpiceSharpParser.CustomComponents` is the optional companion package for |
| 7 | +[SpiceSharpParser](https://github.com/SpiceSharp/SpiceSharpParser). It adds |
| 8 | +LTspice-compatible components and reusable functional subcircuits while keeping |
| 9 | +the resulting circuit compatible with the normal SpiceSharp simulation API. |
| 10 | + |
| 11 | +The package targets .NET Standard 2.0 and .NET 8.0. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```shell |
| 16 | +dotnet add package SpiceSharpParser.CustomComponents |
| 17 | +``` |
| 18 | + |
| 19 | +The package depends on `SpiceSharp-Parser`, so a separate parser package |
| 20 | +reference is not required. |
| 21 | + |
| 22 | +## Enable Custom Netlist Components |
| 23 | + |
| 24 | +The original parser/reader flow keeps parsing, translation, and simulation as |
| 25 | +explicit steps. Enable the custom mappings on `SpiceSharpReader` before calling |
| 26 | +`Read`: |
| 27 | + |
| 28 | +```csharp |
| 29 | +using System.IO; |
| 30 | +using System.Linq; |
| 31 | +using SpiceSharpParser; |
| 32 | +using SpiceSharpParser.CustomComponents; |
| 33 | + |
| 34 | +string netlistText = File.ReadAllText("circuit.cir"); |
| 35 | +var compatibility = CompatibilityOptions.LTspice; |
| 36 | + |
| 37 | +// Parse the SPICE text. |
| 38 | +var parser = new SpiceNetlistParser(); |
| 39 | +parser.Settings.Compatibility = compatibility; |
| 40 | +var parsed = parser.ParseNetlist(netlistText); |
| 41 | + |
| 42 | +// Translate the parsed model into SpiceSharp objects. |
| 43 | +var reader = new SpiceSharpReader(); |
| 44 | +reader.Settings.Compatibility = compatibility; |
| 45 | +reader.Settings.UseCustomComponents(); |
| 46 | +var model = reader.Read(parsed.FinalModel); |
| 47 | + |
| 48 | +// Run the analysis defined by .OP, .TRAN, .AC, or .DC in the netlist. |
| 49 | +var simulation = model.Simulations.Single(); |
| 50 | +simulation.Execute(model.Circuit); |
| 51 | +``` |
| 52 | + |
| 53 | +`UseCustomComponents()` adds custom-aware mappings for `A`, `C`, `D`, and `L` |
| 54 | +elements. Call it before `Read`, and use the same compatibility settings for |
| 55 | +the parser and reader. Ordinary diode, capacitor, and inductor syntax continues |
| 56 | +to use the standard SpiceSharp implementations. |
| 57 | + |
| 58 | +## Included Components |
| 59 | + |
| 60 | +| Family | Supported forms | |
| 61 | +| --- | --- | |
| 62 | +| LTspice A-devices | `SRFLOP`, `DFLOP`, `PHASEDET`, `COUNTER`, `SAMPLEHOLD`, `OTA`, `VARISTOR`, and `MODULATOR`/`MODULATE` | |
| 63 | +| Ideal diodes | LTspice-style `.MODEL D(...)` parameters such as `Ron`, `Roff`, `Vfwd`, reverse clamp, smoothing, and current limits | |
| 64 | +| Nonlinear capacitors | Charge-defined capacitors using `Q=<expression>` | |
| 65 | +| Nonlinear inductors | Flux-defined inductors using `Flux=<expression>` | |
| 66 | +| Digital subcircuits | Gates, Schmitt inputs, tri-state drivers, multiplexers, an adder, decoder, comparator, open-drain output, latches, flip-flops, a counter, phase detector, and functional 555 timer | |
| 67 | +| Analog subcircuits | Sample-and-hold, operational transconductance amplifier, voltage-controlled varistor, and frequency/amplitude modulator | |
| 68 | + |
| 69 | +## A-device Quick Reference |
| 70 | + |
| 71 | +Every native LTspice A-device has eight terminal positions followed by its |
| 72 | +model name. |
| 73 | + |
| 74 | +```spice |
| 75 | +A<name> n1 n2 n3 n4 n5 n6 n7 n8 <model> [parameter=value ...] |
| 76 | +``` |
| 77 | + |
| 78 | +| Model | What it does | |
| 79 | +| --- | --- | |
| 80 | +| `SRFLOP` | Stores one bit using asynchronous set and reset inputs | |
| 81 | +| `DFLOP` | Captures a data bit on a clock edge | |
| 82 | +| `PHASEDET` | Produces signed current pulses from input-edge timing error | |
| 83 | +| `COUNTER` | Counts clock edges and generates a repeating output sequence | |
| 84 | +| `SAMPLEHOLD` | Samples an analog voltage and holds the captured value | |
| 85 | +| `OTA` | Converts differential input voltages into a limited output current | |
| 86 | +| `VARISTOR` | Provides a bidirectional voltage-controlled clamp; like a programmable surge-protection varistor | |
| 87 | +| `MODULATOR` | Generates an amplitude-controlled waveform whose frequency moves between `Space` and `Mark` | |
| 88 | + |
| 89 | +Terminal 8 is the common node. A terminal position is unused when it repeats |
| 90 | +that common node. If common is not ground, a literal `0` is an active connection |
| 91 | +to global ground, not an unused terminal. Unused outputs are electrically |
| 92 | +detached; an unused `MODULATOR` amplitude input selects the native amplitude of |
| 93 | +1. |
| 94 | + |
| 95 | +Example: |
| 96 | + |
| 97 | +```spice |
| 98 | +* Divide a clock by four |
| 99 | +VCLOCK clock 0 PULSE(0 5 5n 100p 100p 2n 10n) |
| 100 | +VRESET reset 0 0 |
| 101 | +
|
| 102 | +ACOUNT clock reset 0 0 0 qb q 0 COUNTER Cycles=4 Duty=0.5 |
| 103 | +RQ q 0 10k |
| 104 | +RQB qb 0 10k |
| 105 | +
|
| 106 | +.tran 100p 42n 0 100p UIC |
| 107 | +.save V(clock) V(q) V(qb) |
| 108 | +.plot tran V(clock) V(q) V(qb) |
| 109 | +.meas tran q_at_30n FIND V(q) AT=30n |
| 110 | +.print tran V(q) |
| 111 | +.end |
| 112 | +``` |
| 113 | + |
| 114 | +## Programmatic Subcircuit Use |
| 115 | + |
| 116 | +The embedded libraries can also add components directly to a SpiceSharp |
| 117 | +`Circuit`: |
| 118 | + |
| 119 | +```csharp |
| 120 | +using SpiceSharp; |
| 121 | +using SpiceSharp.Components; |
| 122 | +using SpiceSharpParser.CustomComponents.Digital; |
| 123 | + |
| 124 | +var circuit = new Circuit( |
| 125 | + new VoltageSource("VDD", "vdd", "0", 5.0), |
| 126 | + new VoltageSource("VA", "a", "0", 5.0), |
| 127 | + new VoltageSource("VB", "b", "0", 5.0), |
| 128 | + new Resistor("RLOAD", "y", "0", 10_000.0)); |
| 129 | + |
| 130 | +var digital = DigitalSubcircuitLibrary.LoadBuiltIn(); |
| 131 | +digital.AddBinaryGate( |
| 132 | + circuit, |
| 133 | + DigitalGateKind.Nand2, |
| 134 | + instanceName: "XU1", |
| 135 | + firstInputNode: "a", |
| 136 | + secondInputNode: "b", |
| 137 | + outputNode: "y", |
| 138 | + positiveSupplyNode: "vdd", |
| 139 | + negativeSupplyNode: "0"); |
| 140 | +``` |
| 141 | + |
| 142 | +Use `AnalogSubcircuitLibrary.LoadBuiltIn()` for the four analog functional |
| 143 | +models. |
| 144 | + |
| 145 | +## Compatibility Notes |
| 146 | + |
| 147 | +- A-device support is experimental and intentionally limited to the eight |
| 148 | + documented families. |
| 149 | +- `MODULATOR` requires both `Mark` and `Space`; `COUNTER` requires `Cycles`. |
| 150 | +- `SAMPLEHOLD Td` is rejected because its native delayed timing semantics are |
| 151 | + not implemented. |
| 152 | +- Sweep-dependent A-device parameters are rejected instead of being silently |
| 153 | + frozen at one value. |
| 154 | +- The digital and analog libraries are functional macromodels, not |
| 155 | + transistor-level replicas of a specific physical part. |
| 156 | + |
| 157 | +## Documentation and Examples |
| 158 | + |
| 159 | +- [A-device guide](https://github.com/SpiceSharp/SpiceSharpParser/blob/main/src/docs/articles/a-devices.md) |
| 160 | +- [Runnable A-device examples](https://github.com/SpiceSharp/SpiceSharpParser/tree/main/circuits/a-devices) |
| 161 | +- [Digital subcircuit reference](https://github.com/SpiceSharp/SpiceSharpParser/blob/main/src/docs/articles/digital-subcircuits.md) |
| 162 | +- [Analog subcircuit reference](https://github.com/SpiceSharp/SpiceSharpParser/blob/main/src/docs/articles/analog-subcircuits.md) |
| 163 | +- [Ideal diode guide](https://github.com/SpiceSharp/SpiceSharpParser/blob/main/src/docs/articles/ideal-diode.md) |
| 164 | +- [Nonlinear passive guide](https://github.com/SpiceSharp/SpiceSharpParser/blob/main/src/docs/articles/nonlinear-passives.md) |
| 165 | + |
| 166 | +## License |
| 167 | + |
| 168 | +MIT. See the repository [license](https://github.com/SpiceSharp/SpiceSharpParser/blob/main/LICENSE). |
0 commit comments