# DC Power Supply¶

In this section, we’ll consider how a DC power supply model could be implemented in Modelica. We’ll show, once again, how a flat system model can be refactored to make use of a reusable subsystem model.

## Flat Power Supply Model¶

In this case, our flat system model will be the DC power supply circuit shown here:

Implemented in Modelica, this model looks like this:

within ModelicaByExample.Subsystems.PowerSupply.Examples;
model FlatCircuit "A model with power source, AC-DC conversion and load in one diagram"
import Modelica.Electrical.Analog;
Analog.Sources.SineVoltage wall_voltage(V=120, freqHz=60)
annotation ...
Analog.Ideal.IdealClosingSwitch switch(Goff=0)
annotation ...
Analog.Ideal.IdealTransformer transformer(Lm1=1, n=10, considerMagnetization=false)
annotation ...
Analog.Ideal.IdealDiode D1(Vknee=0, Ron=1e-5, Goff=1e-5)
annotation ...
Analog.Basic.Capacitor capacitor(C=1e-2)
annotation ...
annotation ...
Modelica.Blocks.Sources.BooleanStep step(startTime=0.25)
annotation ...
Analog.Ideal.IdealDiode D2(Vknee=0, Ron=1e-5, Goff=1e-5)
annotation ...
Analog.Ideal.IdealDiode D3(Vknee=0, Ron=1e-5, Goff=1e-5)
annotation ...
Analog.Ideal.IdealDiode D4(Vknee=0, Ron=1e-5, Goff=1e-5)
annotation ...
Analog.Basic.Ground ground1
annotation ...
equation
connect(D1.p, D3.p) annotation ...
connect(D1.n, D2.p) annotation ...
connect(switch.p, wall_voltage.p) annotation ...
connect(switch.n, transformer.p1) annotation ...
connect(step.y, switch.control) annotation ...
connect(D3.n, D4.p) annotation ...
connect(D2.n, D4.n) annotation ...
connect(transformer.p2, D1.n) annotation ...
connect(D4.p, transformer.n2) annotation ...
connect(wall_voltage.n, transformer.n1) annotation ...
connect(wall_voltage.n, ground1.p) annotation ...
connect(transformer.n2, ground1.p) annotation ...
connect(D1.p, capacitor.n) annotation ...
end FlatCircuit;


This kind of power supply works by taking an AC input voltage (120V at 60Hz), rectifying it and then passing it through a low-pass filter. If we simulate this model, we see the following voltage across the load resistor:

Note that our target here is an output voltage of 12 volts. However, the greater the load on the power supply, the lower the quality of the output signal will be. In this particular simulation, the load is initially zero (because the switch to the power supply is open). But when the switch is closed and current begins to flow through the load (the resistor named load), we start to see some artifact.

## Hierarchical Power Supply¶

Once again, we’ll improve upon the flat version of our system by taking some collection of components and organizing them into a subsystem model. Our system level circuit then becomes:

This model uses the BasicPowerSupply model whose diagram is shown here:

The Modelica source code for this reusable power supply subsystem model is:

within ModelicaByExample.Subsystems.PowerSupply.Components;
model BasicPowerSupply "Power supply with transformer and rectifier"
import Modelica.Electrical.Analog;
parameter Modelica.SIunits.Capacitance C=1e-2
"Filter capacitance"
annotation ...
parameter Modelica.SIunits.Conductance Goff=1e-5
"Backward state-off conductance (opened diode conductance)"
annotation ...
parameter Modelica.SIunits.Resistance Ron=1e-5
"Forward state-on differential resistance (closed diode resistance)"
annotation ...
parameter Real n=10
"Turns ratio primary:secondary voltage"
annotation ...
parameter Boolean considerMagnetization=false
"Choice of considering magnetization"
annotation ...
parameter Modelica.SIunits.Inductance Lm1=1e-2
"Magnetization inductance w.r.t. primary side"
annotation ...

Analog.Interfaces.NegativePin gnd
"Pin to ground power supply"
annotation ...
Analog.Interfaces.PositivePin p
"Positive pin on supply side"
annotation ...
annotation ...
annotation ...
protected
Analog.Ideal.IdealTransformer transformer(
final n=n, final considerMagnetization=considerMagnetization,
final Lm1=Lm1)
annotation ...
Analog.Ideal.IdealDiode D1(final Vknee=0, final Ron=Ron, final Goff=Goff)
annotation ...
Analog.Basic.Capacitor capacitor(C=C)
annotation ...
Analog.Ideal.IdealDiode D2(final Vknee=0, final Ron=Ron, final Goff=Goff)
annotation ...
Analog.Ideal.IdealDiode D3(final Vknee=0, final Ron=Ron, final Goff=Goff)
annotation ...
Analog.Ideal.IdealDiode D4(final Vknee=0, final Ron=Ron, final Goff=Goff)
annotation ...
equation
connect(D1.p, capacitor.n) annotation ...
connect(transformer.p2, D1.n) annotation ...
connect(capacitor.p, D2.n) annotation ...
connect(D2.n, D4.n) annotation ...
connect(D1.p, D3.p) annotation ...
connect(D3.n, D4.p) annotation ...
connect(D1.n, D2.p) annotation ...
connect(D4.p,transformer. n2) annotation ...
connect(transformer.n1, gnd) annotation ...
connect(transformer.n2, gnd) annotation ...
connect(transformer.p1, p) annotation ...
end BasicPowerSupply;


There are a couple of interesting things to note about this model. First, we see the same organizational structure as we have before where parameters and connectors are made public while the internal components are protected. We can also see the use of the Dialog annotation to organize parameters into distinct groupings (in this case, "General" and "Transformer"). We can also see the use of the enable annotation in conjunction with considerMagnetization parameter to selectively expose the Lm1 parameter only in the cases where considerMagnetization is true.

Using our hierarchical system model we get, as expected, exactly the same results as we got for the flat version:

We can augment our system model to include an additional load (that comes online after some delay):

In that case, if we simulate the model we can see the impact that additional load will have on the quality of power supply output:

By increasing the capacitance in the power supply, we can reduce the amplitude of the voltage fluctuations, e.g.,

However, if we increase the capacitance level too much, we will find that the power supply output is very slow to respond to load changes, e.g.,

## Conclusion¶

This example illustrates, once again, how a collection of components can be organized into a reusable subsystem model. This example follows the best practice of placing parameters and connectors in the public section of the resulting subsystem model while keeping the internal components in the protected section.