Demo 3: Pass Parameter to Simulation¶
In this example a SIMetrix schematic is run with a parameter passed to the simulation. The parameter can be used in any expression defining a model or instance parameter. The circuit is simulated three times with different values for the parameter. Here is the whole script:
1import simetrix as sx
2import math
3
4# Function to calculate RMS
5def rms(y, x) :
6 prev_y = y[0]
7 prev_x = x[0]
8 ss = 0
9
10 for idx, v in enumerate(y):
11 time = x[idx]
12
13 ss = ss + (v*v + prev_y*prev_y + v*prev_y)/3 * (time-prev_x)
14 prev_y = v
15 prev_x = time
16
17 return math.sqrt(ss/prev_x)
18
19
20# Get schematic
21schematic = sx.currentSchematic()
22
23# Load resistor values
24rloads = [1000, 500, 200]
25
26for rload in rloads :
27
28 # Run schematic setting RLOAD parameter to rload
29 sim = schematic.run(["RLOAD=" + str(rload)])
30
31 ## Get VOUT vector
32 vout = sim.primaryGroup.vectorOfName("VOUT")
33
34 # Calculate RMS
35 rms_vout = rms(vout.ydata, vout.xdata)
36
37 # Print result
38 print("Rload =", rload, "RMS =", rms_vout)
Notice the run() function in line 29 has an argument which resolves to [“RLOAD=1000”] on the first run. This specifies the value of the RLOAD parameter which in turn sets the value of the resistor R10.