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

From Emagtech Wiki
Jump to: navigation, search
(Applications of Python Scripting in EM.Cube)
(EM.Cube's Function Library)
Line 33: Line 33:
 
== EM.Cube's Function Library ==
 
== 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]] of the Simulate Toolbar or selecting '''Menu > Simulate > Functions...''' or using the keyboard shortcut {{key|Ctrl+I}}.
+
[[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]] of the Simulate Toolbar or selecting the menu item '''Simulate → Functions...''' or using the keyboard shortcut {{key|Ctrl+I}}.
  
 
<table>
 
<table>

Revision as of 04:08, 6 September 2016

Splash-generic2.jpg
Cube-icon.png Cad-ico.png Fdtd-ico.png Prop-ico.png Po-ico.png Static-ico.png Planar-ico.png Metal-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 button Functions icon.png of the Simulate Toolbar or selecting the menu item Simulate → Functions... or using the keyboard shortcut Ctrl+I.

EM.Cube's Python functions list.

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 Models icon.png 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.

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 Models?

EM.Cube models, whether functional, tabular, Python or HDMR, 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 curves or surfaces in Curve Generator and Surface Generator, respectively
  • 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: "Mode_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 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:

  • Parametric Sweep
  • Optimization
  • HDMR Sweep

As mentioned earlier, 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 simulation engines. In this process, successive evaluations of the model are made for all samples of the sweep variable(s). The results are written to an ASCII data file with a .DAT file extension that bears the same name as your model. To run a parametric sweep of a model, first select it from the Model dialog's list and then click the Sweep button of this dialog. This opens up the Parametric Sweep Settings Dialog, just as you will see in a regular parametric sweep in any of EM.Cube's computational modules. Follow the same typical sweep procedure and define your sweep variables from the list of available independent variables, which in this case must be the arguments of your model. At the end of the sweep simulation, open the Data Manager to view or plot the output data file.

Info icon.png Click here to learn more about Running Parametric Sweep Simulations.

In a similar way, you can perform an optimization on your models. In this case you have to define a objective that includes the name of your model (as an output parameter) 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 Optimize button of this dialog. This opens up the Optimization Settings Dialog, just as you will see in a regular optimization in any of EM.Cube's computational modules. In this dialog, you need to define your optimization variables from the list of available independent variables, which in this case must be the arguments of your model. 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.

Info icon.png Click here to learn more about Running Optimization Simulations.

You can also generate an HDMR model from the numerical evaluations of any of your models. To run a HDMR sweep of a model, first select it from the Model dialog's list and then click the HDMR button of this dialog. This opens up the HDMR Settings Dialog, just as you will see in a regular HDMR sweep in most of EM.Cube's computational modules. In this way you can create a new compact HDMR model for your model of a different type.

Info icon.png Click here to learn more about Running HDMR Sweep Simulations.

Attention icon.png 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.

 

Top icon.png Back to the Top of the Page

Back icon.png Back to EM.Cube Main Page