1: //#include<iostream>
2: #include<stdio.h>
3: #include<math.h>
4: #include<conio.h>
5: #include<io.h>
6:
7: int main()
8: {
9: int integer,matrix,step,i,B,condition,left,right,code,p;
10: //for matrix number//
11: printf("enter number of grids \n \n");
12: scanf("%d",&integer);
13: matrix=integer;
14: //for no of steps//
15:
16:
17: //for defining floats and arrays//
18: float d[matrix],dr[matrix],b[matrix],a[matrix],x,y,delTime,alphanot,
beta,length,Told[matrix+1],Tnew[matrix+1],r,Tright,Tleft,ap[matrix],
aw[matrix],ae[matrix],S[matrix],Q[2],residue;
19: float C1,C2,C3,C4,K,h,q,Tamb,rho,Cp,rms,sum;
20: //rms//
21: rms=1;
22: //length//
23: printf("what is the length of system\n \n");
24: scanf("%f",&length);
25: x=length/matrix;
26: printf("%f\n",x);
27: residue = 0.001; //residue
28: dr[1] = 0.5*x;
29: dr[0] = 0.0;
30: dr[matrix+1] = length;
31: for(i=2;i<=matrix;i++)
32: dr[i] = dr[i-1] + x;
33: //time//
34:
35: printf("what is delta time\n \n");
36: scanf("%f",&delTime);
37: printf("%f\n",delTime);
38: //beta//
39: printf("what is beta\n \n");
40: scanf("%f",&beta);
41: printf("%f\n",beta);
42: //alpha not//
43: //desnsity of substance//
44: printf("Value of density in kg/m^3:\n \n");
45: scanf("%f",&rho);
46: printf("%f\n",rho);
47: //thermal conductivity of substance//
48: printf("Value of thermal conductivityin W/k:\n \n");
49: scanf("%f",&K);
50: printf("%f\n",K);
51: //specific heat capicity of material//
52: printf("Value of specific heat capacity in J/kg.k \n \n");
53: scanf("%f",&Cp);
54: printf("%f\n",Cp);
55: //alpha not//
56: alphanot = ((K)/(rho*Cp));
57: //signing old temperature values equals to zero//
58: //OR putting initial temperature of system//
59: for(i=1;i<=matrix;i++)
60: {
61: Told[i]=0;
62: }
63:
64: //loop making for selection of boundary conditons//
65: printf("BOUNDARY CONDITIONS FOR LEFT SIDE\n \n");
66: for(code=1;code<=2;code++)
67: {
68: printf("choose the boundary condition \n 1.Ditchlet boundary
condition for constant temperature\n 2.Insulated boundary condition\n
3.Robins boundary condition for convection\n 4.Newmans boundary
condition of heat flux\n \n");
69: scanf("%d",&condition);
70: Q[code]=condition;
71: if(condition==1)
72: {
73: printf("this is the programm code for one dimensional
implicit time marching when two temeratures at boundary conditions
are given TH and TK NOW \n \n");
74: printf("what is temperature at boundary\n \n");
75: //for ditchlet condition//
76: if(code==1)
77: {
78: //temperature at left side//
79: printf("what is the temperature at left side\n \n");
80: scanf("%f",&Tleft);
81: }
82: else if(code==2)
83: {
84: //temperature at right side//
85: printf("what is the temperature at right side\n \n");
86: scanf("%f",&Tright);
87: }
88:
89: }
90: else if(condition==2)
91: {
92: //for insulation condition//
93: printf("this is the programm code for one dimensional
implicit time marching when insulated boundary condition is applied
at both the boundaries.. NOW \n \n");
94:
95: }
96: else if(condition==3)
97: {
98:
99: //for Robins condition//
100: printf("this is the programm code for one dimensional
implicit time marching when ROBINS boundary conditions is applied at
both east and west sides,, NOW \n \n");
101:
102: printf("what is coefficient of convective heat transfer
for Robins condition \n \n");
103: scanf("%f",&h);
104: //thermal conductivity//
105: printf("you have already selcted the thermal
conductivity\n");
106: printf("%fK==\n",K);
107:
108: printf("what is ambient temperature of surroundings for
Robins condition \n \n");
109: scanf("%f",&Tamb);
110:
111: C1=h*x;
112: C2=C1/K;
113: }
114: else if(condition==4)
115: {
116: //for newmans condition//
117: printf("this is the programm code for one dimensional implicit
time marching when newmans heat flux condition is applied in
horizontal direction on both sides ; NOW \n \n");
118:
119: printf(" what is heat generated q in heat flux in
surroundings \n \n");
120: scanf("%f",&q);
121: //thermal conductivity//
122: printf("you have already selcted the thermal
conductivity\n");
123: printf("%fK==\n",K);
124:
125: C3=q*x;
126: C4=C3/K;
127:
128: }
129: printf("BOUNDARY CONDITION FOR RIGHT SIDE\n \n");
130: }
131:
132: //giving condition values to right and left sides//
133: left=Q[1];
134: right=Q[2];
135:
136: //initiate the main loop//
137: for(step=1;rms>residue;step++)
138: {
139: printf("for step==%d\n \n",step);
140: y=x/2;
141: //initial and final boundary Told (assumed ) for differnt
boundary conditions//
142: //for creating boundary conditions on left//
143: for(i=1;i<=1;i++)
144: {
145: if(left==1)
146: {
147: Told[0]=2*Tleft-Told[1];
148: }
149: else if(left==2)
150: {
151: Told[0]=Told[1];
152:
153: }
154: else if(left==3)
155: {//here it is assumed that initial temperature of the system is
zero and Tamb is more than that on both the side//
156: Told[0]=(1-C2)*Told[1]+C2*Tamb;
157:
158: }
159: else if(left==4)
160: {
161: Told[0]=Told[1]+C4;
162:
163: }
164:
165: }
166: for(i=1;i<=1;i++)
167: {
168: //for creating boundary conditions on right//
169: if(right==1)
170: {
171:
172: Told[matrix+1]=2*Tright-Told[matrix];
173: }
174: else if(right==2)
175: {
176:
177: Told[matrix+1]=Told[matrix];
178: }
179: else if(right==3)
180: {//here it is assumed that initial temperature of the system is
zero and Tamb is more than that on both the side//
181:
182: Told[matrix+1]=(1-C2)*Told[matrix]+C2*Tamb;
183: }
184: else if(right==4)
185: {
186:
187: Told[matrix+1]=Told[matrix]-C4;
188: }
189:
190: }
191:
192:
193: for(i=1;i<=matrix;i++)
194: {
195: r=((alphanot*delTime)/(x*x));
196: ap[i]=(1+2*r+(beta*r*(2*Told[i]+Told[i-1]+Told[i+1]))/(2));
197: aw[i]=-1*(r+(beta*r*(Told[i]+Told[i-1]))/(2));
198: ae[i]=-1*(r+(beta*r*(Told[i]+Told[i+1]))/(2));
199: }
200: //printing ap aw and ae for checking//
201:
202: for(i=1;i<=matrix;i++)
203: {
204: //printf(" The value of ap is %f\n",ap[i]);
205: //printf(" The value of aw is %f\n",aw[i]);
206: //printf(" The value of ae is %f\n",ae[i]);
207: }
208:
209: for(i=1;i<=matrix;i++)
210: {
211: d[i]=ap[i];
212: b[i]=aw[i];
213: a[i]=ae[i];
214: }
215: //diagonal initial and final elements according to different
boundary conditions//
216: for(i=1;i<=1;i++)
217: {
218: if(left==1)
219: {
220: d[1]=ap[1]-aw[1];
221:
222: }
223: else if(left==2)
224: {
225: d[1]=ap[1]+aw[1];
226:
227: }
228: else if(left==3)
229: {
230: d[1]=ap[1]+aw[1]*(1-C2);
231:
232: }
233: else if(left==4)
234: {
235: d[1]=ap[1]+aw[1];
236:
237: }
238: }
239:
240:
241: for(i=1;i<=1;i++)
242: {
243: //recreating boundarry condition on right side//
244: if(right==1)
245: {
246:
247: d[matrix]=ap[matrix]-ae[matrix];
248: }
249: else if(right==2)
250: {
251:
252: d[matrix]=ap[matrix]+ae[matrix];
253: }
254: else if(right==3)
255: {
256:
257: d[matrix]=ap[matrix]+ae[matrix]*(1-C2);
258: }
259: else if(right==4)
260: {
261:
262: d[matrix]=ap[matrix]+ae[matrix];
263: }
264: }
265:
266:
267:
268: //formation of resultant matrix//
269: for(i=1;i<=matrix;i++)
270: {
271: S[i]=Told[i];
272: }
273: //resultant matrix initial and final elments according to
different boundary conditions//
274: for(i=1;i<=1;i++)
275: {
276: if(left==1)
277: {
278: S[1]=Told[1]-2*aw[1]*Tleft;
279:
280: }
281: else if(left==2)
282: {
283: S[1]=Told[1];
284:
285: }
286: else if(left==3)
287: {
288: S[1]=Told[1]-aw[1]*Tamb*C2;
289:
290: }
291: else if(left==4)
292: {
293: S[1]=Told[1]-aw[1]*C4;
294:
295: }
296: }
297:
298: for(i=1;i<=1;i++)
299: {
300: //recreating boundary conditions for right side//
301: if(right==1)
302: {
303:
304: S[matrix]=Told[matrix]-2*ae[matrix]*Tright;
305: }
306: else if(right==2)
307: {
308:
309: S[matrix]=Told[matrix];
310: }
311: else if(right==3)
312: {
313:
314: S[matrix]=Told[matrix]-ae[matrix]*Tamb*C2;
315: }
316: else if(right==4)
317: {
318:
319: S[matrix]=Told[matrix]+ae[matrix]*C4;
320: }
321: }
322:
323:
324:
325: //forward sweep//
326: for(i=2;i<=matrix;i++) //forward sweep
327: {
328: d[i] = (d[i] - ((b[i]/d[i-1])*a[i-1]));
329: S[i] = (S[i] - ((b[i]/d[i-1])*S[i-1]));
330: }
331:
332: //t new//
333: Tnew[matrix] = S[matrix]/d[matrix]; //backword sweep
334:
335: for(i=matrix-1;i>0;i--)
336: {
337: Tnew[i] = ((S[i] - (a[i]*Tnew[i+1]))/d[i]);
338: }
339:
340:
341: //printing Tnew//
342:
343: for(i=1;i<=matrix;i++)
344: {
345: printf(" %d x==%f Temp==%f\n \n",i,y,Tnew[i]);
346: y=y+x;
347: }
348: //for rms and residue//
349: sum=0;
350: for(i=1;i<=matrix;i++)
351: { sum = sum + pow((Told[i]-Tnew[i]),2);
352: Told[i]=Tnew[i];
353: }
354: rms = sqrt(sum/matrix);
355: //for directory//
356: if (step%1==0)
357: {
358: char filename[1000]; //////size of charecter
359: FILE*f2;
360: sprintf(filename,"TDMA\\%d.dat",step);
361: f2=fopen(filename,"w");
362: fprintf(f2,"VARIABLES=X,T\n");
363: fprintf(f2,"\nZONE T=\"0\" i=%d, ZONETYPE=ORDERED,
DATAPACKING=POINT\n",matrix);
364: for (i=1;i<=matrix;i++)
365: {
366:
367: {
368: fprintf(f2,"%f\t%.10f\n ",dr[i],Tnew[i]);
369: }
370: }
371: fclose(f2);
372: }
373:
374:
375: }
376: //printf("%d",step);
377:
378: //-------------file writing------------//
379: FILE *f1;
380: //for(i=0;i<=nx;i++)
381: {
382:
383: f1 = fopen("temp.dat","w");
384: fprintf(f1,"VARIABLES=""X"",""TEMPERATURE""\nZONE I=%d
ZONETYPE=ORDERED DATAPACKING=POINT\n",matrix+2);
385: Tnew[0]=Tleft;
386: Tnew[matrix+1]=Tright;
387: for(i=0;i<=matrix+1;i++)
388: fprintf(f1,"%lf\t%lf\n",dr[i],Tnew[i]);
389: }
390: fclose(f1);
391:
392: return 0;
393: }