Changes

Using Python to Create Functions, Models & Scripts

620 bytes removed, 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 '''Menu > 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><tr><td>[[Image:PY MAN1.png|thumb|left|720px|EM.Cube's models dialog.]]</td></tr></table>
The easiest way of defining a Python function is using the "def" statement. The contents of the blank file are shown below:
-----
== What Can You Do with Your ModelsPython Functions?==
[[EM.Cube]] models, whether functional, tabular, The user defined Python or HDMR, functions can be used just like any standard or library function in many scenarios:
* For defining a dependent variable in Variables variables dialog
* For building expressions that can replace the numeric values of object properties
* For defining custom curves or surfaces geometric objects in Curve Generator and Surface Generator, respectivelycurve generator or surface generator
* For defining custom waveforms for [[EM.Tempo]]'s excitation sources
* For creating new data in Data Generatordata generator* For performing mathematical operations on data in Data Managerdata manager's spreadsheets
In all of the above applications, you instantiate a model using a similar syntax as a regular standard function: "Mode_NameModel_Name(x,y,…)", where x, y, … are the model's arguments.
Besides the above applications, you can also access models directly from the Models Dialog. In this dialog, you can select any model and use the {{key|Edit}} button to modify the definition or attributes of the selected model. You can use the Windows '''Notepad''' to open the model file and view its contents. You can also perform three important operations with any model in [[EM.Cube]]using Python models such as:
* Parametric Sweep
* Optimization
* HDMR SweepMonte Carlo Simulation
As mentioned earlierFor single-variable Python functions, all the arguments of a model are added automatically to the Variables List, and can be used as regular independent variables. You can run a parametric sweep of a model in the same way you run a parametric sweep using any of [[EM.Cube]]'s modes dialog provides an easy way of performing a sweep, optimization of Monte Carlo simulation engines. In this processThree buttons labeled '''Sweep''', successive evaluations of the model '''Optimize''' and '''Monte Carlo''' are made intended for all samples this purpose. In the case of sweep, you must enter '''Min''' and '''Max''' values for the sweep variable(s). The results are written to an ASCII data file with a as well as '''.DATNumber of Samples''' file extension that bears . Select and highlight the same name as your model. To run a parametric sweep of a model, first select it your Python function from the Model dialog's list and then click the {{key|'''Sweep}} ''' button of this dialogto run the sweep. This opens up At the Parametric Sweep Settings Dialogcompletion of the sweep, just as you will see in a regular parametric the sweep data are saved in any of the file "MyFunc_sweep.DAT". Click the <b>Data Manager</b> [[EMImage:fdtd_datamanagerb.Cubepng]]'s computational modules. Follow the same typical sweep procedure and define your sweep variables from the list button of available independent variablesSimulate Toolbar, which in this case must be or use the arguments of your model. At the end of the sweep simulationkeyboard shortcut {{key|Ctrl+D}}, open or select the menu item '''Simulate &rarr; Data Manager''' to view or plot . Select the output data file"MyFunc_sweep. [[Image:Info_iconDAT" from the list by clicking on its name and highlighting its row in the table.png|40px]] Click here to learn more about '''[[Parametric_Modeling,_Sweep_%26_Optimization#Running_Parametric_Sweep_Simulations_in_EM.Cube the {{key| Running Parametric Sweep Simulations]]'''Plot}} button of the dialog. A graph of your sweep data is plotted in PyPlot.
In a similar way, you can perform an optimization on your models<table><tr><td>[[Image:PY MAN4. In this case you have to define png|thumb|left|720px|Running a objective that includes the name sweep of your model "MyFunc(as an output parameterx) or any expression of it. You define your objectives in the '''Objectives Dialog'''. To run a optimization of a model, first select it " from the Model dialog's list and then click the {{key|Optimize}} button of this models dialog. This opens up the Optimization Settings Dialog, just as you will see in a regular optimization in any of ]]</td></tr><tr><td>[[EMImage:PY MAN5.Cube]]'s computational modules. In this dialog, you need to define your optimization variables from the list png|thumb|left|480px|A graph of available independent variables, which "MyFunc(x)" plotted in this case must be the arguments of your modelPyPlot. Note that your model can have several arguments and you can run a multivariable optimization. At the end of the optimization process, assuming that the optimizer algorithm converges, the current value of the optimization variable in the Variables List will be updated with its computed optimal value. ]]</td></tr></table>
[[Image:Info_iconIn 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.png|40px]] Click here to learn more about Select and highlight the name of your Python function from the list and click the '''[[Parametric_Modeling,_Sweep_%26_Optimization#Optimization | Running Optimization Simulations]]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.
You can also generate an HDMR model from <table><tr><td>[[Image:PY MAN6.png|thumb|left|480px|The command window showing the numerical evaluations results of any optimizing "MyFunc(x)" with a target value of your models0. To run a HDMR sweep 5 over the interval [0, 10]. The optimal value is reported to be x = 0.415083.]]</td></tr></table> In the case of a modelMonte Carlo simulation, first select it from you must enter values for '''Mean''' and '''Std Dev''' of the Model dialogsweep variable as well as 's ''Number of Samples'''. Select and highlight the name of your Python function from the list and then click the {{key|HDMR}} '''Monte Carlo''' button of this dialogto run the simulation. This opens up Through this process, the HDMR Settings Dialogprobability density function (PDF) of your observable y = func_name(x) is estimated, just and its graph is plotted as you will see in a regular HDMR sweep function of y in most 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><tr><td>[[EMImage:PY MAN7.Cube]]'s computational modules. In this way you can create a new compact HDMR model for your model png|thumb|left|480px|A graph of a different typethe estimated probability density function of "MyFunc(x)" with &mu; = 0 and &sigma; = 1. ]]</td></tr></table>
[[Image:Info_icon.png|40px]] Click here to learn more about '''[[Running_HDMR_Simulations_in_EM.Cube | Running HDMR Sweep Simulations]]'''.<br />
{{Note|Running an optimization of your structure using one of [[EM.Cube]]'s simulation engines can be a very time consuming and memory-intensive task. Instead, you may consider developing a model for a certain response or quantity of interest in your structure as a function of one or more structure parameters and then run an optimization of that model far more efficiently.}}<hr>
<p>&nbsp;</p>[[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