Languages

CommunityCategory: GLISTERRunning Monte-Carlo simulation with XMODEL

XMODEL GLISTER

Running Monte-Carlo simulation with XMODEL

SA Support Team Staff 2022-02-22

Can I run Monte-Carlo simulations with XMODEL? That is, can I run repeated simulations while randomizing the values of selected parameters in the model?

1 Answers
Best Answer
SA Support Team Staff 2022-02-22

With a few simple steps, it is possible to run a Monte-Carlo simulation with XMODEL, running repeated simulations with randomized parameter values.

Here is a simple example to illustrate the steps for running Monte-Carlo simulation with XMODEL. The model schematic shown below is a resistor-based voltage divider named 'rdivider', which is made of two resistor primitives with the instance names R1 and R2. With the resistors have the nominal resistance values of 100.0, the circuit is expected to drive the output voltage 'out' with a half of the input voltage 'in'. Let's try to run a set of 10 Monte-Carlo simulations while randomizing the values of the resistance parameters, R1.R and R2.R, according to a Gaussian probability distribution.

First, create a new XMODEL testbench view for this schematic. To do so, open the XMODEL Testbench Editor and click the first icon on the righthand-side toolbar that says "Create New Testbench". In this example, the testbench view is named 'tb_mcsweep'.

On the 'Design' tab of the XMODEL Testbench Editor, delete the file tb_mcsweep.sv added by default and add a new file named tb_mcsweep.sv.empy. This file should be prepared separately before you add it to the testbench view. With the extra .empy extension in its name, we are indicating that the file contains embedded Python expressions that must be pre-processed by the XMODEL simulation launcher script (i.e., the 'xmodel' command).

The content of the file tb_mcsweep.sv.empy is shown below. It is basically a SystemVerilog testbench module that feeds a 10MHz sinusoidal input voltage with a 1.0V offset and 0.2V amplitude to the voltage divider instance named DUT. It also contains a few embedded Python expressions, which begin with a '@' character. For example, the two defparam statements marked in red use a pre-defined Python function gauss() to randomize the values of two instance parameters DUT.R1.R and DUT.R2.R according to a Gaussian distribution with a mean of 100.0, standard deviation of 10.0, and minimum value limited to 0.0. That is, each space occupied by @gauss(…) will be replaced with the value returned by the corresponding gauss() function. For more information on the embedded Python (EmPy) expressions, please refer to this link.

When the 'xmodel' command runs a simulation, it processes the EmPy expressions contained in the file tb_mcsweep.sv.empy and instantiates the result to a regular SystemVerilog file named tb_mcsweep.sv. The resulting SystemVerilog file would then be defining the resistance parameters DUT.R1.R and DUT.R2.R with the randomized values. An example of the instantiated tb_mcsweep.sv file is shown below.

Now we just need to repeat this simulation multiple times. To do so, we can add an extra option '--sweep index=1:10' in the 'Simulation' tab of the XMODEL Testbench Editor as shown below to sweep the value of a dummy parameter named 'index' from 1 to 10, therefore running a total of 10 simulations. Each simulation will run with a newly instantiated tb_mcsweep.sv file containing a different set of randomized values for DUT.R1.R and DUT.R2.R.

Lastly, in the 'Commands' tab of the XMODEL Testbench Editor, you can modify the command in the 'Command for Plotting Simulation Results' field to 'xwave --loadall' to instruct XWAVE to load all the waveform files at start-up.

Now you can launch the simulation by clicking the "Run Simulation" icon on the toolbar. As previously mentioned, the XMODEL launcher script will run a total of 10 simulations, sweeping the index parameter value from 1 to 10.

When all the simulations are completed, click the "Plot Waveforms" icon on the toolbar to start XWAVE. With all the waveform files loaded with the '--loadall' option, you can now select 'ALL: All Opened Files' in the Waveform File field and select the signal you want to plot all of its waveforms at once.

For instance, here are the simulated waveforms of the voltage divider output 'out'. When the voltage divider is driven with a sinusoid centered at 1V, its output waveforms are also sinusoids centered near at 0.5V as expected. However, the individual offset and amplitude values are different for each simulation run as the resistance values are being randomized.

NOTE: The example included in this Q&A posting works with the XMODEL 2022.02 or later release.
Attachment: montecarlo_20220202.tar.gz

GLISTER XMODEL

