Automatic Generation of Numerical Codes
Jože KorelcUniversity of Ljubljana, Slovenia
www.fgg.uni-lj.si/Symech/
2 J. KORELC, UNIVERSITY OF LJUBLJANA
Solution procedure required by modern engineering
M5
AceGen
0.465
Min.
0.1277e 1
Max.
DeformedMeshX
0.40
0.34
0.28
0.22
0.16
0.10
0.47e 1
Multi-field
Multi-scale
Multi-body
Multi-phase
Multi-objective
F F
3 J. KORELC, UNIVERSITY OF LJUBLJANA
Hybrid system for FE code generation and FE analysis
AceGencode generator
ComputationalTemplatespackage
FE environmentdependend user subroutine
interface
C FORTRANMathematica
AceGen input
user subroutines• tangent, residual• post-processing
• ....
suplementary subroutines• initialization
• numerical integration
Mathematica
element source file
CDriver• C code
•Math link
FEAPELFENMDriver• full symbolic
cappabilities• Mathematica
ABAQUS
Driver
Optimisation environment
FE environment
Mathematica Inverse
4 J. KORELC, UNIVERSITY OF LJUBLJANA
Concepts of AceGen code generator
simultaneous optimization of expressions and introduction of intermediate
variables,
expression optimisation based on heuristic procedures,
generation of characteristic formulae,
the whole program structure can be generated,
automatic differentiation technique (forward and backward mode),
multi-language code generation (FORTRAN, C/C++, Mathematica language),
multi-environment code generation (automatic interface to numerical environments)
methods for exploring of generated formulae and run time debugging of the
generated programs
• special procedures are needed for non-local (explicit form of the expressions is lost)
• AD inside a single subroutine
5 J. KORELC, UNIVERSITY OF LJUBLJANA
Multi-language code generation with AceGen
1. Mathematical description 2. Symbolic description in AceGenSMSInitialize@"test", Language - >"C++"DSMSModule@"Test",8u1$$@3D, x$$, L$$, g$$@3D<D;8x, L<Ł8SMSReal@x$$D, SMSReal@L$$D<;uiŁ Array@SMSReal@u$$@#DD&, 3D;NiŁ9x
L, 1 -
xL,xLI1 - x
LM=;
uŁ Ni.ui;
fŁ u2;gŁ SMSD@f, uiD;SMSExport@g, g$$D;SMSWrite@D;
3. Automatically generated code
SUBROUTINE Test(v,u1,x,L,g) REAL*8 v(500),u1(3),x,L,g(3) v(6)=x/L v(7)=1d0 - v(6) v(8)=v(6)*v(7) v(12)=2d0*(u(1)*v(6) +- u(2)*v(7) + u(3)*v(8)) g(1)=v(12)*v(6) g(2)=v(12)*v(7) g(3)=v(12)*v(8) END
"Fortran"
2
)}1(,1,{
uf
L
x
L
x
L
x
L
x
uNu ii
N
void Test(double v[501],double u1[3],double *x,double *L,double g[3]){v[6]=*x/*L;v[7]=1e0 - v[6];v[8]=v[6]*v[7];v[12]=2e0*(u[0]*v[6] + u[1]*v[7]+ u[2]*v[8]);g[0]=v[12]*v[6];g[1]=v[12]*v[7];g[2]=v[12]*v[8];};
"C++"Test[]:=Module[{},$VV[6]=x$$/L$$;$VV[7]=1 - $VV[6];$VV[8]=$VV[6]*$VV[7];$VV[12]=2*(u$$[1]*$VV[6] + u$$[2]*$VV[7]+u$$[3]*$VV[8]);g$$[1]=$VV[6]*$VV[12];g$$[2]=$VV[7]*$VV[12];g$$[3]=$VV[8]*$VV[12];]
"Mathematica"
6 J. KORELC, UNIVERSITY OF LJUBLJANA
SMT̀ SetElSpec["test",idata$$_,ic_,gd_]:=Block[{q1,q2,q3},q3=SMTNumIntegration[ic];q1={"test",{"SKR" -> SMT̀ SKR, _ -> Null},{"SpecIndex",2,20,0,4,2,5,ic, "NoTimeStorage",0,q3[[1]],0,0,1},"Q1", {"Const 1", "Const 2"},{},{},{1,2,3,4,0} ,{5, 5, 5, 5},{},{},gd,q3[[2]]//Transpose};q1[[3,9]]=0;q1];
(******************* M O D U L E ***************)SetAttributes[SMT̀ SKR,HoldAll];SMT̀ SKR["test",es$$_,ed$$_,nd$$_,rdata$$_,idata$$_ ,p$$_,s$$_]:=Module[{},p$$[[1]]=nd$$[[1,3,1]];];
Multi-environment code generation
SMSInitialize"test", "Language" "C";SMTInitialize"test", "CDriver","SMTTopology" "Q1", "SMTDOFGlobal" 5,
"SMTGroupDataNames" "Const 1", "Const 2";SMTUserSubroutine"Tangent and residual";SMSExportSMSRealnd$$1, "X", 1, p$$1;SMSWrite"Splice" SMTSplice;
SMSInitialize"test", "Language" "Mathematica";SMTInitialize"test", "MDriver","SMTTopology" "Q1", "SMTDOFGlobal" 5,
"SMTGroupDataNames" "Const 1", "Const 2";SMTUserSubroutine"Tangent and residual";SMSExportSMSRealnd$$1, "X", 1, p$$1;SMSWrite"Splice" SMTSplice;
MDriver CDriver
#include "sms.h"void SKR(double v[501],ElementSpec *es,ElementData *ed,NodeData **nd,double *rdata,int *idata,double*p,double **s);__declspec(dllexport) void SMTSetElSpec(ElementSpec *es,int *idata,int *ic,double *gd){ static int pn[5]={1, 2, 3, 4, 0}; static int dof[4]={5, 5, 5, 5}; static char *gdcs[]={"Const 1","Const 2"}; static char *gpcs[]={""};static char *npcs[]={""}; es->Code="test";es->id.NoDimensions=2;es->id.NoDOFGlobal=20; es->id.NoDOFCondense=0;es->id.NoNodes=4; es->id.NoGroupData=2;es->id.NoSegmentPoints=5; es->id.IntCode=*ic;es->id.NoElementData=0; es->Segments=pn;es->DOFGlobal=dof;es->Data=gd; es->id.NoGPostData=0;es->id.NoNPostData=0; es->id.SymmetricTangent=1; es->IntPoints=SMTIntPoints(ic);es->id.NoTimeStorage=0; es->Topology="Q1";es->GroupDataNames=gdcs; es->GPostNames=gpcs;es->NPostNames=npcs;es->user.SKR=SKR;};
void SKR(double v[501],ElementSpec *es,ElementData *ed,NodeData **nd,double*rdata,int *idata,double *p,double **s){p[0]=nd[0]->X[0];};
Suplementaryroutines
usersubroutines
7 J. KORELC, UNIVERSITY OF LJUBLJANA
Open problems
Interactions:
symbolic system - simultaneous simplification procedure
symbolic system - automatic differentiation
symbolic system - simultaneous generation of program structure
automatic differentiation - simultaneous generation of program
structure
Verification & validation of generated codes:
Are the equations solved correctly?
Are the righ equations solved?
Interactive debugging of automatically generated numerical
codes
8 J. KORELC, UNIVERSITY OF LJUBLJANA
Symbolic system - simultaneous simplification• Simultaneous stochastic optimisation procedure (Korelc 1997)
Additional operators
Auxiliary variables have standardized form $V[i, j], where i is an index of auxiliary variable and j is an instance of the i-th auxiliary variable.
InitializationSMSInitialize"test", "Language"
"C", "Mode" "Optimal";Definition of a new subroutineSMSModule"Test",
Realx$$, f$$, dfdx$$;Numericsymbolic interfacex SMSRealx$$;Derivation of the problem Cosx;SMSIfSMSAbs 1010;f Sin2 2;
SMSElse;f
SMSFreezeLimitSin2 2, 0,"Dependency",, LimitDSin2 2,
Evaluate, 0;SMSEndIfTrue, f; Automatic differentiationdfdx SMSDf, x;Symbolicnumeric interafceSMSExportdfdx, dfdx$$;Code generationSMSWrite;
v exp A new auxiliary variable is created ifAceGen finds out that the introductionof the new variable is necessary,otherwise vexp
v exp A new auxiliary variable is created,regardless on the contents of exp.
v exp Create variable which will appearmore than once on a lefthand sideof equationmultivalued variables.
v exp A new valueexpis assigned to thepreviously created auxiliary variable v.
9 J. KORELC, UNIVERSITY OF LJUBLJANA
Optimisation of an matrix - common sub expressions
K
EI
L
EI
L
EI
L
EI
LEI
L
EI
L
EI
L
EI
LEI
L
EI
L
EI
L
EI
LEI
L
EI
L
EI
L
EI
L
0
3 2 3 2
2 2
3 2 3 2
2 2
12 6 12 6
6 4 6 2
12 6 12 6
6 2 6 4
Original matrix(input for code generator):
Code generator
v EI
L
v L v L
12
2
2
331 2
Vector of 3 new auxiliary variables:
internal data base
K
v v v v
v v vv
v v v v
vv
v v
S
1 2 1 2
2 3 23
1 2 1 2
23
2 3
2
2
Result is simplified matrix, expressed with the new auxiliary variables
Mathematica
10 J. KORELC, UNIVERSITY OF LJUBLJANA
Symbolic system - flow control• "If" construct
• "Do" construct
InitializationSMSInitialize"test", "Language"
"C", "Mode" "Optimal";Definition of a new subroutineSMSModule"Test",
Realx$$, f$$, dfdx$$;Numericsymbolic interfacex SMSRealx$$;Derivation of the problem Cosx;SMSIfSMSAbs 1010;f Sin2 2;
SMSElse;f SMSFreezeLimit
Sin2 2, 0, "Dependency",, LimitDSin2 2, Evaluate, 0;
SMSEndIfTrue, f; Automatic differentiationdfdx SMSDf, x;Symbolicnumeric interafceSMSExportdfdx, dfdx$$;Code generationSMSWrite;
SMSIfx 0;f x2;
SMSElse;f Sinx;SMSEndIff;
f 1;
SMSDoi, 1, n, 1, f;f f xi;
SMSEndDof;
fictive variable $V[i,3]
$V[i,1]
$V[i,2]
11 J. KORELC, UNIVERSITY OF LJUBLJANA
Symbolic system - automatic differentiation
• Automatic differentiation technique (AD):– differentiation of the whole program– automatic differentiation tool generates a program code for the derivative from a
program code for the basic function
• Reverse mode / Forward mode
• Enhancements with respect to the standard AD technique:– AD procedure can be initiated at any time and at any point of
derivation of the formulas and as many times as required
– AD as code-to-code translator has to be replaced by the method that consistently extends current code rather than produce a new one
– the results of all previous uses of AD have to be accounted for when AD is used several times inside the same subroutine
– The user has to be able to use all the capabilities of the symbolic system on the final and intermediate results of AD procedure.
12 J. KORELC, UNIVERSITY OF LJUBLJANA
InitializationSMSInitialize"test", "Language"
"C", "Mode" "Optimal";Definition of a new subroutineSMSModule"Test",
Realx$$, f$$, dfdx$$;Numericsymbolic interfacex SMSRealx$$;Derivation of the problem Cosx;SMSIfSMSAbs 1010;f Sin2 2;
SMSElse;f SMSFreezeLimit
Sin2 2, 0, "Dependency",, LimitDSin2 2, Evaluate, 0;
SMSEndIfTrue, f; Automatic differentiationdfdx SMSDf, x;Symbolicnumeric interafceSMSExportdfdx, dfdx$$;Code generationSMSWrite;
0))2sin(
(
0))2sin(
(:
0)2sin(
0)2sin(
:
2
0
2
2
0
2
Lim
f
Lim
f
AD exceptions
Exceptions handling function:
SMSFreezeexp, Dependency p1,exp
p1,p2,
exp
p2, ...,pn,
exp
pn
The basic situations that have to be considered are:
• there exists implicit dependency between variables
that has to be considered for the differentiation
• there exists explicit dependency between variables
that has to be neglected for the differentiation
• the evaluation of the derivative code would lead to
numerical errors
Example:
13 J. KORELC, UNIVERSITY OF LJUBLJANA
Example: limit load optimisation of cantilever beam• Task: find the shape (h(x)) of cantilever beam that has:
– minimal volume– given ultimate load– ideal elasto-plastic material
• Properties of the problem:– transient coupled problem– gradient based optimisation
AceGen0.1225
Min.
0.2000e3
Max.
Misses
0.201e20.400e20.600e20.800e20.100e30.120e30.140e30.160e30.180e3
ultimate loaduq
x)(xh
bb
A
Av
u
v
Responsedirect analysis
optimal solution
optimal shape?
14 J. KORELC, UNIVERSITY OF LJUBLJANA
Formulation of the problem• Three finite elemnts are needed to describe the problem:
– 2D elasto plastic element
– line load element
– prescribed displacement constrain element
• Objective function
uu u u
u
u
K K Ψ
K K Ψ
obt. el.el.-plast.
el.-plast.
obt. el.
{ , }
displacements , load factor
standard ideal elasto-plastic formulation equations
. load element equations
prescribed d
u
u u u
u
u
A v p
d
v v
a u
u
ΨΨ
Ψ
Ψ Ψ Ψ
Ψ
Ψ q u
Ψ isplacement constrain element equations
20 0 penaltyconstrain ( ) 0min ; ( ) olumeu h x
k
V
factor loadlimit calculated
factor loadlimit prescribed
u
Newton-Raphson scheme to solve direct problem
15 J. KORELC, UNIVERSITY OF LJUBLJANA
Solution• Analytical solution can be obtained by assuming:
– Bernoully beam theory
– in optimal case every material point is in plastic condition
• Optimal finite element solution for given load
AceGen0.2456e2
Min.
0.2000e3
Max.
Misses
0.496e2
0.746e2
0.997e2
0.124e3
0.149e3
0.174e3
2( )( ) ...
( )u
y ypl y
qM xh x x yield stress
W x b
solution numerical2.21)0(
solution analytical41.20)0(
h
h
16 J. KORELC, UNIVERSITY OF LJUBLJANA
Typical examples: Multi - field problems• unduction heating • T. Šuštar , C3M - Ljubljana, Slovenia
Magnetic Thermal Mechanical
17 J. KORELC, UNIVERSITY OF LJUBLJANA
Typical examples: Multi-scale problems
MACRO - elastic properties of contact surface
MICRO - 3D surface roughness
MACRO - thermal properties of contact surfaceMACRO - lubricant flow in contact surface
S. Stupkiewicz, IPPT, Warsaw, Poland
18 J. KORELC, UNIVERSITY OF LJUBLJANA
Typical examples: structural analysis• Sensitivity analysis of single-storey steel building
• four node shell elements and large displacement truss elements
A
0
2
4
6
8
10
12
14
16
18
0 10 20 30 40 50Roof angle (deg)
Sen
siti
vity
Analitic (elastic)
Finite differeces (elastic)
Analitic (plastic)
Finite differeces (plastic)
19 J. KORELC, UNIVERSITY OF LJUBLJANA
Typical examples: Simulation of technological proces
Two phase forging
J. Lengiewicz, IPPT, Warsaw, Poland
Top Related