-
Notifications
You must be signed in to change notification settings - Fork 66
/
face_tri3_fvm.cc
460 lines (383 loc) · 14.1 KB
/
face_tri3_fvm.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
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
/********************************************************************************/
/* 888888 888888888 88 888 88888 888 888 88888888 */
/* 8 8 8 8 8 8 8 8 8 8 */
/* 8 8 8 8 8 8 8 8 8 */
/* 8 888888888 8 8 8 8 8 8 8888888 */
/* 8 8888 8 8 8 8 8 8 8 8 */
/* 8 8 8 8 8 8 8 8 8 8 */
/* 888888 888888888 888 88 88888 88888888 88888888 */
/* */
/* A Three-Dimensional General Purpose Semiconductor Simulator. */
/* */
/* */
/* Copyright (C) 2007-2008 */
/* Cogenda Pte Ltd */
/* */
/* Please contact Cogenda Pte Ltd for license information */
/* */
/* Author: Gong Ding gdiso@ustc.edu */
/* */
/********************************************************************************/
// $Id: face_tri3_fvm.cc,v 1.6 2008/07/10 09:39:38 gdiso Exp $
#include "face_tri3_fvm.h"
#include "edge_edge2_fvm.h"
// TNT matrix-vector library
#include <TNT/tnt.h>
#include <TNT/jama_lu.h>
//#define DEBUG
/*
* TRI3: 2 TRI3: C
* o o
* / \ / \
* / \ / \
* e2 / \ e1 / \
* / \ / \
* / e0 \ / \
* o-----------o o-----------o
* 0 1 A B
*/
AutoPtr<Elem> Tri3_FVM::build_fvm_side (const unsigned int i, bool proxy) const
{
assert (i < this->n_sides());
if (proxy)
{
return Tri3::build_side(i, proxy);
}
else
{
Edge2_FVM* edge = new Edge2_FVM;
switch (i)
{
case 0:
{
edge->set_node(0) = this->get_node(0);
edge->set_node(1) = this->get_node(1);
edge->subdomain_id() = this->subdomain_id();
edge->hold_fvm_node(0, this->get_fvm_node(0));
edge->hold_fvm_node(1, this->get_fvm_node(1));
edge->prepare_for_fvm();
AutoPtr<Elem> ap(edge); return ap;
}
case 1:
{
edge->set_node(0) = this->get_node(1);
edge->set_node(1) = this->get_node(2);
edge->subdomain_id() = this->subdomain_id();
edge->hold_fvm_node(0, this->get_fvm_node(1));
edge->hold_fvm_node(1, this->get_fvm_node(2));
edge->prepare_for_fvm();
AutoPtr<Elem> ap(edge); return ap;
}
case 2:
{
edge->set_node(0) = this->get_node(2);
edge->set_node(1) = this->get_node(0);
edge->subdomain_id() = this->subdomain_id();
edge->hold_fvm_node(0, this->get_fvm_node(2));
edge->hold_fvm_node(1, this->get_fvm_node(0));
edge->prepare_for_fvm();
AutoPtr<Elem> ap(edge); return ap;
}
default:
{
genius_error();
}
}
}
// We will never get here... Look at the code above.
genius_error();
AutoPtr<Elem> ap(NULL); return ap;
}
// build the Geom information here
void Tri3_FVM::prepare_for_fvm()
{
//store this vlaue for efficiency reason
vol = Tri3::volume();
// clear partitial volume
v[0] = v[1] = v[2] = 0;
vt[0] = vt[1] = vt[2] = 0;
// the circle centre of ABC
Point circumcircle_center;
{
Point v12 = this->point(0) - this->point(1);
Point v23 = this->point(1) - this->point(2);
Point v13 = this->point(0) - this->point(2);
Real ccdet = v12.cross(v23).size_sq();
Real alpha = v23.size_sq() * v12.dot(v13) /2.0/ccdet;
Real beta = - v13.size_sq() * v12.dot(v23) /2.0/ccdet;
Real gamma = v12.size_sq() * v13.dot(v23) /2.0/ccdet;
circumcircle_center = alpha * this->point(0) +
beta * this->point(1) +
gamma * this->point(2);
}
Point side_centers[3];
unsigned int obtuse_edge = invalid_uint;
for( unsigned int i=0; i<3; i++ )
{
Point p1 = this->point(side_nodes_map[i][0]) ;
Point p2 = this->point(side_nodes_map[i][1]) ;
Point p3 = this->point((2+i)%3) ;
// the side (edge) center
side_centers[i] = 0.5*(p1+p2);
// the side (edge) length
l[i] = (p1-p2).size();
// the distance from circumcircle center to edge
if( (p1-p3).cos_angle(p2-p3) < 0 )
{
d[i] = -(side_centers[i] - circumcircle_center).size();
obtuse_edge = i; //we need special process to obtuse angle
}
else
d[i] = (side_centers[i] - circumcircle_center).size();
dt[i] = d[i];
}
// special process to obtuse angle: Truncated negative length
if(obtuse_edge!=invalid_uint)
{
/*
* TRI3: p3
* o
* * *
* * *
* * *
* * \ / *
* * a1 \ / a2*
* o-----------o--------o---------o
* p1 m1 m2 p2
*/
unsigned int obtuse_node = (2+obtuse_edge)%3;
Point p1 = this->point(side_nodes_map[obtuse_edge][0]) ;
Point p2 = this->point(side_nodes_map[obtuse_edge][1]) ;
Point p3 = this->point(obtuse_node) ;
Real a1 = (p1-p2).angle(p1-p3);
Real a2 = (p2-p1).angle(p2-p3);
Point pre_edge_center = 0.5*(p1 + p3);
Point pos_edge_center = 0.5*(p2 + p3);
Point m1 = p1 + (p2-p1).unit()*(pre_edge_center-p1).size()/cos(a1);
Point m2 = p2 + (p1-p2).unit()*(pos_edge_center-p2).size()/cos(a2);
unsigned int pre_edge = (obtuse_edge + 3 - 1)%3;
unsigned int pos_edge = (obtuse_edge + 3 + 1)%3;
dt[obtuse_edge] = 0;
dt[pre_edge] = (pre_edge_center-m1).size();
dt[pos_edge] = (pos_edge_center-m2).size();
}
// compute partial volume
for( unsigned int i=0; i<3; i++ ) // has 3 side (edge)
{
unsigned int node1 = side_nodes_map[i][0];
unsigned int node2 = side_nodes_map[i][1];
v[node1] += 0.5 * 0.5 * l[i] * d[i];
v[node2] += 0.5 * 0.5 * l[i] * d[i];
vt[node1] += 0.5 * 0.5 * l[i] * dt[i];
vt[node2] += 0.5 * 0.5 * l[i] * dt[i];
}
// truncated partial volum consistant with element volume
if(obtuse_edge!=invalid_uint)
{
unsigned int obtuse_node = (2+obtuse_edge)%3;
unsigned int n1 = side_nodes_map[obtuse_edge][0];
unsigned int n2 = side_nodes_map[obtuse_edge][1];
vt[obtuse_node] = vol - vt[n1] - vt[n2];
}
// self test
#ifdef DEBUG
Real V = 0;
for( unsigned int i=0; i<3; i++ )
V += v[i];
genius_assert( (vol > 1e-10 ? (std::abs(V-vol) < 1e-3*vol) : (std::abs(V-vol) < std::max(1e-13, 1e-2*vol))) );
#endif
prepare_for_vector_reconstruct();
}
#if 0
// build the Geom information here
void Tri3_FVM::prepare_for_fvm()
{
//store this vlaue for efficiency reason
vol = Tri3::volume();
// clear partitial volume
v[0] = v[1] = v[2] = 0;
// the circle centre of ABC
Point circumcircle_center;
{
Point v12 = this->point(0) - this->point(1);
Point v23 = this->point(1) - this->point(2);
Point v13 = this->point(0) - this->point(2);
Real ccdet = v12.cross(v23).size_sq();
Real alpha = v23.size_sq() * v12.dot(v13) /2.0/ccdet;
Real beta = - v13.size_sq() * v12.dot(v23) /2.0/ccdet;
Real gamma = v12.size_sq() * v13.dot(v23) /2.0/ccdet;
circumcircle_center = alpha * this->point(0) +
beta * this->point(1) +
gamma * this->point(2);
}
Point side_centers[3];
unsigned int obtuse_edge = invalid_uint;
for( unsigned int i=0; i<3; i++ )
{
Point p1 = this->point(side_nodes_map[i][0]) ;
Point p2 = this->point(side_nodes_map[i][1]) ;
Point p3 = this->point((2+i)%3) ;
// the side (edge) center
side_centers[i] = 0.5*(p1+p2);
// the side (edge) length
l[i] = (p1-p2).size();
// the distance from circumcircle center to edge
if( (p1-p3).cos_angle(p2-p3) < 0 )
d[i] = -(side_centers[i] - circumcircle_center).size(); //we need special process to obtuse angle
else
d[i] = (side_centers[i] - circumcircle_center).size();
}
// add volume
for( unsigned int i=0; i<3; i++ ) // has 3 side (edge)
{
unsigned int node1 = side_nodes_map[i][0];
unsigned int node2 = side_nodes_map[i][1];
v[node1] += 0.5 * 0.5 * l[i] * d[i];
v[node2] += 0.5 * 0.5 * l[i] * d[i];
}
// self test
Real V = 0;
for( unsigned int i=0; i<3; i++ )
V += v[i];
genius_assert( vol > 1e-10 ? (std::abs(V-vol) < 1e-3*vol) : (std::abs(V-vol) < 1e-2*vol) );
prepare_for_vector_reconstruct();
}
#endif
#if 0
// use weight center instead of circumcircle center?
void Tri3_FVM::prepare_for_fvm()
{
//weight center
Point c = (this->point(0) + this->point(1) + this->point(2))/3;
Point side_centers[3];
for( unsigned int i=0; i<3; i++ )
{
Point p1 = this->point(side_nodes_map[i][0]) ;
Point p2 = this->point(side_nodes_map[i][1]) ;
Point p3 = this->point((2+i)%3) ;
// the side (edge) center
side_centers[i] = 0.5*(p1+p2);
double angle = (p1-side_centers[i]).angle(c-side_centers[i]);
// the side (edge) length
l[i] = (p1-p2).size();
d[i] = (side_centers[i] - c).size()*sin(angle);
}
//store this vlaue for efficiency reason
vol = Tri3::volume();
// clear partitial volume
v[0] = v[1] = v[2] = 0;
// add volume
for( unsigned int i=0; i<3; i++ ) // has 3 side (edge)
{
unsigned int node1 = side_nodes_map[i][0];
unsigned int node2 = side_nodes_map[i][1];
v[node1] += 0.5 * 0.5 * l[i] * d[i];
v[node2] += 0.5 * 0.5 * l[i] * d[i];
}
// self test
Real V = 0;
for( unsigned int i=0; i<3; i++ )
{
V += v[i];
}
genius_assert( vol > 1e-10 ? (std::abs(V-vol) < 1e-3*vol) : (std::abs(V-vol) < 1e-2*vol) );
}
#endif
VectorValue<PetscScalar> Tri3_FVM::gradient( const std::vector<PetscScalar> & var) const
{
// FIXME we assume the triangle on xy plane here. not a general case
genius_assert( (this->point(0))(2) == (this->point(1))(2) );
genius_assert( (this->point(0))(2) == (this->point(2))(2) );
Real xa = (this->point(0))(0);
Real xb = (this->point(1))(0);
Real xc = (this->point(2))(0);
Real ya = (this->point(0))(1);
Real yb = (this->point(1))(1);
Real yc = (this->point(2))(1);
PetscScalar dx = ((yb-yc)*var[0] + (yc-ya)*var[1] +(ya-yb)*var[2])/(2*vol);
PetscScalar dy = ((xc-xb)*var[0] + (xa-xc)*var[1] +(xb-xa)*var[2])/(2*vol);
return VectorValue<PetscScalar>(dx, dy, 0.0);
}
VectorValue<Complex> Tri3_FVM::gradient( const std::vector<Complex> & var) const
{
// FIXME we assume the triangle on xy plane here. not a general case
genius_assert( (this->point(0))(2) == (this->point(1))(2) );
genius_assert( (this->point(0))(2) == (this->point(2))(2) );
Real xa = (this->point(0))(0);
Real xb = (this->point(1))(0);
Real xc = (this->point(2))(0);
Real ya = (this->point(0))(1);
Real yb = (this->point(1))(1);
Real yc = (this->point(2))(1);
Complex dx = ((yb-yc)*var[0] + (yc-ya)*var[1] +(ya-yb)*var[2])/(2*vol);
Complex dy = ((xc-xb)*var[0] + (xa-xc)*var[1] +(xb-xa)*var[2])/(2*vol);
return VectorValue<Complex>(dx, dy, 0.0);
}
VectorValue<AutoDScalar> Tri3_FVM::gradient( const std::vector<AutoDScalar> & var) const
{
// FIXME we assume the triangle on xy plane here. not a general case
genius_assert( (this->point(0))(2) == (this->point(1))(2) );
genius_assert( (this->point(0))(2) == (this->point(2))(2) );
Real xa = (this->point(0))(0);
Real xb = (this->point(1))(0);
Real xc = (this->point(2))(0);
Real ya = (this->point(0))(1);
Real yb = (this->point(1))(1);
Real yc = (this->point(2))(1);
AutoDScalar dx = ((yb-yc)*var[0] + (yc-ya)*var[1] +(ya-yb)*var[2])/(2*vol);
AutoDScalar dy = ((xc-xb)*var[0] + (xa-xc)*var[1] +(xb-xa)*var[2])/(2*vol);
return VectorValue<AutoDScalar>(dx, dy, 0.0);
}
void Tri3_FVM::prepare_for_vector_reconstruct()
{
//FIXME NOT work for 3D triangle
TNT::Array2D<Real> A (n_edges(), 2, 0.0);
TNT::Array2D<Real> AT(2, n_edges(), 0.0);
for( unsigned int e=0; e<n_edges(); e++ )
{
AutoPtr<Elem> edge = this->build_edge (e);
VectorValue<double> dir = (edge->point(1) - edge->point(0)).unit(); // unit direction of the edge
A[e][0] = dir(0);
A[e][1] = dir(1);
AT[0][e] = dir(0);
AT[1][e] = dir(1);
}
TNT::Array2D<Real> ATA = TNT::matmult(AT, A);
JAMA::LU<Real> solver(ATA);
if( solver.isNonsingular() )
{
TNT::Array2D<Real> inv_ATA = solver.inv();
TNT::Array2D<Real> M = TNT::matmult(inv_ATA, AT);
for(unsigned int m=0; m<M.dim1(); m++)
for( unsigned int e=0; e<M.dim2(); e++ )
least_squares_vector_reconstruct_matrix[m][e] = M[m][e];
}
else
{
for(unsigned int m=0; m<2; m++)
for( unsigned int e=0; e<3; e++ )
least_squares_vector_reconstruct_matrix[m][e] = 0.0;
}
}
VectorValue<PetscScalar> Tri3_FVM::reconstruct_vector( const std::vector<PetscScalar> & projects) const
{
assert(projects.size() == n_edges());
PetscScalar Vx=0, Vy=0, Vz=0;
for( unsigned int e=0; e<n_edges(); e++ )
{
Vx += least_squares_vector_reconstruct_matrix[0][e] * projects[e];
Vy += least_squares_vector_reconstruct_matrix[1][e] * projects[e];
}
return VectorValue<PetscScalar>(Vx, Vy, Vz);
}
VectorValue<AutoDScalar> Tri3_FVM::reconstruct_vector( const std::vector<AutoDScalar> & projects) const
{
assert(projects.size() == n_edges());
AutoDScalar Vx=0, Vy=0, Vz=0;
for( unsigned int e=0; e<n_edges(); e++ )
{
Vx += least_squares_vector_reconstruct_matrix[0][e] * projects[e];
Vy += least_squares_vector_reconstruct_matrix[1][e] * projects[e];
}
return VectorValue<AutoDScalar>(Vx, Vy, Vz);
}