<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mintoc.de/index.php?action=history&amp;feed=atom&amp;title=Controlled_Heating_%28FEniCS%2FCasadi%29</id>
	<title>Controlled Heating (FEniCS/Casadi) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mintoc.de/index.php?action=history&amp;feed=atom&amp;title=Controlled_Heating_%28FEniCS%2FCasadi%29"/>
	<link rel="alternate" type="text/html" href="https://mintoc.de/index.php?title=Controlled_Heating_(FEniCS/Casadi)&amp;action=history"/>
	<updated>2026-06-09T08:04:31Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://mintoc.de/index.php?title=Controlled_Heating_(FEniCS/Casadi)&amp;diff=1233&amp;oldid=prev</id>
		<title>MirkoHahn: Created page with &quot;This page contains a discretized version of the PDE constrained optimization problem Controlled Heating in [http://www.casadi.org CasADi 2.4.3] format. The weak version of...&quot;</title>
		<link rel="alternate" type="text/html" href="https://mintoc.de/index.php?title=Controlled_Heating_(FEniCS/Casadi)&amp;diff=1233&amp;oldid=prev"/>
		<updated>2016-01-12T21:20:30Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;This page contains a discretized version of the PDE constrained optimization problem &lt;a href=&quot;/index.php?title=Controlled_Heating&quot; title=&quot;Controlled Heating&quot;&gt;Controlled Heating&lt;/a&gt; in [http://www.casadi.org CasADi 2.4.3] format. The weak version of...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;This page contains a discretized version of the PDE constrained optimization problem [[Controlled Heating]] in [http://www.casadi.org CasADi 2.4.3] format. The weak version of the problem was discretized using finite element methods (via [http://www.fenicsproject.org FEniCS]) using first-degree Lagrangian elements on a mesh automatically generated using FEniCS&amp;#039; mesh generation component.&lt;br /&gt;
&lt;br /&gt;
=== CasADi ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
from dolfin import *&lt;br /&gt;
import mshr&lt;br /&gt;
&lt;br /&gt;
import numpy&lt;br /&gt;
import scipy, scipy.sparse&lt;br /&gt;
import casadi&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Converts FEniCS matrix to scipy.sparse.coo_matrix&lt;br /&gt;
def matrix_to_coo(A, shape=None):&lt;br /&gt;
    nnz = A.nnz()&lt;br /&gt;
    col = numpy.empty(nnz, dtype=&amp;#039;uint64&amp;#039;)&lt;br /&gt;
    row = numpy.empty(nnz, dtype=&amp;#039;uint64&amp;#039;)&lt;br /&gt;
    val = numpy.empty(nnz)&lt;br /&gt;
&lt;br /&gt;
    if shape is None:&lt;br /&gt;
        shape = (A.size(0), A.size(1))&lt;br /&gt;
&lt;br /&gt;
    it = 0&lt;br /&gt;
    for i in range(A.size(0)):&lt;br /&gt;
        col_local, val_local = A.getrow(i)&lt;br /&gt;
        nnz_local = col_local.shape[0]&lt;br /&gt;
&lt;br /&gt;
        col[it:it+nnz_local] = col_local[:]&lt;br /&gt;
        row[it:it+nnz_local] = i&lt;br /&gt;
        val[it:it+nnz_local] = val_local[:]&lt;br /&gt;
        it                  += nnz_local&lt;br /&gt;
&lt;br /&gt;
    return scipy.sparse.coo_matrix((val, (row, col)), shape=shape)&lt;br /&gt;
&lt;br /&gt;
# Construct a high-resolution mesh of the unit circle&lt;br /&gt;
domain   = mshr.Circle(Point(0.0, 0.0), 1.0)&lt;br /&gt;
mesh     = mshr.generate_mesh(domain, 100)&lt;br /&gt;
&lt;br /&gt;
# Define the function space&lt;br /&gt;
V = FunctionSpace(mesh, &amp;#039;CG&amp;#039;, 1)&lt;br /&gt;
&lt;br /&gt;
# Define trial and test functions&lt;br /&gt;
h = TrialFunction(V)&lt;br /&gt;
t = TrialFunction(V)&lt;br /&gt;
v = TestFunction(V)&lt;br /&gt;
&lt;br /&gt;
# Construct the two parts of the constraint matrix and assemble them&lt;br /&gt;
lhs_t = inner(grad(t), grad(v)) * dx + t * v * ds&lt;br /&gt;
lhs_h = -h * v * dx&lt;br /&gt;
&lt;br /&gt;
A = matrix_to_coo(assemble(lhs_t))&lt;br /&gt;
B = matrix_to_coo(assemble(lhs_h))&lt;br /&gt;
C = casadi.DMatrix(scipy.sparse.hstack([A, B]))&lt;br /&gt;
&lt;br /&gt;
# Data structures for solution data&lt;br /&gt;
sol     = Function(V)&lt;br /&gt;
control = Function(V)&lt;br /&gt;
&lt;br /&gt;
# Define objective functional and assemble it&lt;br /&gt;
sol_ref = interpolate(Expression(&amp;quot;x[0] * x[0] + x[1] * x[1]&amp;quot;), V)&lt;br /&gt;
J       = pow(sol_ref - sol, 2) * dx&lt;br /&gt;
J_grad  = derivative(J, sol)&lt;br /&gt;
J_hess  = derivative(J_grad, sol)&lt;br /&gt;
&lt;br /&gt;
c = assemble(J)&lt;br /&gt;
g = casadi.DMatrix(numpy.concatenate((assemble(J_grad).array(), numpy.zeros(V.dim()))))&lt;br /&gt;
H = casadi.DMatrix(matrix_to_coo(assemble(J_hess), shape=(2*V.dim(), 2*V.dim())))&lt;br /&gt;
&lt;br /&gt;
# Construct CasADi problem&lt;br /&gt;
X = casadi.MX.sym(&amp;#039;X&amp;#039;, V.dim() * 2)&lt;br /&gt;
nlp = casadi.MXFunction(&amp;#039;nlp&amp;#039;, casadi.nlpIn(x=X), casadi.nlpOut(&lt;br /&gt;
    f=0.5*casadi.quad_form(X, H) + casadi.inner_prod(g, X) + c,&lt;br /&gt;
    g=casadi.mul(C, X)&lt;br /&gt;
))&lt;br /&gt;
&lt;br /&gt;
# Construct CasADi solver and solve problem&lt;br /&gt;
solver = casadi.NlpSolver(&amp;#039;solver&amp;#039;, &amp;#039;ipopt&amp;#039;, nlp)&lt;br /&gt;
result = solver({&lt;br /&gt;
    &amp;#039;lbx&amp;#039;: [-numpy.inf] * V.dim() + [-10.0] * V.dim(),&lt;br /&gt;
    &amp;#039;ubx&amp;#039;: [ numpy.inf] * V.dim() + [ 10.0] * V.dim(),&lt;br /&gt;
    &amp;#039;lbg&amp;#039;: 0.0,&lt;br /&gt;
    &amp;#039;ubg&amp;#039;: 0.0&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
# Evaluate results&lt;br /&gt;
sol.vector()[:] = result[&amp;#039;x&amp;#039;][:V.dim()].toArray()[:,0]&lt;br /&gt;
control.vector()[:] = result[&amp;#039;x&amp;#039;][V.dim():].toArray()[:,0]&lt;br /&gt;
&lt;br /&gt;
error = Function(V)&lt;br /&gt;
error.assign(sol_ref - sol)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
&lt;br /&gt;
The solutions is calculated using IPOPT (3.12, default settings, using linear solver MUMPS on Ubuntu 15.10 for x86-64 with Linux 4.2.0-19-generic, Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz, 16 GB RAM). The solution process The objective function value is &amp;lt;math&amp;gt;0.0145336&amp;lt;/math&amp;gt;. IPOPT finds the solution in 17 iterations (62.060 seconds proc time).&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
&lt;br /&gt;
A Gnuplot compatible [[Controlled Heating (Casadi/FEniCS) - Solution Data|tabular version]] of the solution data is provided separately.&lt;br /&gt;
 &lt;br /&gt;
[[Category:Casadi]]&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
[[Category:Casadi]]&lt;/div&gt;</summary>
		<author><name>MirkoHahn</name></author>
	</entry>
</feed>