MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
example_utils.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#include <algorithm>
13#include <assert.h>
14#include <cstdlib>
15#include <memory>
16
17#ifdef MFEM_USE_MPI
18#include <mpi.h>
19#endif // MFEM_USE_MPI
20
21#include "mfem.hpp"
22
24{
25 using namespace std;
26 using namespace mfem;
27
28 int rank = 0;
29#ifdef MFEM_USE_MPI
30 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
31#endif // MFEM_USE_MPI
32
33 if (!args.Good())
34 {
35 if (rank == 0)
36 {
37 args.PrintUsage(cout);
38 }
39
40#ifdef MFEM_USE_MPI
41 MPI_Finalize();
42 MPI_Abort(MPI_COMM_WORLD, 1);
43#else
44 abort();
45#endif // MFEM_USE_MPI
46 }
47
48 if (rank == 0)
49 {
50 args.PrintOptions(cout);
51 }
52}
53
56{
57 using namespace std;
58 using namespace mfem;
59
60 f.SetSpace(&fe);
61 f.ProjectCoefficient(c);
62 f.Update();
63}
64
65inline double example_fun(const mfem::Vector &x)
66{
67 using namespace std;
68 using namespace mfem;
69
70 const int n = x.Size();
71 double ret = 0;
72 for (int k = 0; k < n; ++k)
73 {
74 ret += x(k) * x(k);
75 }
76
77 return sqrt(ret);
78}
79
81{
82 const double n = x.Norml2();
83 f.SetSize(x.Size());
84 f = n;
85}
86
87inline void plot(mfem::Mesh &mesh, mfem::GridFunction &x, std::string title)
88{
89 using namespace std;
90 using namespace mfem;
91
92 int num_procs = 1, rank = 0;
93
94#ifdef MFEM_USE_MPI
95 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
96 MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
97#endif // MFEM_USE_MPI
98
99 char vishost[] = "localhost";
100 int visport = 19916;
101 socketstream sol_sock(vishost, visport);
102 sol_sock << "parallel " << num_procs << " " << rank << "\n";
103 sol_sock.precision(8);
104 sol_sock << "solution\n" << mesh << x
105 << "window_title '"<< title << "'\n" << flush;
106 sol_sock << flush;
107}
Base class Coefficients that optionally depend on space and time. These are used by the BilinearFormI...
Definition: coefficient.hpp:42
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition: fespace.hpp:220
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:31
Mesh data type.
Definition: mesh.hpp:56
void PrintUsage(std::ostream &out) const
Print the usage message.
Definition: optparser.cpp:462
void PrintOptions(std::ostream &out) const
Print the options.
Definition: optparser.cpp:331
bool Good() const
Return true if the command line options were parsed successfully.
Definition: optparser.hpp:159
Vector data type.
Definition: vector.hpp:80
real_t Norml2() const
Returns the l2 norm of the vector.
Definition: vector.cpp:832
int Size() const
Returns the size of the vector.
Definition: vector.hpp:218
void plot(mfem::Mesh &mesh, mfem::GridFunction &x, std::string title)
void vector_fun(const mfem::Vector &x, mfem::Vector &f)
void make_fun(mfem::FiniteElementSpace &fe, mfem::Coefficient &c, mfem::GridFunction &f)
double example_fun(const mfem::Vector &x)
void check_options(mfem::OptionsParser &args)
int visport
char vishost[]
real_t f(const Vector &p)