MFEM  v4.4.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
ortho_solver.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, 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 "ortho_solver.hpp"
13 
14 using namespace mfem;
15 using namespace navier;
16 
17 OrthoSolver::OrthoSolver(MPI_Comm mycomm_) : Solver(0, true),
18  mycomm(mycomm_) {}
19 
21 {
22  oper = &op;
23 }
24 
25 void OrthoSolver::Mult(const Vector &b, Vector &x) const
26 {
27  // Orthogonalize input
28  Orthogonalize(b, b_ortho);
29 
30  // Apply operator
31  oper->Mult(b_ortho, x);
32 
33  // Orthogonalize output
34  Orthogonalize(x, x);
35 }
36 
37 void OrthoSolver::Orthogonalize(const Vector &v, Vector &v_ortho) const
38 {
39  double loc_sum = v.Sum();
40  double global_sum = 0.0;
41  int loc_size = v.Size();
42  int global_size = 0;
43 
44  MPI_Allreduce(&loc_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, mycomm);
45  MPI_Allreduce(&loc_size, &global_size, 1, MPI_INT, MPI_SUM, mycomm);
46 
47  double ratio = global_sum / static_cast<double>(global_size);
48  v_ortho.SetSize(v.Size());
49  v.HostRead();
50  v_ortho.HostWrite();
51  for (int i = 0; i < v_ortho.Size(); ++i)
52  {
53  v_ortho(i) = v(i) - ratio;
54  }
55 }
void SetSize(int s)
Resize the vector to size s.
Definition: vector.hpp:521
virtual double * HostWrite()
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:450
int Size() const
Returns the size of the vector.
Definition: vector.hpp:199
virtual void Mult(const Vector &x, Vector &y) const =0
Operator application: y=A(x).
virtual void SetOperator(const Operator &op)
Set/update the solver for the given operator.
double b
Definition: lissajous.cpp:42
OrthoSolver(MPI_Comm mycomm_)
virtual const double * HostRead() const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:442
void Mult(const Vector &b, Vector &x) const
Operator application: y=A(x).
Vector data type.
Definition: vector.hpp:60
Base class for solvers.
Definition: operator.hpp:651
Abstract operator.
Definition: operator.hpp:24
double Sum() const
Return the sum of the vector entries.
Definition: vector.cpp:900