You are on page 1of 12

#include

#include
#include
#include

<stdio.h>
<stdlib.h>
<math.h>
"matrix.h"

main()
{
int i, j, k, p, q, junk;
FILE *inputPtr;
if ((inputPtr = fopen("TrussInput.txt", "r")) == NULL)
printf("Error: no such file!");
int nodenum;
fscanf(inputPtr, "%s%d", &junk, &nodenum);
fscanf(inputPtr, "%s%s%s", &junk, &junk, &junk);
double node[nodenum][2];
for (i = 0; i < nodenum; i++)
{
fscanf(inputPtr, "%d%lf%lf", &junk, &node[i][0], &node[i][1]);
}
int linenum;
fscanf(inputPtr, "%s%d%s%s%s%s%s", &junk, &linenum, &junk, &junk, &junk, &ju
nk, &junk);
double E[linenum];
double A[linenum];
double start[linenum][2], end[linenum][2];
int startnode[linenum], endnode[linenum];
for (i = 0; i < linenum; i++)
{
fscanf(inputPtr, "%d%lf%lf%d%d", &junk, &E[i], &A[i], &j, &k);
startnode[i] = j - 1;
endnode[i] = k - 1;
start[i][0] = node[j - 1][0];
start[i][1] = node[j - 1][1];
end[i][0] = node[k - 1][0];
end[i][1] = node[k - 1][1];
}
int support[nodenum];
int supportnum;
fscanf(inputPtr, "%s%d%s%s", &junk, &supportnum, &junk, &junk);
for (i = 0; i < nodenum; i++)
{
support[i] = 0;
}
for (i = 0; i < supportnum; i++)
{
fscanf(inputPtr, "%d%d", &j, &junk);
support[j - 1] = junk;
}
double displacement[nodenum * 2];
for (i =0; i < nodenum; i++)
{

if (support[i] == 0)
{
displacement[i * 2] = 1;
displacement[i * 2 + 1] =
}
else if (support[i] == 1)
{
displacement[i * 2] = 0;
displacement[i * 2 + 1] =
}
else if (support[i] == 2)
{
displacement[i * 2] = 1;
displacement[i * 2 + 1] =
}
else if (support[i] == 3)
{
displacement[i * 2] = 0;
displacement[i * 2 + 1] =
}

1;

1;

0;

0;

}
double load[nodenum * 2];
int loadnum;
fscanf(inputPtr, "%s%d%s%s%s", &junk, &loadnum, &junk, &junk, &junk);
for (i = 0; i < nodenum * 2; i++)
{
load[i] = 0;
}
double load1, load2;
for (i = 0; i < loadnum; i++)
{
fscanf(inputPtr, "%d%lf%lf", &junk, &load1, &load2);
load[(junk - 1) * 2] = load1;
load[(junk - 1) * 2 + 1] = load2;
}
double L[linenum];
for (i = 0; i < linenum; i++)
{
L[i] = sqrt(pow((end[i][0] - start[i][0]),2) + pow((end[i][1] - start[i]
[1]),2));
}
double LocalStiff[linenum][4][4];
for (i = 0; i < linenum; i++)
{
LocalStiff[i][0][0] = ((E[i] * A[i])/L[i])
0]) / L[i]),2);
LocalStiff[i][0][1] = ((E[i] * A[i])/L[i])
L[i]) * ((end[i][0] - start[i][0]) / L[i]);
LocalStiff[i][0][2] = ((E[i] * A[i])/L[i])
t[i][0]) / L[i]),2);
LocalStiff[i][0][3] = ((E[i] * A[i])/L[i])
[1])/ L[i]) * ((end[i][0] - start[i][0]) / L[i]);
LocalStiff[i][1][0] = ((E[i] * A[i])/L[i])
L[i]) * ((end[i][0] - start[i][0]) / L[i]);

