How do I make a transformer in LTspice?
Although it is very possible to make to make a dedicated subcircuit for a specific transformer, the preferred method of making a generic transformer when drafting a simulation schematic is to simply place a separate inductor for each separate transformer winding and then couple them all together magnetically via a single Mutual Inductance (K) statement placed as a SPICE Directive on the schematic. Note that inductors called out in a Mutual Inductance statement will be automatically given a phasing dot if one does not already exist.
K1 L1 L2 L3 1 ; causes phasing dots to automatically appear on L1-L3 inductor symbols
When creating a new transformer this way, especially for use in a switched mode power circuit, it is generally best to first specify the mutual coupling coefficient to be exactly 1. By starting with 100 percent coupling there will be no leakage inductance in any winding and this will minimize the likelihood of the windings ringing at extremely high frequencies (which can slow the simulation to a crawl at each switching edge). However, be aware that a mutual inductance value of plus (or minus) unity may lead to simulation difficulties if Skip the initial operating point solution (UIC) is specified for the .tran command (specifying a realistic resistance for each inductor "winding" - ctrl-right-mouse-click - will minimize this tendency).
Note that when coupled inductors are used as transformer windings, individual winding inductances rather than turns ratios must be specified (inductance ratios should be proportional to the square of the turns ratios).
If I want to model leakage inductance, how should I do that?
The most flexible method is to keep the windings' mutual inductance statement at unity and add a small discrete leakage inductance in series with each winding. This is the most straightforward way to model transformers with asymmetrical leakage inductances, however, if your transformer is electrically symmetrical, it may be more convenient to simply set the mutual inductance to a value less than one. For each winding the resulting leakage inductance will be (1-K) times that winding's inductance. Note that each winding's coupled inductance will decrease to K times that winding's inductance, but for typical values of K (>>0.9) this effect will be very small.
How about transformer saturation effects? Can LTspice model that?
Yes, LTspice comes with a non-linear hysteretic core model built in! This is the 1991 model by John Chan et al. Compared to older core models, the Chan model is particularly robust, computationally efficient and compact, requiring only three parameters to define most any commonly encountered magnetic hysteresis loop.
Once the core material's generic magnetic properties are set, establishing the circuit-level non-linear inductance requires specifying three more parameters to set the geometry of the specific core and specifying one additional parameter to set the core winding turn-count. The Chan inductor does not directly support Mutual Inductance, so unless only a single-winding inductor is being modeled, multiple windings must be added on via additional circuitry. The simplest way to do this is to construct an ideal, unity coupled transformer with as many windings as required and then put the Chan inductor directly in parallel with any one of the windings (taking care to set the turns to match that particular winding). Since the magnetic effects (including non-saturated inductance) are already modeled by the Chan inductor, the inductance of the transformer's parallel winding must be enough larger (>>10) not to significantly load the Chan inductance. As before, the inductances for the other windings should be scaled by the square of their individual turns ratios (with respect to the paralleled winding).
Okay, but calculating winding inductances is tedious. Is there some way to just enter a turns count for all the windings?
Yes, this can be done in any of a number of ways.