Time dependent schema optimization for heat equations
First, it is possible to define variational forms, and use this forms to build matrix and vector to make very fast script (4 times faster here).
For example solve the ThermalConduction problem, we must solve the temperature equation in \(\Omega\) in a time interval (0,T).
The variational formulation is in \(L^2(0,T;H^1(\Omega))\); we shall seek \(u^n\) satisfying:
where \(V_0 = \{w\in H^1(\Omega)/ w_{|\Gamma_{24}}=0\}\).
So, to code the method with the matrices \(A=(A_{ij})\), \(M=(M_{ij})\), and the vectors \(u^n, b^n, b',b", b_{cl}\) (notation if \(w\) is a vector then \(w_i\) is a component of the vector).
Where with \(\frac{1}{\varepsilon} = \mathtt{tgv} = 10^{30}\):
The Fast version script:
1...
2Vh u0=fu0, u=u0;
Create three variational formulation, and build the matrices \(A\),\(M\).
1varf vthermic (u, v)
2 = int2d(Th)(
3 u*v/dt
4 + k*(dx(u)*dx(v) + dy(u)*dy(v))
5 )
6 + int1d(Th, 1, 3)(
7 alpha*u*v
8 )
9 + on(2,4,u=1)
10 ;
11
12varf vthermic0 (u, v)
13 = int1d(Th, 1, 3)(
14 alpha*ue*v
15 )
16 ;
17varf vMass (u,v)
18 = int2d(Th)(
19 u*v/dt
20 )
21 + on(2, 4, u=1)
22 ;
23
24real tgv = 1e30;
25matrix A = vthermic(Vh, Vh, tgv=tgv, solver=CG);
26matrix M = vMass(Vh, Vh);
Now, to build the right hand size; we need 4 vectors.
1real[int] b0 = vthermic0(0,Vh); //constant part of RHS
2real[int] bcn = vthermic(0,Vh); //tgv on Dirichlet part
3real[int] bcl = tgv*u0[]; //the Dirichlet B.C. part
4
5// The fast loop
6for(real t = 0; t < T; t += dt){
7 real[int] b = b0; //the RHS
8 b += M*u[]; //add the the time dependent part
9 b = bcn ? bcl : b; //do $\forall i$: b[i] = bcn[i] ? bcl[i] : b[i];
10 u[] = A^-1*b; //solve linear problem
11 plot(u);
12}