* pow(((end[i][0] - start[i][
* ((end[i][1] - start[i][1])/
* -1 * pow(((end[i][0] - star
* -1 * ((end[i][1] - start[i]
* ((end[i][1] - start[i][1])/

LocalStiff[i][1][1] = ((E[i] * A[i])/L[i])


1])/ L[i]),2);
LocalStiff[i][1][2] = ((E[i] * A[i])/L[i])
[1])/ L[i]) * ((end[i][0] - start[i][0]) / L[i]);
LocalStiff[i][1][3] = ((E[i] * A[i])/L[i])
t[i][1])/ L[i]),2);
LocalStiff[i][2][0] = ((E[i] * A[i])/L[i])
t[i][0]) / L[i]),2);
LocalStiff[i][2][1] = ((E[i] * A[i])/L[i])
[1])/ L[i]) * ((end[i][0] - start[i][0]) / L[i]);
LocalStiff[i][2][2] = ((E[i] * A[i])/L[i])
0]) / L[i]),2);
LocalStiff[i][2][3] = ((E[i] * A[i])/L[i])
L[i]) * ((end[i][0] - start[i][0]) / L[i]);
LocalStiff[i][3][0] = ((E[i] * A[i])/L[i])
[1])/ L[i]) * ((end[i][0] - start[i][0]) / L[i]);
LocalStiff[i][3][1] = ((E[i] * A[i])/L[i])
t[i][1])/ L[i]),2);
LocalStiff[i][3][2] = ((E[i] * A[i])/L[i])
L[i]) * ((end[i][0] - start[i][0]) / L[i]);
LocalStiff[i][3][3] = ((E[i] * A[i])/L[i])
1])/ L[i]),2);
}

