MFEM  v4.6.0
Finite element discretization library
example_utils.hpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <assert.h>
3 #include <cstdlib>
4 #include <memory>
5 
6 #ifdef MFEM_USE_MPI
7 #include <mpi.h>
8 #endif // MFEM_USE_MPI
9 
10 #include "mfem.hpp"
11 
13 {
14  using namespace std;
15  using namespace mfem;
16 
17  int rank = 0;
18 #ifdef MFEM_USE_MPI
19  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
20 #endif // MFEM_USE_MPI
21 
22  if (!args.Good())
23  {
24  if (rank == 0)
25  {
26  args.PrintUsage(cout);
27  }
28 
29 #ifdef MFEM_USE_MPI
30  MPI_Finalize();
31  MPI_Abort(MPI_COMM_WORLD, 1);
32 #else
33  abort();
34 #endif // MFEM_USE_MPI
35  }
36 
37  if (rank == 0)
38  {
39  args.PrintOptions(cout);
40  }
41 }
42 
45 {
46  using namespace std;
47  using namespace mfem;
48 
49  f.SetSpace(&fe);
50  f.ProjectCoefficient(c);
51  f.Update();
52 }
53 
54 inline double example_fun(const mfem::Vector &x)
55 {
56  using namespace std;
57  using namespace mfem;
58 
59  const int n = x.Size();
60  double ret = 0;
61  for (int k = 0; k < n; ++k)
62  {
63  ret += x(k) * x(k);
64  }
65 
66  return sqrt(ret);
67 }
68 
70 {
71  const double n = x.Norml2();
72  f.SetSize(x.Size());
73  f = n;
74 }
75 
76 inline void plot(mfem::Mesh &mesh, mfem::GridFunction &x, std::string title)
77 {
78  using namespace std;
79  using namespace mfem;
80 
81  int num_procs = 1, rank = 0;
82 
83 #ifdef MFEM_USE_MPI
84  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
85  MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
86 #endif // MFEM_USE_MPI
87 
88  char vishost[] = "localhost";
89  int visport = 19916;
90  socketstream sol_sock(vishost, visport);
91  sol_sock << "parallel " << num_procs << " " << rank << "\n";
92  sol_sock.precision(8);
93  sol_sock << "solution\n" << mesh << x
94  << "window_title '"<< title << "'\n" << flush;
95  sol_sock << flush;
96 }
int visport
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:30
void PrintOptions(std::ostream &out) const
Print the options.
Definition: optparser.cpp:331
void PrintUsage(std::ostream &out) const
Print the usage message.
Definition: optparser.cpp:462
int Size() const
Returns the size of the vector.
Definition: vector.hpp:197
bool Good() const
Return true if the command line options were parsed successfully.
Definition: optparser.hpp:159
STL namespace.
void check_options(mfem::OptionsParser &args)
char vishost[]
void plot(mfem::Mesh &mesh, mfem::GridFunction &x, std::string title)
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition: fespace.hpp:219
Base class Coefficients that optionally depend on space and time. These are used by the BilinearFormI...
Definition: coefficient.hpp:41
double example_fun(const mfem::Vector &x)
void vector_fun(const mfem::Vector &x, mfem::Vector &f)
double Norml2() const
Returns the l2 norm of the vector.
Definition: vector.cpp:835
Vector data type.
Definition: vector.hpp:58
void make_fun(mfem::FiniteElementSpace &fe, mfem::Coefficient &c, mfem::GridFunction &f)
double f(const Vector &p)