Assigning Spice Code to a Transformer

Dear all, I am checking how to simulate simple transformers, coupling coefficient equal to 0.1 (image 1), and it seems to me that there are three possible ways to do so:

  • 1st - use the integrated Spice model for the mutual induction statement;

  • 2nd - use the integrated Spice model “raw spice element”;

  • 3rd - use the Spice model from an external file.

In the first case, the resulting code is simply K Ref_L1 Ref_L2 0.1, but I don’t quite understand the purpose of referencing two external elements in a separate third element in the schematic - it would perhaps be more practical to add this 1-line code traditionally to a text element. Wouldn’t it be more functional and intuitive if L1 and L2 were internally declared in a subcircuit for the transformer, rather than just specified as a reference? This way, this integrated spice model would be more “ready to use”, being in fact the declaration of a subcircuit, whose pinout would be automatically adjusted when the element is instantiated. I was unable to simulate the transformer in this first mode.

In the second case, the code is placed in the element directly, and looks like this:


.subckt transformer L1_2 L1_1 L2_2 L2_1 params: ind1=1 ind2=1 coef=0.1
L1 L1_2 L1_1 {ind1}
L2 L2_2 L2_1 {ind2}
K1 L1 L2 {coef}

The element name is T1, and the prefix “X” is added to the default parameter “Spice element type (type)”. From what I observed, it is as if the name of the element and the pins were always automatically added to the first line of the spice code in every schematic element, so that the netlist always returns in the first line as XT1 Gnd /In /Net_4 /Out transformer. This way, the simulation works well, although the parameters do not appear as editable in the simulation model editor window (image 2) and sometimes the first line of code is filled incorrectly as “X transformer”, or even as “X X transformer”, so automatic, perhaps due to an error in the default parameter.

In the third case, the one usually recommended from what I see, the code is added from an external file. It works perfectly well.

Ultimately, the simulation was carried out, but I wanted to obtain the following information:

  • A - How to carry out a successful simulation using an integrated Spice model for mutual induction statement? (1st case)

  • B - Why is it that when confirming “Ok” in the simulation model editor window, the value of the “Spice element type (type)” parameter is inserted in the 1st line of the “raw spice element” code? If you confirm “Ok” five times, the result in the 1st line will be “X X X X X transformer” (2nd case).

  • C - Why are the parameters not editable in the simulation model editor window when using the integrated “raw spice element” Spice model? (2nd case)

Thank you in advance.

(image 1):

(image 2):

For 1 you need to
place a first inductor L1 with inductance value
place a second inductor L2 with inductance value
Now the non-intuitive, somewhat strange step:
place another symbol (perhaps not an inductor), but a resistor for example (R1). Attach the “Mutual Inductance Statement” model to this symbol. Choose the inductors for coupling from the drop-down list in the “Value” column. Add the coupling coefficient.

If you look at the resulting ngspice netlist (Inspect->Simulator->Simulation->Show Spice Netlist), you will see that R1 is not part of the netlist, but a “coupling statement” KR1.

I would never use your second approach. It is error prone, and if the developers ever had thought about adding a whole model directly to the symbol, it is implemented incompletely and unsafe.

My favorite is the third approach. Putting a model, as complex as it may be, into a subcircuit, then into a file, and attach it to a transformer symbol, as you attach any (vendor provided) behavioral model to a symbol. You may then make use of the various transformer models supported by ngspice (see ngspice / ngspice / [cf9b88] /examples/various/transformers1.cir).

You may put relevant parameters onto the .subckt line of the model and display them on the Eeschema canvas (see How to use in Kicad a model from ngspice manual? - #3 by holger) for editing them easily.