* pow(((end[i][1] - start[i][
* -1 * ((end[i][1] - start[i]
* -1 * pow(((end[i][1] - star
* -1 * pow(((end[i][0] - star
* -1 * ((end[i][1] - start[i]
* pow(((end[i][0] - start[i][
* ((end[i][1] - start[i][1])/
* -1 * ((end[i][1] - start[i]
* -1 * pow(((end[i][1] - star
* ((end[i][1] - start[i][1])/
* pow(((end[i][1] - start[i][

k = 0;
int globalsize = nodenum * 2;
double GlobalStiff[globalsize][globalsize];
for (i = 0; i < globalsize; i++)
{
for (j = 0; j < globalsize; j++)
{
GlobalStiff[i][j] = 0;
}
}
FILE *globalPtr;
globalPtr = fopen("TrussOutput.csv", "w");
for (i = 0; i < linenum; i++)
{
j = startnode[i];
k = endnode[i];
GlobalStiff[j * 2][j * 2] += LocalStiff[i][0][0];
GlobalStiff[j * 2][j * 2 + 1] += LocalStiff[i][0][1];
GlobalStiff[j * 2 + 1][j * 2] += LocalStiff[i][1][0];
GlobalStiff[j * 2 + 1][j * 2 + 1] += LocalStiff[i][1][1];
GlobalStiff[k * 2][k * 2] += LocalStiff[i][2][2];
GlobalStiff[k * 2][k * 2 + 1] += LocalStiff[i][2][3];
GlobalStiff[k * 2 + 1][k * 2] += LocalStiff[i][3][2];
GlobalStiff[k * 2 + 1][k * 2 + 1] += LocalStiff[i][3][3];
GlobalStiff[j * 2][k * 2] += LocalStiff[i][0][2];
GlobalStiff[j * 2][k * 2 + 1] += LocalStiff[i][0][3];
GlobalStiff[j * 2 + 1][k * 2] += LocalStiff[i][1][2];
GlobalStiff[j * 2 + 1][k * 2 + 1] += LocalStiff[i][1][3];
GlobalStiff[k * 2][j * 2] += LocalStiff[i][2][0];
GlobalStiff[k * 2][j * 2 + 1] += LocalStiff[i][2][1];
GlobalStiff[k * 2 + 1][j * 2] += LocalStiff[i][3][0];
GlobalStiff[k * 2 + 1][j * 2 + 1] += LocalStiff[i][3][1];
}

int num = 0;
int order[globalsize];
for (i = 0; i < globalsize; i++)
{
order[i] = i + 1;
}
for (i = 0; i < globalsize; i++)
{
if (displacement[i] != 0)
{
num++;
}
}
double temprow[globalsize];
double tempcol[globalsize];
int tempsupport;
i = 0;
j = 0;
int tempval;
while (i < nodenum)
{
if (support[i] == 0)
{
j += 2;
i++;
}
if (support[i] == 1)
{
for (p = 0; p < globalsize; p++)
{
temprow[p] = GlobalStiff[j][p];
}
tempval = order[j];
for (p = j + 1; p < globalsize; p++)
{
for (q = 0; q < globalsize; q++)
{
GlobalStiff[p - 1][q] = GlobalStiff[p][q];
}
}
for (p = j + 1; p < globalsize; p++)
{
order[p - 1] = order[p];
}
for (p = 0; p < globalsize; p++)
{
GlobalStiff[globalsize - 1][p] = temprow[p];
}
order[globalsize - 1] = tempval;
for (p = 0; p < globalsize; p++)
{
tempcol[p] = GlobalStiff[p][j];
}
for (p = 0; p < globalsize; p++)

{
for (q = j + 1; q < globalsize; q++)
{
GlobalStiff[p][q - 1] = GlobalStiff[p][q];
}
}
for (p = 0; p < globalsize; p++)
{
GlobalStiff[p][globalsize - 1] = tempcol[p];
}
j++;
i++;
}
else if (support[i] == 2)
{
for (p = 0; p < globalsize; p++)
{
temprow[p] = GlobalStiff[j + 1][p];
}
tempval = order[j + 1];
for (p = j + 1 + 1; p < globalsize; p++)
{
for (q = 0; q < globalsize; q++)
{
GlobalStiff[p - 1][q] = GlobalStiff[p][q];
}
}
for (p = j + 1 + 1; p < globalsize; p++)
{
order[p - 1] = order[p];
}
for (p = 0; p < globalsize; p++)
{
GlobalStiff[globalsize - 1][p] = temprow[p];
}
order[globalsize - 1] = tempval;
for (p = 0; p < globalsize; p++)
{
tempcol[p] = GlobalStiff[p][j + 1];
}
for (p = 0; p < globalsize; p++)
{
for (q = j + 1 + 1; q < globalsize; q++)
{
GlobalStiff[p][q - 1] = GlobalStiff[p][q];
}
}
for (p = 0; p < globalsize; p++)
{
GlobalStiff[p][globalsize - 1] = tempcol[p];
}
j++;
i++;
}

else if (support[i] == 3)
{
for (p = 0; p < globalsize; p++)
{
temprow[p] = GlobalStiff[j][p];
}
tempval = order[j];
for (p = j + 1; p < globalsize; p++)
{
for (q = 0; q < globalsize; q++)
{
GlobalStiff[p - 1][q] = GlobalStiff[p][q];
}
}
for (p = j + 1; p < globalsize; p++)
{
order[p - 1] = order[p];
}
for (p = 0; p < globalsize; p++)
{
GlobalStiff[globalsize - 1][p] = temprow[p];
}
order[globalsize - 1] = tempval;
for (p = 0; p < globalsize; p++)
{
tempcol[p] = GlobalStiff[p][j];
}
for (p = 0; p < globalsize; p++)
{
for (q = j + 1; q < globalsize; q++)
{
GlobalStiff[p][q - 1] = GlobalStiff[p][q];
}
}
for (p = 0; p < globalsize; p++)
{
GlobalStiff[p][globalsize - 1] = tempcol[p];
}
for (p = 0; p < globalsize; p++)
{
temprow[p] = GlobalStiff[j][p];
}
tempval = order[j];
for (p = j + 1; p < globalsize; p++)
{
for (q = 0; q < globalsize; q++)
{
GlobalStiff[p - 1][q] = GlobalStiff[p][q];
}
}
for (p = j + 1; p < globalsize; p++)
{
order[p - 1] = order[p];
}
for (p = 0; p < globalsize; p++)
{

GlobalStiff[globalsize - 1][p] = temprow[p];


}
order[globalsize - 1] = tempval;
for (p = 0; p < globalsize; p++)
{
tempcol[p] = GlobalStiff[p][j];
}
for (p = 0; p < globalsize; p++)
{
for (q = j + 1; q < globalsize; q++)
{
GlobalStiff[p][q - 1] = GlobalStiff[p][q];
}
}
for (p = 0; p < globalsize; p++)
{
GlobalStiff[p][globalsize - 1] = tempcol[p];
}
i++;
}
}
Matrix modglobal, modload, moddisp;
double GlobalStiffmod[num][num];
modglobal.row = num;
modglobal.col = num;
modglobal.mat=(double **)malloc(sizeof(double *)*modglobal.row);
for(i=0;i<modglobal.row;i++)
{
modglobal.mat[i]=(double*)malloc(sizeof(double)*modglobal.col);
}
for (i = 0; i < num; i++)
{
for (j = 0; j < num; j++)
{
GlobalStiffmod[i][j] = GlobalStiff[i][j];
modglobal.mat[i][j] = GlobalStiff[i][j];
}
}
double displacementmod[num];
moddisp.row = num;
moddisp.col = 1;
moddisp.mat=(double **)malloc(sizeof(double *)*moddisp.row);
for(i=0;i<moddisp.row;i++)
{
moddisp.mat[i]=(double*)malloc(sizeof(double)*moddisp.col);
}
j = 0;
for (i = 0; i < globalsize; i++)
{
if (displacement[i] != 0)
{
displacementmod[j] = displacement[i];
moddisp.mat[j][0] = displacement[i];
j++;
}

}
double loadmod[num];
j = 0;
modload.row = num;
modload.col = 1;
modload.mat=(double **)malloc(sizeof(double *)*moddisp.row);
for(i=0;i<modload.row;i++)
{
modload.mat[i]=(double*)malloc(sizeof(double)*modload.col);
}
for (i = 0; i < globalsize; i++)
{
if (displacement[i] != 0)
{
loadmod[j] = load[i];
modload.mat[j][0] = load[i];
j++;
}
}
moddisp = Gauss_Jordan(modglobal, modload);
Matrix reactglobal;
reactglobal.row = globalsize - num;
reactglobal.col = num;
reactglobal.mat=(double **)malloc(sizeof(double *)*reactglobal.row);
for(i=0;i<reactglobal.row;i++)
{
reactglobal.mat[i]=(double*)malloc(sizeof(double)*reactglobal.col);
}
p = 0;
q = 0;
for (i = num; i < globalsize; i++)
{
q = 0;
for (j = 0; j < num; j++)
{
reactglobal.mat[p][q] = GlobalStiff[i][j];
q++;
}
p++;
}
Matrix reactforce;
reactforce = Matrix_Mult(reactglobal, moddisp);
Matrix tempdisp;
tempdisp.row = globalsize;
tempdisp.col = 1;
tempdisp.mat=(double **)malloc(sizeof(double *)*tempdisp.row);
for(i=0;i<tempdisp.row;i++)
{
tempdisp.mat[i]=(double*)malloc(sizeof(double)*tempdisp.col);
}
for (i = 0; i < globalsize; i++)
{
if (i < num)
{

tempdisp.mat[i][0] = moddisp.mat[i][0];
}
else
{
tempdisp.mat[i][0] = 0;
}
}
Matrix disp;
disp.row = globalsize;
disp.col = 1;
disp.mat=(double **)malloc(sizeof(double *)*disp.row);
for(i=0;i<disp.row;i++)
{
disp.mat[i]=(double*)malloc(sizeof(double)*disp.col);
}
for (i = 0; i < globalsize; i++)
{
j = 0;
while (order[j] != i + 1)
{
j++;
}
disp.mat[i][0] = tempdisp.mat[j][0];
}
fprintf(globalPtr, "Diplacements\n\n");
for (i = 0; i < globalsize; i++)
{
fprintf(globalPtr, "u%d,=,%lf\n", i +1, disp.mat[i][0]);
}
fprintf(globalPtr, "\n\n");
double deltanode[i][2];
for (i = 0; i < nodenum; i++)
{
deltanode[i][0] = disp.mat[i * 2][0];
deltanode[i][1] = disp.mat[i * 2 + 1][0];
}
for (i = 0; i < nodenum; i++)
{
for (j = 0; j < 2; j++)
{
node[i][j] = node[i][j] + deltanode[i][j];
}
}
fprintf(globalPtr, "\nNew Coordinates of the Nodes:\n");
fprintf(globalPtr, "Node,x,y\n");
for (i = 0; i < nodenum; i++)
{
fprintf(globalPtr, "%d,", i + 1);
for (j = 0; j < 2; j++)
{
fprintf(globalPtr, "%lf,", node[i][j]);
}
fprintf(globalPtr, "\n");

}
fprintf(globalPtr, "\n\n");
Matrix templocal;
templocal.row = 4;
templocal.col = 4;
templocal.mat=(double **)malloc(sizeof(double *)*templocal.row);
for(i=0;i<templocal.row;i++)
{
templocal.mat[i]=(double*)malloc(sizeof(double)*templocal.col);
}
Matrix tempdisplace;
tempdisplace.row = 4;
tempdisplace.col = 1;
tempdisplace.mat=(double **)malloc(sizeof(double *)*tempdisplace.row);
for(i=0;i<tempdisplace.row;i++)
{
tempdisplace.mat[i]=(double*)malloc(sizeof(double)*tempdisplace.col);
}
Matrix internal;
internal.row = 4;
internal.col = 1;
internal.mat=(double **)malloc(sizeof(double *)*internal.row);
for(i=0;i<internal.row;i++)
{
internal.mat[i]=(double*)malloc(sizeof(double)*internal.col);
}
double internalforce[linenum][4];
double internalphy[linenum];
for (i = 0; i < linenum; i++)
{
for (j = 0; j < 4; j++)
{
for (k = 0; k < 4; k++)
{
templocal.mat[j][k] = LocalStiff[i][j][k];
}
}
tempdisplace.mat[0][0] = disp.mat[startnode[i] * 2][0];
tempdisplace.mat[1][0] = disp.mat[startnode[i] * 2 + 1][0];
tempdisplace.mat[2][0] = disp.mat[endnode[i] * 2][0];
tempdisplace.mat[3][0] = disp.mat[endnode[i] * 2 + 1][0];
internal = Matrix_Mult(templocal, tempdisplace);
for (p = 0; p < 4; p++)
{
internalforce[i][p] = internal.mat[p][0];
}
internalphy[i] = sqrt(pow(internalforce[i][0],2) + pow(internalforce[i][
1],2));
}
int remark[linenum];
for (i = 0; i < linenum; i++)
{
if (internalphy[i] == 0)
remark[i] = 0;
else if (start[i][0] == end[i][0] && start[i][1] < end[i][1] && internal
force[i][0] == 0 && internalforce[i][1] > 0)

remark[i] = 2;
else if (start[i][0] == end[i][0] && start[i][1] < end[i][1] && internal
force[i][0] == 0 && internalforce[i][1] < 0)
remark[i] = 1;
else if (start[i][0] == end[i][0] && start[i][1] < end[i][1] && internal
force[i][0] == 0 && internalforce[i][1] == 0)
remark[i] = 0;
else if (start[i][0] == end[i][0] && start[i][1] > end[i][1] && internal
force[i][0] == 0 && internalforce[i][1] > 0)
remark[i] = 1;
else if (start[i][0] == end[i][0] && start[i][1] > end[i][1] && internal
force[i][0] == 0 && internalforce[i][1] < 0)
remark[i] = 2;
else if (start[i][0] < end[i][0] && start[i][1] == end[i][1] && internal
force[i][0] > 0 && internalforce[i][1] == 0)
remark[i] = 2;
else if (start[i][0] < end[i][0] && start[i][1] == end[i][1] && internal
force[i][0] < 0 && internalforce[i][1] == 0)
remark[i] = 1;
else if (start[i][0] > end[i][0] && start[i][1] == end[i][1] && internal
force[i][0] > 0 && internalforce[i][1] == 0)
remark[i] = 1;
else if (start[i][0] > end[i][0] && start[i][1] == end[i][1] && internal
force[i][0] < 0 && internalforce[i][1] == 0)
remark[i] = 2;
else if (start[i][0] < end[i][0] && start[i][1] < end[i][1] && internalf
orce[i][0] > 0 && internalforce[i][1] > 0)
remark[i] = 2;
else if (start[i][0] < end[i][0] && start[i][1] < end[i][1] && internalf
orce[i][0] < 0 && internalforce[i][1] < 0)
remark[i] = 1;
else if (start[i][0] > end[i][0] && start[i][1] > end[i][1] && internalf
orce[i][0] > 0 && internalforce[i][1] > 0)
remark[i] = 1;
else if (start[i][0] > end[i][0] && start[i][1] > end[i][1] && internalf
orce[i][0] < 0 && internalforce[i][1] < 0)
remark[i] = 2;
else if (start[i][0] > end[i][0] && start[i][1] < end[i][1] && internalf
orce[i][0] > 0 && internalforce[i][1] < 0)
remark[i] = 1;
else if (start[i][0] > end[i][0] && start[i][1] < end[i][1] && internalf
orce[i][0] < 0 && internalforce[i][1] > 0)
remark[i] = 2;
else if (start[i][0] < end[i][0] && start[i][1] > end[i][1] && internalf
orce[i][0] > 0 && internalforce[i][1] < 0)
remark[i] = 2;
else if (start[i][0] < end[i][0] && start[i][1] > end[i][1] && internalf
orce[i][0] < 0 && internalforce[i][1] > 0)
remark[i] = 1;
else
remark[i] = 0;
}
fprintf(globalPtr, "Internal Forces\n\n");
for (i = 0; i < linenum; i++)
{
fprintf(globalPtr, "Member %d\n", i + 1);
for (j = 0; j < 4; j++)
{
fprintf(globalPtr, "F%d,=,%lf\n", j + 1, internalforce[i][j]);

}
fprintf(globalPtr, "Force Magnitude,%lf\n", internalphy[i]);
if (remark == 0 || internalphy[i] == 0)
fprintf(globalPtr, "Force Direction, None");
else if (remark[i] == 1)
fprintf(globalPtr, "Force Direction, Tensile");
else if (remark[i] == 2)
fprintf(globalPtr, "Force Direction, Compressive");
fprintf(globalPtr, "\n\n");
}
printf("You can now see the output file in TrussOutput.csv. Thank you for us
ing the program!");
getch();
return 0;
}