-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfem2d_poisson_rectangle.cpp
3743 lines (3454 loc) · 90.1 KB
/
fem2d_poisson_rectangle.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 <ctime>
using namespace std;
int main ( void );
void area_set ( int node_num, double node_xy[], int nnodes,
int element_num, int element_node[], double element_area[] );
void assemble ( int node_num, double node_xy[], int nnodes,
int element_num, int element_node[], int nq,
double wq[], double xq[], double yq[], double element_area[], int indx[],
int ib, int nunk, double a[], double f[] );
int bandwidth ( int nnodes, int element_num, int element_node[],
int node_num, int indx[] );
void boundary ( int nx, int ny, int node_num, double node_xy[], int indx[],
int ib, int nunk, double a[], double f[] );
void compare ( int node_num, double node_xy[], int indx[], int nunk, double f[] );
int dgb_fa ( int n, int ml, int mu, double a[], int pivot[] );
void dgb_print_some ( int m, int n, int ml, int mu, double a[], int ilo,
int jlo, int ihi, int jhi, char *title );
double *dgb_sl ( int n, int ml, int mu, double a[], int pivot[],
double b[], int job );
void element_write ( int nnodes, int element_num, int element_node[],
char *triangulation_txt_file_name );
void errors ( double element_area[], int element_node[], int indx[],
double node_xy[], double f[], int element_num, int nnodes,
int nunk, int node_num, double *el2, double *eh1 );
void exact ( double x, double y, double *u, double *dudx, double *dudy );
void grid_t6 ( int nx, int ny, int nnodes, int element_num, int element_node[] );
int i4_max ( int i1, int i2 );
int i4_min ( int i1, int i2 );
void i4vec_print_some ( int n, int a[], int max_print, char *title );
void indx_set ( int nx, int ny, int node_num, int indx[], int *nunk );
void nodes_plot ( char *file_name, int node_num, double node_xy[],
bool node_label );
void nodes_write ( int node_num, double node_xy[], char *output_filename );
void qbf ( double x, double y, int element, int inode, double node_xy[],
int element_node[], int element_num, int nnodes,
int node_num, double *bb, double *bx, double *by );
void quad_a ( double node_xy[], int element_node[],
int element_num, int node_num, int nnodes, double wq[], double xq[],
double yq[] );
void quad_e ( double node_xy[], int element_node[],
int element, int element_num, int nnodes, int node_num, int nqe,
double wqe[], double xqe[], double yqe[] );
double r8_huge ( void );
double r8_max ( double x, double y );
double r8_min ( double x, double y );
int r8_nint ( double x );
void r8vec_print_some ( int n, double a[], int max_print, char *title );
double rhs ( double x, double y );
void solution_write ( double f[], int indx[], int node_num, int nunk,
char *output_filename, double node_xy[] );
void timestamp ( void );
void triangulation_order6_plot ( char *file_name, int node_num, double node_xy[],
int tri_num, int triangle_node[], int node_show, int triangle_show );
void xy_set ( int nx, int ny, int node_num, double xl, double xr, double yb,
double yt, double node_xy[] );
//****************************************************************************80
int main ( void )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for FEM2D_POISSON_RECTANGLE.
//
// Discussion:
//
// FEM2D_POISSON_RECTANGLE solves
//
// -Laplacian U(X,Y) = F(X,Y)
//
// in a rectangular region in the plane. Along the boundary,
// Dirichlet boundary conditions are imposed.
//
// U(X,Y) = G(X,Y)
//
// The code uses continuous piecewise quadratic basis functions on
// triangles determined by a uniform grid of NX by NY points.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 September 2008
//
// Author:
//
// John Burkardt
//
// Local parameters:
//
// Local, double A[(3*IB+1)*NUNK], the coefficient matrix.
//
// Local, double ELEMENT_AREA[ELEMENT_NUM], the area of each element.
//
// Local, double C[NUNK], the finite element coefficients, solution of A * C = F.
//
// Local, double EH1, the H1 seminorm error.
//
// Local, double EL2, the L2 error.
//
// Local, int ELEMENT_NODE[ELEMENT_NUM*NNODES]; ELEMENT_NODE(I,J) is the
// global node index of the local node J in element I.
//
// Local, int ELEMENT_NUM, the number of elements.
//
// Local, double F[NUNK], the right hand side.
//
// Local, int IB, the half-bandwidth of the matrix.
//
// Local, int INDX[NODE_NUM], gives the index of the unknown quantity
// associated with the given node.
//
// Local, int NNODES, the number of nodes used to form one element.
//
// Local, double NODE_XY[2*NODE_NUM], the X and Y coordinates of nodes.
//
// Local, int NQ, the number of quadrature points used for assembly.
//
// Local, int NUNK, the number of unknowns.
//
// Local, int NX, the number of points in the X direction.
//
// Local, int NY, the number of points in the Y direction.
//
// Local, double WQ[NQ], quadrature weights.
//
// Local, double XL, XR, YB, YT, the X coordinates of
// the left and right sides of the rectangle, and the Y coordinates
// of the bottom and top of the rectangle.
//
// Local, double XQ[NQ*ELEMENT_NUM], YQ[NQ*ELEMENT_NUM], the X and Y
// coordinates of the quadrature points in each element.
//
{
# define NNODES 6
# define NQ 3
# define NX 7
# define NY 7
# define ELEMENT_NUM ( NX - 1 ) * ( NY - 1 ) * 2
# define NODE_NUM ( 2 * NX - 1 ) * ( 2 * NY - 1 )
double *a;
double *c;
double eh1;
double el2;
int element;
double element_area[ELEMENT_NUM];
bool *element_mask;
int element_node[NNODES*ELEMENT_NUM];
double *f;
int i;
int ib;
int ierr;
int indx[NODE_NUM];
int job;
int local;
int node;
char *node_eps_file_name = "fem2d_poisson_rectangle_nodes.eps";
char *node_txt_file_name = "fem2d_poisson_rectangle_nodes.txt";
bool node_label;
int node_show;
double node_xy[2*NODE_NUM];
int nunk;
int *pivot;
char *solution_txt_file_name = "fem2d_poisson_rectangle_solution.txt";
int triangle_show;
char *triangulation_eps_file_name = "fem2d_poisson_rectangle_elements.eps";
char *triangulation_txt_file_name = "fem2d_poisson_rectangle_elements.txt";
double wq[NQ];
double xl = 0.0E+00;
double xq[NQ*ELEMENT_NUM];
double xr = 1.0E+00;
double yb = 0.0E+00;
double yq[NQ*ELEMENT_NUM];
double yt = 1.0E+00;
timestamp ( );
cout << "\n";
cout << "FEM2D_POISSON_RECTANGLE:\n";
cout << " C++ version\n";
cout << "\n";
cout << " Compiled on " << __DATE__ << " at " << __TIME__ << ".\n";
cout << "\n";
cout << " Solution of the Poisson equation on a unit box\n";
cout << " in 2 dimensions.\n";
cout << "\n";
cout << " - Uxx - Uyy = F(x,y) in the box\n";
cout << " U(x,y) = G(x,y) on the boundary.\n";
cout << "\n";
cout << " The finite element method is used, with piecewise\n";
cout << " quadratic basis functions on 6 node triangular\n";
cout << " elements.\n";
cout << "\n";
cout << " The corner nodes of the triangles are generated by an\n";
cout << " underlying grid whose dimensions are\n";
cout << "\n";
cout << " NX = " << NX << "\n";
cout << " NY = " << NY << "\n";
cout << "\n";
cout << " Number of nodes = " << NODE_NUM << "\n";
cout << " Number of elements = " << ELEMENT_NUM << "\n";
//
// Set the coordinates of the nodes.
//
xy_set ( NX, NY, NODE_NUM, xl, xr, yb, yt, node_xy );
//
// Organize the nodes into a grid of 6-node triangles.
//
grid_t6 ( NX, NY, NNODES, ELEMENT_NUM, element_node );
//
// Set the quadrature rule for assembly.
//
quad_a ( node_xy, element_node, ELEMENT_NUM, NODE_NUM,
NNODES, wq, xq, yq );
//
// Determine the areas of the elements.
//
area_set ( NODE_NUM, node_xy, NNODES, ELEMENT_NUM,
element_node, element_area );
//
// Determine which nodes are boundary nodes and which have a
// finite element unknown. Then set the boundary values.
//
indx_set ( NX, NY, NODE_NUM, indx, &nunk );
cout << " Number of unknowns = " << nunk << "\n";
//
// Determine the bandwidth of the coefficient matrix.
//
ib = bandwidth ( NNODES, ELEMENT_NUM, element_node, NODE_NUM, indx );
cout << "\n";
cout << " Total bandwidth is " << 3 * ib + 1 << "\n";
//
// Make an EPS picture of the nodes.
//
if ( NX <= 10 && NY <= 10 )
{
node_label = true;
nodes_plot ( node_eps_file_name, NODE_NUM, node_xy, node_label );
cout << "\n";
cout << "FEM2D_POISSON_RECTANGLE:\n";
cout << " Wrote an EPS file\n";
cout << " \"" << node_eps_file_name << "\".\n";
cout << " containing a picture of the nodes.\n";
}
//
// Write the nodes to an ASCII file that can be read into MATLAB.
//
nodes_write ( NODE_NUM, node_xy, node_txt_file_name );
cout << "\n";
cout << "FEM2D_POISSON_RECTANGLE:\n";
cout << " Wrote an ASCII node file\n";
cout << " " << node_txt_file_name << "\n";
cout << " of the form\n";
cout << " X(I), Y(I)\n";
cout << " which can be used for plotting.\n";
//
// Make a picture of the elements.
//
if ( NX <= 10 && NY <= 10 )
{
node_show = 1;
triangle_show = 2;
triangulation_order6_plot ( triangulation_eps_file_name, NODE_NUM,
node_xy, ELEMENT_NUM, element_node, node_show, triangle_show );
cout << "\n";
cout << "FEM2D_POISSON_RECTANGLE:\n";
cout << " Wrote an EPS file\n";
cout << " \"" << triangulation_eps_file_name << "\".\n";
cout << " containing a picture of the elements.\n";
}
//
// Write the elements to a file that can be read into MATLAB.
//
element_write ( NNODES, ELEMENT_NUM, element_node,
triangulation_txt_file_name );
cout << "\n";
cout << "FEM2D_POISSON_RECTANGLE:\n";
cout << " Wrote an ASCII element file\n";
cout << " \"" << triangulation_txt_file_name << "\".\n";
cout << " of the form\n";
cout << " Node(1) Node(2) Node(3) Node(4) Node(5) Node(6)\n";
cout << " which can be used for plotting.\n";
//
// Allocate space for the coefficient matrix A and right hand side F.
//
a = new double[(3*ib+1)*nunk];
f = new double[nunk];
pivot = new int[nunk];
//
// Assemble the coefficient matrix A and the right-hand side F of the
// finite element equations.
//
assemble ( NODE_NUM, node_xy, NNODES,
ELEMENT_NUM, element_node, NQ,
wq, xq, yq, element_area, indx, ib, nunk, a, f );
//
// Print a tiny portion of the matrix.
//
dgb_print_some ( nunk, nunk, ib, ib, a, 1, 1, 5, 5,
" Initial 5 x 5 block of coefficient matrix A:" );
r8vec_print_some ( nunk, f, 10, " Part of the right hand side F:" );
//
// Modify the coefficient matrix and right hand side to account for
// boundary conditions.
//
boundary ( NX, NY, NODE_NUM, node_xy, indx, ib, nunk, a, f );
//
// Print a tiny portion of the matrix.
//
dgb_print_some ( nunk, nunk, ib, ib, a, 1, 1, 5, 5,
" A after boundary adjustment:" );
r8vec_print_some ( nunk, f, 10, " F after boundary adjustment:" );
//
// Solve the linear system using a banded solver.
//
ierr = dgb_fa ( nunk, ib, ib, a, pivot );
if ( ierr != 0 )
{
cout << "\n";
cout << "FEM2D_POISSON_RECTANGLE - Error!\n";
cout << " DGB_FA returned an error condition.\n";
cout << "\n";
cout << " The linear system was not factored, and the\n";
cout << " algorithm cannot proceed.\n";
exit ( 1 );
}
job = 0;
c = dgb_sl ( nunk, ib, ib, a, pivot, f, job );
r8vec_print_some ( nunk, c, 10, " Part of the solution vector:" );
//
// Calculate error using 13 point quadrature rule.
//
errors ( element_area, element_node, indx, node_xy, c,
ELEMENT_NUM, NNODES, nunk, NODE_NUM, &el2, &eh1 );
//
// Compare the exact and computed solutions just at the nodes.
//
compare ( NODE_NUM, node_xy, indx, nunk, c );
//
// Write an ASCII file that can be read into MATLAB.
//
solution_write ( c, indx, NODE_NUM, nunk, solution_txt_file_name,
node_xy );
cout << "\n";
cout << "FEM2D_POISSON_RECTANGLE:\n";
cout << " Wrote an ASCII solution file\n";
cout << " " << solution_txt_file_name << "\n";
cout << " of the form\n";
cout << " U( X(I), Y(I) )\n";
cout << " which can be used for plotting.\n";
//
// Deallocate memory.
//
delete [] a;
delete [] c;
delete [] f;
delete [] pivot;
//
// Terminate.
//
cout << "\n";
cout << "FEM2D_POISSON_RECTANGLE:\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
# undef NNODES
# undef NQ
# undef NX
# undef NY
# undef ELEMENT_NUM
# undef NODE_NUM
}
//****************************************************************************80
void area_set ( int node_num, double node_xy[], int nnodes,
int element_num, int element_node[], double element_area[] )
//****************************************************************************80
//
// Purpose:
//
// AREA_SET sets the area of each element.
//
// Discussion:
//
// The areas of the elements are needed in order to adjust
// the integral estimates produced by the quadrature formulas.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 September 2008
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int NODE_NUM, the number of nodes.
//
// Input, double NODE_XY[2*NODE_NUM], the
// coordinates of the nodes.
//
// Input, int NNODES, the number of local nodes per element.
//
// Input, int ELEMENT_NUM, the number of elements.
//
// Input, int ELEMENT_NODE[NNODES*ELEMENT_NUM];
// ELEMENT_NODE(I,J) is the global index of local node I in element J.
//
// Output, double ELEMENT_AREA[ELEMENT_NUM], the area of elements.
//
{
int element;
int i1;
int i2;
int i3;
double x1;
double x2;
double x3;
double y1;
double y2;
double y3;
for ( element = 0; element < element_num; element++ )
{
i1 = element_node[0+element*nnodes];
x1 = node_xy[0+(i1-1)*2];
y1 = node_xy[1+(i1-1)*2];
i2 = element_node[1+element*nnodes];
x2 = node_xy[0+(i2-1)*2];
y2 = node_xy[1+(i2-1)*2];
i3 = element_node[2+element*nnodes];
x3 = node_xy[0+(i3-1)*2];
y3 = node_xy[1+(i3-1)*2];
element_area[element] = 0.5E+00 * fabs
( y1 * ( x2 - x3 )
+ y2 * ( x3 - x1 )
+ y3 * ( x1 - x2 ) );
}
return;
}
//****************************************************************************80
void assemble ( int node_num, double node_xy[], int nnodes,
int element_num, int element_node[], int nq,
double wq[], double xq[], double yq[], double element_area[], int indx[],
int ib, int nunk, double a[], double f[] )
//****************************************************************************80
//
// Purpose:
//
// ASSEMBLE assembles the matrix and right-hand side using piecewise quadratics.
//
// Discussion:
//
// The matrix is known to be banded. A special matrix storage format
// is used to reduce the space required. Details of this format are
// discussed in the routine DGB_FA.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 September 2008
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Output, double A[(3*IB+1)*NUNK], the NUNK by NUNK coefficient matrix,
// stored in a compressed format.
//
// Output, double F[NUNK], the right hand side.
//
// Input, int IB, the half-bandwidth of the matrix.
//
// Input, double NODE_XY[2*NODE_NUM], the X and Y coordinates of nodes.
//
// Input, double XQ[NQ*ELEMENT_NUM], YQ[NQ*ELEMENT_NUM], the X and Y
// coordinates of the quadrature points in each element.
//
// Input, double WQ[NQ], quadrature weights.
//
// Input, double ELEMENT_AREA[ELEMENT_NUM], the area of each element.
//
// Input, int ELEMENT_NODE[NNODES*ELEMENT_NUM]; ELEMENT_NODE(I,J) is the global
// index of local node I in element J.
//
// Input, int INDX[NODE_NUM], gives the index of the unknown quantity
// associated with the given node.
//
// Input, int NNODES, the number of nodes used to form one element.
//
// Input, int NUNK, the number of unknowns.
//
// Input, int NQ, the number of quadrature points used in assembly.
//
// Input, int ELEMENT_NUM, the number of elements.
//
// Input, int NODE_NUM, the number of nodes.
//
// Local parameters:
//
// Local, double BB, BX, BY, the value of some basis function
// and its first derivatives at a quadrature point.
//
// Local, double BJ, DBJDX, DBJDY, the value of another basis
// function and its first derivatives at a quadrature point.
//
// Local, int NODE_NUM, the number of global nodes.
//
{
double aij;
int basis;
double bi;
double bj;
double dbidx;
double dbidy;
double dbjdx;
double dbjdy;
int element;
int i;
int ii;
int ij;
int ip;
int ipp;
int j;
int quad;
int test;
double w;
double x;
double y;
//
// Initialize the arrays to zero.
//
for ( i = 1; i <= nunk; i++ )
{
f[i-1] = 0.0E+00;
}
for ( j = 1; j <= nunk; j++ )
{
for ( i = 1; i <= 3*ib + 1; i++ )
{
a[i-1+(j-1)*(3*ib+1)] = 0.0E+00;
}
}
//
// The actual values of A and F are determined by summing up
// contributions from all the elements.
//
for ( element = 1; element <= element_num; element++ )
{
for ( quad = 1; quad <= nq; quad++ )
{
x = xq[quad-1+(element-1)*nq];
y = yq[quad-1+(element-1)*nq];
w = element_area[element-1] * wq[quad-1];
for ( test = 1; test <= nnodes; test++ )
{
ip = element_node[test-1+(element-1)*nnodes];
i = indx[ip-1];
qbf ( x, y, element, test, node_xy, element_node,
element_num, nnodes, node_num, &bi, &dbidx, &dbidy );
f[i-1] = f[i-1] + w * rhs ( x, y ) * bi;
//
// We are about to compute a contribution associated with the
// I-th test function and the J-th basis function, and add this
// to the entry A(I,J).
//
// Because of the compressed storage of the matrix, the element
// will actually be stored in A(I-J+2*IB+1,J).
//
// Two extra complications: we are storing the array as a vector,
// and C uses 0-based indices rather than 1-based indices.
//
// Therefore, we ACTUALLY store the entry in A[I-J+2*IB+1-1 + (J-1) * (3*IB+1)];
//
for ( basis = 1; basis <= nnodes; basis++ )
{
ipp = element_node[basis-1+(element-1)*nnodes];
j = indx[ipp-1];
qbf ( x, y, element, basis, node_xy, element_node,
element_num, nnodes, node_num, &bj, &dbjdx, &dbjdy );
aij = dbidx * dbjdx + dbidy * dbjdy;
a[i-j+2*ib+(j-1)*(3*ib+1)] = a[i-j+2*ib+(j-1)*(3*ib+1)] + w * aij;
}
}
}
}
return;
}
//****************************************************************************80
int bandwidth ( int nnodes, int element_num, int element_node[],
int node_num, int indx[] )
//****************************************************************************80
//
// Purpose:
//
// BANDWIDTH determines the bandwidth of the coefficient matrix.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 September 2008
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int NNODES, the number of local nodes per element.
//
// Input, int ELEMENT_NUM, the number of elements.
//
// Input, int ELEMENT_NODE[NNODES*ELEMENT_NUM]; ELEMENT_NODE(I,J) is the global
// index of local node I in element J.
//
// Input, int NODE_NUM, the number of nodes.
//
// Input, int INDX[NODE_NUM], indicates how the value associated with the
// node is to be determined. If INDX(I) is positive, then this is the
// index of the unknown in the finite element linear system. The value
// at the node will be determined by solving the finite element system.
// If INDX(I) is negative, then the node is associated with a boundary
// condition; the value of the boundary condition is stored in the array
// UB, in location -INDX(I).
//
// Output, int BANDWIDTH, the half bandwidth of the matrix.
//
{
int element;
int i;
int iln;
int in;
int j;
int jln;
int jn;
int nhba;
nhba = 0;
for ( element = 1; element <= element_num; element++ )
{
for ( iln = 1; iln <= nnodes; iln++ )
{
in = element_node[iln-1+(element-1)*nnodes];
i = indx[in-1];
if ( 0 < i )
{
for ( jln = 1; jln <= nnodes; jln++ )
{
jn = element_node[jln-1+(element-1)*nnodes];
j = indx[jn-1];
nhba = i4_max ( nhba, j - i );
}
}
}
}
return nhba;
}
//****************************************************************************80
void boundary ( int nx, int ny, int node_num, double node_xy[], int indx[],
int ib, int nunk, double a[], double f[] )
//****************************************************************************80
//
// Purpose:
//
// BOUNDARY modifies the linear system for boundary conditions.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 September 2008
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int NX, NY, controls the number of elements along the
// X and Y directions. The number of elements will be
// 2 * ( NX - 1 ) * ( NY - 1 ).
//
// Input, int NODE_NUM, the number of nodes.
//
// Input, double NODE_XY[2*NODE_NUM], the coordinates of nodes.
//
// Input, int INDX[NODE_NUM], gives the index of the unknown quantity
// associated with the given node.
//
// Input, int IB, the half-bandwidth of the matrix.
//
// Input, int NUNK, the number of unknowns.
//
// Input/output, double A[(3*IB+1)*NUNK], the NUNK by NUNK
// coefficient matrix, stored in a compressed format.
// On output, A has been adjusted for boundary conditions.
//
// Input/output, double F[NUNK], the right hand side.
// On output, F has been adjusted for boundary conditions.
//
{
int col;
double dudx;
double dudy;
int i;
int j;
int jhi;
int jlo;
int node;
int row;
double u;
double x;
double y;
//
// Consider each node.
//
node = 0;
for ( row = 1; row <= 2 * ny - 1; row++ )
{
for ( col = 1; col <= 2 * nx - 1; col++ )
{
node = node + 1;
if ( row == 1 ||
row == 2 * ny - 1 ||
col == 1 ||
col == 2 * nx - 1 )
{
i = indx[node-1];
x = node_xy[0+(node-1)*2];
y = node_xy[1+(node-1)*2];
exact ( x, y, &u, &dudx, &dudy );
jlo = i4_max ( i - ib, 1 );
jhi = i4_min ( i + ib, nunk );
for ( j = jlo; j <= jhi; j++ )
{
a[i-j+2*ib+(j-1)*(3*ib+1)] = 0.0;
}
a[i-i+2*ib+(i-1)*(3*ib+1)] = 1.0;
f[i-1] = u;
}
}
}
return;
}
//****************************************************************************80
void compare ( int node_num, double node_xy[], int indx[], int nunk,
double f[] )
//****************************************************************************80
//
// Purpose:
//
// COMPARE compares the exact and computed solution at the nodes.
//
// Discussion:
//
// This is a rough comparison, done only at the nodes. Such a pointwise
// comparison is easy, because the value of the finite element
// solution is exactly the value of the finite element coefficient
// associated with that node.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 September 2008
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int NODE_NUM, the number of nodes.
//
// Input, double NODE_XY[2*NODE_NUM], the nodes.
//
// Input, int INDX[NODE_NUM], the index of the unknown in the finite
// element linear system.
//
// Input, int NUNK, the number of unknowns in the finite element system.
//
// Input, double F[NUNK], the solution vector of the finite
// element system.
//
{
double dudx;
double dudy;
int i;
int node;
double u;
double uh;
double x;
double y;
cout << "\n";
cout << "COMPARE:\n";
cout << " Compare computed and exact solutions at the nodes.\n";
cout << "\n";
cout << " X Y U U\n";
cout << " computed exact\n";
cout << "\n";
for ( node = 0; node < node_num; node++ )
{
x = node_xy[0+node*2];
y = node_xy[1+node*2];
exact ( x, y, &u, &dudx, &dudy );
i = indx[node];
uh = f[i-1];
cout << setw(12) << x << " "
<< setw(12) << y << " "
<< setw(12) << uh << " "
<< setw(12) << u << "\n";
}
return;
}
//****************************************************************************80
int dgb_fa ( int n, int ml, int mu, double a[], int pivot[] )
//****************************************************************************80
//
// Purpose:
//
// DGB_FA performs a LINPACK-style PLU factorization of an DGB matrix.
//
// Discussion:
//
// The DGB storage format is used for an M by N banded matrix, with lower bandwidth ML
// and upper bandwidth MU. Storage includes room for ML extra superdiagonals,
// which may be required to store nonzero entries generated during Gaussian
// elimination.
//
// The original M by N matrix is "collapsed" downward, so that diagonals
// become rows of the storage array, while columns are preserved. The
// collapsed array is logically 2*ML+MU+1 by N.
//
// The two dimensional array can be further reduced to a one dimensional
// array, stored by columns.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 September 2008
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart,
// LINPACK User's Guide,
// SIAM, 1979
//
// Parameters:
//
// Input, int N, the order of the matrix.
// N must be positive.
//
// Input, int ML, MU, the lower and upper bandwidths.
// ML and MU must be nonnegative, and no greater than N-1.
//
// Input/output, double A[(2*ML+MU+1)*N], the matrix in band storage.
// On output, A has been overwritten by the LU factors.
//
// Output, int PIVOT[N], the pivot vector.
//
// Output, int SGB_FA, singularity flag.
// 0, no singularity detected.
// nonzero, the factorization failed on the INFO-th step.
//
{
int col = 2 * ml + mu + 1;
int i;
int i0;
int j;
int j0;
int j1;
int ju;
int jz;
int k;
int l;
int lm;
int m;
int mm;
double t;
m = ml + mu + 1;
//
// Zero out the initial fill-in columns.
//
j0 = mu + 2;
j1 = i4_min ( n, m ) - 1;
for ( jz = j0; jz <= j1; jz++ )
{
i0 = m + 1 - jz;
for ( i = i0; i <= ml; i++ )
{
a[i-1+(jz-1)*col] = 0.0E+00;
}
}
jz = j1;
ju = 0;
for ( k = 1; k <= n-1; k++ )
{
//
// Zero out the next fill-in column.
//
jz = jz + 1;
if ( jz <= n )
{
for ( i = 1; i <= ml; i++ )
{
a[i-1+(jz-1)*col] = 0.0E+00;