Debian Codegraph @

packages/galeri/example-fem/TwoSquares.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Galeri: Finite Element and Matrix Generation Package
4 //
5 // Copyright 2006 ETHZ/NTESS and the Galeri contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include "Galeri_Utils.h"
11 #include "Galeri_FiniteElements.h"
12 #ifdef HAVE_MPI
13 #include "mpi.h"
14 #include "Epetra_MpiComm.h"
15 #else
16 #include "Epetra_SerialComm.h"
17 #endif
18
19 using namespace Galeri;
20 using namespace Galeri::FiniteElements;
21
22 // ==========================================================
23 // This file solves the scalar problem
24 //
25 // - \mu \nabla u + \sigma u = f on \Omega
26 // u = g on \partial \Omega
27 //
28 // where \Omega is a 2D rectangle, divided into triangles.
29 // The input grid should be generated using similar
30 // conventions of file galeri/data/TwoSquares.m:
31 // - the bc ID of 10 and 20 are for the mortar faces
32 // - the bc ID of 0 is for the external domain.
33 // ==========================================================
34
35 double Diffusion(const double& x, const double& y, const double& z)
36 {
37 return (1.0);
38 }
39
40 double Source(const double& x, const double& y, const double& z)
41 {
42 return (0.0);
43 }
44
45 double Force(const double& x, const double& y, const double& z)
46 {
47 if (y < 1)
48 return(1.0);
49 else
50 return(0.5);
51 }
52
53 // Specifies the boundary condition.
54 int BoundaryType(const int& Patch)
55 {
56 if (Patch == 10 || Patch == 20)
57 return(GALERI_DO_NOTHING);
58 else
59 return(GALERI_DIRICHLET);
60 }
61
62 // Specifies the boundary condition.
63 double BoundaryValue(const double& x, const double& y,
64 const double& z, const int& Patch)
65 {
66 if (x == -1.0 || x == 1.0)
67 return(0.0);
68 else if (Patch == 10 || Patch == 20)
69 return(1.0);
70 else
71 return (0.0);
72 }
73
74 // =========== //
75 // main driver //
76 // =========== //
77
78 int main(int argc, char *argv[])
79 {
80 #ifdef HAVE_MPI
81 MPI_Init(&argc,&argv);
82 Epetra_MpiComm Comm(MPI_COMM_WORLD);
83 #else
84 Epetra_SerialComm Comm;
85 #endif
86
87 try {
88
89 // ================================================== //
90 // Defines the grid for this problem, a rectangle, //
91 // with the number of nodes along the X-axis (nx) and //
92 // Y-axis (ny), the length of the rectangle along the //
93 // axes, and the number of processors on each axix. //
94 // ================================================== //
95
96 int nx = 40 * Comm.NumProc();
97 int ny = 40;
98 int mx = Comm.NumProc();
99 int my = 1;
100
101 //TriangleRectangleGrid Grid(Comm, nx, ny, mx, my);
102 FileGrid Grid(Comm, "TwoSquares.grid");
103
104 // ======================================================== //
105 // Prepares the linear system. This requires the definition //
106 // of a quadrature formula compatible with the grid, a //
107 // variational formulation, and a problem object which take //
108 // care of filling matrix and right-hand side. //
109 // ======================================================== //
110
111 Epetra_CrsMatrix A(Copy, Grid.RowMap(), 0);
112 Epetra_Vector LHS(Grid.RowMap());
113 Epetra_Vector RHS(Grid.RowMap());
114
115 int NumQuadratureNodes = 3;
116
117 GalerkinVariational<TriangleQuadrature>
118 Laplace2D(NumQuadratureNodes, Diffusion, Source, Force,
119 BoundaryValue, BoundaryType);
120
121 LinearProblem FiniteElementProblem(Grid, Laplace2D, A, LHS, RHS);
122 FiniteElementProblem.Compute();
123
124 // =================================================== //
125 // The solution must be computed here by solving the //
126 // linear system A * LHS = RHS. //
127 //
128 // NOTE: Solve() IS A SIMPLE FUNCTION BASED ON LAPACK, //
129 // THEREFORE THE MATRIX IS CONVERTED TO DENSE FORMAT. //
130 // IT WORKS IN SERIAL ONLY. //
131 // EVEN MEDIUM-SIZED MATRICES MAY REQUIRE A LOT OF //
132 // MEMORY AND CPU-TIME! USERS SHOULD CONSIDER INSTEAD //
133 // AZTECOO, ML, IFPACK OR OTHER SOLVERS. //
134 // =================================================== //
135
136 Solve(&A, &LHS, &RHS);
137
138 // ================== //
139 // Output using MEDIT //
140 // ================== //
141
142 MEDITInterface MEDIT(Comm);
143 MEDIT.Write(Grid, "output", LHS);
144
145 }
146 catch (int e) {
147 cerr << "Caught exception, value = " << e << endl;
148 }
149 catch (...) {
150 cerr << "Caught generic exception" << endl;
151 }
152
153 #ifdef HAVE_MPI
154 MPI_Finalize();
155 #endif
156
157 return(0);
158 }