You are on page 1of 9

/******************************************************************************

Welcome to GDB Online.


GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#include <math.h>
void main()
{

int a,j,i,ii,jj,iii,jjj,max,aa,bb,r,v,b,type,type1,type2;
printf("Enter the no. of. elements\n");
scanf("%d", &a);
printf("Enter the no. of. nodes\n");
scanf("%d", &max);
//printf("Enter the type of problem '1' for 1-D conduction and '2' 1-D fin
element :");
//scanf("%d", &type);

int Elenode[a][3];
float Nodecd[max],L[a],k[a],Q[a],Tfinal[max],h[a],Tamb[a],Per[a],Ar[a],Qtot;

for(i=1;i<=a;i++)
{
printf("Enter the starting node of element %d \n",i);
scanf("%d", &Elenode[i-1][0]);
printf("Enter the ending node of element %d \n",i);
scanf("%d", &Elenode[i-1][1]);
printf("Enter the type of element: '1' for 1-D conduction,'2' for 1-D fin
for element %d \n",i);
scanf("%d", &Elenode[i-1][2]);
}

for(i=1;i<=max;i++)
{
printf("Enter the x coordinate of node %d :",i);
scanf("%f", &Nodecd[i-1]);
}

//if(type==1)

for(i=1;i<=a;i++)
{
L[i-1]=sqrt(pow((Nodecd[(Elenode[i-1][1])-1]-Nodecd[(Elenode[i-1][0])-
1]),2));

if(Elenode[i-1][2]==1)
{
printf("Enter the thermal conductivity of the element %d :",i);
scanf("%f", &k[i-1]);
printf("Enter the heat generation per unit volume of the element
%d :",i);
scanf("%f", &Q[i-1]);
}

if(Elenode[i-1][2]==2)
{
printf("Enter the thermal conductivity of the element %d :",i);
scanf("%f", &k[i-1]);
printf("Enter the heat generation per unit volume of the element
%d :",i);
scanf("%f", &Q[i-1]);
printf("Enter the convective heat transfer coefficent of the element %d
:",i);
scanf("%f", &h[i-1]);
printf("Enter the ambient environmental temperature around the element
%d :",i);
scanf("%f", &Tamb[i-1]);
printf("Enter the perimeter of the element %d :",i);
scanf("%f", &Per[i-1]);
printf("Enter the cross-sectional area of the element %d :",i);
scanf("%f", &Ar[i-1]);

float Kele[a][2][2],Kglobal[max][max],Fglobal[max],Fele[a][2],fhmod[max];

for(ii=0;ii<=a-1;ii++)
{
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
Kele[ii][i][j]=0;
}

Fele[ii][i]=0;
}
}

for(ii=0;ii<=a-1;ii++)
{

if(Elenode[ii][2]==1)
{
Kele[ii][0][0]=k[ii]/L[ii];
Kele[ii][0][1]=-k[ii]/L[ii];
Kele[ii][1][0]=-k[ii]/L[ii];
Kele[ii][1][1]=k[ii]/L[ii];

Fele[ii][0]=Q[ii]*L[ii]/2;
Fele[ii][1]=Q[ii]*L[ii]/2;
}

if(Elenode[ii][2]==2)
{
Kele[ii][0][0]=(k[ii]/L[ii])+((Per[ii]*h[ii]*L[ii]/(6*Ar[ii]))*2);
Kele[ii][0][1]=-k[ii]/L[ii]+((Per[ii]*h[ii]*L[ii]/(6*Ar[ii]))*1);
Kele[ii][1][0]=-k[ii]/L[ii]+((Per[ii]*h[ii]*L[ii]/(6*Ar[ii]))*1);
Kele[ii][1][1]=k[ii]/L[ii]+((Per[ii]*h[ii]*L[ii]/(6*Ar[ii]))*2);

Fele[ii][0]=(Per[ii]*h[ii]*L[ii]*Tamb[ii]/(2*Ar[ii]));
Fele[ii][1]=(Per[ii]*h[ii]*L[ii]*Tamb[ii]/(2*Ar[ii]));
}
}

for(i=0;i<=(max-1);i++)
{
for(j=0;j<=(max-1);j++)
{

Kglobal[i][j]=0;
}

Fglobal[i]=0;
}

for(ii=0;ii<=a-1;ii++)
{
aa=Elenode[ii][0];
bb=Elenode[ii][1];

Kglobal[aa-1][aa-1]=Kglobal[aa-1][aa-1]+Kele[ii][0][0];
Kglobal[aa-1][bb-1]=Kglobal[aa-1][bb-1]+Kele[ii][0][1];
Kglobal[bb-1][aa-1]=Kglobal[bb-1][aa-1]+Kele[ii][1][0];
Kglobal[bb-1][bb-1]=Kglobal[bb-1][bb-1]+Kele[ii][1][1];

Fglobal[aa-1]=Fglobal[aa-1]+Fele[ii][0];
Fglobal[bb-1]=Fglobal[bb-1]+Fele[ii][1];

printf("\n\n\n************Kglobal*************\n\n\n");

for(i=0;i<=((max))-1;i++)
{ printf("[ ");
for(j=0;j<=((max))-1;j++)
{
printf(" %1.1f",Kglobal[i][j]);
}
printf(" ]\n");
}

int nz=2;
printf("Enter the total no. of. boundary conditions :");
scanf("%d",&nz);

int cons[nz][2],n,m,o;

for(i=0;i<=nz-1;i++)
{
printf("Enter the node no. for which you are entering the boundary
condition :");
scanf("%d",&cons[i][0]);
printf("Enter the type of boundary condition for node %d, '1' for dirichlet
type, '2' for convection type, '3' for heat flux type :",cons[i][0]);
scanf("%d",&cons[i][1]);
}

n=0;
m=0;
o=0;

for(i=0;i<=nz-1;i++)
{
if(cons[i][1]==1)
{
n=n+1;
}

if(cons[i][1]==2)
{
m=m+1;
}

if(cons[i][1]==3)
{
o=o+1;
}
}

float Tcons[n][2],Ccons[m][3],Qcons[m][2];
ii=0;
jj=0;
iii=0;

for(i=0;i<=nz-1;i++)
{
if(cons[i][1]==1)
{
Tcons[ii][0]=cons[i][0];
printf("Enter the value of temperature of node %d :",cons[i][0]);
scanf("%f",&Tcons[ii][1]);
ii=ii+1;
}

if(cons[i][1]==2)
{
Ccons[jj][0]=cons[i][0];
printf("Enter the value of convective heat transfer coefficient at node
%d :",cons[i][0]);
scanf("%f",&Ccons[jj][1]);
printf("Enter the value of ambient temperature at node %d :",cons[i]
[0]);
scanf("%f",&Ccons[jj][2]);
jj=jj+1;
}

if(cons[i][1]==3)
{
Qcons[iii][0]=cons[i][0];
printf("Enter the value of heat flux at node %d :",cons[i][0]);
scanf("%f",&Qcons[iii][1]);
iii=iii+1;
}

for(i=0;i<=((max))-1;i++)
{
fhmod[i]=Fglobal[i];
}

for(i=0;i<=((max))-1;i++)
{
for(j=1-1;j<=n-1;j++)
{
if((i+1)==Tcons[j][0])
{
for(r=0;r<=((max))-1;r++)
{
fhmod[r]=fhmod[r]-(Kglobal[r][i]*Tcons[j][1]);
}

}
}

for(i=0;i<=m-1;i++)
{

fhmod[(int)Ccons[i][0]-1]=fhmod[(int)Ccons[i][0]-1]+(Ccons[i][1]*Ccons[i]
[2]);
Kglobal[(int)Ccons[i][0]-1][(int)Ccons[i][0]-1]=Kglobal[(int)Ccons[i][0]-1]
[(int)Ccons[i][0]-1]+Ccons[i][1];

for(i=0;i<=o-1;i++)
{
fhmod[(int)Qcons[i][0]-1]=fhmod[(int)Qcons[i][0]-1]-Qcons[i][1];
}

float Kmod[((max))-n][((max))-n], Fmod[((max))-n];

for(i=0;i<=((max))-n-1;i++)
{
for(j=0;j<=((max))-n-1;j++)
{
Kmod[i][j]=0;
}

Fmod[i]=0;
}

iii=0;
for(ii=0;ii<=((max))-1;ii++)
{
v=0;
for(i=1-1;i<=n-1;i++)
{
if((ii+1)==Tcons[i][0])
{
v=v+1;
}
}
if(v==0)
{
jjj=0;
for(jj=0;jj<=((max))-1;jj++)
{
b=0;
for(j=1-1;j<=n-1;j++)
{
if((jj+1)==Tcons[j][0])
{
b=b+1;
}
}

if(b==0)
{
Kmod[iii][jjj]=Kglobal[ii][jj];
jjj=jjj+1;
}
}

Fmod[iii]=fhmod[ii];
iii=iii+1;
}

printf("\n\n\n******************Kmodified**********\n\n\n");
for(i=0;i<=((max))-n-1;i++)
{ printf("[ ");
for(j=0;j<=((max))-n-1;j++)
{
printf(" %1.1f",Kmod[i][j]);
}
printf(" ]\n");
}

printf("\n\n\n******************Fmodified**********\n\n\n");
for(i=0;i<=((max))-n-1;i++)
{
printf(" %f\n",Fmod[i]);

float AA[max-n][max-n], X[max-n], BB[max-n],sum,temp;

for(i=0;i<=(max)-n-1;i++)
{
for(j=0;j<=(max)-n-1;j++)
{
AA[i][j]=Kmod[i][j];
}

BB[i]=Fmod[i];
}
for(r=0;r<=((max))-n-1-1;r++)
{
for(i=r+1;i<=((max))-n-1;i++)
{

temp=(AA[i][r]/AA[r][r]);
for(j=r;j<=((max))-n-1;j++)
{
AA[i][j]= AA[i][j] - (temp*AA[r][j] );

BB[i]= BB[i] - (temp*BB[r]);


}
}

X[(max)-n-1]=BB[max-n-1]/AA[max-n-1][max-n-1];

for(i=((max))-n-1-1;i>=0;i--)
{
sum=BB[i];

for(j=i+1;j<=((max))-n-1;j++)
{
sum=sum - (AA[i][j]*X[j]);
}

X[i]=sum/AA[i][i];

printf("\n\n\n******************Solution***********\n\n\n");
for(ii=0;ii<=((max))-n-1;ii++)
{

printf("%f\n",X[ii]);
}

printf("\n\n\n******************bb***********\n\n\n");
for(ii=0;ii<=((max))-n-1;ii++)
{

printf("%f\n",BB[ii]);
}

printf("\n\n\n******************AAGaussian**********\n\n\n");
for(i=0;i<=((max))-n-1;i++)
{ printf("[ ");
for(j=0;j<=((max))-n-1;j++)
{
printf(" %1.1f",AA[i][j]);
}
printf(" ]\n");
}

for(i=0;i<=n-1;i++)
{
Tfinal[(int)Tcons[i][0]-1]=Tcons[i][1];
}
ii=0;
for(i=0;i<=max-1;i++)
{
v=0;
for(j=0;j<=n-1;j++)
{
if(i==(int)Tcons[j][0]-1)
{
v=v+1;
}
}

if(v==0)
{
Tfinal[i]=X[ii];
ii=ii+1;
}

printf("\n\n\n******************Nodal Temperature***********\n\n\n");
for(ii=0;ii<=((max))-1;ii++)
{

printf("The temperature of node %d is %f\n",ii+1,Tfinal[ii]);


}

printf("\n\n\n******************Element Heat Flux***********\n\n\n");


Qtot=0;
for(i=0;i<=a-1;i++)
{
if(Elenode[i][2]==1)
{
printf("The heat flux through conduction of the 1-D conduction element
%d is %f\n",i+1,-(k[i]/L[i])*(Tfinal[Elenode[i][1]-1]-Tfinal[Elenode[i][0]-1]));
}

if(Elenode[i][2]==2)
{
printf("The heat flow to the environment of the 1-D fin element %d is
%f\n",i+1,(h[i]*Per[i]*L[i])*( ((Tfinal[Elenode[i][1]-1]+Tfinal[Elenode[i][0]-
1])/2)- Tamb[i]));
Qtot=Qtot+( (h[i]*Per[i]*L[i])*( ((Tfinal[Elenode[i][1]-
1]+Tfinal[Elenode[i][0]-1])/2)- Tamb[i]));

printf("The total heat lost to the environment is %f",Qtot);

You might also like