GeneralizedTAOSolver_Test.cc 7.62 KB
Newer Older
webmanue's avatar
webmanue committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// © 2022 ETH Zurich, Mechanics and Materials Lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef AE108_PETSC_COMPLEX

#include "ae108/cpppetsc/ParallelComputePolicy.h"
#include "ae108/cpppetsc/SequentialComputePolicy.h"
#include "ae108/cppptest/Matchers.h"
#include "ae108/solve/GeneralizedTAOSolver.h"
#include "ae108/solve/test/Solver_Test.h"
#include <gmock/gmock.h>

using ae108::cppptest::ScalarEqIfLocal;
webmanue's avatar
webmanue committed
25
using ae108::cppptest::ScalarNearIfLocal;
webmanue's avatar
webmanue committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using testing::Types;

namespace ae108 {
namespace solve {
namespace {

template <class Policy> struct Configuration {
  using policy_type = Policy;
  template <class T> using solver_type = GeneralizedTAOSolver<T>;
};

template <class Configuration>
struct GeneralizedTAOSolver_Test : test::Solver_Test<Configuration> {};

using Configurations = Types<Configuration<cpppetsc::SequentialComputePolicy>,
                             Configuration<cpppetsc::ParallelComputePolicy>>;
TYPED_TEST_CASE(GeneralizedTAOSolver_Test, Configurations);

TYPED_TEST(GeneralizedTAOSolver_Test, solves_unconstrained_problem) {
  using assembler_type = typename TestFixture::assembler_type;
  using vector_type = typename TestFixture::vector_type;

  const auto solution = this->solver.computeSolution(
      {}, vector_type::fromGlobalMesh(this->mesh), assembler_type::constantTime,
      &this->assembler);

  EXPECT_THAT(solution.unwrap(), ScalarEqIfLocal(0, 1.));
  EXPECT_THAT(solution.unwrap(), ScalarEqIfLocal(1, 2.));
}

TYPED_TEST(GeneralizedTAOSolver_Test, solves_partially_constrained_problem) {
  using assembler_type = typename TestFixture::assembler_type;
  using matrix_type = typename TestFixture::matrix_type;
  using vector_type = typename TestFixture::vector_type;

  const auto solution = this->solver.computeSolution(
      {
          1,
          [](const cpppetsc::distributed<vector_type> &in,
             cpppetsc::distributed<vector_type> *const out) {
            const auto full = vector_type::fromDistributed(in);
            out->unwrap().setZero();
            out->unwrap().replace().element(0, full(0) - full(1));
          },
          [](const cpppetsc::distributed<vector_type> &,
             matrix_type *const out) {
            out->setZero();
            out->preallocatedAssemblyView(2)
                .replace()
                .element(0, 0, 1.)
                .element(0, 1, -1.);
          },
      },
      vector_type::fromGlobalMesh(this->mesh), assembler_type::constantTime,
      &this->assembler);

webmanue's avatar
webmanue committed
82
83
  EXPECT_THAT(solution.unwrap(), ScalarNearIfLocal(0, 3. / 2., 1e-6));
  EXPECT_THAT(solution.unwrap(), ScalarNearIfLocal(1, 3. / 2., 1e-6));
webmanue's avatar
webmanue committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
}

TYPED_TEST(GeneralizedTAOSolver_Test, solves_fully_constrained_problem) {
  using assembler_type = typename TestFixture::assembler_type;
  using matrix_type = typename TestFixture::matrix_type;
  using vector_type = typename TestFixture::vector_type;

  const auto solution = this->solver.computeSolution(
      {
          2,
          [](const cpppetsc::distributed<vector_type> &in,
             cpppetsc::distributed<vector_type> *const out) {
            const auto full = vector_type::fromDistributed(in);
            out->unwrap().setZero();
            out->unwrap()
                .replace()
                .element(0, full(0) - 3.)
                .element(1, full(1) - 4.);
          },
          [](const cpppetsc::distributed<vector_type> &,
             matrix_type *const out) {
            out->setZero();
            out->preallocatedAssemblyView(1)
                .replace()
                .element(0, 0, 1.)
                .element(1, 1, 1.);
          },
      },
      vector_type::fromGlobalMesh(this->mesh), assembler_type::constantTime,
      &this->assembler);

webmanue's avatar
webmanue committed
115
116
  EXPECT_THAT(solution.unwrap(), ScalarNearIfLocal(0, 3., 1e-6));
  EXPECT_THAT(solution.unwrap(), ScalarNearIfLocal(1, 4., 1e-6));
webmanue's avatar
webmanue committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
}

TYPED_TEST(GeneralizedTAOSolver_Test,
           assembler_can_be_replaced_by_local_assembly_functions) {
  using solver_type = typename TestFixture::solver_type;
  using matrix_type = typename TestFixture::matrix_type;
  using vector_type = typename TestFixture::vector_type;

  const auto solution = this->solver.computeSolution(
      {}, vector_type::fromGlobalMesh(this->mesh),
      TestFixture::assembler_type::constantTime,
      [assembler = &this->assembler](
          const cpppetsc::local<vector_type> &in, const double time,
          typename solver_type::real_type *const out) {
        assembler->assembleEnergy(in, time, out);
      },
      [assembler = &this->assembler](const cpppetsc::local<vector_type> &in,
                                     const double time,
                                     cpppetsc::local<vector_type> *const out) {
        assembler->assembleForceVector(in, time, out);
      },
      [assembler = &this->assembler](const cpppetsc::local<vector_type> &in,
                                     const double time,
                                     matrix_type *const out) {
        assembler->assembleStiffnessMatrix(in, time, out);
      });

  EXPECT_THAT(solution.unwrap(), ScalarEqIfLocal(0, 1.));
  EXPECT_THAT(solution.unwrap(), ScalarEqIfLocal(1, 2.));
}

TYPED_TEST(GeneralizedTAOSolver_Test,
           assembler_can_be_replaced_by_distributed_assembly_functions) {
  using policy_type = typename TestFixture::policy_type;
  using vector_type = typename TestFixture::vector_type;
  using matrix_type = typename TestFixture::matrix_type;
  using real_type = typename TestFixture::solver_type::real_type;

  const auto solution = this->solver.computeSolution(
      {}, vector_type::fromGlobalMesh(this->mesh),
      TestFixture::assembler_type::constantTime,
      [assembler = &this->assembler,
       mesh = &this->mesh](const cpppetsc::distributed<vector_type> &in,
                           const double time, real_type *const out) {
        auto local = vector_type::fromLocalMesh(*mesh);
        mesh->copyToLocalVector(in, &local);
        assembler->assembleEnergy(local, time, out);
        policy_type::handleError(MPI_Allreduce(MPI_IN_PLACE, out, 1, MPIU_REAL,
                                               MPIU_SUM,
                                               policy_type::communicator()));
      },
      [assembler = &this->assembler, mesh = &this->mesh](
          const cpppetsc::distributed<vector_type> &in, const double time,
          cpppetsc::distributed<vector_type> *const out) {
        auto localIn = vector_type::fromLocalMesh(*mesh);
        auto localOut = vector_type::fromLocalMesh(*mesh);
        mesh->copyToLocalVector(in, &localIn);
        assembler->assembleForceVector(localIn, time, &localOut);
        mesh->addToGlobalVector(localOut, out);
      },
      [assembler = &this->assembler,
       mesh = &this->mesh](const cpppetsc::distributed<vector_type> &in,
                           const double time, matrix_type *const out) {
        auto local = vector_type::fromLocalMesh(*mesh);
        mesh->copyToLocalVector(in, &local);
        assembler->assembleStiffnessMatrix(local, time, out);
        out->finalize();
      });

  EXPECT_THAT(solution.unwrap(), ScalarEqIfLocal(0, 1.));
  EXPECT_THAT(solution.unwrap(), ScalarEqIfLocal(1, 2.));
}
} // namespace
} // namespace solve
} // namespace ae108

#endif