using python scripts with opera - · pdf filepython is a powerful, yet user friendly...
TRANSCRIPT
FEA ANALYSIS General-purpose multi-physics design and analysis software for a wide range of applications
INTEROPERABILITY Built-in circuit modelling and interfaces to leading CAD packages
SERVICE Technical support, training and consultancy services available for software usage and applications
OPTIMIZER Automatically selects and manages multiple goal-seeking algorithms
Using Python Scripts with Opera Nikhil Sawant EUGM, June 2015, Windsor
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
1
Overview
• Why Python?
• Introduction to Python
• What Packages are included?
• Memory Scope
• Running Python in Opera
• Exchanging Data between Opera and Python
• Coupling to other Packages or Software
• Opera Python Packages
• Document Resources
• Summary
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
2
Why Python?
• Chaining Different analyses – Multiphysics Simulation • Extended scripting capabilities
– Vector and matrix arithmetic – Array handling – Signal processing – Complex arithmetic
• Link to numerous in-built modules
– Links to 3rd party applications e.g. OpenFOAM, SALOME, Abaqus – Advanced plotting – HDF5 libraries – Reporting (PDF, HTML, Latex)
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
3
Introduction to Python
• Python is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced string manipulation and numerical analysis components. • Terminology Let’s take an example of a football computer game.
– Class: refers to the actual written piece of code which is used to define behaviour of the class
Classes of Football game
Player Ball
Field Weather
Referee Scoreboard
Audience
Player name
number location (x.y)
run jump
kickBall
Class name Member data Member functions
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
4
Terminology
– Object: refers to an actual instance of a class.
• Every object must belong to a class.
– Modules: program unit which allows other program units to access variables,
derived type definitions, classes and procedures declared within it
– Packages: namespace that organizes a set of related classes and interfaces; it contains its own variables and subroutines
Paul:Player Name= “Paul Lee”
Number= “5” location= “3,5”
run jump
kickBall
Class name Member data Member functions
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
5
What’s Included
• Python 3.4.3 interpreter • NumPy and SciPy packages
• Opera Python packages:
– Operafea – OperaDatabaseIndex – OperaDatabaseExtraction – OperaDatabaseUpdate – OperaLossCalculation – OperaSteadyStateDetection
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
6
MEMORY SCOPE
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
7
Three Distinct Memories
• COMMAND language variable memory – Accessible from comi interface – Initialised by $CONSTANT
• Python memory – Accessible from Python interpreter – Initialised in Python
• Opera memory – Accessible from both Opera and Python (copying) – Created through Opera interface
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
8
RUNNING PYTHON
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
9
Within COMMAND Language
• $PYTHON command – serves as a mediator between Opera and an embedded Python interpreter and – allows the execution of Python statements and Python script files
• Example syntax for executing Python statement
$PYTHON COMMAND=‘myvar=2’
$PYTHON COMMAND='myvar=2\nprint('myvar=',myvar)‘
• Example syntax for executing Python files
$PYTHON FILE='my_script_file.py‘
$PYTHON FILE='my_script_file.py' ARGS='arg1 arg2 arg3'
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
10
Within Solvers
• Solver Hooks • Executes Python code during an Opera analysis stage.
• Following are some scenarios in which solver hooks can be used:
– Monitoring field values during a solution, for example the maximum temperature in
a transient thermal solution;
– Averaging losses over multiple time-steps of a transient EM solution to feed a static thermal problem; or
– Monitoring field values during a solution to determine when the system under investigation begins to behave in a time periodic (steady state) manner.
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
11
Solver Hooks
• Ways to attach Python functions to hooks:
– solver.comi: a command file driven initialization to the solution process;
– control.comi: a command file driven process control for transient simulations; or
– from within other Python functions which can be added to any of the solver hooks. • It is necessary to use one of the command file options to attach a first Python function
to one of the hooks as an initialization to the process of using Python within the solvers.
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
12
Solver Hooks
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
13
Solver Hooks - Example
/Import the Python file my_hooks.py $PYTHON 'import my_hooks as mh' / Register function onSimuStart to the on_simu_start hook $PYTHON 'operafea.registerHookCallback('on_simu_start', mh.onSimuStart )‘ The Python function onSimuStart then adds additional Python functions to other hooks.
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
14
EXCHANGING DATA
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
15
OPERAFEA module
• Provides an interface to software internals and allows users to easily extend Opera functionality when needed • Allows access to database and comi variables from the Python
interpreter • Automatically included at the console command prompt, however it is
recommended to always import in any modules for which it is required import operafea
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
16
OPERAFEA module
import operafea
volt=operafea.getSysVar('w1_rv')+operafea.getSysVar('w1_iv')*1j curr=operafea.getSysVar('w1_ri')+operafea.getSysVar('w1_ii')*1j imp=volt/curr operafea.addConstantUsrVar('#zr',str(imp.real),'Impedance (Real)') operafea.addConstantUsrVar('#zi',str(imp.imag),'Impedance (Imag)')
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
17
OPERAFEA Functions
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
18
OperaObject Class
• Purpose is to be a data container/mediator, carrying data between Opera and the embedded Python interpreter • Opera populates OperaObject instances (for example from buffer
generation commands), which can be immediately exposed to Python when requested • Equally, OperaObject class instances can be instantiated and populated
from Python and then can be used as graphing buffers or as data containers for further processing
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
19
Example – Opera to Python
WINDING OPTION=CALCULATE SET=0 PHASES=3 SLOTS=36 POLES=6 HARMONIC=16 $PYTHON COMMAND='windingObject = operafea.lastCreatedObject()‘ $PYTHON COMMAND='print(windingObject.getValue(“HARMONIC_ORDERS”)) '
$PYTHON COMMAND='print(windingObject.getValue(“WINDING_FACTORS")) '
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
20
Example – Python to Opera
# create numpy arrays containing random numbers size = 100 myArray1=numpy.random.ranf(size) myArray2=numpy.random.ranf(size) myArray3=numpy.random.ranf(size) myArray4=numpy.random.ranf(size) # create new object newob = operafea.newObject() # append values to the object newob.addNumPyDblArray("name1", myArray1) newob.addNumPyDblArray("name2", myArray2) newob.addNumPyDblArray("name3", myArray3) newob.addNumPyDblArray("name4", myArray4) # add this object as graphing buffer operafea.addAsBuffer("buffer_from_py", newob)
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
22
COUPLING TO OTHER PACKAGES
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
23
Coupling to other Packages
• In order to extend the functionality, any new Python package needs to be installed preferably (but not necessarily) into the Opera Python distribution location and the new functionality can be accessible immediately via a Python “import package_name” statement • For example, a package like matplotlib can provide functionality to
perform 3D plots where the data can be supplied dynamically, for example during solver solution. • More advanced Python usage can be, for example, chaining Opera
computations with other FEA solvers to perform acoustic, fatigue, fracture, dynamic vibration and other required analysis for any particular application
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
24
Chaining Opera with other FEA solvers
• Opera to EDF Code_Aster1
1EDF, “Code_Aster”, http://www.code-aster.org
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
25
OPERA PYTHON PACKAGES
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
26
Opera Database Extraction
• Extracts data from a given simulation
• Store a subset
• Allows computations during the solving stage
• Uses hooks to specify when in the solve process data are to be extracted
• Uses Opera Database Index package
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
27
Example – Usage
$IF %COMPARE(&TIMESTEP_STAGE&,INIT)==0 / initialize multiIndex $PYTHON COMMAND=import operafea $PYTHON COMMAND=import storeModSolver as sm / set custom index keys if required $PYTHON COMMAND=sm.multiIndex.setIndexKeys(["LABEL_1","LABEL_2"]) $PYTHON COMMAND=sm.dataStore.setIndexKeys(["LABEL_1","LABEL_2"]) / set custom component list $PYTHON COMMAND=sm.dataStore.setComponentList(["RBX","RBY","RBZ"]) / attach functions to appropriate python hooks $PYTHON COMMAND=operafea.registerHookCallback( 'on_store_time_output',sm.onStoreTimeOutput) $END IF
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
28
Opera Steady State Detection
• Determines steady state in transient simulations
• Includes sequential analysis package
• Solves hypothesis-testing problems in which the sample size is not
fixed a priori
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
29
Opera Steady State Detection Module
• “Variance test” method
– Parameter estimation1
• “Mean test” method
– Version of CUSUM algorithm2
1 Killick, R., Fearnhead, P., Eckley, I.A., “Optimal detection of change points with a linear computational cost”, Journal of the American Statistical Association 107(500) 1590-1598, 2012 2 P. Granjon, “The CUSUM algorithm - a small review”, https://hal.archives-ouvertes.fr/hal-00914697 , 2014
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
30
Example - Usage
Parameters
Saving output points
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
31
Loss Calculation
• Calculates losses over a steady state cycle and subsequently uploads such losses to an Opera database • Evaluates losses based on current density (J) and material conductivity (σ) and iron
losses • Includes different iron loss calculation methods
– Two term Steinmetz – Bertotti-Steinmetz – Steinmetz with quadratic exponent as a function of flux density
• Uses functionality from the Opera steady state detection package
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
32
Example - Usage
Loss ‘method’ (averaged over a cycle)
Volume label set in model
Type of Steinmetz formulation
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
33
Document Resources
• Python help
– Detailed information on any of the classes and functions provided by modules provided with the embedded Python in Opera can be using “help”
$PYTHON COMMAND=help(operafea) • A full description of the implementation of Python in Opera is given in the
Opera-3d/2d Reference Manual
– Chapter “Python and Opera” • Examples in user guide describe the use and extension of the distributed
Python modules
– Chapter 11 Application Notes -> Supplied Python Functionality
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
34
Radiation Screen
• Extract Field Data During the Simulation • extract the components of the magnetic field
– at the nodes of the test volume, – calculate their mean and maximum values – and output the results at the end of each output time
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
35
Radiation Screen
• Data at each output time written to a text file • Also, field values saved back into the database
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
36
Single Phase Inductor
• Coupled Transient Electromagnetic / Static Thermal
• Transient simulation run until steady-state operation detected;
• Then averaged results are produced
– for iron loss in the yoke, – copper loss in the winding and – eddy current loss in a conducting plate
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
37
Single Phase Inductor
Monitored current for Steady State detection Time Averaged Eddy current losses
Temperature distribution
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
38
Summary
• Python and terminology used
• Embedded Python in Opera and its contents
• Operafea – Opera’s Python interface for exchanging data
• Different ways of using Python in Opera
• Extending Opera’s functionality by coupling to other software or packages
• Highlight of the Python functionality supplied with Opera
EUGM 2015 Cobham Technical Services Using Python Scripts with Opera
EUGM 2015
39
THANK YOU