using python scripts with opera - · pdf filepython is a powerful, yet user friendly...

39
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

Upload: vankhanh

Post on 06-Mar-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 2: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Presenter
Presentation Notes
Just an overview on what topics will be covered in todays’ session.
Page 3: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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)

Presenter
Presentation Notes
A multiphysics analysis where different analyses are chained involves evaluating certain quantities from a particular solution, which are passed on to the next analysis as input. In some scenarios, this involves accessing the simulation while it is still running. In order to address this, Python (programming language) was embedded in Opera. Python’s functionality such as advanced string manipulation and numerical analysis components were the key features for it being a viable solution for addressing the requirements of the multiphysics analysis. The integration between Python and Opera has also enabled easy interaction of Opera with other 3rd party scientific programs.
Page 4: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Presenter
Presentation Notes
Python is a general purpose, high level programming language that is used in wide variety of applications. Advantages of Python are: powerful and fast, clear readable syntax, dynamic, runs immediately, support for object oriented programming, less coding required, extensive standard libraries, modular, portable.
Page 5: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Presenter
Presentation Notes
Package is a way of segregating different parts of your program Modules could be no packages, a single package, or more than one package
Page 6: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 7: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

EUGM 2015 Cobham Technical Services Using Python Scripts with Opera

EUGM 2015

6

MEMORY SCOPE

Page 8: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Presenter
Presentation Notes
Each of these is completely separate, meaning changes to variables in one memory are not directly available in another, and should a variable be imported into one memory from another, any changes to such a variable in the new memory will not affect the variable's value in the original memory.
Page 9: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

EUGM 2015 Cobham Technical Services Using Python Scripts with Opera

EUGM 2015

8

RUNNING PYTHON

Page 10: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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'

Page 11: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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.

Presenter
Presentation Notes
A Solver simulation runs through various stages of computation, at each stage it may be necessary to perform specific computations via the Python interface.
Page 12: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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.

Page 13: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

EUGM 2015 Cobham Technical Services Using Python Scripts with Opera

EUGM 2015

12

Solver Hooks

Page 14: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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.

Page 15: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

EUGM 2015 Cobham Technical Services Using Python Scripts with Opera

EUGM 2015

14

EXCHANGING DATA

Page 16: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 17: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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)')

Page 18: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

EUGM 2015 Cobham Technical Services Using Python Scripts with Opera

EUGM 2015

17

OPERAFEA Functions

Page 19: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 20: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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")) '

Page 21: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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)

Page 22: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

EUGM 2015 Cobham Technical Services Using Python Scripts with Opera

EUGM 2015

22

COUPLING TO OTHER PACKAGES

Page 23: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 24: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Presenter
Presentation Notes
The following Python script snippet, shown in figure 2, demonstrates how to prepare FE data to Code_Aster using its native data exchange format MED (HDF based) library. The Python script above demonstrates only how to export FE nodes into MED file format. Similarly other FE mesh data (nodal connectivity, faces, and edges) can be exported using Python interfaces from the MED library. The same interface is used to extract field values back from an MED file after a Code_Aster analysis is performed.
Page 25: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

EUGM 2015 Cobham Technical Services Using Python Scripts with Opera

EUGM 2015

25

OPERA PYTHON PACKAGES

Page 26: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 27: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 28: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 29: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Presenter
Presentation Notes
In the first method, called a "variance test" parameter estimation is performed on collected simulation data Once the required criteria for steady state detection have been satisfied, further sampling is disabled The stopping criterion is based on the minimization of a cost function that measures the data heteroscedasticity, the tendency of the standard deviation of the observed quantity to vary The second method, referred to as the "mean test", is a version of CUSUM algorithm designed to detect the transient time at which the mean of the data settles down to zero. A log-likelihood function for the samples is computed and compared to a threshold value to decide whether the parameters of the underlying probability distribution represent steady or transient state. In transient electromagnetic simulations, the data (e.g. current, voltage) is periodic and the assumption is that the samples follow an arcsine distribution, in which the amplitude and the offset are the control parameters.
Page 30: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

EUGM 2015 Cobham Technical Services Using Python Scripts with Opera

EUGM 2015

30

Example - Usage

Parameters

Saving output points

Page 31: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 32: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 33: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 34: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 35: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 36: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 37: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Page 38: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

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

Presenter
Presentation Notes
A Solver simulation runs through various stages of computation, at each stage it may be necessary to perform specific computations via the Python interface.
Page 39: Using Python Scripts with Opera - · PDF filePython is a powerful, yet user friendly programming language, allowing relatively straightforward access to functionality such as advanced

EUGM 2015 Cobham Technical Services Using Python Scripts with Opera

EUGM 2015

39

THANK YOU