Automatic Generation of Numerical Codes Jože Korelc University of Ljubljana, Slovenia www.fgg.uni-lj.si/Symech/ Solution procedure required by modern engineering Multi-field Multi-scale Multi-body F F M5 Multi-phase Multi-objective 0.47e1 0.10 0.16 0.22 0.28 0.34 0.40 DeformedMeshX M a x . 0.1277e1 Min. 0.465 AceGen 2 J. KORELC, UNIVERSITY OF LJUBLJANA Hybrid system for FE code generation and FE analysis Mathematica AceGen input FE environment dependend user subroutine interface AceGen code generator user subroutines • tangent, residual •post-processing •.... element source file C FE CDriver environment • C code Mathematica Driver •Math link Optimisation environment 3 J. KORELC, UNIVERSITY OF LJUBLJANA Mathematica Computational Templates package suplementary subroutines • initialization •numerical integration FORTRAN MDriver • full symbolic cappabilities • Mathematica Inverse ELFEN FEAP ABAQUS 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 4 J. KORELC, UNIVERSITY OF LJUBLJANA Multi-language code generation with AceGen 1. Mathematical description @ D @ 8 @ D @ D < D 8<8@@ D @ D < @ @ D DD 9 IM = @ D @ D @ D 2. Symbolic description in AceGen SMSInitialize "test", Language - > "C++ " SMSModule "Test", u1$$ 3 , x$$, L$$, g$$ 3 x, L Ł SMSReal x$$ , SMSReal L$$ ; u N i ui ui Ł Array SMSReal u$$ # x x x x N { ,1 , (1 )} L L L L 2 f u 3. Automatically generated code "Fortran" 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 5 J. KORELC, UNIVERSITY OF LJUBLJANA "C++" x x x ,1, L L L u Ł Ni . ui; Ni Ł 1- x L ; &, 3 ; ; f Ł u2 ; g Ł SMSD f, ui ; SMSExport g, g$$ ; SMSWrite ; 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]; }; "Mathematica" 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]; ] MDriver Multi-environment code generation CDriver Suplementary routines user subroutines 6 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 7 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. 8 J. KORELC, UNIVERSITY OF LJUBLJANA Optimisation of an matrix - common sub expressions Vector of 3 new auxiliary variables: 1 2 v 3 1 2 E I v L2 v L L 2 3 Original matrix (input for code generator): 12 EI L3 6 EI 2 L K0 12 EI L3 6 EI 2 L 6 EI 2 L 4 EI L 6 EI L2 2 EI L 12 EI L3 6 EI L2 12 EI L3 6 EI L2 6 EI 2 L 2 EI L 6 EI L2 4 EI L internal data base Code generator Mathematica v2 v1 v2 v1 v3 v v v 2 3 2 2 KSv1 v2 v1 v2 v 3 v v2 v3 2 2 Result is simplified matrix, expressed with the new auxiliary variables 9 J. KORELC, UNIVERSITY OF LJUBLJANA Symbolic system - flow control • "If" construct SMSIf x 0 ; $V[i,1] 2 f x ; SMSElse ; f Sin x ; $V[i,2] SMSEndIf f ; fictive variable $V[i,3] • "Do" construct f 1; SMSDo i, 1, n, 1, f ; f f i x; SMSEndDo f ; 10 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. 11 J. KORELC, UNIVERSITY OF LJUBLJANA AD exceptions 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: sin( 2 2 ) 0 f : 2 Lim sin( 2 ) 0 0 sin( 2 2 ) ) 0 ( f : sin( 2 2 ) Lim ( ) 0 0 Exceptions handling function: SMSFreeze exp, Dependency 12 J. KORELC, UNIVERSITY OF LJUBLJANA p1 , exp , p2, p1 exp , ..., pn , p2 exp pn 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 Response direct analysis qu ultimate load u 0.180e3 0.160e3 0.140e3 0.120e3 0.100e3 0.800e2 0.600e2 0.400e2 0.201e2 A Misses Max. h(x) x 0.2000e3 vA optimal solution Min. 0.1225 AceGen optimal shape? 13 J. KORELC, UNIVERSITY OF LJUBLJANA b b v Formulation of the problem • Three finite elemnts are needed to describe the problem: – 2D elasto plastic element – line load element – prescribed displacement constrain element a {u, } u displacements , load factor Ψ Ψ u Ψ obt. el. el.-plast. Ψu Ψu Ψu Ψu el.-plast. Ψu obt. el. standard ideal elasto-plastic formulation equations q. u d load element equations Newton-Raphson scheme to solve direct problem K uu K u u Ψu K Ψ K u Ψ v A v v p prescribed displacement constrain element equations • Objective function min 0 ; 0 ( u ) 2 Volume penaltyconstrain h ( x ) 0 u prescribed limit load factor calculated limit load factor 14 J. KORELC, UNIVERSITY OF LJUBLJANA k Solution • Analytical solution can be obtained by assuming: – Bernoully beam theory – in optimal case every material point is in plastic condition 2 qu M ( x) y h( x ) x Wpl ( x) b y y ... yield stress • Optimal finite element solution for given load 0.174e3 0.149e3 0.124e3 0.997e2 0.746e2 0.496e2 Misses Max. 0.2000e3 Min. 0.2456e2 AceGen h(0) 20.41 analytical solution h(0) 21.2 numerical solution 15 J. KORELC, UNIVERSITY OF LJUBLJANA Typical examples: Multi - field problems • unduction heating • T. Šuštar , C3M - Ljubljana, Slovenia Magnetic 16 J. KORELC, UNIVERSITY OF LJUBLJANA Thermal Mechanical Typical examples: Multi-scale problems S. Stupkiewicz, IPPT, Warsaw, Poland MICRO - 3D surface roughness MACRO - elastic properties of contact surface MACRO - lubricant flow in contact surface 17 J. KORELC, UNIVERSITY OF LJUBLJANA MACRO - thermal properties of contact surface Typical examples: structural analysis • Sensitivity analysis of single-storey steel building • four node shell elements and large displacement truss elements A Sensitivity 18 16 Analitic (elastic) 14 Finite differeces (elastic) 12 Analitic (plastic) 10 Finite differeces (plastic) 8 6 4 2 0 0 18 J. KORELC, UNIVERSITY OF LJUBLJANA 10 20 30 Roof angle (deg) 40 50 Typical examples: Simulation of technological proces J. Lengiewicz, IPPT, Warsaw, Poland Two phase forging 19 J. KORELC, UNIVERSITY OF LJUBLJANA Conclusions • Hybrid system presented is a powerfull tool for solving large-scale engineering problems • System can be dowloaded from www.fgg.uni- lj.si/Symech/ 20 J. KORELC, UNIVERSITY OF LJUBLJANA