- 1. Σ YSTEMS Optimal control of coupled PDE networks with automated code generation Dimitrios Papadopoulos Delta Pi Systems, Thessaloniki ICNAAM 2012, Kos, Greece 1 / 23
- 2. Overview ◮ Application of PDE Networks ◮ Representation of PDE Networks as a Graph ◮ Optimal Control of PDEs ◮ Adjoint Equations and Gradient ◮ Continuous vs. Discrete Adjoint Approach ◮ Discretization of the control variables ◮ Flowchart ◮ Tools of the Trade ◮ DOT Language and PyDot ◮ SymPy ◮ Cog ◮ SfePy ◮ SciPy (and NumPy) ◮ Conclusions Dimitris Papadopoulos — Delta Pi Systems 2 / 23
- 3. Application of PDE Networks Dimitris Papadopoulos — Delta Pi Systems 3 / 23
- 4. Representation of PDE Networks as a Graph 3 1 2 ∂y1 + ∇ · (a · y1 ) − ∇ · (D∇yi ) = f (y1 , y2 , y3 ; u) ∂t ∂y2 + ∇ · (a · y2 ) − ∇ · (D∇yi ) = f (y2 , y1 , y3 ; u) ∂t ∂y3 + ∇ · (a · y3 ) − ∇ · (D∇yi ) = g(y3 , y1 ; u) ∂t Dimitris Papadopoulos — Delta Pi Systems 4 / 23
- 5. Optimal Control of PDEs Problem formulation t1 min J(y, u) = φ(y, u) dxdt subject to: u 0 Ω ∂yi + ∇ · (a · yi ) − ∇ · (D∇yi ) = fi , for i = 1, . . . , nn , ∂t where yi are the state variables, and y the state vector, ui are the control variables, and u the control vector, fi is a function of some yj and u, nn is the number of nodes. Lagrange function nn t1 ∂yi min L(y, u, p) = J(y, u)+ ( −fi )pi +(ayi −D∇yi )·∇pi dxdt+BC y,u i=1 0 Ω ∂t where pi are the Lagrange multipiers or adjoint variables Dimitris Papadopoulos — Delta Pi Systems 5 / 23
- 6. Optimal Control of PDEs (cont.) Adjoint Equations n n ∂pi ∂fj − − ∇ · (a · pi ) − ∇ · (D∇pi ) = φyi (y, u) + pi , for i = 1, . . . , nn , ∂t j=1 ∂yi Gradient n n t1 ∂L ∂J ∂fi = + pi dxdt ∂u ∂u i=1 0 Ω ∂u Dimitris Papadopoulos — Delta Pi Systems 6 / 23
- 7. Continuous vs. Discrete Adjoint nonlinear linear adjoint PDE discrete equations Dimitris Papadopoulos — Delta Pi Systems 7 / 23
- 8. Back to the basics ∂L ∂L How do you calculate ∂u or ∂y ? ◮ Calculate analytical derivatives and implement them by hand (too time consuming and error prone) ◮ Symbolic diﬀerentiation (you have to add some code generation magic) ◮ Automatic (or algorithmic) diﬀerentiation (for each new problem you have to pass your code through your AD-tool) Dimitris Papadopoulos — Delta Pi Systems 8 / 23
- 9. Discretization of the control variables ◮ B-spline basis functions 1 if τi−1 ≤ t < τi Ni0 (t) = , 0 else n t − τj−1 n−1 τj+n − t n−1 Nj (t) = Nj (t) + N (t), n>1 τj+n−1 − τj−1 τj+n − τj j+1 ◮ B-spline curve formed by control points ci L n u(t) = cj Nj (t) j=0 Dimitris Papadopoulos — Delta Pi Systems 9 / 23
- 10. Parametrization - Finding a knot sequence ◮ uniform or equidistant (ti − ti−1 )/(ti+1 − ti ) = 1 ◮ chord length ∆ti ||∆di || = ∆ti+1 ||∆di+1 || ◮ cetripetal 1/2 ∆ti ||∆di || = ∆ti+1 ||∆di+1 || ◮ Natural end conditions ¨ u(t0 ) = 0, ¨ u(tm ) = 0 Dimitris Papadopoulos — Delta Pi Systems 10 / 23
- 11. Tensor Product Patches ◮ B-spline surface m n um,n (t, x) = ci,j Nim (t)Nj (x) n i=0 j=0 where ci,j is a matrix representing all the control points of the (t, x) plane. ◮ A surface with the topology of a sphere is not representable as a tensor product surface, without degeneracies (but we don’t really care) Dimitris Papadopoulos — Delta Pi Systems 11 / 23
- 12. Flowchart parse DOT graph build symbolic PDEs initialize control points build symbolic adjoint PDEs k 1 k k+1 build symbolic gradient solve direct problem generate source code for solution of PDEs objective function compute gradient generate source code for solution of adjoint PDEs no k generate source code for gradient J <ε solve adjoint problem yes solution Dimitris Papadopoulos — Delta Pi Systems 12 / 23
- 13. Tools of the Trade Extended DOT Language Direct Adjoint SymPy Cog Cog SfePy SfePy SciPy Dimitris Papadopoulos — Delta Pi Systems 13 / 23
- 14. DOT Language A DSL for graph representation digraph network { 3->1 [type=1]; 3->2 [type=1]; 1->2 [style=dashed, type=2]; 2->1 [style=dashed, type=2]; 1->3 [style=dotted, type=3]; 3 [shape=box];} 3 1 2 Dimitris Papadopoulos — Delta Pi Systems 14 / 23
- 15. Why Python? ◮ programmer’s time vs. computing time ◮ need for manipulation of the objects at runtime ◮ need for source code generation at runtime ◮ lisp, smalltalk, perl, python, ruby, javascript vs. C, C++, java ◮ Paul Graham’s classical closure example of the accumulator: def foo(n): s = [n] def bar(i): s[0] += i return s[0] return bar ◮ many tools and numerical libraries available Dimitris Papadopoulos — Delta Pi Systems 15 / 23
- 16. PyDot A Python library for the DOT language import pydot graph = pydot.graph_from_dot_file(’test.dot’) shape_dict = {} function_dict = {} for node in graph.get_nodes(): my_name = node.get_name() shape_dict[my_name] = node.get(’shape’) function_dict[my_name] = node.get(’function’) type_dict = {} style_dict = {} for edge in graph.get_edges(): my_edge = (edge.get_source(), edge.get_destination()) type_dict[my_edge] = edge.get(’type’) style_dict[my_edge] = print edge.get(’style’) Dimitris Papadopoulos — Delta Pi Systems 16 / 23
- 17. SymPy A Python library for symbolic mathematics from sympy import Eq, Function, Symbol from sympy import Derivative as D, Integral from sympy.abc import x, t, a y_list = [] # same for u_list for node in graph.get_nodes(): my_name = node.get_name() y_list.append(’y_’+str(my_name)) # same for u_list y = map(Function, y_list) # same for u_list f = y[1]+u # for example eq = Eq(D(y[1](x,t),t)+a*D(y[1](x,t),x)-D(D(y[1](x,t),x),x),f) Dimitris Papadopoulos — Delta Pi Systems 17 / 23
- 18. SymPy (cont.) Derivative of the Lagrangian 1. Build list of symbols for functions, 2. build a list of substitutions for the functions and the reverse, 3. substitute for the functions, 4. run derivatives with respect to the symbols, 5. substitute functions for the symbols, 6. take any derivatives with respect to the function variables. Integral(Derivative(F.subs(u(x),foo),foo).doit().subs(foo,←֓ u(x))*v(x),x) Dimitris Papadopoulos — Delta Pi Systems 18 / 23
- 19. Cog A code generation tool ... # [[[cog # import cog # functions = [direct, adjoint, gradient] # for func in functions: # cog.outl("def %s():" % func) # cog.outl(" pass") # ]]] # [[[end]]] ... Dimitris Papadopoulos — Delta Pi Systems 19 / 23
- 20. SfePy A software for solving systems of coupled PDEs by the FEM ◮ SfePy: Simple ﬁnite element in python ◮ Other alternatives: FENICS or your favorite FE solver (needs to be general enough) ◮ Future work: bring metaprogramming to the next level, i.e., ◮ use FENICS, which generates C++ at runtime from the problem formulation which can also be run parallel graph symbolic intermediated parallel representation representation python code C++ code Dimitris Papadopoulos — Delta Pi Systems 20 / 23
- 21. SciPy (and NumPy) A library for doing everything else import numpy as np from scipy.optimize import fmin_bfgs from newly_created_file import objective, gradient c0 = np.array([0.0, 0.0, 0.0, 0.0]) res = fmin_bfgs(objective, c0, gradient, args, gtol, norm, ...) and the rest is a piece of cake! Dimitris Papadopoulos — Delta Pi Systems 21 / 23
- 22. Conclusions Summary ◮ Framework for optimal control of coupled PDE networks ◮ Automated code generation ◮ Graph representation of coupled PDEs ◮ Symbolic diﬀerentiation for the adjoint equations and the gradient ◮ Solution with the ﬁnite element method Outlook ◮ Extention to more general equations, ◮ with 2D or 3D geometries, ◮ and constraints on the state or control variables. ◮ Adaptive reﬁnement of the control variables. Dimitris Papadopoulos — Delta Pi Systems 22 / 23
- 23. Thank you for your attention! Delta Pi Systems Thessaloniki, Greece http://www.delta-pi-systems.eu Dimitris Papadopoulos — Delta Pi Systems 23 / 23