XMODEL에서 몬테카를로 시뮬레이션 수행하기

SA Support Team Staff 2022-02-22

XMODEL에서 몬테카를로 시뮬레이션을 수행할 수 있나요? 다시 말해, 모델의 선택된 파라메터 값을 랜덤하게 변화시키면서 반복된 시뮬레이션을 수행할 수 있을까요?

1 Answers
Best Answer
SA Support Team Staff 2022-02-22

With a few simple steps, it is possible to run a Monte-Carlo simulation with XMODEL, running repeated simulations with randomized parameter values.

Here is a simple example to illustrate the steps for running Monte-Carlo simulation with XMODEL. The model schematic shown below is a resistor-based voltage divider named 'rdivider', which is made of two resistor primitives with the instance names R1 and R2. With the resistors have the nominal resistance values of 100.0, the circuit is expected to drive the output voltage 'out' with a half of the input voltage 'in'. Let's try to run a set of 10 Monte-Carlo simulations while randomizing the values of the resistance parameters, R1.R and R2.R, according to a Gaussian probability distribution.

First, create a new XMODEL testbench view for this schematic. To do so, open the XMODEL Testbench Editor and click the first icon on the righthand-side toolbar that says "Create New Testbench". In this example, the testbench view is named 'tb_mcsweep'.

On the 'Design' tab of the XMODEL Testbench Editor, delete the file tb_mcsweep.sv added by default and add a new file named tb_mcsweep.sv.empy. This file should be prepared separately before you add it to the testbench view. With the extra .empy extension in its name, we are indicating that the file contains embedded Python expressions that must be pre-processed by the XMODEL simulation launcher script (i.e., the 'xmodel' command).

The content of the file tb_mcsweep.sv.empy is shown below. It is basically a SystemVerilog testbench module that feeds a 10MHz sinusoidal input voltage with a 1.0V offset and 0.2V amplitude to the voltage divider instance named DUT. It also contains a few embedded Python expressions, which begin with a '@' character. For example, the two defparam statements marked in red use a pre-defined Python function gauss() to randomize the values of two instance parameters DUT.R1.R and DUT.R2.R according to a Gaussian distribution with a mean of 100.0, standard deviation of 10.0, and minimum value limited to 0.0. That is, each space occupied by @gauss(…) will be replaced with the value returned by the corresponding gauss() function. For more information on the embedded Python (EmPy) expressions, please refer to this link.

When the 'xmodel' command runs a simulation, it processes the EmPy expressions contained in the file tb_mcsweep.sv.empy and instantiates the result to a regular SystemVerilog file named tb_mcsweep.sv. The resulting SystemVerilog file would then be defining the resistance parameters DUT.R1.R and DUT.R2.R with the randomized values. An example of the instantiated tb_mcsweep.sv file is shown below.

Now we just need to repeat this simulation multiple times. To do so, we can add an extra option '--sweep index=1:10' in the 'Simulation' tab of the XMODEL Testbench Editor as shown below to sweep the value of a dummy parameter named 'index' from 1 to 10, therefore running a total of 10 simulations. Each simulation will run with a newly instantiated tb_mcsweep.sv file containing a different set of randomized values for DUT.R1.R and DUT.R2.R.

Lastly, in the 'Commands' tab of the XMODEL Testbench Editor, you can modify the command in the 'Command for Plotting Simulation Results' field to 'xwave --loadall' to instruct XWAVE to load all the waveform files at start-up.

Now you can launch the simulation by clicking the "Run Simulation" icon on the toolbar. As previously mentioned, the XMODEL launcher script will run a total of 10 simulations, sweeping the index parameter value from 1 to 10.

When all the simulations are completed, click the "Plot Waveforms" icon on the toolbar to start XWAVE. With all the waveform files loaded with the '--loadall' option, you can now select 'ALL: All Opened Files' in the Waveform File field and select the signal you want to plot all of its waveforms at once.

For instance, here are the simulated waveforms of the voltage divider output 'out'. When the voltage divider is driven with a sinusoid centered at 1V, its output waveforms are also sinusoids centered near at 0.5V as expected. However, the individual offset and amplitude values are different for each simulation run as the resistance values are being randomized.

NOTE: The example included in this Q&A posting works with the XMODEL 2022.02 or later release.
Attachment: montecarlo_20220202.tar.gz