Difference between revisions of "Using Python to Create Functions, Models & Scripts"

From Emagtech Wiki
Jump to: navigation, search
 
(22 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Image:Splash-generic2.jpg|right|800px]]
+
[[Image:Splash-generic2.jpg|right|720px]]
 
<table>
 
<table>
 
<tr>
 
<tr>
<td>[[image:Cube-icon.png | link=Getting_Started_with_EM.CUBE]] [[image:cad-ico.png | link=CubeCAD]] [[image:fdtd-ico.png | link=EM.Tempo]] [[image:prop-ico.png | link=EM.Terrano]] [[image:po-ico.png | link=EM.Illumina]] [[image:static-ico.png | link=EM.Ferma]] [[image:planar-ico.png | link=EM.Picasso]] [[image:metal-ico.png | link=EM.Libera]] </td>
+
<td>[[image:Cube-icon.png | link=Getting_Started_with_EM.Cube]] [[image:cad-ico.png | link=Building Geometrical Constructions in CubeCAD]] [[image:fdtd-ico.png | link=EM.Tempo]] [[image:prop-ico.png | link=EM.Terrano]] [[image:static-ico.png | link=EM.Ferma]] [[image:planar-ico.png | link=EM.Picasso]] [[image:metal-ico.png | link=EM.Libera]] [[image:po-ico.png | link=EM.Illumina]] </td>
 
<tr>
 
<tr>
 
</table>
 
</table>
[[Image:Back_icon.png|40px]] '''[[EM.Cube | Back to EM.Cube Main Page]]'''
+
[[Image:Back_icon.png|30px]] '''[[EM.Cube | Back to EM.Cube Main Page]]'''
 
== Applications of Python Scripting in EM.Cube ==
 
== Applications of Python Scripting in EM.Cube ==
  
Line 29: Line 29:
 
* Defining custom temporal excitation waveforms in [[EM.Tempo]]
 
* Defining custom temporal excitation waveforms in [[EM.Tempo]]
  
Mathematical expressions are indeed text strings made up of reserved names like variable or standard output parameters in combination with operators and functions. You can use the basic arithmetic operators: addition "+", subtraction "-", multiplication "*", division "/", as well as the right and left parentheses "(" and ")" as delimiters. <b>For raising powers, you can either use the power function pow(x,y) or simply use the symbol "**", <i>e.g.</i> "x**y"</b>. The Pi number &pi; = 3.1415926... is denoted by "pi" as a reserved name, and letter "e" stands for the Euler number e = 2.7182818... or the base of natural logarithm.
+
Mathematical expressions are indeed text strings made up of reserved names like variable or standard output parameters in combination with operators and functions. You can use the basic arithmetic operators: addition "+", subtraction "-", multiplication "*", division "/", as well as the right and left parentheses "(" and ")" as delimiters. For raising powers, you can either use the power function pow(x,y) or simply use the symbol "**", <i>e.g.</i> "x**y". The Pi number &pi; = 3.1415926... is denoted by "pi" as a reserved name, and letter "e" stands for the Euler number e = 2.7182818... or the base of natural logarithm.
  
 
== EM.Cube's Function Library ==
 
== EM.Cube's Function Library ==
Line 43: Line 43:
 
</table>
 
</table>
  
The table below gives a list of all the currently available library functions in [[EM.Cube]]:
+
[[Image:Info_icon.png|30px]] Click here to learn more about '''[[Glossary of EM.Cube's Python Functions#Standard Python Functions | Python's Standard & Advanced Mathematical Functions]]'''.
 
