This document summarizes a numerical modelling report on the temperature distribution in a double-pipe system for district heating. It describes the heat equation model used and outlines the finite element method for discretization. Key steps include defining the geometry and boundary conditions, selecting physical parameters, generating the mesh with different refinement techniques, computing relevant matrices, and presenting results over time. The goal is to analyze the thermal distribution in the pipes buried in the ground.
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
Β
Temperature Distribution in a ground section of a double-pipe system in a districti heating network
1. Politecnico di Torino
Mechanical Engineering
Numerical Modelling Report
Academic year 2010/2011
TEMPERATURE DISTRIBUTION
IN A GROUND SECTION
OF A DOUBLE-PIPE SYSTEM
IN A DISTRICT HEATING NETWORK
Paolo Fornaseri
Enrico Grassano
Alessandro Tumino
2. 1
INDEX
.......................................................................................................................................................................0
.......................................................................................................................................................................1
Introduction..................................................................................................................................................2
Mathematic model description: the heat equation ..................................................................................2
Boundary conditions for the thermal model.........................................................................................4
Finite elements method theory ..................................................................................................................5
Time discretization ..................................................................................................................................6
Geometry and boundary conditions..........................................................................................................7
Physical Parameters ...................................................................................................................................9
Temperature profile...................................................................................................................................10
Mesh and its Refinement ..........................................................................................................................11
n_mesh parameter .............................................................................................................................11
Standard Refinement .........................................................................................................................12
βManualβ Refinement.........................................................................................................................12
Particular Case (Mesh for ODE45)....................................................................................................13
Computation Time in function of Triangle size...............................................................................14
Mesh quality .......................................................................................................................................15
Matrix and vectors computations............................................................................................................18
Stiffness matrix......................................................................................................................................18
Right hand side vector ..........................................................................................................................18
Mass matrix ............................................................................................................................................18
Initial Solution............................................................................................................................................19
Stationary solution....................................................................................................................................19
Time advancing .........................................................................................................................................20
Linear system solution method ...............................................................................................................21
Time comparison.......................................................................................................................................22
Results........................................................................................................................................................23
ODE time advancing method ...................................................................................................................29
Conclusions...............................................................................................................................................32
Appendix ....................................................................................................................................................33
3. 2
Introduction
Our analysis concerns the distribution network of a suburb in the city of Turin.
We analyzed the thermal needs, the network layout and many other engineering problems regarding
the distribution of heat.
In the following report we are going to analyze the simplified model of a couple of buried ducts,
conveying the fluid used for thermal needs in the houses.
We analyzed the thermal distribution in the pipeline, in particular we focused on a section of the
ground, in which the water passes through the double-pipe system, namely return and supply pipe.
We used the fundamental heat equation (conduction) and the subsequent numerical discretization, in
the transient and in the steady state.
To this aim, we made some simplifications in order to apply our mathematical model.
Mathematic model description: the heat equation
The domain in which we are working is the cross section of the soil that hence becomes the
plate studied at lesson.
The heat quantity in the plate is expressed like:
β« π(π₯)π(π₯)π’(π₯, π‘)ππ₯
where:
c is the heat capacity
Ο is the mass density per unit surface
u is the temperature
This integral has to be performed on the whole surface.
Now, having a heat transfer balance on the plate boundary it can be written that:
π
ππ‘
β« πππ’ ππ₯ = β« π· β π ππ
where
Ξ¦ is the heat flux
n is the unit vector normal to the boundary
Now, we can use the Gauss-Green theorem and we get:
β« ππ
ππ’
ππ‘
ππ₯ = β« β β π· ππ₯
Hence
ππ
ππ’
ππ‘
β β β π· = 0
the Fourier relation, linking the heat flux to the temperature gradient, is now introduce.
Substituting in the previous relation we get :
4. 3
ππ
ππ’
ππ‘
β β β (πβπ’) = 0
Where Ξ» is the thermal conductivity.
The relation holds for every point in the domain of the plate.
The stationary heat equation is:
β β (πβπ’) = 0
Actually, the most general relation includes the forcing term q, namely, the heat absorbed or
sold by the system. In our model thereβs no the need to include this term, since no external
source of heat is considered.
5. 4
Boundary conditions for the thermal model
In the model we dealt with, we considered three kind of boundary condition:
ο· Dirichlet boundary, so the direct imposition of a certain value of temperature on the
boundary
π’ = π
ο· Neumann conditions, so fixing the value of the derivative, with respect to a certain
direction, of the temperature. This is the same as impose a flux
ππ’
ππ
= π
ο· Robin condition that consists in the expression of the flux like a function of the
temperature
ππ’
ππ
= πΌ(πβ β π’)
where Tβ is the known temperature outside and Ξ± is the convective heat transfer
coefficient.
8. 7
Geometry and boundary conditions
The geometry and the boundary conditions are here summarized:
ο· Pipes: Non homogeneous Dirichlet BCs. The temperature of the metal of the pipes is
simply given by the temperature of the water which passes inside them;
ο· Lateral Boundaries: homogeneous Neumann BCs, assuming nil heat flux;
ο· Lower Boundary: here we imposed Robin BC, instead more obvious Dirichlet in order to
represent the heat flux due to the soil moisture;
ο· Upper Boundary: Robin BC, considering air convection. Heat is continuously dissipated
and the temperature tends to a certain value (environment temperature).
The temperature of the ground below the pipes is assumed as in function of the one of
the environment, but with some distinctions in function of the value of this temperature.
If it is below the average of the period, the temperature of the ground is taken a little
higher than this one, while if it is over the average, this temperature is taken a little
lower. Besides, the temperature of the pipes is taken as the mean of these two
temperatures.
9. 8
Moreover, when implemented on Matlab, continuity between shapes is needed, indeed at the
interface ground/pipes continuity was imposed between the external side of the circles and the
side of the hole of the rectangle.
Here below the most important geometric values are reported. These are the ones taken from
our advanced engineering thermodynamics project. Though, in the program these values can
be decided by the user (all but centers distance and depth).
From thermodynamic report Supply Return
Pipe thickness mm 5 10
Inner diameter mm 40 40
Outer diameter mm 50 60
Centers distance m 0.5
Depth m 0.5
Water Temperature Β°C 90 40
10. 9
Physical Parameters
Some considerations have to be done about the physical parameters.
In particular the insulator thermal conductivity has been chosen higher than the real one.
Indeed the mineral wool has a conductivity of 0.047 W/mK while in this case we chose 5 W/mK,
This was done to avoid a sharp drop in temperature distribution between the pipe and the
ground but a slighter gradient. However the presence of the thin metal wall of the pipe in
reality increases slightly the conduction. We did the same consideration for the ground
conductivity, in order to get a prettier result.
Another parameter has to be mentioned is the surface density. In this case, since a thickness
couldnβt to be defined (2D problem), the value of the real volumetric density was adopted also
for the surface density.
Finally the Robin BC requires the definition of the convection heat transfer coefficient. The one
chosen for the air side is Ξ±air = 20 W/m2K (typical value), whereas for the soil moisture Ξ±soil = 10
W/m2K (guessed value).
Here below all the physical parameters used in the calculations are reported.
Parameter Symbol Unit Value
Insulator thermal
conductivity
Ξ»ins W/mK 5
Ground thermal
conductivity
Ξ»g W/mK 30
Outside air temperature
(winter average)
Tβ Β°C 8
Deep Ground temperature Tβ2 Β°C 7
Convection - Air side Ξ±air
W/m2
K
20
Convection - Deep Ground
Side
Ξ±soil
W/m2
K
10
Insulation surface density Οins kg/m2
20
Ground surface density Οg kg/m2
2000
Insulation specific heat
capacity
cins J/kgK 500
Ground specific heat
capacity
cg J/kgK 1480
External thermal power q W/m2
0
11. 10
Temperature profile
The temperature profile for the rising temperature in the pipelines (the metal takes 60 seconds
to reach the water temperature):
These profiles are for:
ο· Supply temperature = 90Β°C;
ο· Return temperature = 40Β°C;
ο· Starting Ground temperature = 8Β° C
0 1 2 3 4 5 6
0
10
20
30
40
50
60
70
80
90
Time [s]
Edge1temperature[Β°C]
Temperature Growth in the Pipes at the Switch ON
12. 11
Mesh and its Refinement
The reliability of the solution depends on how the problem is discretized, and so on the mesh
quality.
n_mesh parameter
An important parameter from which depends the quality of the mesh is the number of
sides of the polygons which render the circular shapes of the two pipes. Again, since the
diameters of the pipes can vary, this value is function of these diameters.
n_meshret=n_mesh+ceil(retdiam*4*n_mesh)
n_meshsupp=n_mesh+ceil(suppdiam*4*n_mesh)
These functions to determine the n_mesh values are simply an example of the ones that
could be used to obtain an n_mesh growing with the size of the pipes.
The starting value n_mesh is function of the desired quality of the mesh. The user in our
program has three choices: 4, 16, 32.
13. 12
Talking about refinement, we tried to use different methods and levels of refinement in order
to get a good compromise between computation time and quality of the solution.
Standard Refinement
Introducing a third value (a scalar, in the example below equal to 0.05) in the use of the
mesh function it is possible to decide the level of the refinement.
Me=Mesh(Sh,Bc,0.05)
Here below there is a figure representing the mesh made with the previous command,
with n_mesh = 16.
βManualβ Refinement
Passing a function of x and y through the InRect function, it is possible to drive the
refinement towards specific zones. In our case obviously the important parts are the
pipes and the insulations.
Raf=@(x,y)0.1-0.08*(InRect([x,y],[0.5 0.5],[
(0.1+retdiam+retins*2) (0.1+retdiam+retins*2)])|InRect([x,y],[1
0.5],[ (0.1+suppdiam+suppins*2) (0.1+suppdiam+suppins*2)]));
Me=Mesh(Sh,Bc,Raf);
The parameters of the InRect function are the coordinates of the centers of the pipes
which represent the centers of the rectangular zones (in our case square zones since a
square approximates a circle better than a rectangle), and the width and the height of
these ones. Since in the last version of our program the dimensions of the pipes can be
0 0.5 1 1.5
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Mesh
14. 13
chosen by the user, the side of the square is function of pipes diameters and insulator
thicknesses.
The refinement is showed in the following figure (n_mesh = 16):
Particular Case (Mesh for ODE45)
Since computations for ODE functions are very heavy, we decide to let the user choose a
very rough discretization in order to make this functions work even with high inspection
times (40000 s, that are indeed necessary to reach the stationary solution).
Of course this is only a trick to obtain anyway a solution of ODE45 with our CPUs. The
mesh is quite ridiculous (made of 148 nodes). For example the circular pipes are
discretized as hexagons (n_mesh = 6).
0 0.5 1 1.5
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Mesh
0 0.5 1 1.5
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Mesh
15. 14
Computation Time in function of Triangle size
The time required to calculate the mesh depends on the geometry and on the triangle
maximum sideβs size. Here below both the number of triangles and the time for mesh
calculation vs the sideβs sizes are reported, as we can see the trends are proportional.
16. 15
Mesh quality
Here the plot of the mesh quality for different cases is reported. As expected, the lower the
triangle sideβs size higher the quality, even though it is not very high in some triangles at the
interface between pipes and ground.
Figure 1 - Low Mesh Quality - Standard Refinement (1) - 148 nodes
Figure 2- Standard Mesh Quality - Standard Refinement (0.05) - 1230 nodes
0 0.5 1 1.5
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Mesh quality
0.7
0.75
0.8
0.85
0.9
0.95
0 0.5 1 1.5
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Mesh quality
18. 17
Figure 5 - High Mesh Quality - Manual Refinement - 1909 nodes
Figure 6 - High Mesh Quality - Manual Refinement - 1909 nodes (Zoom)
As we can see lower the side higher the mesh quality. As explained before, we chose the mesh
refinement related to the best quality.
0 0.5 1 1.5
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Mesh quality
0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8
0.5
0.55
0.6
0.65
0.7
Mesh quality
0.65
0.7
0.75
0.8
0.85
0.9
0.95
19. 18
Matrix and vectors computations
Stiffness matrix
As said before the system to be solved is: ππ’β²
+ π΄π’ = π
Where M is the mass matrix, A the stiffness matrix and b the right-hand side vector, in our case
represented only by the boundary conditions because the external forcing is nil.
Since there is a mixture of boundary conditions we had to compose a proper function that
could manage this variety of conditions. Therefore we combined the function
AssemblaDiffRobin with the Function AsssemblaDiffNOm (we called it
AssemblaDiffNOmRobin2).
The stiffness matrix is the standard one, modified only in the Robin boundary nodes.
Right hand side vector
The computation of the elements in the vector b was done by assembling the following three
parts:
% bRobin : part of b vector due to Robin BCs
% b1 : part of b vector due to Dirichlet BCs on Return Pipe
% b2 : part of b vector due to Dirichlet BCs on Supply Pipe
Even though the temperatures of the metallic surface of the pipes are 40 and 90Β°C, the Dirichlet
boundary conditions in the main program are imposed for 1Β°C. This choice is driven by the need
of having a b vector versatile in function of the temperature during the time advancing analysis
(In the switching on of the system, the temperature of the metal increases from the
environment temperature to the water temperature). In fact in this way is sufficient to multiply
at each time step the part of b vector related to a certain pipe for the effective temperature of
that pipe.
Each pipe needs a different part of b vector since the growth in temperature and the final
temperature are different. To assign the right value of b to a certain node equation, we need to
use the function
Me.DirichletNodes(-jj,2)==3
(3 is the number of the shape, in this case we are searching the Dirichlet
Nodes of the Supply Pipe)
On the contrary, the part of the b vector related to Robin Boundary does not depend on the
temperatures in the pipes and keeps constant during the time advancing analysis.
Mass matrix
No particular modifications are needed in this case since it does not depend on the boundary
conditions. We simply used the function βAssemblaMatriceMassaβ.
20. 19
Initial Solution
The initial solution is obtained solving the following system
π΄π’(π‘ = 0)
in which A is the Stiffness Matrix, and b is the one for the temperature in the pipes at t=0
(Tsupply=Tsoil=8Β°C).
Stationary solution
The Stationary solution is obtained solving the same system, but considering the steady state
(tο β) temperature for the pipes (Tsupply=90Β°C, Treturn=40Β°C):
π΄π’ = π(π‘ β β)
21. 20
Time advancing
The dynamic solution is obtained by solving the system of differential equations:
ππ’β²
+ π΄π’ = π β π’β²
= βπβ1
π΄π’ + πβ1
π
As time advancing method different choices can be done.
In our assignment we decided to analyze the following ones:
1) Explicit Euler;
2) Implicit Euler;
3) Crank-Nicholson.
As known from theory, in the first one thereβs a restriction on the time step, indeed:
βπ‘ πππ₯ =
2
|π πππ₯|
Where Ξ»max is the maximum eigenvalue of the matrix M-1A, in Matlab:
lambda_max=max(eigs(inv(M)*A))); and tis value is, for the default Mesh refinement (the
rougher one) is:
|π πππ₯| = 1047.75
So
βπ‘ πππ₯ = 0.0019 π
Since we observed that our system reaches the stationary solution in 40000 s; this method has
not been applied, because it would have required too much time, for that time step.
Hence we applied only the last two advancing method, that have no constraints on the time
step.
22. 21
Linear system solution method
To solve the linear system the conjugate gradient was adopted. This method was also
integrated with preconditioning by using LU and Cholensky incomplete factorizations and so
obtaining the Preconditioned Conjugate Gradient Method (pcg).
Here below the trends of the relative residuals vs the iteration step are reported, for the
combinations advancing method/preconditioner. Obviously the minimum value is the same for
all these combinations, since the same tolerance was imposed, the things that change is the
slope, namely the velocity of solution converging.
As we can see from this plot the preconditioned methods converge in less number of steps and
implicit Euler method (EI) is faster than Crank-Nicholson (CK) one.
0 20 40 60 80 100 120
10
-8
10
-6
10
-4
10
-2
10
0
10
2
10
4
iteration number
relativeresidual
Methods Comparison
IE - No PreC
IE - LU PreC
IE - Ch PreC
CK - No PreC
CK - LU PreC
CK - Ch PreC
23. 22
Time comparison
The table here reported shows the main results of the various combinations between
advancing method and preconditioning. The EI method with Cholensky incomplete factorization
turns out to be the faster one, whereas the not preconditioned solutions require more time.
Advancing Method Preconditioning Time cond(M-1
A)
Implicit Euler
No prec. 6,38 200,28
LU 6,24 56,94
Cholensky 1,25 56,94
Crank-Nicholson
No prec. 8,54 162,31
LU 4,17 38,48
Cholensky 4,31 38,48
24. 23
Results
Below we are going to expose the results for our preferred geometry, but must be remembered
that in this program there are a few degrees of freedom in the choice of the system.
They are obtained by considering the refined mesh (localized βmanualβ refinement) with
standard n_mesh (around 23).
Geometry:
Mesh:
0 0.5 1 1.5
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Geometry
Shape 1
Shape 2
Shape 3
0 0.5 1 1.5
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Mesh
27. 26
Time Advancing Solution @ 300 s (growth in pipes finished):
Time Advancing Solution @ 15000 s (intermediate situation, temperature of the ground is
growing):
0
0.5
1
1.5
0
0.5
1
0
20
40
60
80
100
Time Advancing
0
0.5
1
1.5
0
0.5
1
0
20
40
60
80
100
Time Advancing
28. 27
Difference between steady state profile temperature and final values with time advancing
@15000 s:
Time Advancing Solution @ 40000 s (equilibrium is reached):
0
0.5
1
1.5
0
0.5
1
-4
-3
-2
-1
0
0
0.5
1
1.5
0
0.5
1
0
20
40
60
80
100
Time Advancing
29. 28
Difference between Stationary Solution and Time Advancing Solution @ 40000 s
On the results obtained we can say that they are reliable, because the temperature, fixed at
pipes internal side (Dirichlet condition) decreases with a strong gradient around the edge of the
tube and gradually on the soil. The temperature is also affected by convection with the outside
world as visible from the inclination of the distribution in the soil side subject to the Robin
boundary condition.
Also the time needed to reach the stationary solution is realistic (40000 s β 10 h).
0
0.5
1
1.5
0
0.5
1
-0.15
-0.1
-0.05
0
0.05
0.1
30. 29
ODE time advancing method
We applied also the function ODE45 to solve our problem.
In this function the time step is very small (β10-4 s) and cannot be modified by the user.
Since it couldnβt be solved in a reasonable time with a high quality mesh, we used a rougher
mesh (148 nodes) to have the solution until the steady state solution without the need of
waiting hours. Here below some plots, for different time interals are reported, and the
difference between ODE45 and Crank-Nicholson method is shown.
0
0.5
1
1.5
0
0.5
1
0
20
40
60
80
100
t= 200
0
0.5
1
1.5
0
0.5
1
-40
-30
-20
-10
0
Difference between time advancing and ODE45
33. 32
Conclusions
Of course the results depend mainly on the properties of insulation and of the ground. Since we
took very high values of conductivity to let the gradient be evident, the results are not so βrealβ.
For example the temperature in the ground near the pipes is increased of about 20-30Β°C (and it
is quite unlikely).
Anyway tuning these parameters to the real one, the results seem reliable.
The difference between time advancing method at tο β is negligible, so the two results
confirm their reliability each other.
Finally the refinement of the mesh has not a strong influence on the results, even though the
quality results to be slightly better, but it has deep effects in computation time.
In the following appendix the scripts are reported
34. 33
Appendix
Main Program (file PROGRAMMA.m)
The following is the main program used for all the calculations discussed. It is
divided in sections and it allows to the user to set the input parameter inside the
ground section. Moreover the user can choose what to evaluate and with which
method. This programs make use of a lot of function inside the foder
05ElementiFiniti2D that must be added to the path. In particular, as said in the
previous chapter the functions AssemblaDiffNOm and AssemblaDiffRobin have
been coupled in the function AssemblaDiffNOmRobin2, that is showed at the end
of the appendix.
clear all
close all
%% Physical Parameters (g=ground; s=supply ;r=return)
rhog=2000; %kg/m3 density
cg=1480; %J/kgK specific heat capacity
kg=30; %W/mK thermal conductivity
mhug=kg/cg;
rhos=20; %kg/m3 density
cs=500; %J/kgK specific heat capacity
ks=5;%W/mK thermal conductivity
mhus=ks/cs;
rhor=20; %%kg/m3
cr=500; %J/kgK specific heat capacity
kr=5;%W/mK thermal conductivity
mhur=kr/cr;
alfaair=20; %%W/m2K air conductivity
alfasoil=10;
%% Temperatures in the Pipes
disp('Temperature in the Supply Line');
tsupp=input('Supply Temperature:');
disp('Temperature in the Return Line');
tret=input('Return Temperature:');
disp('Temperature of the Enviroment');
tenv=input('Enviroment Temperature:');
%% Pipes Diameters
disp('Supply Pipe Diameter (m)');
suppdiam=input('Supply Diameter(suggested value 0.1):');
disp('Return Pipe Diameter (m)');
35. 34
retdiam=input('Return Diameter(suggested value 0.1):');
%% Insulator Thickness
disp('Insulator Thickness in the Supply Line (m)');
suppins=input('Supply Insulation(suggested value 0.1):');
disp('Insulator Thickness in the Supply Line (m)');
retins=input('Return Insulation(suggested value 0.05):');
%% Quality of the Mesh
disp('Quality of the Mesh');
disp('1 - Low (for quite high t_end in ODE solving)');
disp('2 - Standard');
disp('3 - High');
qual=input('Quality (2 default value):');
if qual==1
n_mesh=4;
elseif qual==3
n_mesh=32;
else
n_mesh=16;
end
%% Geometry
n_meshret=n_mesh+ceil(retdiam*4*n_mesh)
n_meshsupp=n_mesh+ceil(suppdiam*4*n_mesh)
Sh(2)=Circle([0.5 0.5],retdiam/2+retins,n_meshret)-Circle([0.5
0.5],retdiam/2,n_meshret); %%return pipe insulation
Sh(3)=Circle([1 0.5],suppdiam/2+suppins,n_meshsupp)-Circle([1
0.5],suppdiam/2,n_meshsupp);%%supply pipe insulation
Sh(1)=Rect([0.75 0.5], [1.5 1])-Circle([0.5 0.5],retdiam/2+retins,n_meshret)-
Circle([1 0.5],suppdiam/2+suppins,n_meshsupp);%-Circle([1 0.5],0.02,n_mesh)-
Circle([0.5 0.5],0.02,n_mesh);%%soil
Sh(1) = set(Sh(1), 'alfa',mhug, 'density', rhog);
Sh(2) = set(Sh(2), 'alfa',mhur, 'density', rhor);
Sh(3) = set(Sh(3), 'alfa',mhus, 'density', rhos);
%% Boundary Conditions
%Robin coefficients
hair=alfaair/kg;
hsoil=alfasoil/kg;
if tenv>7
Tinf=tenv+1;
Tinf2=tenv-1;
elseif tenv>0
Tinf=tenv;
Tinf2=tenv;
else
Tinf=tenv-1;
41. 40
tend=15000;
t=0;
while t<tend
if t<tson
t=t+dt1;
[u,flag]=pcg((M+dt1/2*A), (M*u-
dt1/2*A*u+dt1/2*((bRobin+b1*Temp2(t)+b2*Temp3(t))+(bRobin+b1*Temp2(t-
dt1)+b2*Temp3(t-dt1)))),1e-6,200,[],[],u);%Crank-Nicholson
elseif t<tend/100
t=t+dt2;
[u,flag]=pcg((M+dt2/2*A), (M*u-
dt2/2*A*u+dt2/2*((bRobin+b1*Temp2(t)+b2*Temp3(t))+(bRobin+b1*Temp2(t-
dt2)+b2*Temp3(t-dt2)))),1e-6,200,[],[],u);%Crank-Nicholson
elseif t<tend/20
t=t+dt3;
[u,flag]=pcg((M+dt3/2*A), (M*u-
dt3/2*A*u+dt3/2*((bRobin+b1*Temp2(t)+b2*Temp3(t))+(bRobin+b1*Temp2(t-
dt3)+b2*Temp3(t-dt3)))),1e-6,200,[],[],u);%Crank-Nicholson
else
t=t+dt4;
[u,flag]=pcg((M+dt4/2*A), (M*u-
dt4/2*A*u+dt4/2*((bRobin+b1*Temp2(t)+b2*Temp3(t))+(bRobin+b1*Temp2(t-
dt4)+b2*Temp3(t-dt4)))),1e-6,200,[],[],u);%Crank-Nicholson
end
uutadv(Me.UnknownNodes>0)= u;
uutadv(lato2)=Temp2(t);
uutadv(lato3)=Temp3(t);
hold off;
Draw(Me,uutadv);
zlim([0 100]);
title(['t= ' num2str(t)]);
drawnow;
end
title('Time Advancing');
figure(9)
title('Difference between time advancing and stationary solution');
Draw(Me,uutadv-uu);
%% ODE 45
disp('End Time in ODE45 (choose low value, suggested<1000 s, otherwise wait
more XD)');
tend=input('Tend (s):');
figure(7);
u=pcg(A,bRobin+b1*Temp2(0)+b2*Temp3(0),1e-3,100);
odepar=odeset('mass',M);
[t,u]=ode45(@(t,y)(-
A*y+(bRobin+b1*Temp2(t)+b2*Temp3(t))),linspace(0,tend,10),u,odepar);
uu=zeros(size(Me.Coordinates,1),1);
for k=1:length(t)
uu(Me.UnknownNodes>0)= u(k,:);
uu(lato2)=Temp2(t(k));
uu(lato3)=Temp3(t(k));
hold off;
42. 41
Draw(Me,uu);
zlim([0 100]);
title(['t= ' num2str(t(k))]);
drawnow;
end
figure(10)
Draw(Me,uu-uutadv);
title('Difference between time advancing and ODE45');
Auxiliary function: AssemblaDiffNOmRobin2
function [D,bRobin,b1,b2] = AssemblaDiffNOmRobin2(Me)
%It assembles D Matrix and b vector of this particular problem in which
%are involved non homogeneous Dirichlet, homogeneous Neumann and Robin
%boundary conditions
%
%Input:
% Me : Mesh
%
%Output:
% D : Stiffness Matrix
% bRobin : part of b vector due to Robin BCs
% b1 : part of b vector due to Dirichlet BCs on Return Pipe
% b2 : part of b vector due to Dirichlet BCs on Supply Pipe
Tr=Me.Triangles;
NI=Me.UnknownNodes;
C=Me.Coordinates;
N_Tr = size(Tr,1);
N_int = max(NI);
D = spalloc(N_int,N_int,10*N_int);
bRobin = zeros(N_int,1);
b1 = zeros(N_int,1);
b2 = zeros(N_int,1);
% b3 = zeros(N_int,1);
for e=1:N_Tr
Dx(1) = C(Tr(e,3),1) - C(Tr(e,2),1);
Dx(2) = C(Tr(e,1),1) - C(Tr(e,3),1);
Dx(3) = C(Tr(e,2),1) - C(Tr(e,1),1);
Dy(1) = C(Tr(e,2),2) - C(Tr(e,3),2);
Dy(2) = C(Tr(e,3),2) - C(Tr(e,1),2);
Dy(3) = C(Tr(e,1),2) - C(Tr(e,2),2);
Areass=Me.Aree(e);
%valuto la forza
xb=Me.CenterOfMass(e,1);
yb=Me.CenterOfMass(e,2);
for i=1:3
ii = NI(Tr(e,i));
if( ii > 0)
for j=1:3
jj = NI(Tr(e,j));
43. 42
mu=get(Me.Shapes(Me.Faces(e)),'alfa',[xb,yb]);
d=mu*(Dy(i)*Dy(j)+Dx(i)*Dx(j))/(4.0*Areass) ;
if( jj > 0)
D(ii,jj) = D(ii,jj) + d;
else
xy=C(Tr(e,j),:);
aux=Me.DirichletNodes(-jj,:);
val=Evaluate(Me.Bc, aux,xy);
if Me.DirichletNodes(-jj,2)==2
b1(ii) = b1(ii) - d*val ;
elseif Me.DirichletNodes(-jj,2)==3
b2(ii) = b2(ii) - d*val ;
else
% b3(ii) = b3(ii) - d*val ;
end
end
end
end
end
end
L=Me.Edges;
Ro=Me.EdgesRobin;
for k=1:length(Ro)
Nodo1=L(Ro(k),1);
Nodo2=L(Ro(k),2);
dist=norm(C(Nodo1,:)-C(Nodo2,:));
ii1=NI(Nodo1);
ii2=NI(Nodo2);
xmedia=(Me.Coordinates(Nodo1,1)+Me.Coordinates(Nodo2,1))/2;
ymedia=(Me.Coordinates(Nodo1,2)+Me.Coordinates(Nodo2,2))/2;
val=Evaluate(Me.Bc,Ro(k,:), [xmedia, ymedia]);
h=val(1);
g=val(2);
if ii1>0 && ii2<0 %ii1 Γ¨ incognito, ii2 Γ¨ noto
bRobin(ii1)=bRobin(ii1)+g/2*dist;
D(ii1,ii1)=D(ii1,ii1)+h*dist/3;
elseif ii1<0 && ii2>0 %ii1 Γ¨ noto, ii2 Γ¨ incognito
bRobin(ii2)=bRobin(ii2)+g/2*norm(dist);
D(ii2,ii2)=D(ii2,ii2)+h*dist/3;
else %allora sono entrambi di Neumann
bRobin(ii1)=bRobin(ii1)+g/2*norm(dist);
bRobin(ii2)=bRobin(ii2)+g/2*norm(dist);
D(ii1,ii1)=D(ii1,ii1)+h*dist/3;
D(ii2,ii2)=D(ii2,ii2)+h*dist/3;
D(ii1,ii2)=D(ii1,ii2)+h*dist/6;
D(ii2,ii1)=D(ii2,ii1)+h*dist/6;
end
end