You are on page 1of 36

Copyright February 2010

FLUX

Excel driving Flux


Users Guide
FLUX 10 Table of Content
Table of Content
1. Guide Overview .............................................................................................................1
2. The Excel Interface........................................................................................................2
3. Using Excel to drive Flux..............................................................................................4
4. Start the Solving Process...............................................................................................6
5. The Function initVBAFlux ...........................................................................................7
6. The Function StartVBAFlux ........................................................................................8
6.1. Initializing Flux ............................................................................................................9
6.2. Geometry Input...........................................................................................................12
6.3. Modifying Radius R ..................................................................................................13
6.4. Meshing .....................................................................................................................14
6.5. Defining Physics ........................................................................................................15
6.6. Modifying the Current I ............................................................................................16
6.7. Solving .......................................................................................................................17
6.8. Analysis of solved problem........................................................................................18
6.9. Closing the Project, Closing Flux ..............................................................................19
6.10. Analyzing Results.....................................................................................................20
7. The function closeVBAFlux .......................................................................................21
8. Attachments ................................................................................................................22
8.1. Python geometry input : geometry.py .................................................................23
8.2. Python meshing : meshing.py ..............................................................................24
8.3. Python physical properties : buildphys.py ...........................................................25
8.4. Python solving : solving.py .................................................................................26
8.5. Python postprocessing : postprocessing.py ..........................................................27
8.6. VBA code in the excel file ........................................................................................28
FLUX
10
Excel driving Flux 1
1. Guide Overview
Goal The goal of this document is to explain and demonstrate how to drive Flux
applications using Excel 2003. A basic example is included to support this
goal.
Example
Summary
A copper wire infinite in length carries a current. The radius of the wire is R,
the magnitude of the current is I.
In our example, we will vary two parameters :
The R value, radius of the wire (between 0 and 10 mm).
The I value, magnitude of the current carried by the copper wire.
The goal is:
To measure the magnitude of the flux density B along a 25 mm long
path extending from the center of the copper wire.
Note on Theory The theoretical study of this example is easy to conduct using a close form
solution.
The flux density is computed in a point M at a distance r from the center of
the copper wire.
The next two equations give the close form solution for the flux density:
On the left when the computation point is located inside the copper
wire,
On the right when the computation point is outside the copper wire
2
0
R
I r
B R r
r
I
B R r
2
0
FLUX 10
Excel driving Flux 2
2. The Excel Interface
Excel Interface The screen shot below shows the Excel spreadsheet used as an interface.(2a)
2a
Parameter
variables
The value contained in cell B5 and B6 can be changed:
B5 : radius of the copper wire : R (mm)
B6 : current carried by the wire : I (A)
Solve
With
Flux
When cell B5 and B6 are set, start Flux solving by selecting the menu item
SOLVE WITH FLUX .
Selecting SOLVE WITH FLUX runs a visual basic macro.
The elements and actions executed by this macro are described in the next
section.
FLUX 10
Excel driving Flux 3
Solving Process Once the solving process is started, cell A13 to A18 are updated in real time
giving a constant feedback on the activity of Flux and on the progression of
the process.
The coloring of the cells, yellow in our example, indicates the function active
in the Flux process:
Geometry input: cell A14.
Meshing: cell A15.
Physical properties: cell A16.
Solving: cell A17.
Post processing: cell A18.
Post processing When the solving ends, the post processing updates the information in the
table with the current results.
In our example, the values contained in cells E5 to E30 are updated by the
values computed during the post processing. These cells always show the
results of the last solving process completed..
The plot associated to the table of results is automatically updated by Excel
with the current results.
Flux10
4 Excel driving Flux
3. Using Excel to drive Flux
Introduction Excel supports the development of VBA (Visual Basic for Applications) based
macros.
Flux supports Python based scripting, single commands or files.
To drive Flux with Excel, python scripting of Flux, single commands or files,
will be executed from VBA based macros in Excel.
For the process to work correctly, the VBA macro from Excel must be able to
locate the associated python scripting files. We will adopt a naming convention
and chose a location for the files.
This next section presents the strategy for driving Flux from Excel. It describes
the chosen location for all the necessary scripting files.
Location of the
files
The VBA macro file and scripting files must be stored as follows:
The main directory EXCEL_FLUX (any name not including extended
characters.*
Holds the Excel file EXCEL_FLUX.xls (any name not including
extended characters) of our spreadsheet and the directory w (set name).
The directory w is where all the python scripting files needed for the
execution our processes are stored. The current example is developed using
5 different python files, all called by the Excel VBA macro. The name of
the python file can be any name not including extended characters.
*Note: when using FLUX, paths leading to the file being executed may not
contain extended character, and cannot contain any spaces. This restriction
stands when using Excel to drive Flux (see: Flux Users Guide vol. 1, sec4.1.1.
pg.76).
FLUX 10
Excel driving Flux 5
Driving Flux
From Excel
Steps followed when driving Flux from Excel, in the current example:
Description of
the Steps
In our example, selecting the menu item SOLVE WITH FLUX starts a
macro. The following ten steps are then executed:
1. Initialize Flux, open connection to Flux,
2. Input problems geometry,
3. Read back value in cell B5 to modify value of R in geometry,
4. Mesh domain
5. Define physical properties for the problem, domain and regions,
6. Read back value in cell B6 to modify value of I,
7. Solve problem,
8. Compute results,
9. Close project file, close Flux, import result into Excel,
10. Process results in Excel and display variation curves.
Flux10
6 Excel driving Flux
4. Start the Solving Process
VBA code Selecting the menu item SOLVE WITH FLUX starts the following VBA
macro
Code of the macro :
Filling the Cells The first steps accomplished by the macro associated to the menu item
SOLVE WITH FLUX are:
- Color cell A13 yellow.
- Color cells A14 A18 white.
initVBAFlux Once the cells are colored, the macro starts the function initVBAFlux.
initVBAFlux sets the environment variables and loads the required dll. The
environment variables are needed for the programs to find all necessary files
and code.
StartVBAFlux The next function to run enables the driving of Flux from an Excel
spreadsheet. StartVBAFlux will also execute all of the steps described in the
previous sections.
closeVBAFlux The final function, closeVBAFlux ,will close Flux freeing the memory and
unloading all of the dll called for during initialization.
FLUX 10
Excel driving Flux 7
5. The function initVBAFlux
Introduction The function initVBAFlux initializes all the environment variable necessary
for Flux to run. It also loads all of the dll needed for the running of the
application
VBA Code The next section details the code included in initVBA:
Location of
Flux
The first step is to locate the directory where Flux was installed.
This is done searching in the HKEY_LOCAL_MACHINE section of the
registry for the location where the executable of the active version of Flux
was installed. At the time of updating this document, the current version is
10.3.
To proceed, we set the location where the address of the program we are
looking for is located (key_name), and the name of the value we are trying to
recover.
The value we are looking for is Flux_10.3 (subkey_name) located in the
directory SOFTWARE/Cedrat/INSTALLPATH (key_name) of the
HKEY_LOCAL_MACHINE section of the register.
The function GetRegKeyValue returns into the variable initVBAFlux the
location where the active version of Flux is installed.
*Reminder: The registry can be accessed from the command regedit. Type
regedit in the run command of Windows.
Loading the dll Once the location of the program is known, the dll needed to drive Flux
from Excel are loaded. In our example, we load resiflux.dll and
flux_mp.dll.
Flux10
8 Excel driving Flux
6. The function StartVBAFlux
Introduction When the function InitVBAFlux is completed, the function StartVBAFlux
starts.
This function runs the Python files and commands developed to drive Flux
from an Excel Spreadsheet
VBA Code The code describing this function is too long to be shown as a whole. It will
be explained in segments in the following sections.
The sections will be:
- 6.1. Initializing Flux
- 6.2. Entering problems geometry
- 6.3. Modifying radius R
- 6.4. Meshing
- 6.5. Setting physical properties
- 6.6. Modifying current I
- 6.7. Solving
- 6.8. Analyzing solved problem
- 6.9. Closing files, closing Flux
- 6.10. Analyzing results
FLUX 10
Excel driving Flux 9
6.1. Initializing Flux
Introduction After completing the first function and setting InitVBAFlux, other
initialization functions are required to insure a proper performance to pilot
Flux
This section spells out the necessary steps to take before running the python
files. The first py. file described is the file pertaining to the geometry input,
geometry.py.
Path to the
Project
The project files will be created in the directory w where all of the Python
scripting files used to create the new project are stored.
Note that the directory w is located in the same directory as the Excel
spreadsheet used to drive Flux.
The first step is to set variable holding the location where the project will be
saved.
The following instructions set the path to the project being created:
Saving Excel
File
The second step saves the Excel spreadsheet being used. Saving the file is
required to register the modification performed on cells B5 and B6.
The following instructions save the Excel spreadsheet:
Decimal
Separator
The decimal separator used by Excel (and the Microsoft programs more
generally) depends on the native language support functions and the set
language for Windows.
The French speaking users have selected , as decimal separator, the English
speaking users selected . as the decimal separator.
To make the application usable by everyone, we must identify the type of
decimal separator used by the person running the Excel spreadsheet.
The following code identifies the separator used:
Flux10
10 Excel driving Flux
License
Initialization
The third step sets and initializes the Flux license server (serverUid). This
action requires loading a specific file and choosing a working mode for the
license.
The following code sets and initializes the license server:
The file used to set the license server is multiphys.wfg. The release mode
(RELEASEMODE) is used for initialization.
Setting up
Memory
Requirements
This step sets up and reserves the memory necessary to run the application. It
also sets the language used for the program,
The following instructions set the memory and language:
In our current example, we will use a numerical memory of 600 MB
(NUMERICAL_MEMORY_LABEL). The English language is set by default
(LANGUAGE_LABEL).
FLUX 10
Excel driving Flux 11
Connection to
License Server
The last step of the initialization is the connection to the license server.
The following code connects to the local license server of Flux:
Note that the user must choose the version of Flux she/he wants to run. Four
choices are available with version 10.3:
Choice Description
FLUX2D_10.3_32
2D problem solved on a 32 bit
computer
FLUX2D_10.3_64
2D problem solved on a 64 bit
computer
FLUX3D_10.3_32
3D problem solved on a 32 bit
computer
FLUX3D_10.3_64
3D problem solved on a 64 bit
computer
The user selects the location where the project will be created. In the
current example, the project will be created at the location contained in the
character string Path. This will be in the directory w located in the
same level in the directory tree than the Excel spreadsheet.
Flux10
12 Excel driving Flux
6.2. Geometry Input
Introduction The geometry is created from a python file run from the VBA code associated
to the Excel spreadsheet.
VBA Code The following code creates the geometry by running the Python script file
geometry.py:
Filling Cell A14 Before the script geometry.py starts, the cell A14 is colored yellow. This
indicates to the user that the geometry input is running.
Python Code The detail of the code for the python script file geometry.py is in the
attachment section of this document.
This code includes :
Creating a new project in Flux,
Defining a geometric parameter R with a default value of 0.5 mm,
Entering points in the domain,
Entering lines connecting the points,
Building faces,
Saving project as: testcase_ini.FLU.
FLUX 10
Excel driving Flux 13
6.3. Modifying Radius R
Introduction The radius R was entered as a geometric parameter.
This geometric parameter is modified through a python command run from
the VBA code associated to the Excel spreadsheet.
Python
Command
The following python command modifies the value of parameter R setting it
to 1:
VBA Code The following code builds and calls the python command modifying the
parameter R:
VBA Code The code creates a character string Test set with the python command to
run.
Note that the value in cell B5 is read and converted in character string.
In this character string, the function Replace() replaces , (decimal mark in
French) by authorizing . as the decimal separator used by Flux as the
decimal point ..
The next to last line in the code executes the python command contained in
the character string test, modifying the value of the geometric parameter
R.
Flux10
14 Excel driving Flux
6.4. Meshing
Introduction The geometry is now modified and ready to be meshed.
The Python script file meshing.py holds the instructions to perform the
meshing.
VBA Code The following VBA code handles the meshing:
Coloring Cells
A14 and A15
Cell A14 is colored black and white and cell A15 is colored in yellow before
the file meshing.py is called. These changes of color indicate the
progression of the process in the Excel spreadsheet, from the completion of
the MAKE GEOMETRY geometry to the start of the MESHING.
Python Code The detail of the code in the python script file meshing.py is in the
Attachment section of this document.
This code includes:
Defining the mesh weights,
Assigning the mesh weights to the geometric points,
Meshing the faces,
Saving the file as: meshed_geom.FLU .
FLUX 10
Excel driving Flux 15
6.5. Defining Physics
Introduction The geometry being meshed, the Python script file buildphys.py is run to
define the physics of the problem.
VBA Code The following VBA code handles the physics:
Coloring Cells
A15 and A16
Cell A15 is colored black and white and cell A16 is colored in yellow before
the file buildphysics.py is called. These changes of color indicate the
progression of the process in the Excel spreadsheet, from the completion of
the MESHING to the start of the BUILD PHYSICS process.
Python Code The detail of the code in the python script file buildphys.py is in the
attachment section of this document.
This code includes:
Setting the Application, Magnetostatics, and the solver being called,
Solver3D,
Creating an I/O parameter with a value 10 (Amp.) by default,
Creating the surface regions
Associating surface regions to geometric,
Saving file as: physbuilt.FLU .
Flux10
16 Excel driving Flux
6.6. Modifying the Current I
Introduction The value of the current I is stored in an I/O parameter.
The value of this parameter is modified through a python command run from
the VBA code associated to the Excel spreadsheet.
Python
Command
The following python command modifies the value of parameter I setting it to
5:
VBA Code The following code builds and calls the python command modifying the
parameter I:
VBA Code The code creates a character string Test set with the python command to
run.
Note that the value in cell B6 is read and converted in character string.
In this character string, the function Replace() replaces , (decimal mark in
French) by . as the decimal separator used by Flux is the decimal point ..
The next to last line in the code executes the python command contained in
the character string test, modifying the value of the I/O parameter I.
FLUX 10
Excel driving Flux 17
6.7. Solving
Introduction The physics being completed, the Python script file solving.py is run to
solve the problem.
The problem is solved for the value of the radius R in stored in cell B5 and
the value of the current I stored in cell B6.
VBA Code The following VBA code handles the solving:
Coloring Cells
A16 and A17
Cell A16 is colored black and white and cell A17 is colored in yellow before
the file solving.py is called. These color changes indicate the progression
of the process in the Excel spreadsheet, transitioning from the completion of
the BUILD PHYSICS to the start of the SOLVING process.
Python Code The detail of the code in the python script file solving.py is in the
Attachment section of this document.
This code includes:
Solving of the problem for the set values of the parameters R and I,
Saving solved problem file as: solved.FLU .
Flux10
18 Excel driving Flux
6.8. Analysis of solved problem
Introduction The solving being completed, the Python script file postprocessing.py is
run to analyze the problem.
The magnetic flux density is computed along a path 25 mm long, starting at
the center of the copper wire.
VBA Code The following VBA code handles the post processing:
Coloring Cells
A17 and A18
Cell A17 is colored black and white and cell A18 is colored in yellow before
the file postprocessing.py is called. These changes of color indicate the
progression of the process in the Excel spreadsheet, from the completion of
SOLVING to the start of the POST PROCESSING.
Python Code The detail of the code in the python script file postprocessing.py is in the
attachment section of this document.
This code includes:
Creating a path 25 mm long starting at the center of the copper wire
with 25 subdivisions (1 subdivision every 1 mm),
Plotting the curve of variation of the magnetic induction versus
position along the existing path,
Exporting the computed values into the file Results.txt,
Saving the project as postprocessed.FLU.
FLUX 10
Excel driving Flux 19
6.9. Closing the Project, Closing Flux
Introduction With postprocessing now completed, the Flux project and the program Flux
can be closed.
VBA Code The following VBA code handles the closing of fields and programs :
Coloring Cells
A18 and A13
Cells A17 and A 13 are colored black and white. These changes of color
indicate the progression of the process in the Excel spreadsheet, from the
completion of the POSTPROCESSING to the end of processing.
Flux10
20 Excel driving Flux
6.10. Analyzing Results
Introduction Flux having been closed, we can recover the results computed and stored
when running postprocessing.py
VBA Code This function recovers the values stored in the file Results.txt and
distributes them into the cells E5 to E30 of the Excel spreadsheet. These
results are displayed either with a comma or a point as the decimal separator.
The following code spells the post processing functions:
Update of the
result table
When the function is completed, the cells E5 to E30 are updated.
The plot based on these values is also updated with the newly imported
values.
FLUX 10
Excel driving Flux 21
7. The function closeVBAFlux
Introduction The function closeVBAFlux closes the Flux environment and unloads all the
dll loaded at the start of the macro.
VBA Code The following code describes closeVBAFlux:
Flux10
22 Excel driving Flux
8. Attachments
Introduction This section holds all of the scripts and macros used to drive Flux from Excel
in our example.
Scripts and
Macros
developed
All of the script and macro files are printed in the following subsections:
- 8.1. Python geometry input geometry.py
- 8.2. Python meshing meshing.py
- 8.3. Python physical properties buildphys.py
- 8.4. Python solving solving.py
- 8.5. Python postprocessing postprocessing.py
- 8.6. VBA code in the excel file
FLUX 10
Excel driving Flux 23
8.1. Python File for geometry input: geometry.py
Flux10
24 Excel driving Flux
8.2. Python File for meshing: meshing.py
FLUX 10
Excel driving Flux 25
8.3. Python File for physical properties: buildphys.py
Flux10
26 Excel driving Flux
8.4. Python file for solving: solving.py
FLUX 10
Excel driving Flux 27
8.5. Python File for post processing: postprocessing.py
Flux10
28 Excel driving Flux
8.6. VBA Code in the Excel File
buttonStartFlux_Click()
Private Sub buttonStartFlux_Click()
Range("A13").Interior.ColorIndex = 6
Range("A14:A18").Interior.ColorIndex = 0
initVBAFlux
StartVBAFlux
closeVBAFlux
End Sub
initVBAFlux()
Function initVBAFlux()
Dim key_name As String
Dim subkey_name As String
key_name = "SOFTWARE\Cedrat\INSTALLPATH"
subkey_name = "Flux_10.3"
initVBAFlux = GetRegKeyValue(HKEY_LOCAL_MACHINE, key_name, subkey_name)
lib1 = LoadLibrary(initVBAFlux + "\dll\rsiflux.dll")
lib2 = LoadLibrary(initVBAFlux + "\dll\flux_mp.dll")
End Function
closeVBAFlux()
Function closeVBAFlux()
Dim ignore As Long
ignore = FreeLibrary(lib1)
ignore = FreeLibrary(lib2)
closeVBAFlux = 0
End Function
FLUX 10
Excel driving Flux 29
StartVBAFlux()
Sub StartVBAFlux()
Dim i As Long
Dim row As Integer
Dim lig As String
Dim Test As String
Dim interm As Double
'project path
path = ActiveWorkbook.path + "\w"
' save Excel file
ActiveWorkbook.Save
' Decimal separator "." or ","
DecimalSeparator = Application.International(xlDecimalSeparator)
'############################################################################
' INIT FLUX
'############################################################################
installFlux = initVBAFlux
herror = FMP_init(installFlux + "\bin\multiphys.wfg", RELEASEMODE)
If herror <> 0 Then GoTo ERROR
'############################################################################
' MEMORY
'############################################################################
nbArg = 1
herror = FMP_arg(NUMERICAL_MEMORY_LABEL, "600000000", nbArg, args)
If herror <> 0 Then GoTo ERROR
nbArg = nbArg + 1
herror = FMP_arg(LANGUAGE_LABEL, "2", nbArg, args)
If herror <> 0 Then GoTo ERROR
'############################################################################
' CONNEXION TO FLUX
'############################################################################
herror = FMP_startLocaleServer("FLUX2D_10.3_32", path, args, nbArg, serverUid)
If herror <> 0 Then GoTo ERROR
Flux10
30 Excel driving Flux
'############################################################################
' GEOMETRY
'############################################################################
Range("A14").Interior.ColorIndex = 6
herror = FMP_executeJythonCommand(serverUid, "executeBatchSpy('geometry.py')")
If herror <> 0 Then GoTo ERROR
'############################################################################
' MODIFY RADIUS OF THE WIRE: R (mm)
'############################################################################
Test = "ParameterGeom['R'].expression=" + "'" + Replace(CStr(Range("B5")), ",", ".") + "'"
herror = FMP_executeJythonCommand(serverUid, Test)
If herror <> 0 Then GoTo ERROR
'############################################################################
' MESHING
'############################################################################
Range("A14").Interior.ColorIndex = 0
Range("A15").Interior.ColorIndex = 6
herror = FMP_executeJythonCommand(serverUid, "executeBatchSpy('meshing.py')")
If herror <> 0 Then GoTo ERROR
'############################################################################
' BUILD PHYSICS
'############################################################################
Range("A15").Interior.ColorIndex = 0
Range("A16").Interior.ColorIndex = 6
herror = FMP_executeJythonCommand(serverUid, "executeBatchSpy('buildphys.py')")
If herror <> 0 Then GoTo ERROR
'############################################################################
' MODIFY CURRENT SUPPLIED THE WIRE: I (A)
'############################################################################
Test = "VariationParameter['I'].formula=" + "'" + Replace(CStr(Range("B6")), ",", ".") + "'"
herror = FMP_executeJythonCommand(serverUid, Test)
If herror <> 0 Then GoTo ERROR
FLUX 10
Excel driving Flux 31
'############################################################################
' SOLVE THE PROBLEM
'############################################################################
Range("A16").Interior.ColorIndex = 0
Range("A17").Interior.ColorIndex = 6
herror = FMP_executeJythonCommand(serverUid, "executeBatchSpy('solving.py')")
If herror <> 0 Then GoTo ERROR
'############################################################################
' POSTPROCESSING
'############################################################################
Range("A17").Interior.ColorIndex = 0
Range("A18").Interior.ColorIndex = 6
herror = FMP_executeJythonCommand(serverUid, "executeBatchSpy('postprocessing.py')")
If herror <> 0 Then GoTo ERROR
'############################################################################
' CLOSE PROJECT
'############################################################################
Range("A18").Interior.ColorIndex = 0
Range("A13").Interior.ColorIndex = 0
herror = FMP_executeJythonCommand(serverUid, "closeProject()")
If herror <> 0 Then GoTo ERROR
'############################################################################
' CLOSE FLUX
'############################################################################
herror = FMP_stopServer(serverUid)
If herror <> 0 Then GoTo ERROR
'############################################################################
' SHOW THE RESULTS
'############################################################################
numFile = FreeFile
Open path + "\Result.txt" For Input As #numFile
i = 1
Flux10
32 Excel driving Flux
If DecimalSeparator = "." Then ' excel usa
While Not EOF(1)
Line Input #1, lig
row = 4 + i
Range("E" & CStr(row)) = Format(lig, "0.00E+0")
interm = Range("E" & CStr(row))
Range("E" & CStr(row)) = interm
i = i + 1
Wend
Else ' excel fr
While Not EOF(1)
Line Input #1, lig
row = 4 + i
Range("E" & CStr(row)) = Format(Replace(lig, ".", ","), "0.00E+0")
interm = Range("E" & CStr(row))
Range("E" & CStr(row)) = interm
i = i + 1
Wend
End If
Close #numFile ' close the file
TextBox1.Text = "diameter of the wire :" + vbTab + CStr(2 * Range("B5")) + vbTab + "mm"
TextBox1.Text = TextBox1.Text + vbNewLine + "current :" + CStr(Range("B6")) + "A"
GoTo NOERROR
ERROR:
ignore = FMP_getErrorMessage(herror, txtError, maxSizeErrorMessage)
ignore = FMP_freeError(herror)
MsgBox "Error " & CStr(herror) & Chr(13) & txtError, vbCritical, "Flux Critical Error"
ignore = FMP_stopServer(serverUid)
NOERROR:
End Sub

You might also like