Article count:240 Read by:410258

Account Entry

LTspice (10) .net with sympy

Latest update time:2022-10-21
    Reads:

There are a lot of calculations of addition, subtraction, multiplication and division that need to be done in the work. Although it is not very complicated, it is still troublesome to change one variable after one. Using Mathcad in a windows environment can effectively improve work efficiency, but Mathcad cannot span multiple platforms. , such as linux / mac. Complaint: The battery of a computer starting with L is too weak to handle.

Without further ado, let’s get started!

First, you need to download and install the python development environment. Then type the following code into the console

P ip install sympy

If your installation is successful, then enter the following code to see if the installation is successful.

P ip list

Figure 1 : pip list results

When I was studying Kirchhoff's/Superposition Theorem in high school, when there were too many sources, I had to write out a lot of equations, which was also troublesome to calculate. Let’s calculate one manually first, and then try to use sympy to calculate one.

Figure 2 : Calculating Ua

Suddenly we found that this could be used as a good example for learning spice statements, so we started writing the spice model directly. The model is as follows⬇️

*Connect one end of the power supply to the negative terminal and one end to 0. The voltage is 2V

*Reference document: spice a guide to circuit simulation and analysis using pspice

Wine R1_N 0 2

*One end is connected to R1N and one end is connected to A. The resistance is 1ohm.

R1 R1_N A 1

*One end is connected to A and one end is connected to GND. The resistance is 1ohm.

R2 A 0 1

*One end is connected to A and the other is connected to GND. The current is 1A.

I1 A 0 -1

*Transient simulation 1mS

.tran 1m

*Indicates finished writing

.end



Code 1 : LTspice model

Figure 3: Open with Ltspice

Figure 4: Simulation results

What Spice writes is a netlist, but the netlist is given some meaning. If you can draw a PCB without a schematic diagram, you have probably mastered the essence of the netlist, and you are already very familiar with it. However, the readability is very poor if you do this. Generally, you can only make mistakes after drawing once, otherwise you will make mistakes if you are distracted.

Next we go back to Sympy and use Sympy to input the superposition formula as follows⬇️

Figure 5: Sympy simplifies the superposition formula

# -*- coding: utf-8 -*-

"""

Spyder Editor


This is a temporary script file.

"""

import sympy as sy


Vin=sy.symbols('Vin')

R=sy.symbols('R')

I=sy.symbols('I')


#Calculation formula for circuit superposition

Va=(Vin/(2*R))*R+(R/2)*I

#Simplify the expression

exps=sy.sympify(Va)


print(exps)


Code 2 : Sympy Simplification

Figure 6: Sympy brings in parameters and calculates the result equal to 1.5 V

# -*- coding: utf-8 -*-

"""

Spyder Editor


This is a temporary script file.

"""

import sympy as sy


Vin=sy.symbols('Vin')

R=sy.symbols('R')

I=sy.symbols('I')

#Bring in parameters

Vin=2

R=1

I=1

#Calculation formula for circuit superposition

Va=(Vin/(2*R))*R+(R/2)*I

#Simplify the expression

exps=sy.sympify(Va)


print(exps)



Code 3 : Sympy brings in parameters for calculation

If we change the resistor value at this time, but the two resistors still want to wait, we can directly change the Sympy resistor value and get the calculation result directly. However, in LTspice, how do we get the resistor value at the end of the netlist? What about punishing him?

As shown below, we introduce the .param reference code as follows⬇️

*Connect one end of the power supply to the negative terminal and one end to 0. The voltage is 2V

*Reference document: spice a guide to circuit simulation and analysis using pspice

Wine R1_N 0 2

*One end is connected to R1N and one end is connected to A. The resistance is 1ohm.

R1 R1_N A {x}

*One end is connected to A and one end is connected to GND. The resistance is 1ohm.

R2 A 0 {x}

*One end is connected to A and the other is connected to GND. The current is 1A.

I1 A 0 -1

*Transient simulation 1mS

.tran 1m

*Use {x} to represent the resistance of R

.param x 1

*Indicates finished writing

.end



Code 4 : param

Figure 7: Code 4 running results

That’s all for today, ????

Reference documentation

S ympy 's guide

LTspice help doc

S pice a guide to circuit simulation and analysis using pspice


 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号