X-Git-Url: http://git.treefish.org/~alex/seamulator.git/blobdiff_plain/5baf6f709bb7943c95318e709efa907a0f3f1986..f2e44f70d56b60e6eef78656ca4b558917474276:/sea.cpp?ds=sidebyside diff --git a/sea.cpp b/sea.cpp index b76a9dd..85191d4 100644 --- a/sea.cpp +++ b/sea.cpp @@ -6,7 +6,7 @@ #include "watersurface.h" -const double Sea::PHILLIPS_CONSTANT{0.0000003}; +const double Sea::PHILLIPS_CONSTANT{0.0000001}; const double Sea::GRAVITATIONAL_CONSTANT{9.8}; Sea::Sea(WaterSurfacePtr surface) : @@ -16,12 +16,9 @@ Sea::Sea(WaterSurfacePtr surface) : m_randomGenerator{m_randomDevice()}, m_normalDistribution{0.0, 1.0} { - m_fourierAmplitudes.resize(pow(m_surface->size(), 2)); + m_fourierAmplitudes.resize(pow(m_surface->size() + 1, 2)); generateFourierAmplitudes(); - fftw_init_threads(); - fftw_plan_with_nthreads(4); - m_fftwIn = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * pow(m_surface->size(), 2)); m_fftwOut = (fftw_complex*) @@ -63,8 +60,8 @@ void Sea::update() const double omega = sqrt(GRAVITATIONAL_CONSTANT * k); std::complex amplitude = - fourierAmplitudeAt(n, m).first * exp(1i * omega * runtime) + - fourierAmplitudeAt(n, m).second * exp(-1i * omega * runtime); + fourierAmplitudeAt(n, m) * exp(1i * omega * runtime) + + std::conj(fourierAmplitudeAt(-n, -m)) * exp(-1i * omega * runtime); const int positiveN = (n + m_surface->size()) % m_surface->size(); int fftwIndex = positiveM + positiveN * m_surface->size(); @@ -96,16 +93,7 @@ double Sea::phillipsSpectrum(double k_x, double k_y) const cosineFactor; } -ComplexPair Sea::generateFourierAmplitude(double k_x, double k_y) -{ - std::complex cDist(m_normalDistribution(m_randomGenerator), - m_normalDistribution(m_randomGenerator)); - - return {cDist * sqrt(phillipsSpectrum(k_x, k_y)) / sqrt(2), - std::conj(cDist) * sqrt(phillipsSpectrum(-k_x, -k_y)) / sqrt(2)}; -} - -ComplexPair& Sea::fourierAmplitudeAt(int n, int m) +std::complex& Sea::fourierAmplitudeAt(int n, int m) { return m_fourierAmplitudes.at (n + m_surface->size()/2 + @@ -120,12 +108,28 @@ double Sea::spatialFrequencyForIndex(int n) const void Sea::generateFourierAmplitudes() { for (int m = -m_surface->size()/2; m < m_surface->size()/2; ++m) { + const double k_y = spatialFrequencyForIndex(m); + for (int n = -m_surface->size()/2; n < m_surface->size()/2; ++n) { + const double k_x = spatialFrequencyForIndex(n); + + std::complex cDist(m_normalDistribution(m_randomGenerator), + m_normalDistribution(m_randomGenerator)); + fourierAmplitudeAt(n, m) = - generateFourierAmplitude(spatialFrequencyForIndex(n), - spatialFrequencyForIndex(m)); + cDist * sqrt(phillipsSpectrum(k_x, k_y)) / sqrt(2); } } + for (int n = -m_surface->size()/2; n < m_surface->size()/2; ++n) { + fourierAmplitudeAt(n, m_surface->size()/2) = + fourierAmplitudeAt(n, -m_surface->size()/2); + } + + for (int m = -m_surface->size()/2; m < m_surface->size()/2; ++m) { + fourierAmplitudeAt(m_surface->size()/2, m) = + fourierAmplitudeAt(-m_surface->size()/2, m); + } + fourierAmplitudeAt(0, 0) = {0, 0}; }