File: ctg.c

package info (click to toggle)
felt 3.06-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 17,512 kB
  • ctags: 7,946
  • sloc: ansic: 85,476; fortran: 3,614; yacc: 2,803; lex: 1,178; makefile: 305; sh: 302
file content (351 lines) | stat: -rw-r--r-- 8,788 bytes parent folder | download | duplicates (2)
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
/*
    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:	ctg.c							*
 *									*
 * Description: This file contains the definition structure and the	*
 *		stiffness and stress functions for the constant 	*
 *		temperature gradient triangular element for heat 	*
 *		transfer problems.					*
 ************************************************************************/

# include <math.h>
# include <stdio.h>
# include "allocate.h"
# include "fe.h"
# include "error.h"
# include "misc.h"

int CTGLumpedCapacityMatrix ( );
int CTGConsistentCapacityMatrix ( );
Vector	CTGResolveConvection ( );
Matrix	CTGLocalB ( );
Matrix	PlanarConductivity ( );
int	ctgEltSetup ( );
int	ctgEltStress ( );

struct definition ctgDefinition = {
   "ctg", ctgEltSetup, ctgEltStress, 
   Planar, 3, 3, 0, 1, {0, 1, 0, 0, 0, 0, 0}, 0
};

int ctgEltSetup (element, mass_mode, tangent)
   Element	element;
   char		mass_mode;
   int		tangent;
{
   unsigned		i;
   Vector		equiv;
   int			count;
   Matrix		B,
			D;
   double		factor;
   double		area;

   if (element -> material -> Kx == 0) {
      error ("CTG element %d has 0.0 for x-conductivity (Kx)", element -> number);
      return 1;
   }
   if (element -> material -> Ky == 0) {
      error ("CTG element %d has 0.0 for y-conductivity (Ky)", element -> number);
      return 1;
   }
   if (element -> material -> t == 0) {
      error ("CTG element %d has 0.0 for thickness (t)", element -> number);
      return 1;
   }
   if (mass_mode && element -> material -> c == 0) {
      error ("CTG element %d has 0.0 for heat capacitance (c)", element -> number);
      return 1;
   }

   B = CTGLocalB (element, &area);
   if (B == NullMatrix)
      return 1;
   
   D = PlanarConductivity (element);

   if (D == NullMatrix)
      return 1;

   factor = element -> material -> t * area;
   
   if (element -> K == NullMatrix)
      element -> K = CreateMatrix (3,3);

   MultiplyAtBA (element -> K, B, D);

   ScaleMatrix (element -> K, element -> K, factor, 0.0);

   if (element -> numdistributed > 0) {
      equiv = CTGResolveConvection (element, &count);
      if (equiv == NullMatrix)
         return count;

       for (i = 1; i <= 3 ; i++) 
          element -> node[i] -> eq_force[1] += VectorData (equiv) [i];
   }

   if (mass_mode) {
      if (element -> M == NullMatrix)
         element -> M = CreateMatrix (3,3);
     
      if (mass_mode == 'l') 
         CTGLumpedCapacityMatrix (element, area);
      else if (mass_mode == 'c') 
         CTGConsistentCapacityMatrix (element, area);
   }

   return 0;
}

int ctgEltStress (element)
   Element	element;
{
   element -> ninteg = 0;
   return 0;
} 

int CTGLumpedCapacityMatrix (e, A)
   Element	e;
   double	A;
{
   double	factor;

   factor = e -> material -> t * e -> material -> c *
            e -> material -> rho * A / 3;

   ZeroMatrix (e -> M);
   MatrixData (e -> M) [1][1] = factor;
   MatrixData (e -> M) [2][2] = factor;
   MatrixData (e -> M) [3][3] = factor;

   return 0;
}

int CTGConsistentCapacityMatrix (e, area)
   Element	e;
   double	area;
{
   return 0;
}

Matrix PlanarConductivity (element)
   Element	element;
{
   static Matrix	D = NullMatrix;

   if (D == NullMatrix) {
      D = CreateMatrix (2,2);
      ZeroMatrix (D);
   }

   MatrixData (D) [1][1] = element -> material -> Kx;
   MatrixData (D) [2][2] = element -> material -> Ky;

   return D;
}

