MFEM v4.10.0
Finite element discretization library
Loading...
Searching...
No Matches
doperator.cpp
Go to the documentation of this file.
1// Copyright (c) 2010-2026, 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 "doperator.hpp"
13
14#ifdef MFEM_USE_MPI
15
16using namespace mfem;
17using namespace mfem::future;
18
19void DifferentiableOperator::SetParameters(std::vector<Vector *> p) const
20{
21 MFEM_ASSERT(parameters.size() == p.size(),
22 "number of parameters doesn't match descriptors");
23 for (size_t i = 0; i < parameters.size(); i++)
24 {
25 p[i]->Read();
26 parameters_l[i] = *p[i];
27 }
28}
29
31 const std::vector<FieldDescriptor> &solutions,
32 const std::vector<FieldDescriptor> &parameters,
33 const ParMesh &mesh) :
34 mesh(mesh),
35 solutions(solutions),
36 parameters(parameters)
37{
38 fields.resize(solutions.size() + parameters.size());
39 fields_e.resize(fields.size());
40 solutions_l.resize(solutions.size());
41 parameters_l.resize(parameters.size());
42
43 for (size_t i = 0; i < solutions.size(); i++)
44 {
45 fields[i] = solutions[i];
46 }
47
48 for (size_t i = 0; i < parameters.size(); i++)
49 {
50 fields[i + solutions.size()] = parameters[i];
51 }
52}
53
54
55void FDJacobian::Mult(const Vector &v, Vector &y) const
56{
57 // See [1] for choice of eps.
58 //
59 // [1] Woodward, C.S., Gardner, D.J. and Evans, K.J., 2015. On the use of
60 // finite difference matrix-vector products in Newton-Krylov solvers for
61 // implicit climate dynamics with spectral elements. Procedia Computer
62 // Science, 51, pp.2036-2045.
63 real_t eps;
64 if (fixed_eps > 0.0)
65 {
66 eps = fixed_eps;
67 }
68 else
69 {
70 const real_t vnorm_local = v.Norml2();
71 real_t vnorm;
72 MPI_Allreduce(&vnorm_local, &vnorm, 1, MPITypeMap<real_t>::mpi_type, MPI_SUM,
73 MPI_COMM_WORLD);
74 eps = lambda * (lambda + xnorm / vnorm);
75 }
76
77 // x + eps * v
78 {
79 const auto d_v = v.Read();
80 const auto d_x = x.Read();
81 auto d_xpev = xpev.Write();
82 mfem::forall(x.Size(), [=] MFEM_HOST_DEVICE (int i)
83 {
84 d_xpev[i] = d_x[i] + eps * d_v[i];
85 });
86 }
87
88 // y = f(x + eps * v)
89 op.Mult(xpev, y);
90
91 // y = (f(x + eps * v) - f(x)) / eps
92 {
93 const auto d_f = f.Read();
94 auto d_y = y.ReadWrite();
95 mfem::forall(f.Size(), [=] MFEM_HOST_DEVICE (int i)
96 {
97 d_y[i] = (d_y[i] - d_f[i]) / eps;
98 });
99 }
100}
101
102#endif // MFEM_USE_MPI
virtual void Mult(const Vector &x, Vector &y) const =0
Operator application: y=A(x).
Class for parallel meshes.
Definition pmesh.hpp:35
Vector data type.
Definition vector.hpp:82
virtual const real_t * Read(bool on_dev=true) const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
Definition vector.hpp:520
virtual real_t * ReadWrite(bool on_dev=true)
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
Definition vector.hpp:536
real_t Norml2() const
Returns the l2 norm of the vector.
Definition vector.cpp:968
int Size() const
Returns the size of the vector.
Definition vector.hpp:234
virtual real_t * Write(bool on_dev=true)
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
Definition vector.hpp:528
DifferentiableOperator(const std::vector< FieldDescriptor > &solutions, const std::vector< FieldDescriptor > &parameters, const ParMesh &mesh)
Definition doperator.cpp:30
void SetParameters(std::vector< Vector * > p) const
Set the parameters for the operator.
Definition doperator.cpp:19
void Mult(const Vector &v, Vector &y) const override
Operator application: y=A(x).
Definition doperator.cpp:55
float real_t
Definition config.hpp:46
void forall(int N, lambda &&body)
Definition forall.hpp:1134
real_t p(const Vector &x, real_t t)
Helper struct to convert a C++ type to an MPI type.