Changes

Using Python to Create Functions, Models & Scripts

881 bytes added, 15:51, 25 June 2018
[[Image:Splash-generic2.jpg|right|800px720px]]
<table>
<tr>
<td>[[image:Cube-icon.png | link=Getting_Started_with_EM.CUBECube]] [[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:postatic-ico.png | link=EM.IlluminaFerma]] [[image:staticplanar-ico.png | link=EM.FermaPicasso]] [[image:planarmetal-ico.png | link=EM.PicassoLibera]] [[image:metalpo-ico.png | link=EM.LiberaIllumina]] </td>
<tr>
</table>
[[Image:Back_icon.png|40px30px]] '''[[EM.Cube | Back to EM.Cube Main Page]]'''
== Applications of Python Scripting in EM.Cube ==
* 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 "**", <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]] 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''' button [[File:Functions_icon.png]] button of the Simulate Toolbar or selecting the menu item '''Simulate &rarr; Functions...''' or using the keyboard shortcut {{key|Ctrl+I}}.
<table>
<tr>
<td>
[[Image:Variable6PY MAN2.png|thumb|left|540px720px|EM.Cube's Python functions list.]]
</td>
</tr>
</table>
 
[[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]]'''.
== 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''' button [[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 {{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.
<table>
-----
== What Can You Do with Your Python ModelsFunctions?==
The user defined Python functions can be used just like any standard or library function in many scenarios:
* 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_nameMyFunc_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 a highlighting its row in the table. Click the {{key|Plot}} button of the dialog. A graph of your sweep data is plotted in EM.GridPyPlot.
<table>
<tr>
<td>
[[Image:PY MAN5.png|thumb|left|480px|A graph of "MyFunc(x)" plotted in EM.GridPyPlot.]]
</td>
</tr>
</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 convergesconverge, the optimal value of x and the optimization error are reported in the command window.
<table><tr><td>[[Image:PY MAN6.png|thumb|left|480px|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.]]</td></tr></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 EMPyPlot (file "MyFunc_mc.GridDAT" from the Data Base list). The mean and standard deviation of y are also reported in the command window.
<table>
</table>
<pbr />&nbsp; </phr> [[Image:Top_icon.png|48px30px]] '''[[#Applications of Python Scripting in EM.Cube.27s_Function_Library | Back to the Top of the Page]]'''
[[Image:Back_icon.png|40px30px]] '''[[EM.Cube | Back to EM.Cube Main Page]]'''
4,612
edits