Python Forum

Full Version: Parameters aren't seen inside function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

The question is related to the use of the Lcapy library (circuit analysis).
The 1st version of the Vin declaration (which is commented out) does not work: freq and pi are not visible in the Circuit function.

from lcapy import Circuit
import numpy as np
from matplotlib.pyplot import subplots, savefig
from math import pi
freq = 130e6
cct = Circuit('''
##... Vin 1 0 {1*sin(2*pi*freq*t)}
... Vin 1 0 {sin(130e6*2*3.14159*t)}
... Rin 1 2 50
... Cs  2 4 1e-12
... L   2 3 0.32e-6
... Rs  3 4 5
... Cl  4 0 3e-12''')

t = np.linspace(0, 2e-7, 1000)
vcl = cct.Cl.v.evaluate(t)

fig, ax = subplots(1)
ax.plot(t, vcl, linewidth=2)
ax.set_xlabel('Time (s)')
ax.set_ylabel('Output voltage (V)')
ax.grid(True)

savefig('antenna_output.png')
How to make them visible inside function?

Thanks.

Sancho.
You define the circuit using symbols. Numbers are symbols. Some trig functions have been defined as symbols. pi is defined as a symbol, so that would work if you stopped using math.pi.

If you want to use a variable I think you need to define it as a symbol or expression. Take a look at the documentation to see if this is what you want.

http://lcapy.elec.canterbury.ac.nz/expressions.html
Hello,

If I properly understood your suggestion, I have to define parameters as expression.
Doesn't work. In the example below when I build a ircuit, using numeric values, it works.
Once I try to use parameters instead of values, it doesn't.

from lcapy import Circuit, j, omega, expr
import numpy as np
from math import pi

Rin = expr(50)
Cs  = expr(1e-12)
La  = expr(0.32e-6)
Rs  = expr(5)
Rl  = expr(100)

cct = Circuit('''
... Rin 1 2 Rin
... Cs  2 4 Cs
... La  2 3 La
... Rs  3 4 Rs
... Rl  4 0 Rl''')

H = cct.transfer(1,0,4,0)
A = H(j*omega).simplify()
Am = A.magnitude

f1 = 130
f2 = 140
f3 = 150
omega_arr = np.array([f1, f2, f3])*1.0e6*2*pi
out = np.zeros(len(omega_arr))
for i in range(len(omega_arr)):
    out[i] = Am.evaluate(omega_arr[i])
I don't think that is how expr is used. The link I provided has an expression example under User Defined Symbols. I suggest you try something similar.
From the link it isn't clear how to use symbols.
For example, if I create symbol for Rin:
Rin = symbol('Rin')
Then, how to associate this symbol with a real value (eg 50.0) so that Rin is perceived as 50.0 in the circuit definition

cct = Circuit('''
... Rin 1 2 Rin
... Cs  2 4 Cs
... La  2 3 La
... Rs  3 4 Rs
... Rl  4 0 Rl''')
I was thinking something like this:
freq = 60
v = expr(sin(2*pi*freq*t))
cct = Circuit(
... Vin 1 0 v
... Rin 1 2 50
... Cs  2 4 1e-12
... L   2 3 0.32e-6
... Rs  3 4 5
... Cl  4 0 3e-12''')
And if you want to use math.pi, use it as math.pi. I think "from math import pi" hides the "pi" symbol.
Vin is no longer relevant.
What is interests me: how to pass Rin, Cs, L, Rs, Rl as values, defined outside of Circuit, into Circuit.
The same way. Using symbols or expressions. I think you failed for the same reason you couldn't use pi. There is a mixup on what are Python variables and what are lcapy symbols.

Do you know much about SymPy? A brief read of the lcapy documentation and an even briefer scan of sympy documentation has me thinking that knowing how to make symbols and expressions in sympy, and what symbols and expressions mean, is key to you solving your problem.
Finally here is the solution: use subs function, that creates new circuit and substitute the parameters:

from lcapy import network, Circuit, j, omega, symbol
import numpy as np
from math import pi

cct = Circuit('''
... Rin 1 2
... Cs  2 4
... La  2 3
... Rs  3 4
... Rl  4 0''')

cct1 = cct.subs({'Rin': 50, 'Cs':1e-12, 'La': 0.3e-6, 'Rs': 1.0, 'Rl': 100})

H = cct1.transfer(1,0,4,0)
A = H(j*omega).simplify()
Am = A.magnitude

f1 = 130
f2 = 140
f3 = 150
omega_arr = np.array([f1, f2, f3])*1.0e6*2*pi
out = np.zeros(len(omega_arr))
for i in range(len(omega_arr)):
    out[i] = Am.evaluate(omega_arr[i])
Sincerely,

Sancho.