1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
|
/*
This file is part of the FElt finite element analysis package.
Copyright (C) 1993-2000 Jason I. Gobat and Darren C. Atkinson
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/***************************************************************************
*
* File: fe.c
*
* Description: Contains code to implement various mathematical features of
* the finite element method.
*
* Notes: The compact column storage scheme is managed invisibly by the
* low-level matrix manipulation routines sdata and mdata.
*
* History: v2.3x by Jason Gobat and Darren Atkinson
*
***************************************************************************/
# include <stdio.h>
# include <math.h>
# include "allocate.h"
# include "problem.h"
# include "fe.h"
# include "error.h"
extern Matrix ZeroRowCol ( );
/**************************************************************************
*
* Function: FindDOFS
*
* Description: FindDOFS will search through all of the elements for a
* problem and determine which DOFs (out of the six that
* are physically possible) must be considered in this
* problem based on the different element types. The
* list of affected DOFs is built and ...
*
*****************************************************************************/
int FindDOFS ( )
{
Element *e;
unsigned ne;
unsigned i,
j;
Definition type,
otype;
unsigned flag[7];
unsigned count;
ne = problem.num_elements;
e = problem.elements;
for (i = 1 ; i <= 6 ; i++) {
flag[i] = 0;
problem.dofs_pos[i] = 0;
problem.dofs_num[i] = 0;
}
otype = NULL;
for (i = 1 ; i <= ne ; i++) {
type = e[i] -> definition;
if (type != otype) {
for (j = 1 ; j <= e[i] -> definition -> numdofs ; j++)
flag [e[i] -> definition -> dofs[j]] = 1;
otype = type;
}
}
count = 0;
for (i = 1 ; i <= 6 ; i++) {
if (flag[i]) {
problem.dofs_pos [i] = ++count;
problem.dofs_num [count] = i;
}
}
problem.num_dofs = count;
return count;
}
/****************************************************************************
*
* Function: ConstructStiffness
*
* Description: For a given set of elements (possibly of varying types)
* this will assemble all element stiffness matrices into
* the global stiffness matrix according to what DOFs each
* individual element affects (a function of both its node
* numbers and the DOFs that it affects and their relation
* to the global DOFs as indexed by dofs).
*
* Before we do anything we figure out how the compact
* column storage scheme is going to work, i.e., we need
* to set up the height and diag arrays which contain
* information needed to go from a standard row,column
* notation (which I think is easier to read) to
* an address into the compact column vector representation
* of the global stiffness matrix
*
****************************************************************************/
Matrix ConstructStiffness (status)
int *status;
{
Element *element;
unsigned numelts,
numnodes;
unsigned active;
unsigned *dofs;
unsigned row,
col,
i,
j,
l,
k,
m;
unsigned size,
ndofs,
nodes;
unsigned base_row,
base_col,
affected_row_dof,
affected_col_dof;
unsigned *ht,*dg;
unsigned address;
double value;
Vector K;
int err,
err_count;
element = problem.elements;
numelts = problem.num_elements;
numnodes = problem.num_nodes;
active = problem.num_dofs;
dofs = problem.dofs_pos;
err_count = 0;
/*
* first we make a pass over the elements to see how all the
* stiffnesses fit together so we can set up our compact column
* storage sceme. Inefficient as hell I concede.
*/
size = numnodes*active;
ht = Allocate (unsigned, size);
if (ht == NULL)
Fatal ("allocation error setting up compact column heights");
UnitOffset (ht);
dg = Allocate (unsigned, size);
if (dg == NULL)
Fatal ("allocation error setting up compact column diagonal addresses");
UnitOffset (dg);
for (i = 1; i <= size ; i++)
ht[i] = dg[i] = 0;
for (i = 1 ; i <= numelts ; i++) {
err = ElementSetup (element [i], 0);
if (err) {
err_count += err;
continue;
}
ndofs = element[i] -> definition -> numdofs;
nodes = element[i] -> definition -> numnodes;
if (element[i] -> K == NullMatrix || !IsSquare(element[i] -> K) ||
Mrows(element[i] -> K) > ndofs*nodes) {
error ("%s element %d has an invalid stiffness matrix",
element[i] -> definition -> name, element[i] -> number);
err_count ++;
continue;
}
for (j = 1 ; j <= nodes ; j++) {
if (element [i] -> node[j] == NULL) continue;
base_row = (element[i] -> node[j] -> number - 1)*active + 1;
for (k = 1 ; k <= nodes ; k++) {
if (element [i] -> node[k] == NULL) continue;
base_col = (element[i] -> node[k] -> number - 1)*active + 1;
for (l = 1 ; l <= ndofs ; l++) {
affected_row_dof = dofs[element[i] -> definition -> dofs[l]];
row = base_row + affected_row_dof - 1;
for (m = 1 ; m <= ndofs ; m++) {
affected_col_dof = dofs[element[i] -> definition -> dofs[m]];
col = base_col + affected_col_dof - 1;
value = MatrixData (element[i] -> K) [(j-1)*ndofs + l]
[(k-1)*ndofs + m];
if (value != 0.0 && row <= col) {
if (col-(row-1) > ht [col])
ht [col] = col - (row - 1);
}
}
}
}
}
} /* end first loop over elements */
if (err_count) {
*status = err_count;
return NULL;
}
/*
* setup the diagonal address array and figure out how big
* we need to make the compact column vector
*/
dg[1] = 1;
size = 1;
if (ht [1] == 0)
ht [1] = 1;
for (i = 2 ; i <= numnodes*active ; i++) {
if (ht[i] == 0)
ht[i] = 1;
size += ht[i];
dg [i] = ht [i] + dg [i-1];
}
ZeroOffset (ht);
Deallocate (ht);
K = CreateCompactMatrix (numnodes*active, numnodes*active, size, dg);
detail ("stiffness matrix size is %d", size);
ZeroMatrix (K);
/*
* now we make just about the identical passes over the elements,
* the result of this pass however will be that we actually
* start sticking stuff into the vector which is the compact
* column representation of the global stiffness matrix
*/
for (i = 1 ; i <= numelts ; i++) {
ndofs = element[i] -> definition -> numdofs;
nodes = element[i] -> definition -> numnodes;
for (j = 1 ; j <= nodes ; j++) {
if (element [i] -> node[j] == NULL) continue;
base_row = (element[i] -> node[j] -> number - 1)*active + 1;
for (k = 1 ; k <= nodes ; k++) {
if (element [i] -> node[k] == NULL) continue;
base_col = (element[i] -> node[k] -> number - 1)*active + 1;
for (l = 1 ; l <= ndofs ; l++) {
affected_row_dof = dofs[element[i] -> definition -> dofs[l]];
row = base_row + affected_row_dof - 1;
for (m = 1 ; m <= ndofs ; m++) {
affected_col_dof = dofs[element[i] -> definition -> dofs[m]];
col = base_col + affected_col_dof - 1;
value = MatrixData (element[i] -> K) [(j-1)*ndofs + l]
[(k-1)*ndofs + m];
if (row <= col) {
address = ConvertRowColumn (row, col, K);
if (address)
VectorData (K) [address] += value;
}
}
}
}
}
if (!element[i] -> definition -> retainK) {
DestroyMatrix (element[i] -> K);
element[i] -> K = NullMatrix;
}
} /* end second loop over elements */
/*
* set some things up for the return
*/
*status = err_count;
return K;
}
/****************************************************************************
*
* Function: RemoveConstrainedDOF
*
* Description: As opposed to simply zeroing out the rows and columns
* associated with a constrained DOF, here we actually reduce
* the size of the stiffness and mass matrices by removing
* those rows and columns entirely.
*
****************************************************************************/
void RemoveConstrainedDOF (K, M, C, Kcond, Mcond, Ccond)
Matrix K;
Matrix M;
Matrix C;
Matrix *Kcond;
Matrix *Mcond;
Matrix *Ccond;
{
Node *node;
unsigned numnodes,
active;
unsigned *dofs;
unsigned orig_dofs;
unsigned new_dofs;
unsigned height;
char *dof_map;
Matrix Kc, Mc, Cc;
unsigned size;
unsigned *diag;
unsigned start;
unsigned i, j, n, m,
affected_dof,
base_dof;
numnodes = problem.num_nodes;
node = problem.nodes;
active = problem.num_dofs;
dofs = problem.dofs_num;
orig_dofs = numnodes * active;
dof_map = Allocate (char, orig_dofs);
UnitOffset (dof_map);
for (i = 1 ; i <= orig_dofs ; i++)
dof_map [i] = 1;
size = K -> size;
new_dofs = orig_dofs;
/*
* first we get a count of the number of constrained DOF and build
* a bitmap vector to tell us where they are in the original scheme
*/
for (i = 1 ; i <= numnodes ; i++) {
base_dof = active*(node[i] -> number - 1);
for (j = 1 ; j <= active ; j++) {
if (node [i] -> constraint -> constraint [dofs[j]]) {
affected_dof = base_dof + j;
dof_map [affected_dof] = 0;
new_dofs --;
if (affected_dof == 1)
size -= 1;
else
size -= (K -> diag [affected_dof] -
K -> diag [affected_dof - 1]);
}
}
}
/*
* now we know how much space we are saving so we can allocate
* space for the condensed stiffness and mass matrices
*/
Cc = NullMatrix;
Kc = CreateCompactMatrix (new_dofs, new_dofs, size, NULL);
Mc = CreateCompactMatrix (new_dofs, new_dofs, size, NULL);
if (C != NullMatrix)
Cc = CreateCompactMatrix (new_dofs, new_dofs, size, NULL);
diag = Allocate (unsigned, new_dofs);
UnitOffset (diag);
n = 1;
m = 1;
/*
* now we make a column loop over all of the original DOF to see
* which ones to copy through
*/
for (i = 1 ; i <= orig_dofs ; i++) {
if (dof_map [i]) {
if (i == 1) {
height = 1;
start = 1;
}
else {
height = K -> diag [i] - K -> diag [i-1];
start = K -> diag [i - 1] + 1;
}
/*
* check all the active rows in this column and copy them through
* if necessary - updating the diagonal address array for this
* column once we are through
*/
for (j = start ; j <= K -> diag [i] ; j++) {
affected_dof = i - height + 1 + (j - start);
if (dof_map [affected_dof]) {
Kc -> data [m][1] = K -> data [j][1];
Mc -> data [m][1] = M -> data [j][1];
if (C != NullMatrix)
Cc -> data [m][1] = C -> data [j][1];
m++;
}
}
diag [n++] = m - 1;
}
}
Kc -> diag = diag;
/*
* allocate, copy and assign a different diag pointer for the mass
* matrix to allow for the possibility that the stiffness and the
* mass matrices will be destroyed at different times
*/
Mc -> diag = Allocate (unsigned, new_dofs);
UnitOffset (Mc -> diag);
for (i = 1 ; i <= new_dofs ; i++)
Mc -> diag [i] = diag [i];
if (C != NullMatrix) {
Cc -> diag = Allocate (unsigned, new_dofs);
UnitOffset (Cc -> diag);
for (i = 1 ; i <= new_dofs ; i++)
Cc -> diag [i] = diag [i];
}
/*
* set the pointers for return
*/
*Kcond = Kc;
*Mcond = Mc;
if (C != NullMatrix)
*Ccond = Cc;
ZeroOffset (dof_map);
Deallocate (dof_map);
return;
}
/****************************************************************************
*
* Function: ZeroConstrainedDOF
*
* Description: For a fixed BC at a given DOF all we'll do is zero
* out the rows and columns of the stiffness matrix
* associated with that DOF (with a one on the diagonal
* for stability). For a displacement BC, we'll need
* to adjust the force vector, also, we should put
* the displacement into the force vector so when we
* go to solve, we'll just go ahead and get that
* displacement right back. We don't deal with hinges
* here really because we already dealt with them when we had
* the element stiffness matrix laying around. All we'll
* do here is make sure that the displacement will come
* out zero.
*
****************************************************************************/
void ZeroConstrainedDOF (K, F, Kc, Fc)
Vector K;
Vector F;
Vector *Kc;
Vector *Fc;
{
Node *node;
unsigned active;
unsigned *dofs;
Vector Kcond;
Vector Fcond;
unsigned i,j,
affected_dof,
base_dof;
node = problem.nodes;
active = problem.num_dofs;
dofs = problem.dofs_num;
/*
* allocate and copy the condensed objects
*/
Kcond = CreateCopyMatrix (K);
Fcond = NullMatrix;
if (F != NULL)
Fcond = CreateCopyMatrix (F);
for (i = 1 ; i <= problem.num_nodes ; i++) {
base_dof = active*(node[i] -> number - 1);
for (j = 1 ; j <= active ; j++) {
if (node[i] -> constraint -> constraint[dofs[j]]) {
affected_dof = base_dof + j;
if (node[i] -> constraint -> dx[dofs[j]].value == 0.0 ||
node[i] -> constraint -> constraint[dofs[j]] == 'h') {
Kcond = ZeroCompactRowCol (Kcond, affected_dof);
if (F != NULL)
VectorData (Fcond) [affected_dof] = 0;
}
else {
if (F != NULL) {
AdjustForceVector (Fcond, Kcond, affected_dof,
node[i] -> constraint -> dx[dofs[j]].value);
VectorData (Fcond) [affected_dof] =
node[i] -> constraint -> dx[dofs[j]].value;
}
Kcond = ZeroCompactRowCol (Kcond, affected_dof);
}
}
}
}
*Kc = Kcond;
if (F != NULL)
*Fc = Fcond;
return;
}
/****************************************************************************
*
* Function: AdjustForceVector
*
* Description: Given a displacement boundary condition, we can't just
* knock out the rows and columns of K for that DOF. We
* need to adjust the force vector by adding the effect
* of the stiffness in that DOF times the given displacement
* to the external force vector. The simplest way to do
* that is to just loop over all DOFs and adjust them
* as appropriate. In some cases the adjustment is unnecessary
* because the displaced DOF may not affect all DOFs or
* a given DOF may be fixed anyways. It won't hurt to
* operate in these cases anyways though, so that's what I do.
*
****************************************************************************/
void AdjustForceVector (Fcond, Kcond, affected_dof, dx)
Vector Fcond;
Vector Kcond;
unsigned affected_dof;
double dx;
{
unsigned i;
unsigned address;
unsigned size;
size = Mrows(Fcond);
for (i = 1 ; i <= size ; i++) {
address = ConvertRowColumn (i, affected_dof, Kcond);
if (address)
VectorData (Fcond) [i] -= VectorData (Kcond) [address]*dx;
}
return;
}
/****************************************************************************
*
* Function: ZeroCompactRowCol
*
* Description: Zeros out the row and column given by dof. Places
* a one on the diagonal.
*
****************************************************************************/
Vector ZeroCompactRowCol (K, dof)
Vector K;
unsigned dof;
{
unsigned i;
unsigned address;
unsigned size;
size = Mrows(K);
for (i = 1 ; i <= size ; i++) {
address = ConvertRowColumn (i, dof, K);
if (address)
VectorData (K) [address] = 0;
}
address = ConvertRowColumn (dof, dof, K);
if (address) /* though this should always be valid */
VectorData (K) [address] = 1;
return K;
}
/****************************************************************************
*
* Function: ConstructForceVector
*
* Description: Constructs the global nodal force vector based on all
* nodal forces and the global DOFs active at those nodes.
* Global DOF determination is by node number and the
* and the relationship between the force and its actual
* physical DOF and the location of this DOF in problem space.
*
****************************************************************************/
Vector ConstructForceVector ( )
{
Node *node;
unsigned active;
unsigned numnodes;
unsigned *dofs;
unsigned i,j,
base_dof;
unsigned size;
double force;
Vector F;
node = problem.nodes;
active = problem.num_dofs;
dofs = problem.dofs_num;
numnodes = problem.num_nodes;
size = numnodes*active;
F = CreateVector (size);
if (F == NullVector)
Fatal ("allocation error constructing global nodal force vector");
for (i = 1 ; i <= size ; i++)
VectorData (F) [i] = 0;
for (i = 1 ; i <= numnodes ; i++) {
base_dof = active*(node[i] -> number - 1);
for (j = 1 ; j <= active ; j++) {
force = 0.0;
if (node[i] -> force != NULL) {
if (node[i] -> force -> force[dofs[j]].value)
force += node[i] -> force -> force[dofs[j]].value;
}
if (node[i] -> eq_force != NULL) {
if (node[i] -> eq_force[dofs[j]])
force += node[i] -> eq_force[dofs[j]];
}
VectorData (F) [base_dof + j] = force;
}
}
return F;
}
/****************************************************************************
*
* Function: ClearNodes
*
* Description: sets all the displacements on the nodes to zero and
* clears the equivalent force vector
*
****************************************************************************/
void ClearNodes ( )
{
unsigned i,j;
for (i = 1 ; i <= problem.num_nodes ; i++) {
for (j = 1 ; j <= 6 ; j++)
problem.nodes [i] -> dx[j] = 0.0;
if (problem.nodes [i] -> eq_force != NULL)
for (j = 1 ; j <= 6 ; j++)
problem.nodes [i] -> eq_force[j] = 0.0;
}
}
/****************************************************************************
*
* Function: FactorStiffnessMatrix
*
* Description: Factorizes the problem stiffness matrix in place
*
****************************************************************************/
int FactorStiffnessMatrix (K)
Vector K;
{
Node *node;
unsigned numnodes;
unsigned active;
unsigned *dofs;
unsigned i;
unsigned size;
active = problem.num_dofs;
dofs = problem.dofs_pos;
node = problem.nodes;
numnodes = problem.num_nodes;
size = active*numnodes;
for (i = 1 ; i <= size ; i++) {
if (VectorData (K) [K -> diag[i]] == 0.0) {
error ("zero on the diagonal (row %d) of stiffness matrix",i);
return 1;
}
}
if (CroutFactorMatrix (K)) {
error ("could not factorize global stiffness matrix");
return 1;
}
return 0;
}
/****************************************************************************
*
* Function: SolveForDisplacements
*
* Description: Solves the linear system Kd=F for the vector of global
* nodal displacements. The system must not be singular
* (i.e. K and F should be condensed)
*
****************************************************************************/
Vector SolveForDisplacements (K, F)
Vector K;
Vector F;
{
if (FactorStiffnessMatrix (K))
return NullVector;
if (CroutBackSolveMatrix (K, F)) {
error ("could not back substitute for nodal displacements");
return NullVector;
}
ApplyNodalDisplacements (F);
return F;
}
/****************************************************************************
*
* Function: SolveStaticLoadCases
*
* Description: builds a table of nodal DOF displacements for all defined
* loadcases
*
****************************************************************************/
Matrix SolveStaticLoadCases (K, Fbase)
Matrix K;
Matrix Fbase;
{
unsigned i,j,k;
Matrix dtable;
Matrix F;
LoadCase lc;
int *mask;
if (FactorStiffnessMatrix (K))
return NullMatrix;
F = CreateColumnVector (Mrows(Fbase));
mask = BuildConstraintMask ( );
dtable = CreateFullMatrix (problem.num_loadcases,
analysis.numnodes * analysis.numdofs);
for (i = 1 ; i <= problem.num_loadcases ; i++) {
lc = problem.loadcases [i];
ZeroMatrix (F);
AssembleLoadCaseForce (F, lc);
/*
* Fbase already contains everything we need to know
* about displacment BC so all we need to do is to
* make sure that we add _nothing_ at all into the
* force vector at any constrained DOF
*/
for (j = 1 ; j <= Mrows(F) ; j++)
sdata(F, j, 1) = (mask [j] ? 0.0 : mdata(F,j,1));
AddMatrices (F, F, Fbase);
if (CroutBackSolveMatrix (K, F)) {
error ("could not back substitute for displacements in loadcase %s",
lc -> name);
return NullMatrix;
}
for (k = 1 ; k <= analysis.numnodes ; k++) {
for (j = 1 ; j <= analysis.numdofs ; j++) {
sdata(dtable, i, (k-1)*analysis.numdofs + j) =
mdata(F, GlobalDOF (analysis.nodes [k] -> number, analysis.dofs[j]), 1);
}
}
}
ZeroOffset (mask); Deallocate (mask);
return dtable;
}
/****************************************************************************
*
* Function: SolveStaticLoadRange
*
* Description: builds a table of nodal DOF displacements for
* input forcing at a single DOF over a range of
* force magnitudes
*
****************************************************************************/
Matrix SolveStaticLoadRange (K, Fbase)
Matrix K;
Matrix Fbase;
{
unsigned i,j,k;
Matrix dtable;
int *mask;
unsigned num_cases;
double force;
unsigned input_pos;
Matrix F;
if (FactorStiffnessMatrix (K))
return NullMatrix;
mask = BuildConstraintMask ( );
num_cases = (fabs(analysis.stop - analysis.start) + 0.5*fabs(analysis.step))
/ fabs(analysis.step) + 1;
dtable = CreateFullMatrix (num_cases, analysis.numnodes * analysis.numdofs);
input_pos = GlobalDOF (analysis.input_node -> number, analysis.input_dof);
F = CreateColumnVector (Mrows(Fbase));
for (i = 1 ; i <= num_cases ; i++) {
force = analysis.start + (i - 1)*analysis.step;
CopyMatrix (F, Fbase);
if (!mask [input_pos])
sdata(F, input_pos, 1) = mdata(F,input_pos,1) + force;
if (CroutBackSolveMatrix (K, F)) {
error ("could not back substitute for displacements");
return NullMatrix;
}
for (k = 1 ; k <= analysis.numnodes ; k++) {
for (j = 1 ; j <= analysis.numdofs ; j++) {
sdata(dtable, i, (k-1)*analysis.numdofs + j) =
mdata(F, GlobalDOF (analysis.nodes [k] -> number, analysis.dofs[j]), 1);
}
}
}
ZeroOffset (mask); Deallocate (mask);
return dtable;
}
/***************************************************************************
*
* Function: AssembleLoadCaseForce
*
* Description:
*
****************************************************************************/
void AssembleLoadCaseForce (F, lc)
Matrix F;
LoadCase lc;
{
unsigned active;
unsigned *dofs;
unsigned i,j;
unsigned base_dof;
double force;
active = problem.num_dofs;
dofs = problem.dofs_num;
for (i = 1 ; i <= lc -> numforces ; i++) {
base_dof = active*(lc -> nodes [i] -> number - 1);
for (j = 1 ; j <= active ; j++) {
force = 0.0;
force += lc -> forces [i] -> force[dofs[j]].value;
/*
if (node[i] -> eq_force != NULL) {
if (node[i] -> eq_force[dofs[j]])
force += node[i] -> eq_force[dofs[j]];
}
*/
sdata(F, base_dof + j, 1) = force;
}
}
return;
}
/***************************************************************************
*
* Function: ApplyNodalDisplacements
*
* Description:
*
****************************************************************************/
void ApplyNodalDisplacements (d)
Matrix d;
{
unsigned i, j;
unsigned base_dof;
unsigned prob_dof;
Node *node;
unsigned *dofs;
unsigned numnodes;
unsigned active;
active = problem.num_dofs;
dofs = problem.dofs_pos;
node = problem.nodes;
numnodes = problem.num_nodes;
for (i = 1 ; i <= numnodes ; i++) {
base_dof = active*(node[i] -> number - 1);
prob_dof = 1;
for (j = 1 ; j <= 6 ; j++) {
if (dofs [j]) {
node[i] -> dx[j] = mdata(d, base_dof + prob_dof, 1);
prob_dof++;
}
else
node[i] -> dx[j] = 0.0;
}
}
return;
}
/***************************************************************************
*
* Function: SolveForReactions
*
* Description: Pretty simple really, first we find how many reaction
* forces there should be, then we allocate space for them,
* then we multiply rows of the stiffness matrix by the
* global displacement vector to get an entry that was
* previously unknown in the global force vector
*
****************************************************************************/
unsigned SolveForReactions (K, d, old_numbers, reac)
Vector K;
Vector d;
unsigned *old_numbers;
Reaction **reac;
{
Node *node;
unsigned numnodes,
active,
*dofs;
unsigned i,j,k,m,
affected_dof,
base_dof,
num_reactions;
unsigned size;
unsigned address;
double sum;
node = problem.nodes;
numnodes = problem.num_nodes;
active = problem.num_dofs;
dofs = problem.dofs_num;
size = active * numnodes;
/*
* find the number of reactions and allocate some space for them
*/
num_reactions = 0;
for (i = 1 ; i <= numnodes ; i++) {
for (j = 1 ; j <= active ; j++) {
if (node[i] -> constraint -> constraint[dofs[j]] == 1)
num_reactions++;
}
}
if (num_reactions == 0)
return 0;
if (!(*reac = Allocate(Reaction, num_reactions)))
Fatal ("allocation error finding reactions");
UnitOffset (*reac);
for (i = 1 ; i <= num_reactions ; i++) {
if (!((*reac) [i] = Allocate (struct reaction, 1)))
Fatal ("allocation error finding reactions");
}
m = 1;
for (i = 1 ; i <= numnodes ; i++) {
base_dof = active*(node[i] -> number - 1);
for (j = 1 ; j <= active ; j++) {
if (node[i] -> constraint -> constraint[dofs[j]] == 1) {
sum = 0;
affected_dof = base_dof + j;
for (k = 1 ; k <= size ; k++) {
address = ConvertRowColumn (affected_dof, k, K);
if (address)
sum += VectorData (K) [address]*VectorData (d) [k];
}
if (old_numbers == NULL)
(*reac) [m] -> node = node[i] -> number;
else
(*reac) [m] -> node = old_numbers [i];
(*reac) [m] -> dof = dofs[j];
if (node [i] -> eq_force != NULL)
sum -= node [i] -> eq_force [dofs[j]];
(*reac) [m++] -> force = sum;
}
}
}
return num_reactions;
}
/***************************************************************************
*
* Function: ElementSetup
*
* Description: calls the appropriate function to assemble the element
* stiffness matrix for an element. Each element stiffness
* function should be of the form: xxxSetup(element,mass_mode)
* where x is the element type (as defined in element.h)
* element is the element to assemble the stiffness for
* and mass_mode is 0 in static cases or 'c' or 'l' in
* transient analysis when a mass matrix should be formed.
*
****************************************************************************/
# if defined (__STDC__)
int ElementSetup (Element element, char mass_mode)
# else
int ElementSetup (element, mass_mode)
Element element;
char mass_mode;
# endif
{
int status;
status = element -> definition -> setup (element, mass_mode, 0);
return status;
}
/***************************************************************************
*
* Function: ElementStresses
*
* Description: calls the element stress functions for all of the elements
*
****************************************************************************/
int ElementStresses ( )
{
Element *e;
Node *n;
unsigned ne;
unsigned nn;
int i, j, status;
e = problem.elements;
ne = problem.num_elements;
n = problem.nodes;
nn = problem.num_nodes;
status = 0;
for (i = 1 ; i <= ne ; i++)
status += e [i] -> definition -> stress (e [i]);
/*
* compute the nodally averaged stresses
*/
for (i = 1 ; i <= nn ; i++) {
if (n [i] -> stress && n [i] -> numelts) {
for (j = 1 ; j <= 10 ; j++)
n [i] -> stress [j] /= n [i] -> numelts;
}
}
return status;
}
/***************************************************************************
*
* Function: CheckAnalysisParameters
*
* Description: Verifies that everything in the analysis parameters
* section is set (or at least the minimum number of
* things that we need) for the given analysis type.
*
***************************************************************************/
int CheckAnalysisParameters (mode)
AnalysisType mode;
{
unsigned count;
count = 0;
switch (mode) {
case StaticLoadCases:
if (analysis.numnodes <= 0) {
error ("need to specify a node list for load cases w/static analysis");
count ++;
}
if (analysis.numdofs <= 0) {
error ("need to specify a DOF list for load cases w/static analysis");
count ++;
}
break;
case StaticLoadRange:
if (analysis.numnodes == 0) {
error ("need to specify node list for load ranges w/static analysis");
count++;
}
if (analysis.numdofs == 0) {
error ("need to specify DOF list for load ranges w/static analysis");
count++;
}
if (analysis.input_dof == 0) {
error ("need to specify input DOF for load ranges w/static analysis");
count++;
}
if (analysis.input_node == 0) {
error ("need to specify input node for load ranges w/static analysis");
count++;
}
if (analysis.start == analysis.stop) {
error ("start cannot equal stop for load ranges w/static analysis");
}
if (analysis.step == 0.0) {
error ("step must be non-zero for load ranges w/static analysis");
count++;
}
if (analysis.step * (analysis.stop - analysis.start) < 0) {
error ("start and stop not compatible with step direction");
count ++;
}
break;
case StaticSubstitution:
if (analysis.tolerance <= 0.0) {
error ("tolerance must be defined for static substitution analysis");
count++;
}
if (analysis.iterations <= 0) {
error ("iterations must be defined for static substitution analysis");
count ++;
}
if (analysis.load_steps <= 0) {
error ("load steps must be defined for static substitution analysis");
count ++;
}
break;
case StaticIncremental:
if (analysis.load_steps <= 0) {
error ("load-steps must be defined for static substitution analysis");
count++;
}
break;
case Transient:
if (analysis.mass_mode == 0) {
error ("mass-mode must be defined for transient analysis");
count++;
}
if (analysis.numnodes == 0) {
error ("need to specify a node list for transient analysis");
count++;
}
if (analysis.numdofs == 0) {
error ("need to specify a list of DOFs for transient analysis");
count++;
}
if (analysis.beta <= 0) {
error ("beta musty be greater than zero for transient analysis");
count++;
}
if (analysis.stop <= 0) {
error ("duration needs to be greater than zero for transient analysis");
count++;
}
if (analysis.step <= 0) {
error ("time step needs to be greater than zero for transient analysis");
count++;
}
break;
case Spectral:
if (analysis.mass_mode == 0) {
error ("mass-mode must be defined for spectral analysis");
count++;
}
if (analysis.numnodes == 0) {
error ("need to specify an output node list for spectral analysis");
count++;
}
if (analysis.numdofs == 0) {
error ("need to specify a list of output DOFs for spectral analysis");
count++;
}
if (analysis.step <= 0) {
error ("frequency scale increment must be greater than zero");
count ++;
}
if (analysis.start > analysis.stop) {
error ("frequency range stop must be greater than start");
count ++;
}
break;
case TransientThermal:
if (analysis.mass_mode == 0) {
error ("mass-mode must be defined for transient analysis");
count++;
}
if (analysis.numnodes == 0) {
error ("need to specify a node list for transient analysis");
count++;
}
if (analysis.stop <= 0) {
error ("duration needs to be greater than zero for transient analysis");
count++;
}
if (analysis.step <= 0) {
error ("time step needs to be greater than zero for transient analysis");
count++;
}
break;
case Modal:
if (analysis.mass_mode == 0) {
error ("mass-mode must be defined for modal analysis");
count++;
}
break;
default:
break;
}
return count;
}
/***************************************************************************
*
* Function: GlobalDOF
*
* Description: calculates the global DOF number based on a given local
* DOF (Tx ... Rz), a node number and a dofs map. Zero is
* returned if the given local DOF is not active in the
* current dofs map.
*
***************************************************************************/
int GlobalDOF (node, dx)
unsigned node;
unsigned dx;
{
if (!problem.dofs_pos [dx])
return 0;
return problem.num_dofs*(node - 1) + problem.dofs_pos [dx];
}
/***************************************************************************
*
* Function: LocalDOF
*
* Description: finds the node and local DOF of a given global DOF
*
***************************************************************************/
void LocalDOF (global_dof, node, local_dof)
unsigned global_dof;
unsigned *node;
unsigned *local_dof;
{
unsigned i;
unsigned active;
active = problem.num_dofs;
i = (global_dof - 1) % active + 1;
*local_dof = problem.dofs_num [i];
*node = (global_dof - i) / active + 1;
return;
}
/***************************************************************************
*
* Function: FindForcedDOF
*
* Description: builds a list of global DOF numbers which have some sort
* of input applied to them. We make two passes rather than
* dealing with reallocation (and deallocation in the case
* of no forcing)
*
***************************************************************************/
void FindForcedDOF (forced, numforced)
NodeDOF **forced;
unsigned *numforced;
{
Node *node;
unsigned numnodes;
unsigned *dofs;
unsigned active;
unsigned i, j;
unsigned n;
node = problem.nodes;
active = problem.num_dofs;
node = problem.nodes;
numnodes = problem.num_nodes;
dofs = problem.dofs_num;
/*
* make one pass to figure out how many forced DOF
* we are dealing with
*/
n = 0;
for (i = 1 ; i <= numnodes ; i++) {
for (j = 1 ; j <= active ; j++) {
if (node [i] -> force != NULL)
if (node [i] -> force -> force [dofs[j]].value ||
node [i] -> force -> force [dofs[j]].expr ||
node [i] -> force -> spectrum [dofs[j]].value ||
node [i] -> force -> spectrum [dofs[j]].expr)
n++;
}
}
*numforced = n;
if (n > 0) {
*forced = Allocate (NodeDOF, n);
UnitOffset (*forced);
for (i = 1 ; i <= n ; i++)
(*forced) [i] = AllocNew (struct nodeDOF);
n = 1;
for (i = 1 ; i <= numnodes ; i++) {
for (j = 1 ; j <= active ; j++) {
if (node [i] -> force != NULL) {
if (node [i] -> force -> force [dofs[j]].value ||
node [i] -> force -> force [dofs[j]].expr ||
node [i] -> force -> spectrum [dofs[j]].value ||
node [i] -> force -> spectrum [dofs[j]].expr) {
(*forced) [n] -> dof = dofs[j];
(*forced) [n++] -> node = node[i];
}
}
}
}
}
else
*forced = NULL;
return;
}
/****************************************************************************
*
* Function: RemoveConstrainedMatrixDOF
*
* Description: a generalized form of RemoveConstrainedDOF for a single
* matrix. If a matrix is input, it needs to be in compact
* column format.
*
****************************************************************************/
Matrix RemoveConstrainedMatrixDOF (a)
Matrix a;
{
Node *node;
unsigned numnodes,
active;
unsigned *dofs;
unsigned orig_dofs;
unsigned new_dofs;
unsigned height;
char *dof_map;
Matrix b;
unsigned size;
unsigned *diag;
unsigned start;
unsigned i, j, n, m,
affected_dof,
base_dof;
numnodes = problem.num_nodes;
node = problem.nodes;
active = problem.num_dofs;
dofs = problem.dofs_num;
orig_dofs = numnodes * active;
j = 0; /* gcc -Wall */
dof_map = Allocate (char, orig_dofs);
UnitOffset (dof_map);
for (i = 1 ; i <= orig_dofs ; i++)
dof_map [i] = 1;
size = a -> size;
new_dofs = orig_dofs;
/*
* first we get a count of the number of constrained DOF and build
* a bitmap vector to tell us where they are in the original scheme
*/
for (i = 1 ; i <= numnodes ; i++) {
base_dof = active*(node[i] -> number - 1);
for (j = 1 ; j <= active ; j++) {
if (node [i] -> constraint -> constraint [dofs[j]]) {
affected_dof = base_dof + j;
dof_map [affected_dof] = 0;
new_dofs --;
if (IsCompact(a)) {
if (affected_dof == 1)
size -= 1;
else
size -= (a -> diag [affected_dof] -
a -> diag [affected_dof - 1]);
}
}
}
}
/*
* now we know how much space we are saving so we can allocate
* space for the stiffness and mass matrices
*/
if (IsCompact(a)) {
b = CreateCompactMatrix (new_dofs, new_dofs, size, NULL);
diag = Allocate (unsigned, new_dofs);
UnitOffset (diag);
n = 1;
m = 1;
/*
* now we make a column loop over all of the original DOF to see
* which ones to copy through
*/
for (i = 1 ; i <= orig_dofs ; i++) {
if (dof_map [i]) {
if (i == 1) {
height = 1;
start = 1;
}
else {
height = a -> diag [i] - a -> diag [i-1];
start = a -> diag [i - 1] + 1;
}
/*
* check all the active rows in this column and copy them through
* if necessary - updating the diagonal address array for this
* column once we are through
*/
for (j = start ; j <= a -> diag [i] ; j++) {
affected_dof = i - height + 1 + (j - start);
if (dof_map [affected_dof]) {
b -> data [m][1] = a -> data [j][1];
m++;
}
}
diag [n++] = m - 1;
}
}
b -> diag = diag;
}
else if (IsColumnVector(a)) {
b = CreateColumnVector (new_dofs);
m = 1;
for (i = 1 ; i <= orig_dofs ; i++) {
if (dof_map [i]) {
sdata(b, m ,1) = mdata(a,i,j);
m ++;
}
}
}
else {
b = CreateFullMatrix (new_dofs, new_dofs);
m = 1;
n = 1;
for (i = 1 ; i <= orig_dofs ; i++) {
if (dof_map [i]) {
for (j = 1 ; j <= orig_dofs ; j++) {
if (dof_map [j]) {
sdata(b, n, m) = mdata(a,i,j);
m ++;
}
}
n ++;
}
}
}
ZeroOffset (dof_map);
Deallocate (dof_map);
return b;
}
/****************************************************************************
*
* Function: ZeroConstrainedMatrixDOF
*
* Description: sort of like ZeroConstrainedDOF only simpler and more
* general because it only works on one thing at a time.
*
****************************************************************************/
int ZeroConstrainedMatrixDOF (b, a)
Matrix b;
Matrix a;
{
Node *node;
unsigned active;
unsigned *dofs;
unsigned i,j,
affected_dof,
base_dof;
if (Mrows(a) != Mrows(b) || Mcols(a) != Mcols(b))
return M_SIZEMISMATCH;
node = problem.nodes;
active = problem.num_dofs;
dofs = problem.dofs_num;
if (b != a)
CopyMatrix (b, a);
for (i = 1 ; i <= problem.num_nodes ; i++) {
base_dof = active*(node[i] -> number - 1);
for (j = 1 ; j <= active ; j++) {
if (node[i] -> constraint -> constraint[dofs[j]]) {
affected_dof = base_dof + j;
if (IsCompact(b))
b = ZeroCompactRowCol (b, affected_dof);
else if (IsColumnVector(b))
sdata(b, affected_dof, 1) = 0.0;
else
b = ZeroRowCol (b, affected_dof);
}
}
}
return 0;
}
|