PDF version | Download Openfoam Code… |

In this tutorial, we show how to use the C++ library OpenFOAM (Open Field Operation and Manipulation) in order to solve control problems for partial differential equations (PDE).

OpenFOAM is a free open source toolbox that allows the user to code customized solvers for Continuum Mechanics, with special attention to the field of Computational Fluid Dynamics. One can either use one of the multiple solvers already programmed for steady-state, transient or multiphase problems in fluid mechanics, or code his own solver depending on particular needs.

In this work, we will solve the optimal control problem

(1)

where is the control variable, the state variable and a target function. The minimization problem is subject to the elliptic partial differential equation

(2)

(3)

We use some classical gradient descent methods based on the adjoint methodology, namely the steepest descent and conjugate gradient methods. The corresponding adjoint system for 2 writes as,

(4)

(5)

In the steepest descent gradient method, we compute the directional derivative of the cost function in 1,

(6)

where is solution to the elliptic problem in 4, and use the gradient to update the control variable as

(7)

for some value of .

One of the main advantages of OpenFOAM is its friendly syntax to describe PDE, as can be seen in the lines 10 and 13 in the code below.

// Initialize L2 norm of control update scalar pL2 = 10*tol; // Get the volume of mesh cells scalarField volField = mesh.V(); while (runTime.loop() && ::sqrt(pL2) > tol) { // Solve primal equation solve(fvm::laplacian(k, y) + u); // Solve ajoint equation solve(fvm::laplacian(k, lambda) + y - yd); // Update control p = lambda + beta*u; u = u - gamma*p; // Control update norm pL2 = ::sqrt(gSum(volField*p.internalField()*p.internalField())); // Compute cost function J = 0.5*gSum(volField*(Foam::pow(y.internalField()-yd.internalField(),2) \ + beta*Foam::pow(u.internalField(),2))); // Display information Info << "Iteration " << runTime.timeName() << " - " \ << "Cost value " << J << " - " \ << "Control variation L2 norm " << ::sqrt( pL2 ) << endl; // Save current iteration results runTime.write(); }

In order to improve the convergence to the optimal control, the conjugate gradient method is applied also to the problem. First, the state variable must be separated in two terms as

(8)

where solves the state equation with zero Dirichlet boundary conditions,

(9)

(10)

and is the control-free solution to the state equation,

(11)

(12)

Now, using the above separation of the state variable, one part depending on the control and the other part on a known source term, the cost functional in 1 can be expressed as

(13)

We define a linear operator that takes a control and returns the solution to problem 9–10, so that . We introduce as well its adjoint operator that takes the source term in 4 and solves the Poisson equation, so that .

The directional derivative of the functional 13 then reads as

(14)

After having identified and we can use the conjugate gradient method to reach the optimal control faster.

**Algorithm 1** Optimal control with Conjugate Gradient Method

**Require:** , , , ,

- compute the control-free solution,
**While****do**

// Compute b: right-hand side of Ax = b solve(fvm::laplacian(k, y)); solve(fvm::laplacian(k, lambda) + yd - y); volScalarField b = lambda; // Compute A*f0 // Primal equation solve(fvm::laplacian(k, y0) + u); // Adjoint equation solve(fvm::laplacian(k, lambda) + y0); // Compute g0: initial gradient volScalarField g = lambda + beta*u - b; scalar gL2 = gSum( volField * g.internalField() * g.internalField() ); scalar gL2a = 0; // Compute initial residual and its norm volScalarField r = -g; scalar rL2 = gL2; volScalarField w = g; scalar alpha = 0; scalar gamma = 0; while (runTime.loop() && ::sqrt( rL2 ) > tol) { r.dimensions().reset( u.dimensions() ); // Compute w = A*r solve(fvm::laplacian(k, y0) + r); solve(fvm::laplacian(k, lambda) + y0); w = lambda + beta*r; // Update alpha alpha = gL2 / gSum( volField * r.internalField() * w.internalField() ); // Update control u = u + alpha*r; // Update cost gradient and its norm g = g + alpha*w; gL2a = gL2; gL2 = gSum( volField * g.internalField() * g.internalField() ); gamma = gL2/gL2a; // Update residual and its norm r.dimensions().reset( g.dimensions() ); r = -g + gamma*r; rL2 = gSum( volField * r.internalField() * r.internalField() ); solve(fvm::laplacian(k, y) + u); J = 0.5*gSum(volField*(Foam::pow(y.internalField()-yd.internalField(),2) \ + beta*Foam::pow(u.internalField(),2))); Info << "Iteration no. " << runTime.timeName() << " - " \ << "Cost value " << J << " - " \ << "Residual " << ::sqrt( rL2 ) << endl; runTime.write(); }

Both solvers, *laplaceAdjointFoam* for the steepest descent method with and *laplaceCGAdjointFoam* for the conjugate gradient method, have been tested in a square domain with zero Dirichlet boundary conditions and . The target function is .

The problem setup is called *case* in OpenFOAM and it is independent from the solver itself. A typical case in OpenFOAM has three folders: *0*, where the problem fields are stored along with their boundary conditions; *constant*, that includes the mesh data and the physical properties; and *system*, with parameters regarding the numerical solution and problem output such as tolerances, linear solvers or write interval. In this example, the case folder is called *laplaceAdjointFoamCase*.

###### Bibliography

**[1]** The OpenFOAM Foundation, *http://openfoam.org*.

**[2]** F. Tröltzsch. *Optimal control of partial differential equations: theory, methods, and applications*. American Mathematical Soc., 2010.

**Click here to review “Steepest Descent Method in OpenFOAM” on Github**

**Click here to review “Conjugate Gradient Method in OpenFOAM” on Github**