Nektar++
Loading...
Searching...
No Matches
TestForcingMovingBody.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestForcingMovingBody.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Unit tests for the cable model of ForcingMovingBody
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <algorithm>
36#include <cmath>
37#include <vector>
38
39#include <boost/test/tools/floating_point_comparison.hpp>
40#include <boost/test/unit_test.hpp>
41
44
45#include "../ForcingMovingBody.h"
46
48{
49static const NekDouble tol = 1e-12;
50
51/// Arbitrary, non-symmetric data to transform
53{
55 for (size_t k = 0; k < N; ++k)
56 {
57 u[k] = 0.3 + std::sin(1.7 * k) + 0.1 * k * k;
58 }
59 return u;
60}
61
62// The Pinned-Pinned forward and backward sine transforms are inverses of
63// each other, in both orders.
64BOOST_AUTO_TEST_CASE(TestPinnedSineRoundTrip)
65{
66 for (size_t N : {1, 2, 5, 8, 16})
67 {
68 Array<OneD, NekDouble> u = TestData(N), uhat(N), v(N);
69
72 for (size_t k = 0; k < N; ++k)
73 {
74 BOOST_CHECK_SMALL(v[k] - u[k], tol);
75 }
76
79 for (size_t k = 0; k < N; ++k)
80 {
81 BOOST_CHECK_SMALL(uhat[k] - u[k], tol);
82 }
83 }
84}
85
86// The cable sampled at z_k = (k + 1/2) L / N with the sine mode of
87// wavenumber CableWavenumber(m) transforms to slot m only.
88BOOST_AUTO_TEST_CASE(TestPinnedSineModes)
89{
90 size_t N = 8;
91 Array<OneD, NekDouble> u(N), uhat(N);
92
93 for (size_t m = 0; m < N; ++m)
94 {
95 unsigned int K = ForcingMovingBody::CableWavenumber("Pinned-Pinned", m);
96 BOOST_CHECK_EQUAL(K, m + 1);
97
98 for (size_t k = 0; k < N; ++k)
99 {
100 u[k] = std::sin(M_PI * K * (k + 0.5) / N);
101 }
102
104 for (size_t i = 0; i < N; ++i)
105 {
106 NekDouble expected = 0.0;
107 if (i == m)
108 {
109 expected = (m == N - 1) ? N : 0.5 * N;
110 }
111 BOOST_CHECK_SMALL(uhat[i] - expected, tol);
112 }
113 }
114}
115
116// Free-Free modes are stored in the layout of the Fourier basis: the cosine
117// and sine coefficients of wavenumber k in slots 2k and 2k + 1.
118BOOST_AUTO_TEST_CASE(TestFreeFreeWavenumber)
119{
120 size_t N = 8;
121 std::vector<unsigned int> expected = {0, 0, 1, 1, 2, 2, 3, 3};
122
123 for (size_t plane = 0; plane < N; ++plane)
124 {
125 BOOST_CHECK_EQUAL(
126 ForcingMovingBody::CableWavenumber("Free-Free", plane),
127 expected[plane]);
128 }
129}
130
131// CableWavenumber matches the layout of the NekFFTW transform that the
132// Free-Free model is solved with: cos(2 pi K z / L) and sin(2 pi K z / L)
133// transform only to slots whose wavenumber is K, and the Nyquist mode
134// K = N / 2 is not represented. Skipped when Nektar++ is built without FFTW,
135// in which case the MovingBody forcing cannot be used either.
136BOOST_AUTO_TEST_CASE(TestFreeFreeWavenumberFFTW)
137{
138 if (!LibUtilities::GetNektarFFTFactory().ModuleExists("NekFFTW"))
139 {
140 BOOST_TEST_MESSAGE("NekFFTW is not available, skipping");
141 return;
142 }
143
144 size_t N = 8;
147
148 Array<OneD, NekDouble> u(N), uhat(N);
149
150 for (unsigned int K = 0; K < N / 2; ++K)
151 {
152 for (NekDouble phase : {0.0, 0.5 * M_PI})
153 {
154 for (size_t k = 0; k < N; ++k)
155 {
156 u[k] = std::cos(2.0 * M_PI * K * k / N - phase);
157 }
158
159 fft->FFTFwdTrans(u, uhat);
160
161 NekDouble maxInK = 0.0;
162 for (size_t plane = 0; plane < N; ++plane)
163 {
164 if (ForcingMovingBody::CableWavenumber("Free-Free", plane) == K)
165 {
166 maxInK = std::max(maxInK, std::abs(uhat[plane]));
167 }
168 else
169 {
170 BOOST_CHECK_SMALL(uhat[plane], tol);
171 }
172 }
173
174 // sin(0) vanishes at every plane; any other signal must appear
175 // in a slot of wavenumber K
176 if (Vmath::Vamax(N, u, 1) > 0.5)
177 {
178 BOOST_CHECK_GT(maxInK, 0.1);
179 }
180 }
181 }
182
183 // The Nyquist mode cos(pi k) transforms to zero in every slot
184 for (size_t k = 0; k < N; ++k)
185 {
186 u[k] = std::cos(M_PI * k);
187 }
188 fft->FFTFwdTrans(u, uhat);
189 for (size_t plane = 0; plane < N; ++plane)
190 {
191 BOOST_CHECK_SMALL(uhat[plane], tol);
192 }
193}
194
195} // namespace Nektar::ForcingMovingBodyTests
#define N
static void PinnedSineBwdTrans(const Array< OneD, const NekDouble > &in, Array< OneD, NekDouble > &out)
Inverse of PinnedSineFwdTrans.
static void PinnedSineFwdTrans(const Array< OneD, const NekDouble > &in, Array< OneD, NekDouble > &out)
Sine transform of the cable motion for Pinned-Pinned supports.
static unsigned int CableWavenumber(const std::string &supptype, size_t plane)
Wavenumber of the cable mode stored in slot plane for the support type supptype.
tBasePtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
The TestData class is responsible for parsing a test XML file and storing the data.
Definition TestData.h:79
BOOST_AUTO_TEST_CASE(TestPinnedSineRoundTrip)
NektarFFTFactory & GetNektarFFTFactory()
Definition NektarFFT.cpp:40
std::shared_ptr< NektarFFT< double > > NektarFFTSharedPtr
Definition NektarFFT.h:49
T Vamax(int n, const T *x, const int incx)
Return the maximum absolute element in x called vamax to avoid conflict with max.
Definition Vmath.hpp:688