Debian Codegraph @

packages/galeri/example-fem/Laplacian3D.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_ConfigDefs.h"
11 #include "Galeri_Utils.h"
12 #include "Galeri_FiniteElements.h"
13 #ifdef HAVE_MPI
14 #include "mpi.h"
15 #include "Epetra_MpiComm.h"
16 #else
17 #include "Epetra_SerialComm.h"
18 #endif
19
20 // ==========================================================
21 // This file solves the scalar elliptic problem
22 //
23 // - \mu \nabla u + \sigma u = f on \Omega
24 // u = g on \partial \Omega
25 //
26 // where \Omega is a 3D cube, divided into hexahedra.
27 // `f' is specified by function `Force()', the Dirichlet boundary condition
28 // by function `BoundaryValue()', and the value of \mu and
29 // \sigma can be changed in the functions Diffusion() and
30 // Source(). The code solves the corresponding linear system
31 // using AztecOO with ML preconditioner, and writes the
32 // solution into a MEDIT-compatible format. Then, it computes
33 // the L2 and H1 norms of the solution and the error.
34 //
35 // \author Marzio Sala, ETHZ/COLAB
36 //
37 // \date Last updated on 15-Sep-05
38 // ==========================================================
39
40 double Diffusion(const double& x, const double& y, const double& z)
41 {
42 return (1.0);
43 }
44
45 double Source(const double& x, const double& y, const double& z)
46 {
47 return (0.0);
48 }
49
50 double Force(const double& x, const double& y, const double& z)
51 {
52 return (-6.0);
53 }
54
55 // Specifies the boundary condition.
56 double BoundaryValue(const double& x, const double& y,
57 const double& z, const int& PatchID)
58 {
59 return(x * x + y * y + z * z);
60 }
61
62 // Specifies the boundary condition.
63 int BoundaryType(const int& PatchID)
64 {
65 return(Galeri::FiniteElements::GALERI_DIRICHLET);
66 }
67
68 // Returns the value of the exact solution and its first
69 // derivatives with respect to x, y and z.
70 int ExactSolution(double x, double y, double z, double* res)
71 {
72 res[0] = x * x + y * y + z * z;
73 res[1] = 2 * x;
74 res[2] = 2 * y;
75 res[3] = 2 * z;
76
77 return(0);
78 }
79
80 using namespace Galeri;
81 using namespace Galeri::FiniteElements;
82
83 // =========== //
84 // main driver //
85 // =========== //
86
87 int main(int argc, char *argv[])
88 {
89
90 #ifdef HAVE_MPI
91 MPI_Init(&argc,&argv);
92 Epetra_MpiComm Comm(MPI_COMM_WORLD);
93 #else
94 Epetra_SerialComm Comm;
95 #endif
96
97 try {
98
99 // ============================================================ //
100 // Prepares the computational domain. For simplicity, //
101 // the computation domain has (nx * NumProcs, ny, nz) elements, //
102 // and it is partitioned into (NumProcs, 1, 1) subdomains. //
103 // If you want to change the grid element, remember to modify //
104 // the quadrature in GalerkinVariational<T>. Now T is set to //
105 // HexQuadrature. //
106 // ============================================================ //
107
108 int nx = 4 * Comm.NumProc();
109 int ny = 4;
110 int nz = 4;
111 int mx = Comm.NumProc(), my = 1, mz = 1;
112
113 HexCubeGrid Grid(Comm, nx, ny, nz, mx, my, mz);
114
115 // ======================================================== //
116 // Prepares the linear system. This requires the definition //
117 // of a quadrature formula compatible with the grid, a //
118 // variational formulation, and a problem object which take //
119 // care of filling matrix and right-hand side. //
120 // ======================================================== //
121
122 Epetra_CrsMatrix A(Copy, Grid.RowMap(), 0);
123 Epetra_Vector LHS(Grid.RowMap());
124 Epetra_Vector RHS(Grid.RowMap());
125
126 int NumQuadratureNodes = 1;
127
128 GalerkinVariational<HexQuadrature>
129 Laplacian(NumQuadratureNodes, Diffusion, Source, Force,
130 BoundaryValue, BoundaryType);
131
132 LinearProblem FiniteElementProblem(Grid, Laplacian, A, LHS, RHS);
133 FiniteElementProblem.Compute();
134
135 // =================================================== //
136 // The solution must be computed here by solving the //
137 // linear system A * LHS = RHS. //
138 //
139 // NOTE: Solve() IS A SIMPLE FUNCTION BASED ON LAPACK, //
140 // THEREFORE THE MATRIX IS CONVERTED TO DENSE FORMAT. //
141 // IT WORKS IN SERIAL ONLY. //
142 // EVEN MEDIUM-SIZED MATRICES MAY REQUIRE A LOT OF //
143 // MEMORY AND CPU-TIME! USERS SHOULD CONSIDER INSTEAD //
144 // AZTECOO, ML, IFPACK OR OTHER SOLVERS. //
145 // =================================================== //
146
147 Solve(&A, &LHS, &RHS);
148
149 // ========================= //
150 // After the solution: //
151 // - computation of the norm //
152 // - output using MEDIT //
153 // ========================= //
154
155 FiniteElementProblem.ComputeNorms(LHS, ExactSolution);
156
157 MEDITInterface MEDIT(Comm);
158 MEDIT.Write(Grid, "Laplacian3D", LHS);
159
160 }
161 catch (Exception& rhs)
162 {
163 if (Comm.MyPID() == 0)
164 rhs.Print();
165 }
166 catch (int e) {
167 cerr << "Caught exception, value = " << e << endl;
168 }
169 catch (...) {
170 cerr << "Caught generic exception" << endl;
171 }
172
173 #ifdef HAVE_MPI
174 MPI_Finalize();
175 #endif
176
177 return(0);
178 }