+
{| class="wikitable"
+
!scope="col"| Function Name / Syntax
+
!scope="col"| Description
+
!scope="col"| Notes
+
|-
+
| sign(x)
+
| Sign function
+
| 1 if x>0, -1 if x<0
+
|-
+
| fac(x)
+
| Factorial
+
| For integer values of x: n! = n(n-1)(n-2)...3.2.1
+
|-
+
| fac2(x)
+
| Double factorial
+
| For even integer values of x: n!! = n(n-2)(n-4)...4.2, for odd integer values of x: n!! = n(n-2)(n-4)...5.3
+
|-
+
| ceiling(x)
+
| Ceiling function
+
| Nearest integer >= x
+
|-
+
| floor(x)
+
| Floor function
+
| Nearest integer <= x
+
|-
+
| power(x,y)
+
| Power function
+
| x^y
+
|-
+
| sqrt(x)
+
| Square root function
+
| x^(1/2)
+
|-
+
| exp(x)
+
| Exponential function
+
| e^x
+
|-
+
| log10(x)
+
| Base 10 logarithm function
+
| ln(x)/ln(10)
+
|-
+
| db(x)
+
| Decibel function
+
| 10*log10(x)
+
|-
+
| ln(x)
+
| Natural logarithm function
+
| -
+
|-
+
| log(x,a)
+
| Base a logarithm function
+
| ln(x)/ln(a)
+
|-
+
| rms(x)
+
| Root mean square function
+
| -
+
|-
+
| abs(x)
+
| Absolute value function
+
| x if x>0, -x if x<0
+
|-
+
| sin(x)
+
| Sine function
+
| -
+
|-
+
| cos(x)
+
| Cosine function
+
| -
+
|-
+
| tan(x)
+
| Tangent function
+
| -
+
|-
+
| sec(x)
+
| Secant function
+
| -
+
|-
+
| csc(x)
+
| Cosecant function
+
| -
+
|-
+
| cot(x)
+
| Cotangent function
+
| -
+
|-
+
| asin(x)
+
| Inverse sine function
+
| -
+
|-
+
| acos(x)
+
| Inverse cosine function
+
| -
+
|-
+
| atan(x)
+
| Inverse tangent function
+
| -
+
|-
+
| atan2(x,y)
+
| Inverse tangent function
+
| -
+
|-
+
| asec(x)
+
| Inverse secant function
+
| -
+
|-
+
| acsc(x)
+
| Inverse cosecant function
+
| -
+
|-
+
| acot(x)
+
| Inverse cotangent function
+
| -
+
|-
+
| sinh(x)
+
| Hyperbolic sine function
+
| -
+
|-
+
| cosh(x)
+
| Hyperbolic cosine function
+
| -
+
|-
+
| tanh(x)
+
| Hyperbolic tangent function
+
| -
+
|-
+
| sech(x)
+
| Hyperbolic secant function
+
| -
+
|-
+
| csch(x)
+
| Hyperbolic cosecant function
+
| -
+
|-
+
| coth(x)
+
| Hyperbolic cotangent function
+
| -
+
|-
+
| asinh(x)
+
| Inverse hyperbolic sine function
+
| -
+
|-
+
| acosh(x)
+
| Inverse hyperbolic cosine function
+
| -
+
|-
+
| atanh(x)
+
| Inverse hyperbolic tangent function
+
| -
+
|-
+
| asech(x)
+
| Inverse hyperbolic secant function
+
| -
+
|-
+
| acsch(x)
+
| Inverse hyperbolic cosecant function
+
| -
+
|-
+
| acoth(x)
+
| Inverse hyperbolic cotangent function
+
| -
+
|-
+
| Ci(x)
+
| Cosine integral function
+
| See [https://en.wikipedia.org/wiki/Trigonometric_integral | Trigonometric Integral on Wikipedia.]
+
|-
+
| Si(x)
+
| Sine integral function
+
| See [https://en.wikipedia.org/wiki/Trigonometric_integral | Trigonometric Integral on Wikipedia.]
+
|-
+
| C(x)
+
| Cosine Fresnel integral function
+
| See [https://en.wikipedia.org/wiki/Fresnel_integral | Fresnel Integral on Wikipedia.]
+
|-
+
| S(x)
+
| Sine Fresnel integral function
+
| See [https://en.wikipedia.org/wiki/Fresnel_integral: Fresnel Integral on Wikipedia.]
+
| -
+
|-
+
| Ellip_E(x)
+
| Elliptic function of the first kind
+
| See [https://en.wikipedia.org/wiki/Elliptic_integral: Elliptic Integral on Wikipedia.]
+
|-
+
| Ellip_K(x)
+
| Elliptic function of the second kind
+
| See [https://en.wikipedia.org/wiki/Elliptic_integral: Elliptic Integral on Wikipedia.]
+
| -
+
|-
+
| Ei(x)
+
| Exponential integral function
+
| See [https://en.wikipedia.org/wiki/Exponential_integral: Exponential Integral on Wikipedia.]
+
|-
+
| En(n,x)
+
| Generalized exponential integral function of order n
+
| See [https://en.wikipedia.org/wiki/Exponential_integral: Exponential Integral on Wikipedia.]
+
|-
+
| gauss(x)
+
| Gaussian function
+
| -
+
|-
+
| erf(x)
+
| Error function
+
| See [https://en.wikipedia.org/wiki/Error_function: Error Function on Wikipedia.]
+
|-
+
| gamma(x)
+
| Gamma function
+
| See [https://en.wikipedia.org/wiki/Gamma_function: Gamma Function on Wikipedia.]
+
|-
+
| Airy_A(x)
+
| Airy function of the first kind
+
| See [https://en.wikipedia.org/wiki/Airy_function: Airy Function on Wikipedia.]
+
|-
+
| Airy_B(x)
+
| Airy function of the second kind
+
| See [https://en.wikipedia.org/wiki/Airy_function: Airy Function on Wikipedia.]
+
|-
+
| Jn(n,x)
+
| Bessel function of the first kind and order n
+
| See [https://en.wikipedia.org/wiki/Bessel_function: Bessel Function on Wikipedia.]
+
|-
+
| Yn(n,x)
+
| Bessel function of the second kind and order n
+
| See [https://en.wikipedia.org/wiki/Bessel_function: Bessel Function on Wikipedia.]
+
|-
+
| In(n,x)
+
| Modified Bessel function of the first kind and order n
+
| See [https://en.wikipedia.org/wiki/Bessel_function: Bessel Function on Wikipedia.]
+
|-
+
| Kn(n,x)
+
| Modified Bessel function of the second kind and order n
+
| See [https://en.wikipedia.org/wiki/Bessel_function: Bessel Function on Wikipedia.]
+
|-
+
| SB_jn(n,x)
+
| Spherical Bessel function of the first kind and order n
+
| See [https://en.wikipedia.org/wiki/Bessel_function: Bessel Function on Wikipedia.]
+
|-
+
| SB_yn(n,x)
+
| Spherical Bessel function of the second kind and order n
+
| See [https://en.wikipedia.org/wiki/Bessel_function: Bessel Function on Wikipedia.]
+
|-
+
| SB_in(n,x)
+
| Modified spherical Bessel function of the first kind and order n
+
| See [https://en.wikipedia.org/wiki/Bessel_function: Bessel Function on Wikipedia.]
+
|-
+
| SB_kn(n,x)
+
| Modified spherical Bessel function of the second kind and order n
+
| See [https://en.wikipedia.org/wiki/Bessel_function: Bessel Function on Wikipedia.]
+
|-
+
| Pn(n,x)
+
| Legendre function of the first kind and order n
+
| See [https://en.wikipedia.org/wiki/Legendre_polynomial: Legendre Polynomial on Wikipedia.]
+
|-
+
| Qn(n,x)
+
| Legendre function of the second kind and order n
+
| See [https://en.wikipedia.org/wiki/Legendre_polynomial: Legendre Polynomial on Wikipedia.]
+
|-
+
| Tn(n,x)
+
| Chebyshev polynomial of the first kind and order n
+
| See [https://en.wikipedia.org/wiki/Chebyshev_polynomials: Chebyshev Polynomial on Wikipedia.]
+
|-
+
| Un(n,x)
+
| Chebyshev polynomial of the second kind and order n
+
| See [https://en.wikipedia.org/wiki/Chebyshev_polynomials: Chebyshev Polynomials on Wikipedia.]
+
|-
+
| LGn(n,x)
+
| Laguerre polynomial of order n
+
| See [https://en.wikipedia.org/wiki/Laguerre_polynomials: Laguerre Polynomials on Wikipedia.]
+
|-
+
| Hn(n,x)
+
| Hermite polynomial of order n
+
| See [https://en.wikipedia.org/wiki/Hermite_polynomials: Hermite Polynomials on Wikipedia.]
+
|-
+
| Un(n,x)
+
| Hermite-Gauss function of order n
+
| -
+
|-
+
| Math_ce(n,r,x)
+
| Even periodic (cosine) Mathieu function of order n
+
| See [https://en.wikipedia.org/wiki/Mathieu_function: Mathieu Function on Wikipedia.]
+
|-
+
| Math_se(n,r,x)
+
| Odd periodic (sine) Mathieu function of order n
+
| See [https://en.wikipedia.org/wiki/Mathieu_function: Mathieu Function on Wikipedia.]
+
|-
+
| Rect(x)
+
| Rectangle function
+
| 1 if |x|&le;0.5, 0 elsewhere
+
|-
+
| Tri(x)
+
| Triangle function
+
| 1 if |1-x|&le;1, 0 elsewhere
+
|-
+
| Spln2(x)
+
| Quadratic spline function
+
| -
+
|-
+
| Spln3(x)
+
| Cubic spline function
+
| -
+
|-
+
| Step(x)
+
| Step function
+
| 1 if x>0, 0 if x<0
+
|-
+
| Haar(x)
+
| Haar function
+
| sign(x) if |x|<0.5, 0 elsewhere
+
|-
+
| Ramp(x)
+
| Ramp function
+
| -
+
|-
+
| Trapz(a,x)
+
| Trapezoidal function
+
| -
+
|-
+
| Sqwv(x)
+
| Square wave function
+
| -
+
|-
+
| Stwv(x)
+
| Sawtooth wave function
+
| -
+
|-
+
| Trwv(x)
+
| Triangle wave function
+
| -
+
|-
+
| Plstr(d,x)
+
| Pulse train function with duty cycle d
+
| -
+
|-
+
| Sinc(d,x)
+
| Sinc function
+
| sin(pi*x)/(pi*x)
+
|-
+
| Fejer(n,x)
+
| Fejer function of order n
+
| -
+
|-
+
| Rand(x)
+
| Random function
+
| -
+
|-
+
| Fltr_B(n,x)
+
| Butterworth filter function of order n
+
| -
+
|-
+
| Fltr_CH1(n,e,x)
+
| Chebyshev filter function of the first kind, order n
+
| e: ripple factor e
+
|-
+
| Fltr_CH2(n,e,x)
+
| Chebyshev filter function of the second kind, order n
+
| e: ripple factor e
+
|-
+
| Fltr_E(n,s,e,x)
+
| Elliptic filter function of order n
+
| s: selectivity factor s, ripple factor e, n = 0, 1, 2, 3, 4, 6, 8, 9, 12
+
|-
+
| Z0_MS(e,x)
+
| Characteristic impedance of a microstrip transmission line
+
| x: width-to-height ratio, e: substrate permittivity
+
|-
+
| w_MS(z,e,t)
+
| Width of a microstrip transmission line 
+
| z: characteristic impedance, e: substrate permittivity, t: substrate thickness
+
|-
+
| eeff_MS(e,x)
+
| Effective permittivity of a microstrip transmission line
+
| x: width-to-height ratio, e: substrate permittivity
+
|-
+
| woh_MS(e,z)
+
| Width-to-height ratio of a microstrip transmission line
+
| z: characteristic impedance z, e: substrate permittivity
+
|-
+
| Z0_CPW(e,x,y)
+
| Characteristic impedance of a coplanar waveguide transmission line
+
| x: slot width-to-height ratio, y: center strip width-to-height ratio, e: substrate permittivity
+
|-
+
| eeff_CPW(e,x,y)
+
| Effective permittivity of a coplanar waveguide transmission line 
+
| x: slot width-to-height ratio, y: center strip width-to-height ratio, e: substrate permittivity
+
|-
+
| horn_a(d,x,y)
+
| Wavelength-normalized a-dimension of an optimal pyramidal horn antenna 
+
| d: directivity d, x and y: wavelength-normalized waveguide dimensions
+
|-
+
| horn_b(d,x,y)
+
| Wavelength-normalized b-dimension of an optimal pyramidal horn antenna 
+
| d: directivity d, x and y: wavelength-normalized waveguide dimensions
+
|-
+
| horn_l(d,x,y)
+
| Wavelength-normalized length of an optimal pyramidal horn antenna 
+
| d: directivity d, x and y: wavelength-normalized waveguide dimensions
+
|}
+
  
 
== Creating Custom Python Functions ==
 
== Creating Custom Python Functions ==
Line 445: Line 49:
 
Besides the standard mathematical functions and a large set of library functions, [[EM.Cube]] allows you to create custom Python functions of your own. If you are an avid Python programmer, you already know how to write Python function scripts and import the corresponding module using [[EM.Cube]]'s Python command line.  
 
Besides the standard mathematical functions and a large set of library functions, [[EM.Cube]] allows you to create custom Python functions of your own. If you are an avid Python programmer, you already know how to write Python function scripts and import the corresponding module using [[EM.Cube]]'s Python command line.  
  
For those new to Python programming, [[EM.Cube]] offers a blank Python file, which you can open and modify as a starting point. [[EM.Cube]] provides a list of all the preloaded Python scripts along with your own "user-defined" Python functions in its "Models Dialog". Open [[EM.Cube]]'s Models Dialog by clicking the '''Models''' button [[File:Models_icon.png]] of the Simulate Toolbar or selecting the menu item '''Simulate &rarr; Models...''' or using the keyboard shortcut {{key|Ctrl+L}}. Locate the Python file called "blank.PY" in the list, select and highlight it and click the {{key|Open}} button of the dialog. The Python file is opened using the text editor you have associated with ".PY" files in Windows. For example, Notepad++ is a powerful and popular choice.  
+
For those new to Python programming, [[EM.Cube]] offers a blank Python file, which you can open and modify as a starting point. [[EM.Cube]] provides a list of all the preloaded Python scripts along with your own "user-defined" Python functions in its "Models Dialog". Open [[EM.Cube]]'s Models Dialog by clicking the '''Models''' [[File:Models_icon.png]] button of the Simulate Toolbar or selecting the menu item '''Simulate &rarr; Models...''' or using the keyboard shortcut {{key|Ctrl+L}}. Locate the Python file called "blank.PY" in the list, select and highlight it and click the '''Open''' button of the dialog. The Python file is opened using the text editor you have associated with ".PY" files in Windows. For example, Notepad++ is a powerful and popular choice.  
  
 
<table>
 
<table>
Line 511: Line 115:
 
-----
 
-----
  
== What Can You Do with Your Python Models?==
+
== What Can You Do with Your Python Functions?==
  
 
The user defined Python functions can be used just like any standard or library function in many scenarios:
 
The user defined Python functions can be used just like any standard or library function in many scenarios:
Line 530: Line 134:
 
* Monte Carlo Simulation
 
* Monte Carlo Simulation
  
For single-variable Python functions, [[EM.Cube]]'s modes dialog provides an easy way of performing a sweep, optimization of Monte Carlo simulation. Three buttons labeled {{key|Sweep}}, {{key|Optimize}} and {{key|Monte Carlo}} are intended for this purpose. In the case of sweep, you must enter '''Min''' and '''Max''' values for the sweep variable as well as '''Number of Samples'''. Select and highlight the name of your Python function from the list and click the {{key|Sweep}} button to run the sweep. At the completion of the sweep, the sweep data are saved in the file "func_name.DAT", and a graph of your sweep data is plotted in EM.Grid.   
+
For single-variable Python functions, [[EM.Cube]]'s modes dialog provides an easy way of performing a sweep, optimization of Monte Carlo simulation. Three buttons labeled '''Sweep''', '''Optimize''' and '''Monte Carlo''' are intended for this purpose. In the case of sweep, you must enter '''Min''' and '''Max''' values for the sweep variable as well as '''Number of Samples'''. Select and highlight the name of your Python function from the list and click the '''Sweep''' button to run the sweep. At the completion of the sweep, the sweep data are saved in the file "MyFunc_sweep.DAT". Click the <b>Data Manager</b> [[Image:fdtd_datamanagerb.png]] button of Simulate Toolbar, or use the keyboard shortcut {{key|Ctrl+D}}, or select the menu item '''Simulate &rarr; Data Manager'''. Select the file "MyFunc_sweep.DAT" from the list by clicking on its name and highlighting its row in the table. Click  the {{key|Plot}} button of the dialog. A graph of your sweep data is plotted in PyPlot.   
  
 
<table>
 
<table>
Line 540: Line 144:
 
<tr>
 
<tr>
 
<td>
 
<td>
[[Image:PY MAN5.png|thumb|left|480px|A graph of "MyFunc(x)" plotted in EM.Grid.]]
+
[[Image:PY MAN5.png|thumb|left|480px|A graph of "MyFunc(x)" plotted in PyPlot.]]
 
</td>
 
</td>
 
</tr>
 
</tr>
 
</table>
 
</table>
  
In the case of optimization, you must enter '''Min''' and '''Max''' values for the optimization variable as well as a '''Target''' value for your Python function. Select and highlight the name of your Python function from the list and click the {{key|Optimize}} button to run the optimization. An objective function of the form e(x) = |func_name(x) - Target| is constructed and minimized. After the optimization algorithms converge, the optimal value of x and the optimization error are reported in the command window.  
+
In the case of optimization, you must enter '''Min''' and '''Max''' values for the optimization variable as well as a '''Target''' value for your Python function. Select and highlight the name of your Python function from the list and click the '''Optimize''' button to run the optimization. An objective function of the form e(x) = |func_name(x) - Target| is constructed and minimized. After the optimization algorithms converge, the optimal value of x and the optimization error are reported in the command window.  
  
 
<table>
 
<table>
Line 555: Line 159:
 
</table>
 
</table>
  
In the case of Monte Carlo simulation, you must enter values for '''Mean''' and '''Std Dev''' of the sweep variable as well as '''Number of Samples'''. Select and highlight the name of your Python function from the list and click the {{key|Monte Carlo}} button to run the simulation. Through this process, the probability density function (PDF) of your observable y = func_name(x) is estimated, and its graph is plotted as a function of y in EM.Grid. The mean and standard deviation of y are also reported in the command window.  
+
In the case of Monte Carlo simulation, you must enter values for '''Mean''' and '''Std Dev''' of the sweep variable as well as '''Number of Samples'''. Select and highlight the name of your Python function from the list and click the '''Monte Carlo''' button to run the simulation. Through this process, the probability density function (PDF) of your observable y = func_name(x) is estimated, and its graph is plotted as a function of y in PyPlot (file "MyFunc_mc.DAT" from the Data Base list). The mean and standard deviation of y are also reported in the command window.  
  
 
<table>
 
<table>
Line 565: Line 169:
 
</table>
 
</table>
  
<p>&nbsp;</p>
+
<br />
[[Image:Top_icon.png|48px]] '''[[#Applications of Python Scripting in EM.Cube | Back to the Top of the Page]]'''
+
 
 +
<hr>
 +
 
 +
[[Image:Top_icon.png|30px]] '''[[#Applications of Python Scripting in EM.Cube | Back to the Top of the Page]]'''
  
[[Image:Back_icon.png|40px]] '''[[EM.Cube | Back to EM.Cube Main Page]]'''
+
[[Image:Back_icon.png|30px]] '''[[EM.Cube | Back to EM.Cube Main Page]]'''

Latest revision as of 15:51, 25 June 2018

Splash-generic2.jpg
Cube-icon.png Cad-ico.png Fdtd-ico.png Prop-ico.png Static-ico.png Planar-ico.png Metal-ico.png Po-ico.png

Back icon.png Back to EM.Cube Main Page

Applications of Python Scripting in EM.Cube

Python is a very powerful and versatile programming language. It can easily be used to create simple functions and scripts. You can use Python in EM.Cube in a variety of ways:

  • As an online calculator through EM.Cube's command line
  • To generate simple mathematical expressions or sophisticated functions involving conditional statements and recursive loops
  • To export the geometry of the whole physical structure or parts of it to a Python script and import it later in other projects
  • To create wizards that readily reproduce reusable structures
  • To generate computationally simpler surrogate models based on full-wave simulation data
  • To write scripts for controlling the simulation flow

Working with Mathematical Expressions

Mathematical expressions are widely used throughout EM.Cube for a variety of purposes:

  • Defining dependent variables based on existing independent variables
  • Defining custom output parameters based on standard output parameters
  • Defining design objectives based on standard or custom output parameters
  • Performing operation on and modifying existing data files
  • Generating new data files from the ground up
  • Defining new custom geometric objects using CubeCAD's curve generator and surface generator
  • Defining custom temporal excitation waveforms in EM.Tempo

Mathematical expressions are indeed text strings made up of reserved names like variable or standard output parameters in combination with operators and functions. You can use the basic arithmetic operators: addition "+", subtraction "-", multiplication "*", division "/", as well as the right and left parentheses "(" and ")" as delimiters. For raising powers, you can either use the power function pow(x,y) or simply use the symbol "**", e.g. "x**y". The Pi number π = 3.1415926... is denoted by "pi" as a reserved name, and letter "e" stands for the Euler number e = 2.7182818... or the base of natural logarithm.

EM.Cube's Function Library

EM.Cube provides an extensive library of standard and advanced mathematical functions as well as some special functions related to signal processing and RF design. You can see a list of this functions by clicking the Functions Functions icon.png button of the Simulate Toolbar or selecting the menu item Simulate → Functions... or using the keyboard shortcut Ctrl+I.

EM.Cube's Python functions list.

Info icon.png Click here to learn more about Python's Standard & Advanced Mathematical Functions.

Creating Custom Python Functions

Besides the standard mathematical functions and a large set of library functions, EM.Cube allows you to create custom Python functions of your own. If you are an avid Python programmer, you already know how to write Python function scripts and import the corresponding module using EM.Cube's Python command line.

For those new to Python programming, EM.Cube offers a blank Python file, which you can open and modify as a starting point. EM.Cube provides a list of all the preloaded Python scripts along with your own "user-defined" Python functions in its "Models Dialog". Open EM.Cube's Models Dialog by clicking the Models Models icon.png button of the Simulate Toolbar or selecting the menu item Simulate → Models... or using the keyboard shortcut Ctrl+L. Locate the Python file called "blank.PY" in the list, select and highlight it and click the Open button of the dialog. The Python file is opened using the text editor you have associated with ".PY" files in Windows. For example, Notepad++ is a powerful and popular choice.

EM.Cube's models dialog.

The easiest way of defining a Python function is using the "def" statement. The contents of the blank file are shown below:




import sys

from math import *

from emag import *

import numpy as np

import scipy.special as sp


def func_name(x):

        #Write your python code here


        return x




It is recommended that you choose the same name for both your Python function and your Python file. First, save the opened blank file as a new Python file with your new name using Windows' "Save As..." operation. Then, replace "func_name" in the above script with your new name. By default, a single argument "x" is assumed. You can have any arbitrary number of arguments using a comma-separated list. Keep in mind that indentation is very important in Python. You can use either spaces or tabs to create indentation. But you cannot mix them together in the same script. Enter the body of your function in the space provided with the proper indentation. Make sure you return a value for your function.

As an example, the script below shows a new Python function called "MyFunc" save in the Python file "MyFunc.PY":




import sys

from math import *

from emag import *

import numpy as np

import scipy.special as sp


def MyFunc(x):

        #Write your python code here

        return cos(x)-x



What Can You Do with Your Python Functions?

The user defined Python functions can be used just like any standard or library function in many scenarios:

  • For defining a dependent variable in variables dialog
  • For building expressions that can replace the numeric values of object properties
  • For defining custom geometric objects in curve generator or surface generator
  • For defining custom waveforms for EM.Tempo's excitation sources
  • For creating new data in data generator
  • For performing mathematical operations on data in data manager's spreadsheets

In all of the above applications, you instantiate a model using a similar syntax as a regular standard function: "Model_Name(x,y,…)", where x, y, … are the model's arguments.

Besides the above applications, you can also perform important operations using Python models such as:

  • Parametric Sweep
  • Optimization
  • Monte Carlo Simulation

For single-variable Python functions, EM.Cube's modes dialog provides an easy way of performing a sweep, optimization of Monte Carlo simulation. Three buttons labeled Sweep, Optimize and Monte Carlo are intended for this purpose. In the case of sweep, you must enter Min and Max values for the sweep variable as well as Number of Samples. Select and highlight the name of your Python function from the list and click the Sweep button to run the sweep. At the completion of the sweep, the sweep data are saved in the file "MyFunc_sweep.DAT". Click the Data Manager Fdtd datamanagerb.png button of Simulate Toolbar, or use the keyboard shortcut Ctrl+D, or select the menu item Simulate → Data Manager. Select the file "MyFunc_sweep.DAT" from the list by clicking on its name and highlighting its row in the table. Click the Plot button of the dialog. A graph of your sweep data is plotted in PyPlot.

Running a sweep of "MyFunc(x)" from the models dialog.
A graph of "MyFunc(x)" plotted in PyPlot.

In the case of optimization, you must enter Min and Max values for the optimization variable as well as a Target value for your Python function. Select and highlight the name of your Python function from the list and click the Optimize button to run the optimization. An objective function of the form e(x) = |func_name(x) - Target| is constructed and minimized. After the optimization algorithms converge, the optimal value of x and the optimization error are reported in the command window.

The command window showing the results of optimizing "MyFunc(x)" with a target value of 0.5 over the interval [0, 10]. The optimal value is reported to be x = 0.415083.

In the case of Monte Carlo simulation, you must enter values for Mean and Std Dev of the sweep variable as well as Number of Samples. Select and highlight the name of your Python function from the list and click the Monte Carlo button to run the simulation. Through this process, the probability density function (PDF) of your observable y = func_name(x) is estimated, and its graph is plotted as a function of y in PyPlot (file "MyFunc_mc.DAT" from the Data Base list). The mean and standard deviation of y are also reported in the command window.

A graph of the estimated probability density function of "MyFunc(x)" with μ = 0 and σ = 1.



Top icon.png Back to the Top of the Page

Back icon.png Back to EM.Cube Main Page