Debian Codegraph @

packages/galeri/example-fem/AdvDiff2D.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 + c_x * u_x + c_y * u_y = f on \Omega
26 // u = g on \partial \Omega
27 //
28 // where \Omega is a 2D rectangle, divided into triangles.
29 // `f' is specified by function `Force()', the Dirichlet boundary condition
30 // by function `BoundaryValue()', and the value of \mu and
31 // c_x and c_y can be changed in the functions Diffusion() and
32 // ConvX() and ConvY(). The code solves the corresponding
33 // linear system using a simple LAPACK interface, and writes
34 // the solution inot a MEDIT-compatible format.
35 //
36 // \author Marzio Sala, ETHZ/COLAB
37 //
38 // \date Last updated on 15-Sep-05.
39 // ==========================================================
40
41 double Diffusion(const double& x, const double& y, const double& z)
42 {
43 return (1.0);
44 }
45
46 double conv = 5000;
47 double ConvX(const double& x, const double& y, const double& z)
48 {
49 return (conv);
50 }
51
52 double ConvY(const double& x, const double& y, const double& z)
53 {
54 return (-conv);
55 }
56
57 double ConvZ(const double& x, const double& y, const double& z)
58 {
59 return (0.0);
60 }
61
62 double Source(const double& x, const double& y, const double& z)
63 {
64 return (0.0);
65 }
66
67 double Force(const double& x, const double& y, const double& z)
68 {
69 return (0.0);
70 }
71
72 // Specifies the boundary condition.
73 double BoundaryValue(const double& x, const double& y,
74 const double& z, const int& Patch)
75 {
76 if ((x == 0.0 && y >= 0.0) || (y == 1.0 && x <= 0.2))
77 return(1.0);
78 else
79 return (0.0);
80 }
81
82 int BoundaryType(const int& Patch)
83 {
84 return(GALERI_DIRICHLET);
85 }
86
87 // =========== //
88 // main driver //
89 // =========== //
90
91 int main(int argc, char *argv[])
92 {
93
94 #ifdef HAVE_MPI
95 MPI_Init(&argc,&argv);
96 Epetra_MpiComm Comm(MPI_COMM_WORLD);
97 #else
98 Epetra_SerialComm Comm;
99 #endif
100
101 try {
102
103 // ================================================== //
104 // Defines the grid for this problem, a rectangle, //
105 // with the number of nodes along the X-axis (nx) and //
106 // Y-axis (ny), the length of the rectangle along the //
107 // axes, and the number of processors on each axix. //
108 // ================================================== //
109
110 // int nx = 40 * Comm.NumProc(); // unused
111 // int ny = 40; // unused
112 // int mx = Comm.NumProc(); // unused
113 // int my = 1; // unused
114
115 //TriangleRectangleGrid Grid(Comm, nx, ny, mx, my);
116 FileGrid Grid(Comm, "Square.grid");
117
118 // ======================================================== //
119 // Prepares the linear system. This requires the definition //
120 // of a quadrature formula compatible with the grid, a //
121 // variational formulation, and a problem object which take //
122 // care of filling matrix and right-hand side. //
123 // ======================================================== //
124
125 Epetra_CrsMatrix A(Copy, Grid.RowMap(), 0);
126 Epetra_Vector LHS(Grid.RowMap());
127 Epetra_Vector RHS(Grid.RowMap());
128
129 int NumQuadratureNodes = 3;
130
131 SUPGVariational<TriangleQuadrature>
132 AdvDiff(NumQuadratureNodes, Diffusion, ConvX, ConvY, ConvZ,
133 Source, Force, BoundaryValue, BoundaryType);
134
135 LinearProblem FiniteElementProblem(Grid, AdvDiff, A, LHS, RHS);
136 FiniteElementProblem.Compute();
137
138 // =================================================== //
139 // The solution must be computed here by solving the //
140 // linear system A * LHS = RHS. //
141 //
142 // NOTE: Solve() IS A SIMPLE FUNCTION BASED ON LAPACK, //
143 // THEREFORE THE MATRIX IS CONVERTED TO DENSE FORMAT. //
144 // IT WORKS IN SERIAL ONLY. //
145 // EVEN MEDIUM-SIZED MATRICES MAY REQUIRE A LOT OF //
146 // MEMORY AND CPU-TIME! USERS SHOULD CONSIDER INSTEAD //
147 // AZTECOO, ML, IFPACK OR OTHER SOLVERS. //
148 // =================================================== //
149
150 Solve(&A, &LHS, &RHS);
151
152 // ================== //
153 // Output using MEDIT //
154 // ================== //
155
156 MEDITInterface MEDIT(Comm);
157 MEDIT.Write(Grid, "AdvDiff2D", LHS);
158
159 }
160 catch (int e) {
161 cerr << "Caught exception, value = " << e << endl;
162 }
163 catch (...) {
164 cerr << "Caught generic exception" << endl;
165 }
166
167 #ifdef HAVE_MPI
168 MPI_Finalize();
169 #endif
170
171 return(0);
172 }