Matrix CTGLocalB (element, area)
   Element	element;
   double	*area;
{
   static Matrix 	B = NullMatrix;
   double		xc1,yc1,
			xc2,yc2,
			xc3,yc3,
             		beta[4],
			gamma[4],
			A,
			factor;
   unsigned		j;

   if (B == NullMatrix) 
      B = CreateMatrix (2,3);

   ZeroMatrix (B);

   xc1 = element -> node[1] -> x;
   xc2 = element -> node[2] -> x;
   xc3 = element -> node[3] -> x;
   yc1 = element -> node[1] -> y;
   yc2 = element -> node[2] -> y;
   yc3 = element -> node[3] -> y;

   beta[1] = yc2 - yc3;
   beta[2] = yc3 - yc1;
   beta[3] = yc1 - yc2;

   gamma[1] = xc3 - xc2;
   gamma[2] = xc1 - xc3;
   gamma[3] = xc2 - xc1;

   A = 0.5*(xc1*(beta[1]) + xc2*(beta[2]) + xc3*(beta[3]));
   
   if (A < 0) {
      error("incorrect node ordering for element %d (must be ccw)",element -> number);
      return NullMatrix;
   }
   if (A == 0) {
      error ("area of element %d is zero, check node numbering",element -> number);
      return NullMatrix;
   }
   
   for (j = 1 ; j <= 3 ; j++) {
      MatrixData (B) [1][j] = beta[j];
      MatrixData (B) [2][j] = gamma[j];
   }

   factor = 0.5/A;
   ScaleMatrix (B,B,factor,0.0);

   if (area != NULL)
      (*area) = A;

   return B;
}

Vector CTGResolveConvection (element, err_count)
   Element	element;
   int		*err_count;
{
   double		L;
   double		factor;
   int			count;
   double		xc1,xc2,
			yc1,yc2;
   double		thick;
   double		conv_coeff;
   double		Tinf;
   unsigned		node_a,
			node_b;
   unsigned		i;
   static Vector 	equiv = NullMatrix;
   static Matrix	convK;
 
   if (equiv == NullMatrix) {
      equiv = CreateVector (3);
      convK = CreateMatrix (3,3);
   }

   count = 0;
 
   if (element -> numdistributed > 3) {
      error ("ctg element %d can have at most three convecting edges",
              element -> number);
      count++;
   }

   thick = element -> material -> t;

   ZeroMatrix (convK);

   for (i = 1 ; i <= 3 ; i++)
      VectorData (equiv) [i] = 0.0;

   for (i = 1 ; i <= element -> numdistributed ; i++) {

      if (element -> distributed[i] -> nvalues != 2) {
         error ("convection %s does not have 2 nodal values (element %d)",
                 element -> distributed[i] -> name,element -> number);
         count++;
      }

      node_a = element -> distributed[i] -> value[1].node;
      node_b = element -> distributed[i] -> value[2].node;

      if (node_a < 1 || node_a > 3 || node_b < 1 || node_b > 3) {
         error ("incorrect node numbering for convection %s (element %d)", 
                element -> distributed[i] -> name,element -> number);
         count++;
      }

      if (node_a == node_b) {
         error ("incorrect node numbering for convection %s (element %d)", 
                element -> distributed[i] -> name,element -> number);
         count++;
      }

	/* 
	 * Thats all the error checking we can do right now, 
	 * bail out if we've had any
	 */

      if (count) {
         *err_count = count;
         return NullMatrix;
      }

      xc1 = element -> node[node_a] -> x;
      xc2 = element -> node[node_b] -> x;
      yc1 = element -> node[node_a] -> y;
      yc2 = element -> node[node_b] -> y;

      L = sqrt ((xc1 - xc2)*(xc1 - xc2) + (yc1 - yc2)*(yc1 - yc2));

      if (L <= TINY) {
         error ("length of side of element %d is zero to machine precision",
                 element -> number);
         *err_count = 1;
         return NullMatrix;
      } 

	/*
	 * calculate the additional "force" that we will store in the
	 * nodes eq_force structure
	 */

      conv_coeff = element -> distributed[i] -> value[1].magnitude;
      Tinf = element -> distributed[i] -> value[2].magnitude;
      factor = conv_coeff*Tinf*L*thick/2.0;

      VectorData (equiv) [node_a] += factor;
      VectorData (equiv) [node_b] += factor;

	/*
 	 * calculate the contribution of this convecting edge to
	 * the overall element stiffness matrix
	 */

      factor = conv_coeff*L*thick/6.0;

      MatrixData (convK) [node_a][node_a] += 2.0*factor;
      MatrixData (convK) [node_b][node_b] += 2.0*factor;       
      MatrixData (convK) [node_a][node_b] += factor;
      MatrixData (convK) [node_b][node_a] += factor;       
   }

	/*	
	 * add all of the convective contributions into the
	 * element -> K stiffness matrix
	 */

   AddMatrices (element -> K, element -> K, convK);

	/*
	 * Now that we know all is okay, allocate some memory if we
	 * haven't already done so for some other element
	 */

   SetEquivalentForceMemory (element);

   *err_count = 0;
   return equiv; 
}