-
Notifications
You must be signed in to change notification settings - Fork 3
Home
CoSimPy is an open source Python library aiming to combine results from electromagnetic (EM) simulation with circuit analysis through a cosimulation environment. The library is developed specifically to deal with Magnetic Resonance Imaging (MRI) radiofrequency (RF) coils. Nevertheless, it is sufficiently general to be adopted in any context involving EM multiport simulations.
The library is composed of three different classes:
The S_Matrix class is defined to manage the scattering parameters associated with any specific circuit. These can be either the scattering parameters associated with the simulated RF coil or with any other circuit which has to be connected. Specific methods are defined to handle the connection between different instances of its class.
The EM_Field class is defined to manage the EM field generated by any port of the simulated device.
The RF_Coil class represents the highest level class of the library. An instance of the RF_Coil class virtually represents the simulated device being, its properties, an S_Matrix instance managing the scattering parameters associated with the RF coil and an EM_Field instance managing the EM field generated by any port of the RF coil. This class is designed to handle the interaction between the scattering parameters and the EM field allowing the computation of the latter when the RF coil is connected to any other passive device.
This class is defined to manage the scattering matrix (S matrix) associated with any specific circuit. These can be either the scattering parameters associated with the simulated RF coil or with any other circuit which has to be connected.
-
S_Matrix.S
: numpy ndarray
Nf 🞩 NP 🞩 NP numpy ndarray representing an NP ports S matrix defined over Nf frequency values -
S_Matrix.frequencies
: numpy ndarray
Nf numpy ndarray representing the frequency values, in hertz, over which the scattering parameters are defined -
S_Matrix.n_f
: int
number of frequency values over which the scattering parameters are defined (Nf) -
S_Matrix.nPorts
: int
number of ports associated with the S Matrix (NP) -
S_Matrix.z0
: numpy ndarray
NP long numpy ndarray representing the real port impedances -
S_Matrix._S0
: S_Matrix
S_Matrix associated with the original, unconnected S Matrix. This property is used to store the S_Matrix of the original, unconnected S matrix if a connection is performed. If the S_Matrix is not the results of any previous connections or connections data has not been stored, this property will beNone
. In addition to the standard properties of the S_Matrix instance,S_Matrix._S0
also hasS_Matrix._p_incM
andS_Matrix._phaseM
properties. These are Nf 🞩 NP 🞩 NP0 numpy.ndarray where Nf is the number of frequency values over which the S matrix is defined, NP is equal toself.nPorts
and NP0 is equal toself._S0.nPorts
.S_Matrix._p_incM(S_Matrix._phaseM)[i,j,k]
represent the absolute value(phase) of the power incident to portk
of the original, unconnected S matrix, when portj
of theself
S matrix is supplied with 1 W incident power.
It creates an S_Matrix instance.
Parameters
- S : numpy ndarray
Nf 🞩 NP 🞩 NP numpy ndarray representing an NP ports S Matrix defined over Nf frequency values - freqs : list or numpy ndarray
Nf list or numpy ndarray representing the frequency values, in hertz, over which the scattering parameters are defined - z0 : int, float, list or numpy ndarray, optional
port impedances in ohm. These can be given as a NP list or numpy ndarray. If all the ports share the same impedances, a float or int value can be passed as parameter. Default is 50 ohm
Returns
- S_Matrix : S_Matrix
Example
import numpy as np
from cosimpy import *
n_p = 5 #Number of ports
n_f = 10 #Number of frequency values
frequencies = np.linspace(50e6, 150e6, n_f, endpoint=False))
s_real = np.random.rand(n_f,n_p,n_p)
s_imag = np.random.rand(n_f,n_p,n_p)
s = (s_real + 1j*s_imag) /np.max(np.abs(s_real + 1j*s_imag))
S_Matrix(s, frequencies)
'''
Out:
"""""""""""""""
S MATRIX
"""""""""""""""
|V-| = |S||V+|
|5 x 1| = |5 x 5||5 x 1|
Number of frequency values = 10
'''
Method for returning a printable representation of the S_Matrix instance.
Parameters
- self : S_Matrix
Returns
- string : string
The string identifies the class of the instance. It gives a textual representation of the S matrix shape and reports the number of frequency values over which the scattering parameters are defined
Indexing method for S_Matrix instances. Indices are interpreted as frequency values.
Parameters
- key : tuple, list, numpy ndarray, int, float, slice
frequency values used to index theself
S_Matrix
Returns
- S_Matrix : S_Matrix
Example
import numpy as np
from cosimpy import *
n_p = 5 #Number of ports
n_f = 10 #Number of frequency values
frequencies = np.linspace(50e6, 150e6, n_f, endpoint=False))
S_Mat = S_Matrix(np.random.uniform(size=(n_f,n_p,n_p)), frequencies)
S_Mat.frequencies
'''
Out:
array([5.0e+07, 6.0e+07, 7.0e+07, 8.0e+07, 9.0e+07, 1.0e+08, 1.1e+08,
1.2e+08, 1.3e+08, 1.4e+08])
'''
S_Mat[90e6:].frequencies
'''
Out:
array([9.0e+07, 1.0e+08, 1.1e+08, 1.2e+08, 1.3e+08, 1.4e+08])
'''
S_Mat[90e6:130e6].frequencies
'''
Out:
array([9.0e+07, 1.0e+08, 1.1e+08, 1.2e+08])
'''
S_Mat[95e6].frequencies
'''
Out:
'WARNING: 9.500000e+07 Hz is not contained in the frequencies list. 9.000000e+07 Hz is returned instead'
array([90000000.])
'''
__add__ method for S_Matrix instances. It computes the S_Matrix resultant from the series among 1-port self
and other
S_Matrix instances. self
and other
must be defined over the same frequency values and must share the same port impedance.
Parameters
- self : S_Matrix
1-port S_Matrix - other : S_Matrix
1-port S_Matrix
Returns
- S_Matrix : S_Matrix
1-port S_Matrix
Example
import numpy as np
from cosimpy import *
n_f = 10 #Number of frequency values
frequencies = np.linspace(50e6, 150e6, n_f, endpoint=False))
S_Mat1 = S_Matrix(np.random.uniform(size=(n_f,1,1)), frequencies)
S_Mat2 = S_Matrix.sMatrixShort(frequencies) #S_Mat2: short circuit
S_res = S_Mat1 + S_Mat2
(np.round(S_res.S,6) == np.round(S_Mat1.S,6)).all()
'''
Out:
True
'''
__mul__ method for S_Matrix instances. It computes the S_Matrix resultant from the parallel among 1-port self
and other
S_Matrix instances. self
and other
must be defined over the same frequency values and must share the same port impedance.
Parameters
- self : S_Matrix
1-port S_Matrix - other : S_Matrix
1-port S_Matrix
Returns
- S_Matrix : S_Matrix
1-port S_Matrix
Example
import numpy as np
from cosimpy import *
n_f = 10 #Number of frequency values
frequencies = np.linspace(50e6, 150e6, n_f, endpoint=False))
S_Mat1 = S_Matrix(np.random.uniform(size=(n_f,1,1)), frequencies)
S_Mat2 = S_Matrix.sMatrixShort(frequencies) #S_Mat2: short circuit
S_res = S_Mat1 * S_Mat2
(S_res.S == -1).all()
'''
Out:
True
'''
__sub__ method for S_Matrix instances. It computes the S_Matrix resultant from the cascade among the last port of self
and the first port of other
S_Matrix instances. self
and other
must be defined over the same frequency values and the impedance of the last port of self
and of the first port of other
must be equal. At least one among self
and other
S_Matrix instances must have more than 1 port.
Parameters
- self : S_Matrix
First S_Matrix instance - other : S_Matrix
Second S_Matrix instance whose first port is connected to the last port ofself
Returns
- S_Matrix : S_Matrix
S_Matrix instance resulting from the connection between the last port ofself
and the first port ofother
. The returned S_Matrix instance has a number of ports equal to the sum of the number of ports ofself
and those ofother
minus one.
Example
import numpy as np
from cosimpy import *
n_f = 10 #Number of frequency values
frequencies = np.linspace(50e6, 150e6, n_f, endpoint=False)
S_Mat1 = S_Matrix(np.random.uniform(size=(n_f,1,1)), frequencies)
S_Mat2 = S_Matrix(np.repeat(np.array([[[0,1],[1,0]]]),n_f,axis=0),frequencies) #S_Mat2: straight connection from port 1 to 2
S_res = S_Mat1 - S_Mat2
np.isclose(S_res.S,S_Mat1.S).all()
'''
Out:
True
'''
The method is used to plot the scattering parameters as a function of the frequency values over which they are defined.
Parameters
- self : S_Matrix
- parameters : list or numpy ndarray
list or numpy ndarray identifying the scattering parapeters to plot. Each element of parameters must be a string formatted as "S<n1>-<n2>" where <n1> and <n2> are the relevant port numbers - dB : bool, optional
ifTrue
, the scattering parameters are plotted in Decibel. Otherwise, they are plotted as magnitude and phase. Default isTrue
- smooth : bool, optional
ifTrue
, the scattering parameters are interpolated along their frequency range to produce smoother plots. Default isTrue
Returns
- fig : matplotlib.figure.Figure
instance of matplotlib.figure.Figure relevant to the produced plot
Example
import numpy as np
from cosimpy import *
n_p = 5 #Number of ports
n_f = 10 #Number of frequency values
frequencies = np.linspace(50e6, 150e6, n_f, endpoint=False)
S_Mat = S_Matrix(np.random.uniform(size=(n_f,n_p,n_p)), frequencies)
fig = S_Mat.plotS(["S1-1, S1-2, S3-2"])
The method is used to plot the scattering parameters in dB as a function of the frequency values over which they are defined in a multi-panel figure. Each panel il relevant to a Sn,n parameter with n from one to the number of ports of self
.
Parameters
- self : S_Matrix
- num_nn : int
numbers of nearest neighbours to be plotted in each panel. For each port N, in each figure panel will be plotted the S parameters from SN,N-num_nn to SN,N+num_nn - smooth : bool, optional
ifTrue
, the scattering parameters are interpolated along their frequency range to produce smoother plots. Default isTrue
Returns
- fig : matplotlib.figure.Figure
instance of matplotlib.figure.Figure relevant to the produced plot
Example
import numpy as np
from cosimpy import *
n_p = 5 #Number of ports
n_f = 10 #Number of frequency values
frequencies = np.linspace(50e6, 150e6, n_f, endpoint=False)
S_Mat = S_Matrix(np.random.uniform(size=(n_f,n_p,n_p)), frequencies)
fig = S_Mat.plotSPanel(3)
it returns the Z matrix associated with the S_Matrix self
Parameters
- self : S_Matrix
Returns
- Z : numpy ndarray
Nf 🞩 NP 🞩 NP numpy ndarray of impedance parameters. Nf is the number of frequency values over whichself
is defined and NP is the number of ports
it returns the Y matrix associated with the S_Matrix self
Parameters
- self : S_Matrix
Returns
- Y : numpy ndarray
Nf 🞩 NP 🞩 NP numpy ndarray of admittance parameters. Nf is the number of frequency values over whichself
is defined and NP is the number of ports
The method returns the voltages and currents at the S matrix ports when 1 W power is incident to one port and the other ports are closed over their characteristic impedance. If a connection with an external network has been performed and connection data have been stored, it also returns voltages and currents at the ports of the unconnected S matrix (i.e unconnected system in the figure below).
Parameters
- self : S_Matrix
Returns
- v_sup : numpy ndarray
Nf 🞩 NP 🞩 NP numpy ndarray where Nf is the number of frequency values over whichself
is defined and NP is the number of ports ofself
.v_sup[i,j,k]
represents the voltage, at a frequency equal toself.frequencies[i]
, at the j-port when 1 W power is incident to the k-port and all the other ports are closed over their characteristic load - i_sup : numpy ndarray
Nf 🞩 NP 🞩 NP numpy ndarray where Nf is the number of frequency values over whichself
is defined and NP is the number of ports ofself
.i_sup[i,j,k]
represents the current, at a frequency equal toself.frequencies[i]
, at the j-port when 1 W power is incident to the k-port and all the other ports are closed over their characteristic load. If the current phase is between$-\pi/2$ and$\pi/2$ , the current is entering the relevant port. Otherwise, the current is exiting the relevant port - v0 : numpy ndarray, optional
Nf 🞩 NP,0 🞩 NP numpy ndarray where Nf is the number of frequency values over whichself
is defined, NP,0 is the number of ports of the unconnected S matrix with an external network and NP is the number of ports ofself
.v0[i,j,k]
represents the voltage, at a frequency equal toself.frequencies[i]
, at the j-port of the unconnected S matrix when 1 W power is incident to the k-port ofself
and all its other ports are closed over their characteristic load - i0 : numpy ndarray, optional
Nf 🞩 NP,0 🞩 NP numpy ndarray where Nf is the number of frequency values over whichself
is defined, NP,0 is the number of ports of the unconnected S matrix and NP is the number of ports ofself
.i0[i,j,k]
represents the current, at a frequency equal toself.frequencies[i]
, at the j-port of the unconnected S matrix when 1 W power is incident to the k-port ofself
and all its other ports are closed over their characteristic load. If the current phase is between$-\pi/2$ and$\pi/2$ , the current is entering the relevant port. Otherwise, the current is exiting the relevant port
The method returns the power accepted by the system when each port of the S matrix is supplied according to the incident powers passed in the p_inc list. If a connection with an external network has been performed and connection data have been stored, it also returns the power accepted by the S matrix before the last connection (i.e unconnected system in the figure below). The difference of the two accepted powers reflects the losses in the connected network
Parameters
- self : S_Matrix
- p_inc : list, numpy ndarray
list or numpy ndarray with length equal to the number of ports ofself
.p_inc[i]
is the power incident to the i-th port ofself
to be considered for the power balance computation
Returns
- p_acc_1 : numpy ndarray
numpy ndarray with length equal to the number of frequencies over whichself
is defined.p_acc_1[i]
is the power accepted by the system at a frequency equal toself.frequencies[i]
when all the S matrix ports are supplied according to the incident powers listed in the p_inc argument - p_acc_2 : numpy ndarray, optional
numpy ndarray with length equal to the number of frequencies over whichself
is defined.p_acc_2[i]
is the power accepted by the system, before have been connected with external networks, at a frequency equal toself.frequencies[i]
when all the S matrix ports ofself
(i.e. the connected system) are supplied according to the incident powers listed in the p_inc argument
For a passive system, the matrix defined as self.S
S matrix, being compliant with the above constraint.
Parameters
- self : S_Matrix
- report : bool, optional
ifTrue
the method returns a report relevant to the new computed S matrix - f_tol : float, optional
absolute tolerance (in max-norm) for the residual related to the Newton-Krylov algorithm adopted to compute the new S matrix. Default is 1e-10 - rdiff : float, optional
relative step size to use in numerical differentiation related to the Newton-Krylov algorithm adopted to compute the new S matrix. Default isNone
Returns
- S_Matrix : S_Matrix
S_Matrix obtained from the recomputed S matrix -
dict, optional
dictionary with the following keys:-
max_abs_Sdiff : numpy ndarray
numpy ndarray with a length equal to the number of frequency values over which the S matrix is defined. For each frequency, it reports the maximum absolute value of the difference between the original and recomputed S parameters -
min_eig_val : numpy ndarray
numpy ndarray with a length equal to the number of frequency values over which the S matrix is defined. For each frequency, it reports the minimum eigenvalue of the matrix defined as$II - S^HS$ where$S$ is the recomputed S matrix
-
max_abs_Sdiff : numpy ndarray
When a device is connected to external circuitries, its original S matrix and the complex powers incident to its ports, when 1 W power is incident to the new ports of the connected deveice, can be stored. These data allows to compute the power balance distingushing the power absorbed by the unconnected device and that absorbed by the external circuitry. This method set the S_Matrix instance as unconnected. As a consequence, any previously connected circuitry becomes part of the original device and are not treated separately when relevant methods are called
Parameters
- self : S_Matrix
Returns
- None
Method designed to export the S_Matrix instance in a Touchstone file.
Parameters
-
self : S_Matrix
-
filename : string
string reporting the Touchstone file name -
version : string or None, optional
string orNone
representing the Touchstone file version. In the preset version of the library, version can either be "1.1" or None. If "1.1" the Touchstone file is compliant to the Touchstone® File Format Specification Rev. 1.1. If version isNone
, the file is formatted as a multiple rows text file where each row corresponds to a single frequency value followed by the associated parameters separated by space or tab characters:f1 p11 p12 ... p1n p21 p22 ... pn1 pn2 ... pnn
f2 ...
.
.
. -
options : dict or None
dict orNone
argument reporting the options relevant to the exported file. In case it is a dictionary, it may contain the following key:-
format : string, optional
string defining the format of the parameters saved in the file. The string can be equal to:- 'RI' : Real and Imaginary parts
- 'MA' : Magnitude and phase in degrees
- 'DB' : Magnitude in decibel and phase in degrees
- 'MA_Rad' : Magnitude and phase in radians (This is not contemplated in Touchstone Rev. 1.1)
- 'DB_Rad' : Magnitude in decibel and phase in radians (This is not contemplated in Touchstone Rev 1.1)
Default is 'MA'
-
frequency_unit : string, optional
string defining the measurement unit of the frequency values reported in the Touchstone file. It can be equal to 'Hz', 'kHz, 'MHz' or 'GHz'. Default is 'GHz' -
parameter : string, optional
string defining the parameter type reported in the file. It can be equal to 'S', 'Z', 'Y'. Default is 'S'
If
None
, all the default values reported above are considered as exporting options -
Returns
- None
The method performs the cascade connection between self
and the S_Matrix instances contained in networks
list. This method is not meant to be used directly but it is called by the singlePortConnRFcoil
method of RF_Coil class. The latter method should be used by the user.
Parameters
- self : S_Matrix
- networks : list
list, with length equal to the number of ports ofself
, either containingNone
values or S_Matrix instances defined over the same frequency values ofself
. If the n-th element ofnetworks
isNone
, the n-th port ofself
is not connected to any other network in the returned S_Matrix. If the n-th element ofnetworks
is an N-ports S_Matrix, its first port must share the same impedance of the n-th port ofself
and the two ports are connected together. In the returned S_Matrix, the n-th port ofself
is therefore expanded into (N-ports - 1) ports. The example proposed in the image below should clarify the workflow - comp_Pinc : bool, optional
ifTrue
the method returns the magnitude and 'phase' of the incident powers across the ports ofself
when each port of the returned S matrix is supplied with 1 W of incident power and all its other ports are closed on characteristic loads. Default isFalse
Returns
- S_res : S_Matrix
The S_Matrix which results from the connection. The S_Matrix is defined over the same frequency values ofself
and has a number of ports equal to NP - NT + ∑i=1NT(Np,i - 1) where NP is the number of ports ofself
, NT is the number of S_Matrix instances innetworks
and Np,i is the number of ports of the i-th S_Matrix ofnetworks
. The n-th port impedance of S_res is equal to the impedance of the relevant port ofself
if it derives from aNone
element ofnetworks
. Otherwise, it is equal to the impedance of the relevant port of the S_Matrix ofnetworks
from which the port derives - p_incM : numpy ndarray, optional
Nf 🞩 NPout 🞩 NP numpy ndarray where Nf is the number of frequency values over whichself
is defined, NPout is the number of ports of the returned S_Matrix and NP is the number of ports ofself
.PincM[i,j,k]
represents the magnitude of the incident power at the k-port ofself
when the j-port of the output S_Matrix is supplied with 1 W incident power at a frequency equal toself.frequencies[i]
- phaseM : numpy ndarray, optional
Nf 🞩 NPout 🞩 NP numpy ndarray where Nf is the number of frequency values over whichself
is defined, NPout is the number of ports of the returned S_Matrix and NP is the number of ports ofself
.phaseM[i,j,k]
represents the 'phase' of the incident power at the k-port ofself
when the j-port of the output S_Matrix is supplied with 1 W incident power at a frequency equal toself.frequencies[i]
Example
import numpy as np
from cosimpy import *
n_f = 10 #Number of frequency values
frequencies = np.linspace(50e6, 150e6, n_f, endpoint=False))
s = np.repeat(np.array([[[0, 1], [1, 0]]]), n_f, axis=0) #Straight connection between port 1 and port 2
S_Mat = S_Matrix(s, frequencies)
S_ch = S_Matrix(np.zeros((n_f,1,1)), frequencies) #Characteristic load
S_res, p_incM, phaseM = S_Mat._singlePortConnSMatrix([None, S_ch], True)
np.array_equal(S_res.S,np.repeat([[[0]]],10,axis=0))
'''
Out:
True
'''
np.array_equal(np.round(p_incM,10),np.repeat([[[1., 0.]]],n_f,axis=0))
'''
Out:
True
'''
np.equal(phaseM, np.zeros_like(phaseM)).all()
'''
Out:
True
'''
The method performs the cascade connection between self
and other
S_Matrix instances. This method is not meant to be used directly but it is called by the fullPortsConnRFcoil
method of RF_Coil class. The latter method should be used by the user.
Parameters
- self : S_Matrix
- other : S_Matrix
S_Matrix instance defined over the same frequency values ofself
and with a number of ports higher than those ofself
. In the returned S_Matrix, the first NP ports ofother
are connected with the NP ports ofself
and must share the same port impedances - comp_Pinc : bool, optional
ifTrue
the method returns the magnitude and 'phase' of the incident powers across the ports ofself
when each port of the returned S matrix is supplied with 1 W of incident power and all its other ports are closed on characteristic loads. Default isFalse
Returns
-
S_res : S_Matrix
The S_Matrix which results from the connection. The S_Matrix is defined over the same frequency values ofself
and has a number of ports equal to NPo - NP being NP the number of ports ofself
and NPo the number of ports ofother
. The port impedances of S_res will be equal to those of the last NPo ports of other -
p_incM : numpy ndarray, optional
Nf 🞩 NPout 🞩 NP numpy ndarray where Nf is the number of frequency values over whichself
is defined, NPout is the number of ports of the returned S_Matrix and NP is the number of ports ofself
. PincM[i,j,k] represents the magnitude of the incident power at the k-port ofself
when the j-port of the output S_Matrix is supplied with 1 W incident power at a frequency equal toself.frequencies[i]
-
phaseM : numpy ndarray, optional
Nf 🞩 NPout 🞩 NP numpy ndarray where Nf is the number of frequency values over whichself
is defined, NPout is the number of ports of the returned S_Matrix and NP is the number of ports ofself
. phaseM[i,j,k] represents the 'phase' of the incident power at the k-port ofself
when the j-port of the output S_Matrix is supplied with 1 W incident power at a frequency equal toself.frequencies[i]
Example
import numpy as np
from scipy.linalg import circulant
from cosimpy import *
n_p = 2 #Number of ports
n_f = 10 #Number of frequency values
frequencies = np.linspace(50e6, 150e6, n_f, endpoint=False))
S_Mat = S_Matrix(np.random.uniform(size=(n_f,n_p,n_p)), frequencies)
s_other = circulant([0,0,1,0]).T #Straight connection between ports 1 with port 3 and port 2 with port 4
s_other
'''
Out:
array([[0, 0, 1, 0],
[0, 0, 0, 1],
[1, 0, 0, 0],
[0, 1, 0, 0]])
'''
other = S_Matrix(np.repeat([s_other], n_f, axis=0), frequencies)
S_res, p_incM, phaseM = S_Mat._fullPortsConnSMatrix(other, True)
(np.round(S_res.S,10) == np.round(S_Mat.S,n_f)).all()
'''
Out:
True
'''
np.equal(np.round(p_incM, 10),np.repeat([[[1,0],[0,1]]], n_f, axis=0)).all()
'''
Out:
True
'''
np.equal(phaseM, np.zeros_like(phaseM)).all()
'''
Out:
True
'''
The method returns an S_Matrix instance that, if connected to the self
S_Matrix, results in a S_Matrix instance where the port pairs listed in port_pairs
aurgument are connected together through the S_Matrix instances listed in sMats
. This method is not meant to be used directly but it is called by the connectPorts
method of RF_Coil class. The latter method should be used by the user.
Parameters
- self : S_Matrix
- port_pairs : list, numpy.ndarray
NPP 🞩 2 list or numpy.ndarray listing the port pairs that have to be connected together through the NPP S_Matrix instances listed insMats
- sMats : list, numpy.ndarray
NPP list or numpy.ndarray listing the S_Matrix instances used to connect together the port pairs listed inport_pairs
. Each S_Matrix instance has to be defined over the same frequency values over which theself
S_Matrix instance is defined
Returns
- s_load : S_Matrix
S_Matrix instance with a number of ports equal to 2(NP-NPP), where NP is the number of ports ofself
and NPP is the number of port pairs that have to be connected together through the S_Matrix instances listed insMats
.
Private method used to compute the S_Matrix instance resulting from the connection of the NP ports of self
with the first NP ports of other
.
Parameters
- self : S_Matrix
- other : S_Matrix
S_Matrix instance* defined over the same frequency values ofself
and with a number of ports higher than those ofself
. In the returned S_Matrix, the first NP ports ofother
are connected with the NP ports ofself
and must share the same port impedances
Returns
- S_res : S_Matrix
The S_Matrix which results from the connection ofself
withother
. The S_Matrix is defined over the same frequency values ofself
and has a number of ports equal to NPo - NP being NP the number of ports ofself
and NPo the number of ports ofother
. The port impedances of S_res will be equal to those of the last NPo ports of other
Private method used to compute the magnitude and 'phase' of the powers incident to the ports of self
when the last port of other
is supplied with 1 W incident power.
Parameters
- self : S_Matrix
- other : S_Matrix
S_Matrix instance* defined over the same frequency values ofself
and with a number of ports equal to (NP + 1) where NP is equal to the number of ports ofself
. The first NP ports ofother
must share the same port impedances of the ports ofself
Returns
- p_incM : numpy ndarray
Nf 🞩 1 🞩 NP numpy ndarray where Nf is the number of frequency values over whichself
is defined and NP is the number of ports ofself
. PincM[i,0,k] represents the magnitude of the incident power at the k-port ofself
when the last port ofother
is supplied with 1 W incident power at a frequency equal toself.frequencies[i]
- phaseM : numpy ndarray
Nf 🞩 1 🞩 NP numpy ndarray where Nf is the number of frequency values over whichself
is defined and NP is the number of ports ofself
. phaseM[i,0,k] represents the 'phase' of the incident power at the k-port ofself
when the last port ofother
is supplied with 1 W incident power at a frequency equal toself.frequencies[i]
Private method which returns the index of self.frequencies
corresponding to the frequency freq
.
Parameters
- self : S_Matrix
- freq : float
Returns
- idx : int
Index of ofself.frequencies
corresponding to the frequencyfreq
. In casefreq
is not inself.frequencies
, the lower index of the nearest element ofself.frequencies
tofreq
is returned
class method used to return an S_Matrix starting from an impedance matrix defined over the frequency values listed in freqs
.
Parameters
- cls : S_Matrix class
- Z : numpy ndarray
Nf 🞩 NP 🞩 NP numpy ndarray of impedance parameters. Nf is the number of frequency values over which the impedance parameters are defined. NP is the number of ports - freqs : list, numpy ndarray
Nf list or numpy ndarray containing the frequency values, in hertz, over which the impedance parameters are defined - z0 : int, float, list or numpy ndarray, optional
port impedances in ohm. These can be given as a NP list or numpy ndarray. If all the ports share the same impedances, an int or float value can be passed as parameter. Default is 50 ohm
Returns
- S_Matrix : S_Matrix
S_Matrix obtained from the conversion of the impedance parameters and defined over the frequency values listed infreqs
. The port impedances of the returned S_Matrix are given by the relevant method parameter
class method used to return an S_Matrix starting from an admittance matrix defined over the frequency values listed in freqs
.
Parameters
- cls : S_Matrix class
- Z : numpy ndarray
Nf 🞩 NP 🞩 NP numpy ndarray of admittance parameters. Nf is the number of frequency values over which the admittance parameters are defined. NP is the number of ports - freqs : list, numpy ndarray
Nf list or numpy ndarray containing the frequency values, in hertz, over which the admittance parameters are defined - y0 : int, float, list or numpy ndarray, optional
port admittances. These can be given as a NP list or numpy ndarray. If all the ports share the same admittances, an int or float value can be passed as parameter. Default is 0.02 S
Returns
- S_Matrix : S_Matrix
S_Matrix obtained from the conversion of the admittance parameters and defined over the frequency values listed infreqs
. The port impedances of the returned S_Matrix are obtained from the port admittances given by the relevant method parameter
class method used to return an S_Matrix importing the scattering parameters from a Touchstone file.
Parameters
-
cls : S_Matrix class
-
filename : string
string reporting the Touchstone file name -
version : string or None, optional
string orNone
representing the Touchstone file version. In the preset version of the library, version can either be "1.1" orNone
. If "1.1" the Touchstone file has to be compliant with the Touchstone® File Format Specification Rev. 1.1. If version isNone
, the file to be imported has to be formatted as a multiple rows text file where each row corresponds to a single frequency value followed by the associated parameters separated by space or tab characters:f1 p11 p12 ... p1n p21 p22 ... pn1 pn2 ... pnn
f2 ...
.
.
.
Default is "1.1" -
options : dict or None, optional
This argument is not considered if version is different fromNone
. Otherwise, it has to be a dictionary with the following keys:-
format : string
string defining the format of the parameters saved in the file. The string can be equal to:- 'RI' : Real and Imaginary parts
- 'MA' : Magnitude and phase in degrees
- 'DB' : Magnitude in decibel and phase in degrees
- 'MA_Rad' : Magnitude and phase in radians
- 'DB_Rad' : Magnitude in decibel and phase in radians
-
frequency_unit : string
string defining the measurement unit of the frequency values reported in the Touchstone file. It can be equal to 'Hz', 'kHz, 'MHz' or 'GHz'. -
parameter : string
string defining the parameter type reported in the file. It can be equal to 'S', 'Z', 'Y'. -
number_of_ports : int
int defining the number of ports saved in the file. -
port_references : int, float, list, numpy ndarray
port impedances, if "parameter" key is equal to 'S' or 'Z', port admittance if "parameter" key is equal to 'Y'. If int or float, all the ports are assumed to share the same port reference. If list or numpy ndarray, its length must be equal to the number of ports saved in the file. -
comments : list, numpy ndarray, optional
list containing the characters identifying the comment lines in the file. Default is ['#', '!']
Default is
None
-
Returns
- S_Matrix : S_Matrix
S_Matrix obtained from the Touchstone file
class method used to return an S_Matrix of a 1-port short circuit.
Parameters
- cls : S_Matrix class
- freqs : list, numpy ndarray
list, numpy ndarray reporting the frequency values, in hertz, over which the returned S_Matrix is defined - z0 : float, optional
The impedance, in ohm, of the port of the returned S_Matrix. Default is 50 ohm
Returns
- S_Matrix : S_Matrix
1-port S_Matrix, defined over the frequency values passed as method parameter, of a short circuit. The port impedance is specified as method parameter
class method used to return an S_Matrix of a 1-port open circuit.
Parameters
- cls : S_Matrix class
- freqs : list, numpy ndarray
list, numpy ndarray reporting the frequency values, in hertz, over which the returned S_Matrix is defined - z0 : float, optional
The impedance, in ohm, of the port of the returned S_Matrix. Default is 50 ohm
Returns
- S_Matrix : S_Matrix
1-port S_Matrix, defined over the frequency values passed as method parameter, of an open circuit. The port impedance is specified as method parameter
class method used to return an S_Matrix of a 1-port Resistance-Capacitance series circuit.
Parameters
- cls : S_Matrix class
- Rvalue : float
The resistance value in ohm - Cvalue : float
The capacitance value in farad - freqs : list, numpy ndarray
list, numpy ndarray reporting the frequency values, in hertz, over which the returned S_Matrix is defined - z0 : float, optional
The impedance, in ohm, of the port of the returned S_Matrix. Default is 50 ohm
Returns
- S_Matrix : S_Matrix
1-port S_Matrix, defined over the frequency values passed as method parameter, of a Resistance-Capacitance series. The port impedance is specified as method parameter
class method used to return an S_Matrix of a 1-port Resistance-Inductance series circuit.
Parameters
- cls : S_Matrix class
- Rvalue : float
The resistance value in ohm - Lvalue : float
The inductance value in henry - freqs : list, numpy ndarray
list, numpy ndarray reporting the frequency values, in hertz, over which the returned S_Matrix is defined - z0 : float, optional
The impedance, in ohm, of the port of the returned S_Matrix. Default is 50 ohm
Returns
- S_Matrix : S_Matrix
1-port S_Matrix, defined over the frequency values passed as method parameter, of a Resistance-Inductance series. The port impedance is specified as method parameter
class method used to return an S_Matrix of a 1-port Resistance-Capacitance parallel circuit.
Parameters
- cls : S_Matrix class
- Rvalue : float
The resistance value in ohm - Cvalue : float
The capacitance value in farad - freqs : list, numpy ndarray
list, numpy ndarray reporting the frequency values, in hertz, over which the returned S_Matrix is defined - z0 : float, optional
The impedance, in ohm, of the port of the returned S_Matrix. Default is 50 ohm
Returns
- S_Matrix : S_Matrix
1-port S_Matrix, defined over the frequency values passed as method parameter, of a Resistance-Capacitance parallel. The port impedance is specified as method parameter
class method used to return an S_Matrix of a 1-port Resistance-Inductance parallel circuit.
Parameters
- cls : S_Matrix class
- Rvalue : float
The resistance value in ohm - Lvalue : float
The inductance value in henry - freqs : list, numpy ndarray
list, numpy ndarray reporting the frequency values, in hertz, over which the returned S_Matrix is defined - z0 : float, optional
The impedance, in ohm of the port of the returned S_Matrix. Default is 50 ohm
Returns
- S_Matrix : S_Matrix
1-port S_Matrix, defined over the frequency values passed as method parameter, of a Resistance-Inductance parallel. The port impedance is specified as method parameter
class method used to return an S_Matrix of a 2-port T-network.
Parameters
- cls : S_Matrix class
- SL_1 : S_Matrix, None
The 1-port S_Matrix of the longitudinal parameter of the T-network towards the first port. IfNone
, a short circuit will be considered - SL_2 : S_Matrix, None
The 1-port S_Matrix of the longitudinal parameter of the T-network towards the second port. IfNone
, a short circuit will be considered - ST : S_Matrix
The 1-port S_Matrix of the transversal parameter of the T-network - z0 : int, float, list or numpy ndarray, optional
port impedances in ohm. These can be given as a 2-element list or numpy ndarray. If all the ports share the same impedances, an int or float value can be passed as parameter. Default is 50 ohm
Returns
- S_Matrix : S_Matrix
2-port S_Matrix, of the T-network defined over the same frequency values over which SL_1, SL_2 and ST are defined. The impedances of the two ports are specified by the relevant method parameter
class method used to return an S_Matrix of a 2-port PI-network.
Parameters
- cls : S_Matrix class
- ST_1 : S_Matrix, None
The 1-port S_Matrix of the transversal parameter of the PI-network towards the first port. IfNone
, an open circuit will be considered - ST_2 : S_Matrix, None
The 1-port S_Matrix of the transversal parameter of the PI-network towards the second port. IfNone
, an open circuit will be considered - SL : S_Matrix
The 1-port S_Matrix of the longitudinal parameter of the PI-network - z0 : int, float, list or numpy ndarray, optional
port impedances. These can be given as a 2-element list or numpy ndarray. If all the ports share the same impedances, an int or float value can be passed as parameter. Default is 50 ohm
Returns
- S_Matrix : S_Matrix
2-port S_Matrix, of the PI-network defined over the same frequency values over which ST_1, ST_2 and SL are defined. The impedances of the two ports are specified by the relevant method parameter
class method used to return an S_Matrix of a low-losses transmission line.
Parameters
- cls : S_Matrix class
- l : float
length, in meters, of the transmission line - freqs : list, numpy ndarray
list, numpy ndarray reporting the frequency values, in hertz, over which the returned S_Matrix is defined - z0_line : int, float, optional
characteristic impedance, in ohm, of the transmission line. Default is 50 ohm - c_f : float, optional <br ratio between the propagation velocity in the transmission line and the speed of light. Default is 1
- alpha : float, optional
attenuating coefficient of the transmission line. Default is 0 - z0 : int, float, list, numpy ndarray, optional
port impedances. These can be given as a 2-element list or numpy ndarray. If all the ports share the same impedance value, an int or float value can be passed as parameter. Default is 50 ohm
Returns
- S_Matrix : S_Matrix
2-port S_Matrix, of the transmission line defined over the frequency values given as method parameter. The impedances of the two ports are equal to the characteristic impedance of the transmission line given as method parameter
class method used to return an S_Matrix of a 3-port Y-like network. All the network ports shared the same port impedance.
Parameters
- cls : S_Matrix class
- freqs : list, numpy ndarray
list, numpy ndarray reporting the frequency values, in hertz, over which the returned S_Matrix is defined - z0 : int, float, optional
port impoedances. Default is 50 ohm
Returns
- S_Matrix : S_Matrix
3-port S_Matrix, of the Y-like network defined over the frequency values listed in freqs. The impedances of the three ports are equal to those specified by the relevan method parameter
class method used to return an S_Matrix of a 3-port E-like network. All the network ports shared the same port impedance.
Parameters
- cls : S_Matrix class
- freqs : list, numpy ndarray
list, numpy ndarray reporting the frequency values, in hertz, over which the returned S_Matrix is defined - z0 : int, float, optional
port impoedances. Default is 50 ohm
Returns
- S_Matrix : S_Matrix
3-port S_Matrix, of the E-like network defined over the frequency values listed in freqs. The impedances of the three ports are equal to those specified by the relevan method parameter
private class method used to return an S_Matrix where the Smat
port at index idx0
is exchanged with the port at index idx1
.
Parameters
- cls : S_Matrix class
- Smat : S_Matrix
- idx0 : int
start point index. It can be included in the range 0 to (NP-1) where NP is the number of ports ofSmat
. The index can be also included in the range -NP to -1. In the last caseidx0 += N_P
- idx1 : int
end point index. It can be included in the range 0 to (NP-1) where NP is the number of ports ofSmat
. The index can be also included in the range -NP to -1. In the last caseidx1 += N_P
Returns
- S_Matrix : S_Matrix
S_Matrix where theSmat
port at indexidx0
is exchanged with the port at indexidx1
. Ifidx0 < idx1
it moves the port atidx0
ofSmat
after the port atidx1
ofSmat
. Ifidx0 > idx1
it moves the port atidx0
ofSmat
before the port atidx1
ofSmat
If:
Smat_t = movePort(Smat, idx0, idx1)
it is always verified:
Smat == movePort(Smat_t, idx1, idx0)
This class is defined to manage harmonic electric and magnetic flux density fields generated by any multiport device. The EM field must be computed on a regular cartesian grid (Δx = Δy = Δz).
-
EM_Field.b_field
: numpy ndarray or None
Nf 🞩 NP 🞩 3 🞩 NN numpy ndarray where Nf is the number of frequency values over which the magnetic flux density field, expressed in tesla, is defined. NP is the number of ports of the device and NN is the number of spatial points over which the magnetic flux density field is defined.EM_Field.b_field[i,k,j,n]
represents the rms of thej
component of the magnetic flux density field at the frequency identified by the indexi
, when the portk
is supplied with 1 W incident power in the point identified by indexn
. The values along the last axis of the array follow a fortran-like index ordering (first index changing faster) considering a Cartesian coordinate system. It can beNone
if the magnetic flux density field is not defined. -
EM_Field.e_field
: numpy ndarray or None
Nf 🞩 NP 🞩 3 🞩 NN numpy ndarray where Nf is the number of frequency values over which the electric field, expressed in volts per meter, is defined. NP is the number of ports of the device and NN is the number of spatial points over which the electric field is defined.EM_Field.e_field[i,k,j,n]
represents the rms of thej
component of the electric field at the frequency identified by the indexi
, when the portk
is supplied with 1 W incident power in the point identified by indexn
. The values along the last axis of the array follow a fortran-like index ordering (first index changing faster) considering a Cartesian coordinate system. It can beNone
if the electric field is not defined. -
EM_Field.frequencies
: numpy ndarray
Nf numpy ndarray representing the frequency values, in hertz, over which electric and magnetic flux density fields are defined -
EM_Field.nPoints
: numpy ndarray
numpy ndarray with a length equal to three reporting the number of spatial points over which the electric and magnetic flux density fields are defined: [Nx, Ny, Nz] -
EM_Field.n_f
: int
number of frequency values over which the electric and magnetic flux density fields are defined (Nf) -
EM_Field.nPorts
: int
number of ports associated with the electric and magnetic flux density fields (NP) -
EM_Field.properties
: dict
dictionary which include additional properties related to the points on which the electric and magnetic flux density fields are defined. These can be, for example, the electric permittivity and electrical conductivity. Refer to theEM_Field.__init__
method for further details about the way this dictionary has to be defined.
It creates an EM_Field instance. At least one among b_field and e_field must be different from None
Parameters
-
freqs : list or numpy ndarray
Nf list or numpy ndarray representing the frequency values, in hertz, over which the electric and magnetic flux density fields are defined -
nPoints : list or numpy ndarray
list or numpy ndarray with a length equal to three reporting the number of spatial points over which the electric and magnetic flux density fields are defined: [Nx, Ny, Nz] -
b_field : numpy ndarray, optional
Nf 🞩 NP 🞩 3 🞩 NN numpy ndarray where Nf is the number of frequency values over which the magnetic flux density field, expressed in tesla, is defined. NP is the number of ports of the device and NN is the number of spatial points over which the magnetic flux density field is defined.b_field[i,k,j,n]
represents the rms of thej
component of the magnetic flux density field at the frequency identified by the indexi
, when the portk
is supplied with 1 W incident power in the point identified by indexn
. The values along the last axis of the array follow a fortran-like index ordering (first index changing faster) considering a Cartesian coordinate system. It can beNone
(default value) if the magnetic flux density field is not defined -
e_field : numpy ndarray, optional
Nf 🞩 NP 🞩 3 🞩 NN numpy ndarray where Nf is the number of frequency values over which the electric field, expressed in volts per meter, is defined. NP is the number of ports of the device and NN is the number of spatial points over which the electric field is defined.EM_Field.e_field[i,k,j,n]
represents the rms of thej
component of the electric field at the frequency identified by the indexi
, when the portk
is supplied with 1 W incident power in the point identified by indexn
. The values along the last axis of the array follow a fortran-like index ordering (first index changing faster) considering a Cartesian coordinate system. It can beNone
(default value) if the electric field is not defined -
props : dict, optional
additional properties defined over the same NN spatial points over which the electric and magnetic flux density fields are defined. Theprops
dictionary has to be composed in the following way:- A
idxs
key has to be present.props['idxs']
is a NN list or numpy ndarray of integer numbers representing the indeces of the specific points. The indices have to start from zero and no integer numbers have to be skipped from zero to the maximum index value. The values in the array follow a fortran-like index ordering (first index changing faster) considering a Cartesian coordinate system. - Any other key has to refer to a list or numpy ndarray with a legth equal to the maximum index contained in
props['idxs']
plus one. The propertyprops['prop_1'][idx]
is the 'prop_1' property relevant to all the spatial points identified by an index equal to idx inprops['idxs']
Default is {}
- A
Returns
- EM_Field : EM_Field
Example
import numpy as np
from cosimpy import *
n_p = 5 #Number of ports
n_f = 10 #Number of frequency values
nPoints = [3,4,5] #Number of points along the three Cartesian directions
frequencies = np.linspace(50e6, 150e6, n_f, endpoint=False))
bfield = np.random.rand(n_f,n_p,3,np.prod(nPoints)) + 1j*np.random.rand(n_f,n_p,3,np.prod(nPoints))
cond = np.random.uniform(size=np.prod(nPoints))
EM_Field(frequencies, nPoints, b_field=bfield, conductivity=cond)
'''
Out:
"""""""""""""""
EM FIELD
"""""""""""""""
Number of frequency values = 10
Number of ports = 5
Number of points (nx, ny, nz) = 3, 4, 5
E field not defined
'conductivity' additional property defined
'''
Method for returning a printable representation of the EM_Field instance.
Parameters
- self : EM_Field
Returns
- string : string
The string identifies the class of the instance. It reports the number of frequency values, the number of ports and the number of spatial points over which the EM field is defined
Indexing method for EM_Field instances. Indices are interpreted as frequency values.
Parameters
- self : EM_Field
- key : tuple, list, numpy ndarray, int, float, slice
frequency values used to index theself
EM_Field
Returns
- EM_Field : EM_Field
Example
import numpy as np
from cosimpy import *
n_p = 5 #Number of ports
n_f = 10 #Number of frequency values
n_points = [10,10,10] #Number of points
frequencies = np.linspace(50e6, 150e6, n_f, endpoint=False)
e_field = np.random.random(size=(n_f,n_p,3,np.prod(n_points))) + 1j*np.random.random(size=(n_f,n_p,3,np.prod(n_points)))
b_field = None
em_field = EM_Field(frequencies, n_points, b_field, e_field)
em_field.frequencies
'''
Out:
array([5.0e+07, 6.0e+07, 7.0e+07, 8.0e+07, 9.0e+07, 1.0e+08, 1.1e+08,
1.2e+08, 1.3e+08, 1.4e+08])
'''
em_field[90e6:].frequencies
'''
Out:
array([9.0e+07, 1.0e+08, 1.1e+08, 1.2e+08, 1.3e+08, 1.4e+08])
'''
em_field[90e6:130e6].frequencies
'''
Out:
array([9.0e+07, 1.0e+08, 1.1e+08, 1.2e+08])
'''
em_field[95e6].frequencies
'''
Out:
'WARNING: 9.500000e+07 Hz is not contained in the frequencies list. 9.000000e+07 Hz is returned instead'
array([90000000.])
'''
The method returns the self.properties[prop_key]
distribution in the voxels where the EM field is defined.
Parameters
- self : EM_Field
- prop_key : string,
string matching the key inside theself.properties
dictionary and referring to the property that is wanted to be extracted
Returns
- property : numpy ndarray
1D numpy ndarray containing the value of the requested property in each voxel. The values in the array follow a fortran-like index ordering (first index changing faster) considering a Cartesian coordinate system.
Method to be used to add a property to the self.properties
dictionary.
Parameters
- self : EM_Field
- prop_key : string
string identifying the property name - prop_value : list, numpy ndarray
list or numpy ndarray relevant to the property that is wanted to be added to theself.properties
dictionary of the EM_Field instance. The length of the array has to follow the rules specified for theself.properties
dictionary keys (see__init__
method)
Returns
- None
The method can be used to set to NaN the EM field values in the voxels where the properties[""idxs"]
is equal to idx
Parameters
- self : EM_Field
- idx : int
integer value representing the index where the EM field values are wanted to be NaN. Its value refers to the index values declared in theself.properties
dictionary
Returns
- None
If the magnetic flux density field is defined, the method returns the complex B1+ and B1- values.
Parameters
- self : EM_Field
Returns
- sens : numpy ndarray
Nf 🞩 NP 🞩 2 🞩 NN numpy ndarray where Nf is the number of frequency values over which the sensitivity, expressed in tesla, are defined. NP is the number of ports of the device and NN is the number of spatial points over which the sensitivities are defined.sens[i,k,j,n]
represents the B1+ (if j is equal to 0) or B1- (if j is equal to 1) complex value at the frequency identified by the index i, generated by 1 W incident power at port k in the point identified by index n. The values along the last axis of the array follow a fortran-like index ordering (first index changing faster) considering a Cartesian coordinate system
If the electric field is defined, the method returns the power density in W/m3.
Parameters
- self : EM_Field
- elCond_key : string
string of the key in theself.properties
dictionary associated with the electrical conductivity - p_inc : list, numpy ndarray, optional
list or numpy ndarray with a length equal to the number of ports of the device.p_inc[i]
is the power incident to the i-th port considered for the power density computation. Default isNone
Returns
- powDens : numpy ndarray
if p_inc isNone
: Nf 🞩 NP x NN numpy ndarray where Nf is the number of frequency values over which the power density, expressed in W/m3, is defined. NP is the number of ports of the device and NN is the number of spatial points over which the power density is defined.powDens[i,k,n]
represents the power density at the frequency identified by the index i, generated by 1 W incident power at port k with all the other ports closed over their characteristic impedance, in the point identified by index n. The values along the last axis of the array follow a Fortran-like index ordering (first index changing faster) considering a Cartesian coordinate system. If p_inc is notNone
: Nf x NN numpy ndarray where the power density is computed in the NN spatial points at the Nf frequency values when the device is supplied according to the p_inc list passed as argument to the method
If the electric field is defined, the method returns the power, in watt, deposited in the voxels over which the electric field is defined
Parameters
- self : EM_Field
- voxVols : int, float
volume, in m3, of each voxel over which the electric field has been defined - elCond_key : string
string of the key in theself.properties
dictionary associated with the electrical conductivity - p_inc : list, numpy ndarray, optional
list or numpy ndarray with a length equal to the number of ports of the device. p_inc[i] is the power incident to the i-th port considered for the power density computation. Default isNone
Returns
- depPow : numpy ndarray
if p_inc isNone
: Nf 🞩 NP numpy ndarray where Nf is the number of frequency values over which the deposited power, expressed in watt, is defined and NP is the number of ports of the device.depPow[i,k]
represents the deposited power at the frequency identified by the index i, generated by 1 W incident power at port k with all the other ports closed over their characteristic impedance. If p_inc is notNone
: Nf numpy ndarray where the deposited power is computed at the Nf frequency values when the device is supplied according to the p_inc list passed as argument to the method
If the electric field is defined, the method returns the Q matrix referred to the specified spatial point.
PD = V+H Q V+where PD is the power density in the specified spatial point (W/m3), V+ is the vector of the voltages incident to the device ports and H is conjugate transpose.
Parameters
- self : EM_Field
- point : list, numpy ndarray
three element list or numpy ndarray used to specify the spatial position where the Q matrix points - freq : int, float
frequency value at which the Q matrix is evaluated. The value has to be included in theself.frequencies
array - z0_ports list, numpy ndarray, int, float, optional
port impedances in ohm. These can be given as a list or numpy ndarray with length equal to the number of ports of the device. If all the ports share the same impedances, a float or int value can be passed as parameter. The format is compatible with that of the S_Matrix propertyz0
. Default is 50 ohm - elCond_key : string
string of the key in theself.properties
dictionary associated with the electrical conductivity. IfNone
, the Q matrix is reported for unit conductivity. Default isNone
Returns
- q_matrix : numpy ndarray
NP 🞩 NP numpy ndarray, being NP the number of ports of the device, representative of the Q matrix computed in the specified point. If the "elCond" argument isNone
and no "elCond" property is found among the **kwargs of the class, the Q matrix is relevant to 1 S/m electrical conductivity
Method for plotting the magnetic flux density field over a specific slice
Parameters
- self : EM_Field
- prop_key : string,
string matching the key inside theself.properties
dictionary and referring to the property that is wanted to be extracted - plane : string
slice to be plotted. It can take the following values:- 'xy' : axial slice
- 'xz' : coronal slice
- 'yz' : sagittal slice
- sliceIdx : int
index of the slice to be plotted. It can take negative values. In that casesliceIdx = Ns + sliceIdx
where Ns is the total number of slices in the relevant direction - vmin float, optional
minimum value of the chromatic bar. Default isNone
- vmax float, optional
maximum value of the chromatic bar. Default isNone
Returns
- fig : matplotlib.figure.Figure
instance of matplotlib.figure.Figure relevant to the produced plot
Method for plotting the magnetic flux density field over a specific slice
Parameters
- self : EM_Field
- em_field : string
string used to identify the EM field type to be plotted. Accepted values are "e_field" for the electric field and "b_field" for the magentic flux density - comp : string
component of the magnetic flux density field to be plotted. It can take the following values:- 'x' : x-component
- 'y' : y-component
- 'z' : z-component
- 'mag' : magnitude of the magnetic flux density field vector (rms value)
- 'b1+' : magnitude of B1+ (accepted only if
em_field="b_field"
) - 'b1-' : magnitude of B1- (accepted only if
em_field="b_field"
)
- freq : float
frequency, in hertz, of the magnetic flux density field to be plotted - ports : list, numpy ndarray
list or numpy ndarray of the port numbers whose EM field has to be plotted. Each entry, can take values from 1 to NP being NP the number of ports of the device - plane : string
slice to be plotted. It can take the following values:- 'xy' : axial slice
- 'xz' : coronal slice
- 'yz' : sagittal slice
- sliceIdx : int
index of the slice to be plotted. It can take negative values. In that casesliceIdx = Ns + sliceIdx
where Ns is the total number of slices in the relevant direction - vmin float, optional
minimum value (in microtesla ifem_field='b_field'
) of the chromatic bar. Default isNone
- vmax float, optional
maximum value (in microtesla ifem_field='b_field'
) of the chromatic bar. Default isNone
Returns
- fig : matplotlib.figure.Figure
instance of matplotlib.figure.Figure relevant to the produced plot
NOTE: This method is replaced by the plotEMField
method and will be removed in the next versions of CoSimPy
Method for plotting the magnetic flux density field over a specific slice
Parameters
- self : EM_Field
- comp : string
component of the magnetic flux density field, in microtesla, to be plotted. It can take the following values:- 'x' : x-component
- 'y' : y-component
- 'z' : z-component
- 'mag' : magnitude of the magnetic flux density field vector (rms value)
- 'b1+' : magnitude of B1+
- 'b1-' : magnitude of B1-
- freq : float
frequency, in hertz, of the magnetic flux density field to be plotted - port : int
port number which is supplied with 1 W incident power to generate the magnetic flux density field. All the other ports are closed on matched loads. It can take values from 1 to NP being NP the number of ports of the device - plane : string
slice to be plotted. It can take the following values:- 'xy' : axial slice
- 'xz' : coronal slice
- 'yz' : sagittal slice
- sliceIdx : int
index of the slice to be plotted. It can take negative values. In that casesliceIdx = Ns + sliceIdx
where Ns is the total number of slices in the relevant direction - vmin float, optional
minimum value, in microtesla, of the chromatic bar. Default isNone
- vmax float, optional
maximum value, in microtesla, of the chromatic bar. Default isNone
Returns
- None
NOTE: This method is replaced by the plotEMField
method and will be removed in the next versions of CoSimPy
Method for plotting the melectric field over a specific slice
Parameters
- self : EM_Field
- comp : string
component of the electric field, in volts per meter, to be plotted. It can take the following values:- 'x' : x-component
- 'y' : y-component
- 'z' : z-component
- 'mag' : magnitude of the electric field vector (rms value)
- freq : float
frequency, in hertz, of the electric field to be plotted - port : int
port number which is supplied with 1 W incident power to generate the electric field. All the other ports are closed on matched loads. It can take values from 1 to NP being NP the number of ports of the device - plane : string
slice to be plotted. It can take the following values:- 'xy' : axial slice
- 'xz' : coronal slice
- 'yz' : sagittal slice
- sliceIdx : int
index of the slice to be plotted. It can take negative values. In that casesliceIdx = Ns + sliceIdx
where Ns is the total number of slices in the relevant direction - vmin float, optional
minimum value, in volts per meter, of the chromatic bar. Default isNone
- vmax float, optional
maximum value, in volts per meter, of the chromatic bar Default isNone
Returns
- None
Method for generating a .xmf and a .h5 file to be imported in data analysis and visualization applications such as ParaView. In the .h5 files, the electric field and magnetic flux density field are stored as <f>MHz-p<n>-<F>_<type> where <f> is the frequency value in megahertz, <n> is the port number, <F> is 'E' (electric field) or 'B' (magnetic flux density field) and <type> can be 'real' or 'imag' identifying the real or imaginary part of the field phasor. Additional properties, defined along the EM_Field instance, are stored with their full name in the .h5 file.
Parameters
- self : EM_Field
- filename : string
name of the .xmf and .h5 files
Returns
- None
It returns an EM_Field instance where the EM fields are updated according to new incident powers whose magnitude and 'phase' are contained in p_inc
and phase
numpy ndarray
Parameters
- self : EM_Field
- p_incM : numpy ndarray
Nf 🞩 NPout 🞩 NP numpy ndarray where Nf is the number of frequency values over whichself
is defined, NPout is the number of ports of the returned EM_Field instance and NP is the number of ports ofself
. PincM[i,j,k] represents the magnitude of the incident power at the k-port ofself
when the j-port of the output EM_Field is supplied with 1 W incident power at a frequency equal toself.frequencies[i]
- phaseM : numpy ndarray
Nf 🞩 NPout 🞩 NP numpy ndarray where Nf is the number of frequency values over whichself
is defined, NPout is the number of ports of the returned EM_Field instance and NP is the number of ports ofself
. PincM[i,j,k] represents the magnitude of the incident power at the k-port ofself
when the j-port of the output EM_Field is supplied with 1 W incident power at a frequency equal toself.frequencies[i]
Returns
- EM_Field : EM_Field
EM_Field defined over the same frequency values ofself
EM_Field. The returned EM_Field is characterised by NPout different distributions of EM fields corresponding to the number of rows ofp_incM
andphaseM
method parameters
importFields_cst(directory, freqs, nPorts, nPoints=None, Pinc_ref=1, b_multCoeff=1, pkORrms='pk', imp_efield=True, imp_bfield=True, fileType = 'ascii', col_ascii_order = 0, **kwargs)
class method which returns an EM_Field instance importing the data from CST® STUDIO SUITE standard ASCII of HDF5 export files.
Results files must be collected in a dedicated directory and named as <field_str>_<freq_str>_port<n>.txt where <field_str> can be either 'efield', for the electric field result, or 'bfield' for magnetic flux density field results, <freq_str> is a string identifying the frequency value in megahertz (e.g. '128.4') and <n> is the number of the port supplied, in the simulation environment, to generate the relevant EM fields. All the EM quantities must be exported on the same regular grid.
Parameters
- directory : string
string reporting the path of the directory which contains the text files - freqs : list, numpy ndarray
list or numpy ndarray of string values reporting the frequency values as they are indicated in the results filenames (<freq_str>) - nPorts : int
number of ports for which the results are available - nPoints : list, numpy ndarray, optional
list or numpy ndarray with a length equal to three reporting the number of spatial points over which the electric and magnetic flux density fields are defined: [Nx, Ny, Nz]. IfNone
(default value) the method deduces them from the result files at the expenses of a slight longer import process - Pinc_ref : float, optional
magnitude of the power incident at the ports which generate the EM fields. Default is 1 W. - b_multCoeff : float, optional
multiplicative factor for the magnetic results. In case the exported results are magnetic field values (A/m), it has to be equal to 4π10-7 to obtain magnetic flux density values in tesla. Default is 1 - pkORrms : string, optional
if 'pk' the values contained in the result files are interpreted as peak values, if 'rms' they are interpreted as rms values. Default is 'pk' - imp_efield : bool, optional
ifTrue
the method imports the results relevant to the electric field. Default isTrue
- imp_bfield : bool, optional
ifTrue
the method imports the results relevant to the magnetic flux density field. Default default is "ascii" - fileType : string, optional
string identifying the type of files collecting the field data. It can be either "ascii" or "hdf5". Default is "ascii" - col_ascii_order : int, optional
int identifying the column order in the ascii files.
if 0: |x, y, z, <EM>xRe, <EM>yRe, <EM>zRe, <EM>xIm, <EM>yIm, <EM>zIm|
if 1: |x, y, z, <EM>xRe, <EM>xIm, <EM>yRe, <EM>yIm, <EM>zRe, <EM>zIm|
If fileType = "hdf5" this argument is not considered. Default is 0 - **kwargs : list, numpy ndarray, optional
additional properties defined over the same NN spatial points over which the electric and magnetic flux density fields are defined. They can be list or numpy ndarray with a length equal to NN
Returns
- EM_Field : EM_Field
EM_Field obtained from the EM results exported from CST® STUDIO SUITE.EM_Field.properties
is a dictionary based on the**kwargs
parameters passed to the method
importFields_s4l(directory, freqs, nPorts, Pinc_ref=1, b_multCoeff=1, pkORrms='pk', imp_efield=True, imp_bfield=True, **kwargs)
class method which returns an EM_Field instance importing the data from Sim4Life standard export .mat files. Results files must be collected in a dedicated directory and named as <field_str/>_port.mat where <field_str/> can be either 'efield', for the electric field results, or 'bfield' for magnetic flux density field results and is the number of the ports supplied, in the simulation environment, to generate the relevant EM field. All the EM quantities must be exported on the same regular grid.
Parameters
- directory : string
string reporting the path of the directory which contains the .mat files - freqs : list, numpy ndarray
list or numpy ndarray of float values reporting the sorted frequency values at which the results have been exported from Sim4Life - nPorts : int
number of ports for which the results are available - nPoints : list, numpy ndarray, optional
list or numpy ndarray with a length equal to three reporting the number of spatial points over which the electric and magnetic flux density fields are defined: [Nx, Ny, Nz]. IfNone
(default value) the method deduces them from the result files at the expenses of a slight longer import process - Pinc_ref : float, optional
magnitude of the power incident at the ports which generate the EM fields. Default is 1 W. - b_multCoeff : float, optional
multiplicative factor for the magnetic results. In case the exported results are magnetic field values (A/m), it has to be equal to 4π10-7 to obtain magnetic flux density values in tesla. Default is 1 - pkORrms : string, optional
if 'pk' the values contained in the result files are interpreted as peak values, if 'rms' they are interpreted as rms values. Default is 'pk' - imp_efield : bool, optional
ifTrue
the method imports the results relevant to the electric field. Default isTrue
- imp_bfield : bool, optional
ifTrue
the method imports the results relevant to the magnetic flux density field. Default isTrue
- **kwargs : list, numpy ndarray, optional
additional properties defined over the same NN spatial points over which the electric and magnetic flux density fields are defined. They can be list or numpy ndarray with a length equal to NN
Returns
- EM_Field : EM_Field
EM_Field obtained from the EM results exported from Sim4Life.EM_Field.properties
is a dictionary based on the**kwargs
parameters passed to the method
This class represents the link between the S_Matrix and EM_Field classes. An instance of this class represents the simulated device being, its properties, an S_Matrix and an EM_Field instance. The S_Matrix must be defined over all the frequency values at which the EM_Field is defined.
-
RF_Coil.s_matrix
: S_Matrix
S_Matrix instance defined over Nf,s frequency values and with NP ports -
RF_Coil.em_field
: EM_Field
EM_Field instance defined over Nf,em frequency values and with NP ports -
RF_Coil.nPorts
: int
number of ports associated with the RF coil (NP)
It creates an RF_Coil instance.
Parameters
- s_matrix : S_Matrix
S_Matrix instance defined over Nf,s frequency values and with NP ports - em_field : EM_Field, optional
EM_Field instance defined over Nf,em frequency values and with NP ports. Default isNone
Returns
- RF_Coil : RF_Coil
Example
import numpy as np
from cosimpy import *
n_p = 5 #Number of ports
n_f = 10 #Number of frequency values
nPoints = [3,4,5] #Number of points for EM field along the three Cartesian directions
frequencies = np.linspace(50e6, 150e6, n_f, endpoint=False))
s_matrix = S_Matrix(np.random.uniform(size=(n_f,n_p,n_p)), freqs=frequencies)
bfield = np.random.rand(n_f,n_p,3,np.prod(nPoints))
em_field = EM_Field(frequencies, nPoints, b_field=bfield)
'''
Out:
"""""""""""""""
RF COIL
"""""""""""""""
"""""""""""""""
S MATRIX
"""""""""""""""
|V-| = |S||V+|
|5 x 1| = |5 x 5||5 x 1|
Number of frequency values = 10
"""""""""""""""
EM FIELD
"""""""""""""""
Number of frequency values = 10
Number of points = (3, 4, 5)
E field not defined
'''
Method for returning a printable representation of the RF_Coil instance.
Parameters
- self : RF_Coil
Returns
- string : string
The string identifies the class of the instance. It reports information about the S_Matrix and EM_Field properties
The method performs the cascade connection between self
and the S_Matrix instances contained in networks
list.
Parameters
- self : RF_Coil
- networks : list
list with length equal to the number of port ofself.s_matrix
either containingNone
values or S_Matrix instances defined over the same frequency values ofself.s_matrix
. If the n-th element ofnetworks
isNone
, the n-th port ofself
is not connected to any other network in the returned RF_Coil. If the n-th element ofnetworks
is an N-ports S_Matrix, its first port must share the same impedance of the n-th port ofself
and the two ports are connected together. In the returned RF_Coil, the n-th port ofself
is therefore expanded into (N-ports - 1) ports. The example proposed in the image below should clarify the workflow
- comp_Pinc : bool, optional
ifTrue
and theself.em_field
property is notNone
, the method returns an RF_Coil instance whoseem_field
property is updated according to the new connections. If theself.em_field
property isNone
, theem_field
property of the returned RF_Coil isNone
but connection data are stored in the new RF_Coil. IfFalse
(default value), theem_field
property of the returned RF_Coil isNone
and no connection data are stored
Returns
- RF_Coil : RF_Coil
The new RF_Coil resulting form the connection of the originalself
RF_Coil with the S_Matrix instances contained innetworks
. It is worth noting that, if any of the network in thenetworks
list, generates, under the given supplying conditions, an EM field which is not negligible with respect to that generated by the unconnected RF coil, the EM field associated with the returned RF_Coil can be inaccurate
The method performs the cascade connection between self
and other
S_Matrix instance.
Parameters
- self : RF_Coil
- other : S_Matrix
S_Matrix instance defined over the same frequency values ofself.s_matrix
and with a number of ports higher than those ofself
. In the returned RF_Coil, the first NP ports ofother
are connected with the NP ports ofself
and must share the same port impedances - comp_Pinc : bool, optional
ifTrue
and theself.em_field
property is notNone
, the method returns an RF_Coil instance whoseem_field
property is updated according to the new connections. If theself.em_field
property isNone
, theem_field
property of the returned RF_Coil isNone
but connection data are stored in the new RF_Coil. IfFalse
(default value), theem_field
property of the returned RF_Coil isNone
and no connection data are stored
Returns
- RF_Coil : RF_Coil
The new RF_Coil resulting form the connection of the originalself
RF_Coil with the S_Matrixother
. It is worth noting that, if the network, described by the S_Matrixother
, generates, under the given supplying conditions, an EM field which is not negligible with respect to that generated by the unconnected RF coil, the EM field associated with the returned RF_Coil can be inaccurate
The method splits in two parallel ports, all the ports listed in ports.
Parameters
- cls : RF_Coil class
- ports : list, numpy.ndarray
list or numpy.ndarray listing the port numbers (starting from one) that have to be splitted. Each port can be splitted once only, therefore no repetitions are expected inside ports - comp_Pinc : bool, optional
ifTrue
(default value) and theself.em_field
property is notNone
, the method returns an RF_Coil instance whoseem_field
property is updated according to the new connections. If theself.em_field
property isNone
, theem_field
property of the returned RF_Coil isNone
but connection data are stored in the new RF_Coil. IfFalse
, theem_field
property of the returned RF_Coil isNone
and no connection data are stored
Returns
- RF_Coil : RF_Coil RF_Coil instance whose selected ports are splitted in two parallel ports.
The method splits in two serires ports, all the ports listed in ports.
Parameters
- cls : RF_Coil class
- ports : list, numpy.ndarray
list or numpy.ndarray listing the port numbers (starting from one) that have to be splitted. Each port can be splitted once only, therefore no repetitions are expected inside ports - comp_Pinc : bool, optional
ifTrue
(default value) and theself.em_field
property is notNone
, the method returns an RF_Coil instance whoseem_field
property is updated according to the new connections. If theself.em_field
property isNone
, theem_field
property of the returned RF_Coil isNone
but connection data are stored in the new RF_Coil. IfFalse
, theem_field
property of the returned RF_Coil isNone
and no connection data are stored
Returns
- RF_Coil : RF_Coil RF_Coil instance whose selected ports are splitted in two parallel ports.
when the ports of the device are supplied according to the incident powers listed in p_inc, the method computes the power balance comprising: the total reflected power, the power lost in the last connected network, the power deposited by the electric field, the sum of the radiated power and power lost in the unconnected device
Parameters
- p_inc : list, numpy ndarray
list or numpy ndarray with a length equal to the number of ports of the device.
p_inc[i]
is the power incident to the i-th port considered for the power balance computation - voxVols : int, float
volume, in m3, of each voxel over which the electric field has been defined. If
None
, the power deposited by the elctric field is not accounted in the power balance computation. Default isNone
. - elCond_key : string
string of the key in theself.properties
dictionary associated with the electrical conductivity. IfNone
, the power deposited by the elctric field is not accounted in the power balance computation. Default isNone
- printReport : bool
ifTrue
a power balance report is printed when the method is called
Returns
- powBal : dict
dictionary with the following keys:-
P_inc_tot : numpy ndarray
Nf elements numpy ndarray reporting the total incident power at each frequency value. If theem_field
property of theRF_Coil
instance, thee_field
property of the EM_Field instance, thevoxVols
argument or theelCond_key
argument areNone
, Nf is the number of frequency values over which the S_Matrix instance has been defined. Otherwise Nf is the number of frequency values over which the EM_Field instance has been defined -
P_refl : numpy ndarray
Nf elements numpy ndarray reporting the total reflected power at each frequency value. If theem_field
property of theRF_Coil
instance, thee_field
property of the EM_Field instance, thevoxVols
argument or theelCond_key
argument areNone
, Nf is the number of frequency values over which the S_Matrix instance has been defined. Otherwise Nf is the number of frequency values over which the EM_Field instance has been defined -
P_dep : numpy ndarray, optional
Nf elements numpy ndarray reporting the power deposited by the electric field. Nf is the number of frequency values over which the EM_Field instance has been defined. If theem_field
property of theRF_Coil
instance, thee_field
property of the EM_Field instance, thevoxVols
argument or theelCond_key
argument areNone
, this key will not be defined -
P_circ_loss : Nf elements numpy ndarray reporting the power lost in the last connected network at each frequency value. If the
em_field
property of theRF_Coil
instance, thee_field
property of the EM_Field instance, thevoxVols
argument or theelCond_key
argument areNone
, Nf is the number of frequency values over which the S_Matrix instance has been defined. Otherwise Nf is the number of frequency values over which the EM_Field instance has been defined. If the RF_Coil instance has never been connected to any external network or connection data have not been stored, this key will not be defined -
P_other : Nf elements numpy ndarray reporting the power lost due to other type of losses (e.g radiation, power lost in the unconnected device) at each frequency value. P_other is computed as the total (at all ports) incident power subtracted by the other computed power losses. If the
em_field
property of theRF_Coil
instance, thee_field
property of the EM_Field instance, thevoxVols
argument or theelCond_key
argument areNone
, Nf is the number of frequency values over which the S_Matrix instance has been defined. Otherwise Nf is the number of frequency values over which the EM_Field instance has been defined
-
P_inc_tot : numpy ndarray
The method returns an RF_Coil instance where the port pairs of the self
RF_Coil instance, listed in port_pairs
aurgument, are connected together through the S_Matrix instances listed in sMats
.
Parameters
- self : RF_Coil
- port_pairs : list, numpy.ndarray
NPP 🞩 2 list or numpy.ndarray listing the port pairs that have to be connected together through the NPP S_Matrix instances listed insMats
- sMats : list, numpy.ndarray
NPP list or numpy.ndarray listing the S_Matrix instances used to connect together the port pairs listed inport_pairs
. Each S_Matrix instance has to be defined over the same frequency values over which theself.s_matrix
S_Matrix instance is defined - comp_Pinc : bool, optional
ifTrue
(default value) and theself.em_field
property is notNone
, the method returns an RF_Coil instance whoseem_field
property is updated according to the new connections. If theself.em_field
property isNone
, theem_field
property of the returned RF_Coil isNone
but connection data are stored in the new RF_Coil. IfFalse
, theem_field
property of the returned RF_Coil isNone
and no connection data are stored
Returns
- s_load : S_Matrix
S_Matrix instance with a number of ports equal to 2(NP-NPP), where NP is the number of ports ofself
and NPP is the number of port pairs that have to be connected together through the S_Matrix instances listed insMats
.
The method allows to save the RF_Coil instance in a binary format with .cspy extension.
Parameters
- self : RF_Coil
- filename : string
string reporting the path and filename of the binary file containing the data relevant to the RF_Coil instance - description : string, optional
string containing optional information that can be saved as ASCII in the binary file header. Default is ""
Returns
- None
class method that allows to load the RF_Coil instance stored in the binary file generated by the saveRFCoil
method.
Parameters
- cls : RF_Coil class
- filename : string
string reporting the path and filename of the binary .cspy file containing the data relevant to the RF_Coil instance
Returns
- RF_Coil : RF_Coil RF_Coil instance loaded from the .cspy file