-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfem3d_project.cpp
4283 lines (3954 loc) · 94.3 KB
/
fem3d_project.cpp
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
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <fstream>
# include <cmath>
# include <cstring>
# include <ctime>
using namespace std;
int main ( int argc, char *argv[] );
void basis_mn_tet4 ( double t[3*4], int n, double p[], double phi[] );
char ch_cap ( char ch );
bool ch_eqi ( char ch1, char ch2 );
int ch_to_digit ( char ch );
double *fem3d_transfer ( int sample_node_num, int sample_element_order,
int sample_element_num, int sample_value_dim, int sample_value_num,
double sample_node_xyz[], int sample_element_node[],
int sample_element_neighbor[], double sample_value[], int fem_node_num,
int fem_element_order, int fem_element_num, int fem_value_dim,
int fem_value_num, double fem_node_xyz[], int fem_element_node[] );
int file_column_count ( string input_filename );
int file_row_count ( string input_filename );
int i4_max ( int i1, int i2 );
int i4_min ( int i1, int i2 );
int i4col_compare ( int m, int n, int a[], int i, int j );
void i4col_sort_a ( int m, int n, int a[] );
void i4col_swap ( int m, int n, int a[], int icol1, int icol2 );
void i4i4i4_sort_a ( int i1, int i2, int i3, int *j1, int *j2, int *j3 );
int *i4mat_data_read ( string input_filename, int m, int n );
void i4mat_header_read ( string input_filename, int *m, int *n );
int i4mat_min ( int m, int n, int a[] );
double *projection ( int fem_node_num, double fem_node_xyz[],
int fem_element_order, int fem_element_num, int fem_element_node[],
int fem_element_neighbor[], int fem_value_dim, double fem_value[],
int sample_node_num, double sample_node_xyz[] );
float r4_abs ( float x );
int r4_nint ( float x );
double r8_abs ( double x );
double r8_min ( double x, double y );
double *r8ge_fss_new ( int n, double a[], int nb, double b[] );
double *r8mat_data_read ( string input_filename, int m, int n );
double r8mat_det_4d ( double a[] );
void r8mat_header_read ( string input_filename, int *m, int *n );
int r8mat_solve ( int n, int rhs_num, double a[] );
void r8mat_write ( string output_filename, int m, int n, double table[] );
double *r8mat_zero_new ( int m, int n );
bool r8vec_is_nonnegative ( int n, double x[] );
int s_len_trim ( string s );
int s_to_i4 ( string s, int *last, bool *error );
bool s_to_i4vec ( string s, int n, int ivec[] );
double s_to_r8 ( string s, int *lchar, bool *error );
bool s_to_r8vec ( string s, int n, double rvec[] );
int s_word_count ( string s );
void sort_heap_external ( int n, int *indx, int *i, int *j, int isgn );
int *tet_mesh_neighbor_tets ( int tetra_order, int tetra_num,
int tetra_node[] );
int tet_mesh_search_delaunay ( int node_num, double node_xyz[], int tet_order,
int tet_num, int tet_node[], int tet_neighbor[], double p[], int *face,
int *step_num );
int tet_mesh_search_naive ( int node_num, double node_xyz[],
int tet_order, int tet_num, int tet_node[], double p[], int *step_num );
double *tetrahedron_barycentric ( double tetra[3*4], double p[3] );
double tetrahedron_volume ( double tetra[3*4] );
void timestamp ( );
//****************************************************************************80
int main ( int argc, char *argv[] )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for FEM3D_PROJECT.
//
// Discussion:
//
// FEM3D_PROJECT reads files defining a sampling of a (scalar or vector)
// function of 3 arguments, and a list of nodes and tetrahedral elements
// to use for a finite element representation of the data.
//
// It computes a set of finite element coefficients to be associated with
// the given finite element mesh, and writes that information to a file
// so that an FEM representation is formed by the node, element and value
// files.
//
// Usage:
//
// fem3d_project sample_prefix fem_prefix
//
// where 'sample_prefix' is the common prefix for the SAMPLE files:
//
// * sample_prefix_nodes.txt, the node coordinates where samples were taken,
// * sample_prefix_elements.txt, the 4 nodes that make up each element;
// * sample_prefix_values.txt, the sample values.
//
// and 'fem_prefix' is the common prefix for the FEM files:
//
// * fem_prefix_nodes.txt, the node coordinates.
// * fem_prefix_elements.txt, the 4 nodes that make up each element;
// * fem_prefix_values.txt, the values defined at each node (output).
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 25 August 2009
//
// Author:
//
// John Burkardt
//
{
int element_min;
string fem_element_filename;
int *fem_element_node;
int fem_element_num;
int fem_element_order;
int fem_node_dim;
string fem_node_filename;
int fem_node_num;
double *fem_node_xyz;
string fem_prefix;
double *fem_value;
int fem_value_dim;
string fem_value_filename;
int fem_value_num;
int i;
int j;
string sample_element_filename;
int *sample_element_neighbor;
int *sample_element_node;
int sample_element_num;
int sample_element_order;
string sample_prefix;
int sample_node_dim;
string sample_node_filename;
int sample_node_num;
double *sample_node_xyz;
int sample_value_dim;
int sample_value_num;
double *sample_value;
string sample_value_filename;
timestamp ( );
cout << "\n";
cout << "FEM3D_PROJECT\n";
cout << " C++ version.\n";
cout << "\n";
cout << " Read files defining a sampling of a function of 3 arguments.\n";
cout << " Read files defining a finite element mesh.\n";
cout << " Project the sample data onto the mesh, and\n";
cout << " write a file of FEM coefficient values.\n";
//
// Get the number of command line arguments.
//
if ( 1 < argc )
{
sample_prefix = argv[1];
}
else
{
cout << "\n";
cout << "Enter the sample file prefix:\n";
cin >> sample_prefix;
}
if ( 2 < argc )
{
fem_prefix = argv[2];
}
else
{
cout << "\n";
cout << "Enter the FEM file prefix:\n";
cin >> fem_prefix;
}
//
// Create the filenames.
//
sample_node_filename = sample_prefix + "_nodes.txt";
sample_element_filename = sample_prefix + "_elements.txt";
sample_value_filename = sample_prefix + "_values.txt";
fem_node_filename = fem_prefix + "_nodes.txt";
fem_element_filename = fem_prefix + "_elements.txt";
fem_value_filename = fem_prefix + "_values.txt";
//
// Read the SAMPLE NODE, ELEMENT and VALUE data.
//
r8mat_header_read ( sample_node_filename, &sample_node_dim,
&sample_node_num );
cout << "\n";
cout << " Sample node spatial dimension is " << sample_node_dim << "\n";
cout << " Sample node number is " << sample_node_num << "\n";
if ( sample_node_dim != 3 )
{
cout << "\n";
cout << "FEM3D_PROJECT - Fatal error!\n";
cout << " Spatial dimension of the sample nodes is not 3.\n";
exit ( 1 );
}
sample_node_xyz = r8mat_data_read ( sample_node_filename, sample_node_dim,
sample_node_num );
i4mat_header_read ( sample_element_filename, &sample_element_order,
&sample_element_num );
cout << "\n";
cout << " Sample element order is " << sample_element_order << "\n";
cout << " Sample element number is " << sample_element_num << "\n";
if ( sample_element_order != 4 )
{
cout << "\n";
cout << "FEM3D_PROJECT - Fatal error!\n";
cout << " The sample element order must be 4.\n";
exit ( 1 );
}
sample_element_node = new int[sample_element_order*sample_element_num];
sample_element_node = i4mat_data_read ( sample_element_filename,
sample_element_order, sample_element_num );
element_min = i4mat_min ( sample_element_order, sample_element_num,
sample_element_node );
if ( element_min == 1 )
{
cout << "\n";
cout << " Converting 1-based sample element array to 0 base.\n";
for ( j = 0; j < sample_element_num; j++ )
{
for ( i = 0; i < sample_element_order; i++ )
{
sample_element_node[i+j*sample_element_order] =
sample_element_node[i+j*sample_element_order] - 1;
}
}
}
r8mat_header_read ( sample_value_filename, &sample_value_dim,
&sample_value_num );
cout << "\n";
cout << " The sample value dimension is " << sample_value_dim << "\n";
cout << " The sample value number is " << sample_value_num << "\n";
if ( sample_value_num != sample_node_num )
{
cout << "\n";
cout << "FEM3D_PROJECT - Fatal error!\n";
cout << " Number of sample values and nodes differ.\n";
exit ( 1 );
}
sample_value = r8mat_data_read ( sample_value_filename, sample_value_dim,
sample_value_num );
//
// Create the sample element neighbor array.
//
sample_element_neighbor = tet_mesh_neighbor_tets ( sample_element_order,
sample_element_num, sample_element_node );
cout << "\n";
cout << " The element neighbor array has been computed.\n";
//
// Read the FEM NODE and ELEMENT data.
//
r8mat_header_read ( fem_node_filename, &fem_node_dim, &fem_node_num );
cout << "\n";
cout << " The FEM node dimension is " << fem_node_dim << "\n";
cout << " The FEM node number is " << fem_node_num << "\n";
if ( fem_node_dim != 3 )
{
cout << "\n";
cout << "FEM3D_PROJECT - Fatal error!\n";
cout << " Spatial dimension of the nodes is not 3.\n";
exit ( 1 );
}
fem_node_xyz = r8mat_data_read ( fem_node_filename, fem_node_dim, fem_node_num );
i4mat_header_read ( fem_element_filename, &fem_element_order, &fem_element_num );
cout << " The FEM element order is " << fem_element_order << "\n";
cout << " The FEM element number is " << fem_element_num << "\n";
if ( fem_element_order != 4 )
{
cout << "\n";
cout << "FEM3D_PROJECT - Fatal error!\n";
cout << " The FEM element order is not 4.\n";
exit ( 1 );
}
fem_element_node = i4mat_data_read ( fem_element_filename, fem_element_order,
fem_element_num );
element_min = i4mat_min ( fem_element_order, fem_element_num,
fem_element_node );
if ( element_min == 1 )
{
cout << "\n";
cout << " Converting 1-based FEM element array to 0 base.\n";
for ( j = 0; j < fem_element_num; j++ )
{
for ( i = 0; i < fem_element_order; i++ )
{
fem_element_node[i+j*fem_element_order] =
fem_element_node[i+j*fem_element_order] - 1;
}
}
}
//
// Compute the FEM values.
//
fem_value_dim = sample_value_dim;
fem_value_num = fem_node_num;
fem_value = fem3d_transfer ( sample_node_num, sample_element_order,
sample_element_num, sample_value_dim, sample_value_num,
sample_node_xyz, sample_element_node, sample_element_neighbor, sample_value,
fem_node_num, fem_element_order,
fem_element_num, fem_value_dim, fem_value_num,
fem_node_xyz, fem_element_node );
//
// Write the FEM values.
//
r8mat_write ( fem_value_filename, fem_value_dim, fem_value_num,
fem_value );
cout << "\n";
cout << " FEM value data written to \"" << fem_value_filename << "\"\n";
//
// Terminate.
//
cout << "\n";
cout << "FEM3D_PROJECT\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
delete [] fem_element_node;
delete [] fem_node_xyz;
delete [] fem_value;
delete [] sample_element_neighbor;
delete [] sample_element_node;
delete [] sample_node_xyz;
delete [] sample_value;
return 0;
}
//****************************************************************************80
void basis_mn_tet4 ( double t[3*4], int n, double p[], double phi[] )
//****************************************************************************80
//
// Purpose:
//
// BASIS_MN_TET4: all bases at N points for a TET4 element.
//
// Discussion:
//
// The routine is given the coordinates of the vertices of a tetrahedron.
//
// It works directly with these coordinates, and does not refer to a
// reference element.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 07 August 2009
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Olgierd Zienkiewicz,
// The Finite Element Method,
// Sixth Edition,
// Butterworth-Heinemann, 2005,
// ISBN: 0750663200,
// LC: TA640.2.Z54.
//
// Parameters:
//
// Input, double T[3*4], the coordinates of the vertices.
//
// Input, int N, the number of evaluation points.
//
// Input, double P[3*N], the points where the basis functions
// are to be evaluated.
//
// Output, double PHI[4*N], the value of the basis functions
// at the evaluation points.
//
{
int j;
double volume;
//
// | x1 x2 x3 x4 |
// Volume = | y1 y2 y3 y4 |
// | z1 z2 z3 z4 |
// | 1 1 1 1 |
//
volume =
t[0+0*3] * (
t[1+1*3] * ( t[2+2*3] - t[2+3*3] )
- t[1+2*3] * ( t[2+1*3] - t[2+3*3] )
+ t[1+3*3] * ( t[2+1*3] - t[2+2*3] ) )
- t[0+1*3] * (
t[1+0*3] * ( t[2+2*3] - t[2+3*3] )
- t[1+2*3] * ( t[2+0*3] - t[2+3*3] )
+ t[1+3*3] * ( t[2+0*3] - t[2+2*3] ) )
+ t[0+2*3] * (
t[1+0*3] * ( t[2+1*3] - t[2+3*3] )
- t[1+1*3] * ( t[2+0*3] - t[2+3*3] )
+ t[1+3*3] * ( t[2+0*3] - t[2+1*3] ) )
- t[0+3*3] * (
t[1+0*3] * ( t[2+1*3] - t[2+2*3] )
- t[1+1*3] * ( t[2+0*3] - t[2+2*3] )
+ t[1+2*3] * ( t[2+0*3] - t[2+1*3] ) );
if ( volume == 0.0 )
{
cerr << "\n";
cerr << "BASIS_MN_TET4 - Fatal error!\n";
cerr << " Element has zero volume.\n";
exit ( 1 );
}
//
// | xp x2 x3 x4 |
// Phi(1,P) = | yp y2 y3 y4 | / volume
// | zp z2 z3 z4 |
// | 1 1 1 1 |
//
for ( j = 0; j < n; j++ )
{
phi[0+j*4] = (
p[0+j*3] * (
t[1+1*3] * ( t[2+2*3] - t[2+3*3] )
- t[1+2*3] * ( t[2+1*3] - t[2+3*3] )
+ t[1+3*3] * ( t[2+1*3] - t[2+2*3] ) )
- t[0+1*3] * (
p[1+j*3] * ( t[2+2*3] - t[2+3*3] )
- t[1+2*3] * ( p[2+j*3] - t[2+3*3] )
+ t[1+3*3] * ( p[2+j*3] - t[2+2*3] ) )
+ t[0+2*3] * (
p[1+j*3] * ( t[2+1*3] - t[2+3*3] )
- t[1+1*3] * ( p[2+j*3] - t[2+3*3] )
+ t[1+3*3] * ( p[2+j*3] - t[2+1*3] ) )
- t[0+3*3] * (
p[1+j*3] * ( t[2+1*3] - t[2+2*3] )
- t[1+1*3] * ( p[2+j*3] - t[2+2*3] )
+ t[1+2*3] * ( p[2+j*3] - t[2+1*3] ) ) ) / volume;
//
// | x1 xp x3 x4 |
// Phi(2,P) = | y1 yp y3 y4 | / volume
// | z1 zp z3 z4 |
// | 1 1 1 1 |
//
phi[1+j*4] = (
t[0+0*3] * (
p[1+j*3] * ( t[2+2*3] - t[2+3*3] )
- t[1+2*3] * ( p[2+j*3] - t[2+3*3] )
+ t[1+3*3] * ( p[2+j*3] - t[2+2*3] ) )
- p[0+j*3] * (
t[1+0*3] * ( t[2+2*3] - t[2+3*3] )
- t[1+2*3] * ( t[2+0*3] - t[2+3*3] )
+ t[1+3*3] * ( t[2+0*3] - t[2+2*3] ) )
+ t[0+2*3] * (
t[1+0*3] * ( p[2+j*3] - t[2+3*3] )
- p[1+j*3] * ( t[2+0*3] - t[2+3*3] )
+ t[1+3*3] * ( t[2+0*3] - p[2+j*3] ) )
- t[0+3*3] * (
t[1+0*3] * ( p[2+j*3] - t[2+2*3] )
- p[1+j*3] * ( t[2+0*3] - t[2+2*3] )
+ t[1+2*3] * ( t[2+0*3] - p[2+j*3] ) ) ) / volume;
//
// | x1 x2 xp x4 |
// Phi(3,P) = | y1 y2 yp y4 | / volume
// | z1 z2 zp z4 |
// | 1 1 1 1 |
//
phi[2+j*4] = (
t[0+0*3] * (
t[1+1*3] * ( p[2+j*3] - t[2+3*3] )
- p[1+j*3] * ( t[2+1*3] - t[2+3*3] )
+ t[1+3*3] * ( t[2+1*3] - p[2+j*3] ) )
- t[0+1*3] * (
t[1+0*3] * ( p[2+j*3] - t[2+3*3] )
- p[1+j*3] * ( t[2+0*3] - t[2+3*3] )
+ t[1+3*3] * ( t[2+0*3] - p[2+j*3] ) )
+ p[0+j*3] * (
t[1+0*3] * ( t[2+1*3] - t[2+3*3] )
- t[1+1*3] * ( t[2+0*3] - t[2+3*3] )
+ t[1+3*3] * ( t[2+0*3] - t[2+1*3] ) )
- t[0+3*3] * (
t[1+0*3] * ( t[2+1*3] - p[2+j*3] )
- t[1+1*3] * ( t[2+0*3] - p[2+j*3] )
+ p[1+j*3] * ( t[2+0*3] - t[2+1*3] ) ) ) / volume;
//
// | x1 x2 x3 xp |
// Phi(4,P) = | y1 y2 y3 yp | / volume
// | z1 z2 z3 zp |
// | 1 1 1 1 |
//
phi[3+j*4] = (
t[0+0*3] * (
t[1+1*3] * ( t[2+2*3] - p[2+j*3] )
- t[1+2*3] * ( t[2+1*3] - p[2+j*3] )
+ p[1+j*3] * ( t[2+1*3] - t[2+2*3] ) )
- t[0+1*3] * (
t[1+0*3] * ( t[2+2*3] - p[2+j*3] )
- t[1+2*3] * ( t[2+0*3] - p[2+j*3] )
+ p[1+j*3] * ( t[2+0*3] - t[2+2*3] ) )
+ t[0+2*3] * (
t[1+0*3] * ( t[2+1*3] - p[2+j*3] )
- t[1+1*3] * ( t[2+0*3] - p[2+j*3] )
+ p[1+j*3] * ( t[2+0*3] - t[2+1*3] ) )
- p[0+j*3] * (
t[1+0*3] * ( t[2+1*3] - t[2+2*3] )
- t[1+1*3] * ( t[2+0*3] - t[2+2*3] )
+ t[1+2*3] * ( t[2+0*3] - t[2+1*3] ) ) ) / volume;
}
return;
}
//****************************************************************************80
char ch_cap ( char ch )
//****************************************************************************80
//
// Purpose:
//
// CH_CAP capitalizes a single character.
//
// Discussion:
//
// This routine should be equivalent to the library "toupper" function.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 19 July 1998
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, char CH, the character to capitalize.
//
// Output, char CH_CAP, the capitalized character.
//
{
if ( 97 <= ch && ch <= 122 )
{
ch = ch - 32;
}
return ch;
}
//****************************************************************************80
bool ch_eqi ( char ch1, char ch2 )
//****************************************************************************80
//
// Purpose:
//
// CH_EQI is true if two characters are equal, disregarding case.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 13 June 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, char CH1, CH2, the characters to compare.
//
// Output, bool CH_EQI, is true if the two characters are equal,
// disregarding case.
//
{
if ( 97 <= ch1 && ch1 <= 122 )
{
ch1 = ch1 - 32;
}
if ( 97 <= ch2 && ch2 <= 122 )
{
ch2 = ch2 - 32;
}
return ( ch1 == ch2 );
}
//****************************************************************************80
int ch_to_digit ( char ch )
//****************************************************************************80
//
// Purpose:
//
// CH_TO_DIGIT returns the integer value of a base 10 digit.
//
// Example:
//
// CH DIGIT
// --- -----
// '0' 0
// '1' 1
// ... ...
// '9' 9
// ' ' 0
// 'X' -1
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 13 June 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, char CH, the decimal digit, '0' through '9' or blank are legal.
//
// Output, int CH_TO_DIGIT, the corresponding integer value. If the
// character was 'illegal', then DIGIT is -1.
//
{
int digit;
if ( '0' <= ch && ch <= '9' )
{
digit = ch - '0';
}
else if ( ch == ' ' )
{
digit = 0;
}
else
{
digit = -1;
}
return digit;
}
//****************************************************************************80
double *fem3d_transfer ( int sample_node_num, int sample_element_order,
int sample_element_num, int sample_value_dim, int sample_value_num,
double sample_node_xyz[], int sample_element_node[],
int sample_element_neighbor[], double sample_value[], int fem_node_num,
int fem_element_order, int fem_element_num, int fem_value_dim,
int fem_value_num, double fem_node_xyz[], int fem_element_node[] )
//****************************************************************************80
//
// Purpose:
//
// FEM3D_TRANSFER "transfers" from one finite element mesh to another.
//
// BAD THINGS:
//
// 1) the linear system A*X=B is defined with A being a full storage matrix.
// 2) the quadrature rule used is low order.
// 3) the elements are assumed to be linear.
//
// Discussion:
//
// We are also given a set of "sample" finite element function defined
// by SAMPLE_NODE_XYZ, SAMPLE_ELEMENT, and SAMPLE_VALUE.
//
// We are given a second finite element mesh, FEM_NODE_XYZ and
// FEM_ELEMENT_NODE.
//
// Our aim is to "project" the sample data values into the finite element
// space, that is, to come up with a finite element function FEM_VALUE which
// well approximates the sample data.
//
// Now let W(x,y,z) represent a function interpolating the sample data, and
// let Vijk(x,y,z) represent the finite element basis function associated with
// node IJK.
//
// Then we seek the coefficient vector U corresponding to a finite element
// function U(x,y,z) of the form:
//
// U(x,y,z) = sum ( 1 <= IJK <= N ) Uijk * Vijk(x,y,z)
//
// To determine the coefficent vector entries U, we form a set of
// projection equations. For node IJK at grid point (I,J,K), the associated
// basis function Vk(x,y,z) is used to pose the equation:
//
// Integral U(x,y,z) Vijk(x,y,z) dx dy dz
// = Integral W(x,y,z) Vijk(x,y,z) dx dy dz
//
// The left hand side is the usual stiffness matrix times the desired
// coefficient vector U. To complete the system, we simply need to
// determine the right hand side, that is, the integral of the data function
// W against the basis function Vk.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 27 August 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int SAMPLE_NODE_NUM, the number of nodes.
//
// Input, int SAMPLE_ELEMENT_ORDER, the element order.
//
// Input, int SAMPLE_ELEMENT_NUM, the number of elements.
//
// Input, int SAMPLE_VALUE_DIM, the value dimension.
//
// Input, int SAMPLE_VALUE_NUM, the number of values.
//
// Input, double SAMPLE_NODE_XYZ[3*SAMPLE_NODE_NUM], the nodes.
//
// Input, int SAMPLE_ELEMENT_NODE[SAMPLE_ELEMENT_ORDER*SAMPLE_ELEMENT_NUM],
// the nodes that make up each element.
//
// Input, int SAMPLE_ELEMENT_NEIGHBOR[3*SAMPLE_ELEMENT_NUM],
// the neighbor triangles.
//
// Input, double SAMPLE_VALUE[SAMPLE_VALUE_DIM*SAMPLE_NODE_NUM],
// the values.
//
// Input, int FEM_NODE_NUM, the number of nodes.
//
// Input, int FEM_ELEMENT_ORDER, the element order.
//
// Input, int FEM_ELEMENT_NUM, the number of elements.
//
// Input, int FEM_VALUE_DIM, the value dimension.
//
// Input, int FEM_VALUE_NUM, the number of values.
//
// Input, double FEM_NODE_XYZ[3*FEM_NODE_NUM], the nodes.
//
// Input, int FEM_ELEMENT_NODE[FEM_ELEMENT_ORDER*FEM_ELEMENT_NUM],
// the nodes that make up each element.
//
// Output, double FEM3D_TRANSFER[FEM_VALUE_DIM*FEM_VALUE_NUM],
// the values.
//
{
double *a;
double *b;
int element;
double *fem_value;
int i;
int j;
int j2;
int k;
int ni;
int nj;
double *phi;
int project_node_num = 1;
double project_node_xyz[3*1];
double *project_value;
int quad;
int quad_num = 4;
double *ref_quad;
double *ref_weight;
double *tet_quad;
double *tet_xyz;
double volume;
double *x;
//
// Assemble the coefficient matrix A and the right-hand side B.
//
b = r8mat_zero_new ( fem_node_num, fem_value_dim );
a = r8mat_zero_new ( fem_node_num, fem_node_num );
phi = new double[4];
ref_weight = new double[quad_num];
ref_quad = new double[4*quad_num];
tet_quad = new double[3*quad_num];
tet_xyz = new double[3*4];
for ( element = 0; element < fem_element_num; element++ )
{
for ( j = 0; j < 4; j++ )
{
for ( i = 0; i < 3; i++ )
{
j2 = fem_element_node[j+element*4];
tet_xyz[i+j*3] = fem_node_xyz[i+j2*3];
}
}
volume = tetrahedron_volume ( tet_xyz );
for ( j = 0; j < quad_num; j++ )
{
for ( i = 0; i < 3; i++ )
{
tet_quad[i+j*3] = 0.0;
for ( k = 0; k < 4; k++ )
{
tet_quad[i+j*3] = tet_quad[i+j*3] + tet_xyz[i+k*3] * ref_quad[k+j*4];
}
}
}
//
// Consider each quadrature point.
// Here, we use the midside nodes as quadrature points.
//
for ( quad = 0; quad < quad_num; quad++ )
{
for ( i = 0; i < 3; i++ )
{
project_node_xyz[i+0*3] = tet_quad[i+quad*3];
}
basis_mn_tet4 ( tet_xyz, 1, project_node_xyz, phi );
for ( i = 0; i < 4; i++ )
{
ni = fem_element_node[i+element*fem_element_order];
//
// The projection takes place here. The finite element code needs the value
// of the sample function at the point (XQ,YQ). The call to PROJECTION
// locates (XQ,YQ) in the triangulated mesh of sample data, and returns a
// value produced by piecewise linear interpolation.
//
project_value = projection ( sample_node_num, sample_node_xyz,
sample_element_order, sample_element_num, sample_element_node,
sample_element_neighbor, sample_value_dim, sample_value,
project_node_num, project_node_xyz );
for ( j = 0; j < fem_value_dim; j++ )
{
b[ni+j*fem_node_num] = b[ni+j*fem_node_num]
+ volume * ref_weight[quad] * ( project_value[j+0*fem_value_dim] * phi[i] );
}
delete [] project_value;
//
// Consider each basis function in the element.
//
for ( j = 0; j < 4; j++ )
{
nj = fem_element_node[j+element*fem_element_order];
a[ni+nj*fem_node_num] = a[ni+nj*fem_node_num]
+ volume * ref_weight[quad] * ( phi[i] * phi[j] );
}
}
}
}
//
// SOLVE the linear system A * X = B.
//
x = r8ge_fss_new ( fem_node_num, a, fem_value_dim, b );
//
// Copy solution.
//
fem_value = new double[fem_value_dim*fem_value_num];
for ( j = 0; j < fem_value_num; j++ )
{
for ( i = 0; i < fem_value_dim; i++ )
{
fem_value[i+j*fem_value_dim] = x[j+i*fem_value_num];
}
}
delete [] a;
delete [] b;
delete [] phi;
delete [] ref_quad;
delete [] ref_weight;
delete [] tet_quad;
delete [] tet_xyz;
delete [] x;
return fem_value;
}
//****************************************************************************80
int file_column_count ( string filename )
//****************************************************************************80
//
// Purpose:
//
// FILE_COLUMN_COUNT counts the columns in the first line of a file.
//
// Discussion:
//
// The file is assumed to be a simple text file.
//
// Most lines of the file are presumed to consist of COLUMN_NUM words,
// separated by spaces. There may also be some blank lines, and some
// comment lines, which have a "#" in column 1.
//
// The routine tries to find the first non-comment non-blank line and
// counts the number of words in that line.
//
// If all lines are blanks or comments, it goes back and tries to analyze
// a comment line.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 05 July 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string FILENAME, the name of the file.
//
// Output, int FILE_COLUMN_COUNT, the number of columns assumed
// to be in the file.
//
{
int column_num;
ifstream input;
bool got_one;
string text;
//
// Open the file.
//
input.open ( filename.c_str ( ) );
if ( !input )
{
column_num = -1;
cerr << "\n";
cerr << "FILE_COLUMN_COUNT - Fatal error!\n";
cerr << " Could not open the file:\n";
cerr << " \"" << filename << "\"\n";
return column_num;
}
//
// Read one line, but skip blank lines and comment lines.
//
got_one = false;
for ( ; ; )
{
getline ( input, text );
if ( input.eof ( ) )
{
break;
}