-
Notifications
You must be signed in to change notification settings - Fork 0
/
elementGradient.cc
319 lines (271 loc) · 9.51 KB
/
elementGradient.cc
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
#include "phParAdapt.h"
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#ifndef ibm
#define ludcmp ludcmp_
#define lubksb lubksb_
#endif
#include "Eigen.h"
#ifdef SIM
#include "MeshSim.h"
#include "MeshTypes.h"
#include <math.h>
extern "C" double XYZ_volume(dArray*);
#endif
#ifdef FMDB
#include "mEntity.h"
#endif
using namespace std;
#ifdef __cplusplus
extern "C" {
#endif
extern pMeshDataId ybarID;
extern pMeshDataId nodalGradientID;
extern int adaptOption;
extern double refthreshold;
extern double rhoinp;
// reconstruct the element gradient
void
elementGradient(pRegion region, double* elementGradient)
{
pVertex v;
double* nodalData;
for(int i=0; i<3; i++) {
elementGradient[i] = 0.;
}
double matrix[16];
double fieldVector[4];
int fieldIndexForGrad=4;
int four = 4;
double rho = rhoinp;
double scale = refthreshold;
pPList regionVerts;
// regionVerts are deleted at the end of the routine
regionVerts = R_vertices(region, 1);
#ifdef FMDB
int rTopoType = region->getType();
switch(rTopoType) {
case mEntity::TET : {
#endif
#ifdef SIM
int rTopoType = R_topoType(region);
switch(rTopoType) {
case Rtet : {
#endif
// build the linear system
buildSystem(region,matrix);
// get the field vals
for (int i=0; i<PList_size(regionVerts); i++) {
v = (pVertex)PList_item(regionVerts,i);
if(EN_getDataPtr((pEntity)v, ybarID,
(void**)&nodalData)==NULL){
cout<<"\nerror in elementGradient: no data attached to vertex (tet element)\n";
exit(0);
}
// fieldVector[i]=sqrt(nodalData[1]*nodalData[1]+nodalData[2]*nodalData[2]+nodalData[3]*nodalData[3]);
fieldVector[i]=nodalData[3]+scale*rho*(nodalData[1]*nodalData[1]+nodalData[2]*nodalData[2]+nodalData[0]*nodalData[0]);
// fieldVector[i]=nodalData[fieldIndexForGrad];
// fieldVector[i]=nodalData[3]+scale*rho*nodalData[4]*nodalData[4];
// fieldVector[i]=nodalData[0]+nodalData[1];
// fieldVector[i]=nodalData[4]*nodalData[4];
// fieldVector[i]=(nodalData[3]+50*nodalData[4])*nodalData[5]/0.2;
// fieldVector[i]=100*(nodalData[3]/9000.0+nodalData[4]/170.0+nodalData[5]/0.15);
}
int indx[4];
double fnumber;
ludcmp(matrix , &four, &four, indx, &fnumber);
// fieldVector is going to be overridden and will
// contain the solution
lubksb(matrix , &four, &four, indx, fieldVector );
for(int i=0; i<3; i++) {
// the poly's structure is
// a0 + a1*x + a2*y + a3*z
elementGradient[i] = fieldVector[i+1];
#if ( defined DEBUG )
// cout<<"\ngradient["<<i<<"] "<<elementGradient[i]<<"\n";
#endif
}
}
break;
#ifdef FMDB
case mEntity::PRISM : {
double volume;
volume = R_Volume2(region);
#endif
#ifdef SIM
case Rwedge : {
double volume;
#endif
double volTet, x_y_z[4][3];
// fit three tets in a prism
// (diagonal edges of a prism are : V0-V4, V1-V5 and V5-V0)
int tetVerts[3][4] = {0,1,2,5, 0,1,5,4, 0,4,5,3};
double v01[3],v02[3],v03[3];
double normal[3];
// three tets in a prism
// compute gradient on prism as vol. averaged gradients on three child tets
for(int iTet=0; iTet<3; iTet++) {
for(int iVert=0; iVert<4; iVert++) {
v = (pVertex)PList_item(regionVerts,tetVerts[iTet][iVert]);
if(EN_getDataPtr((pEntity)v, ybarID,
(void**)&nodalData)==NULL) {
cout<<"\nerror in elementGradient: no data attached to vertex (wedge element)\n";
exit(0);
}
// fieldVector[iVert]=sqrt(nodalData[1]*nodalData[1]+nodalData[2]*nodalData[2]+nodalData[3]*nodalData[3]);
fieldVector[iVert]=nodalData[3]+scale*rho*(nodalData[1]*nodalData[1]+nodalData[2]*nodalData[2]+nodalData[0]*nodalData[0]);
// fieldVector[iVert]=nodalData[fieldIndexForGrad];
// fieldVector[iVert]=nodalData[3]+scale*rho*nodalData[4]*nodalData[4];
// fieldVector[iVert]=nodalData[0]+nodalData[1];
// fieldVector[iVert]=nodalData[4]*nodalData[4];
// fieldVector[iVert]=(nodalData[3]+50*nodalData[4])*nodalData[5]/0.2;
// fieldVector[iVert]=100*(nodalData[3]/9000.0+nodalData[4]/170.0+nodalData[5]/0.15);
V_coord(v,x_y_z[iVert]);
}
// volTet = XYZ_volume(x_y_z);
diffVt(x_y_z[1],x_y_z[0],v01);
diffVt(x_y_z[2],x_y_z[0],v02);
crossProd(v01,v02,normal);
diffVt(x_y_z[3],x_y_z[0],v03);
volTet = dotProd(normal,v03)/6;
#ifdef FMDB
// #ifdef DEBUG
// volume of wedge should not be zero but
// volume of child tet.(s) can be negative
// if quad. face(s) are warped (and if base tri. is thin)
// --- probably can just check the volume of the wedge/prism
if(volume<0.&&volTet<0.) {
cout<<"\nError in elementGradient()..."<<endl;
cout<<"negative volume of tet ["<<iTet<<"] in prism/wedge of negative volume"<<endl;
R_info(region);
exit(0);
}
// #endif
#endif
// volume of wedge should not be zero but
// volume of child tet.(s) can be negative
// if quad. face(s) are warped (and if base tri. is thin)
volTet = fabs(volTet);
// build the linear system
buildSystemXYZ(x_y_z,matrix);
int indx[4];
double fnumber;
ludcmp(matrix , &four, &four, indx, &fnumber);
// fieldVector is going to be overridden and will
// contain the solution
lubksb(matrix , &four, &four, indx, fieldVector );
for(int i=0; i<3; i++) {
// the poly's structure is
// a0 + a1*x + a2*y + a3*z
// elementGradient[i] += volTet*fieldVector[i+1];
elementGradient[i] += fieldVector[i+1];
}
} // loop over three tets fitted into prism
// gradient for prism/wedge element is
// vol. weighted average of gradients on three fitted child tets
for(int i=0; i<3; i++) {
// elementGradient[i] = elementGradient[i]/volume;
elementGradient[i] = elementGradient[i]/3;
#if ( defined DEBUG )
// cout<<"\ngradient["<<i<<"] "<<elementGradient[i]<<"\n";
#endif
}
}
break;
#ifdef FMDB
case mEntity::PYRAMID : {
double volume;
volume = R_Volume2(region);
#endif
#ifdef SIM
case Rpyramid : {
double volume;
#endif
double volTet, x_y_z[4][3];
// fit two tets in a pyramid
// (diagonal edge of a pyramid is : V0-V2)
int tetVerts[2][4] = {{0,1,2,4}, {0,2,3,4}};
double v01[3],v02[3],v03[3];
double normal[3];
// two tets in a pyramid
// compute gradient on pyramid as vol. averaged gradients on two child tets
for(int iTet=0; iTet<2; iTet++) {
for(int iVert=0; iVert<4; iVert++) {
v = (pVertex)PList_item(regionVerts,tetVerts[iTet][iVert]);
if(EN_getDataPtr((pEntity)v, ybarID,
(void**)&nodalData)==NULL) {
cout<<"\nerror in elementGradient: no data attached to vertex (wedge element)\n";
exit(0);
}
// fieldVector[iVert]=sqrt(nodalData[1]*nodalData[1]+nodalData[2]*nodalData[2]+nodalData[3]*nodalData[3]);
fieldVector[iVert]=nodalData[3]+scale*rho*(nodalData[1]*nodalData[1]+nodalData[2]*nodalData[2]+nodalData[0]*nodalData[0]);
// fieldVector[iVert]=nodalData[fieldIndexForGrad];
// fieldVector[iVert]=nodalData[3]+scale*rho*nodalData[4]*nodalData[4];
// fieldVector[iVert]=nodalData[0]+nodalData[1];
// fieldVector[iVert]=nodalData[4]*nodalData[4];
// fieldVector[iVert]=(nodalData[3]+50*nodalData[4])*nodalData[5]/0.2;
// fieldVector[iVert]=100*(nodalData[3]/9000.0+nodalData[4]/170.0+nodalData[5]/0.15);
V_coord(v,x_y_z[iVert]);
}
// volTet = XYZ_volume(x_y_z);
diffVt(x_y_z[1],x_y_z[0],v01);
diffVt(x_y_z[2],x_y_z[0],v02);
crossProd(v01,v02,normal);
diffVt(x_y_z[3],x_y_z[0],v03);
volTet = dotProd(normal,v03)/6;
#ifdef FMDB
// #ifdef DEBUG
// volume of pyramid should not be zero but
// volume of child tet.(s) can be negative
// if quad. face(s) are warped
// --- probably can just check the volume of the pyramid
if(volume<0.&&volTet<0.) {
cout<<"\nError in elementGradient()..."<<endl;
cout<<"negative volume of tet ["<<iTet<<"] in pyramid of negative volume"<<endl;
R_info(region);
exit(0);
}
// #endif
#endif
// volume of pyramid should not be zero but
// volume of child tet.(s) can be negative
// if quad. face(s) are warped
volTet = fabs(volTet);
// build the linear system
buildSystemXYZ(x_y_z,matrix);
int indx[4];
double fnumber;
ludcmp(matrix , &four, &four, indx, &fnumber);
// fieldVector is going to be overridden and will
// contain the solution
lubksb(matrix , &four, &four, indx, fieldVector );
for(int i=0; i<3; i++) {
// the poly's structure is
// a0 + a1*x + a2*y + a3*z
// elementGradient[i] += volTet*fieldVector[i+1];
elementGradient[i] += fieldVector[i+1];
}
} // loop over two tets fitted into prism
// gradient for pyramid element is
// vol. weighted average of gradients on two fitted child tets
for(int i=0; i<3; i++) {
// elementGradient[i] = elementGradient[i]/volume;
elementGradient[i] = elementGradient[i]/2;
#if ( defined DEBUG )
// cout<<"\ngradient["<<i<<"] "<<elementGradient[i]<<"\n";
#endif
}
}
break;
default: {
cout<<"\nError in elementGradient()..."<<endl;
cout<<"element topology ["<<rTopoType<<"] CANNOT be handelled"<<endl;
exit(0);
}
}
PList_delete(regionVerts);
}
#ifdef __cplusplus
}
#endif