00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef OCEAN_WAVE_GENERATOR
00025 #define OCEAN_WAVE_GENERATOR
00026
00027 #include <common/Vector.h>
00028 #ifdef __DARWIN__
00029 #include <FFTW3/fftw3.h>
00030 #else
00031 #include "fftw3.h"
00032 #endif
00033 #include <complex>
00034 #include <vector>
00035
00036 #ifdef FFTW_USE_DOUBLE
00037 #define FFT_COMPLEX_TYPE fftw_complex
00038 #define FFT_REAL_TYPE double
00039 #define FFT_PLAN_TYPE fftw_plan
00040 #define FFT_CREATE_PLAN fftw_plan_dft_c2r_2d
00041 #define FFT_DELETE_PLAN fftw_destroy_plan
00042 #define FFT_EXECUTE_PLAN fftw_execute
00043 #else
00044 #define FFT_COMPLEX_TYPE fftwf_complex
00045 #define FFT_REAL_TYPE float
00046 #define FFT_PLAN_TYPE fftwf_plan
00047 #define FFT_CREATE_PLAN fftwf_plan_dft_c2r_2d
00048 #define FFT_DELETE_PLAN fftwf_destroy_plan
00049 #define FFT_EXECUTE_PLAN fftwf_execute
00050 #endif
00051
00052 #define GRAVITY 10.0f
00053
00054 #ifndef M_PI
00055 #define M_PI 3.1415926535897932 // should be in math.h, but not for Windows. *sigh*
00056 #endif
00057
00058
00059 template <class T>
00060 class ocean_wave_generator
00061 {
00062 int N;
00063 Vector W;
00064 T v;
00065 T a;
00066 T Lm;
00067 T w0;
00068 std::vector<std::complex<T> > h0tilde;
00069 std::vector<std::complex<T> > htilde;
00070
00071 ocean_wave_generator& operator= (const ocean_wave_generator& );
00072 ocean_wave_generator(const ocean_wave_generator& );
00073 static T myrnd();
00074 static std::complex<T> gaussrand();
00075 T phillips(Vector& K) const;
00076 std::complex<T> h0_tilde(Vector& K) const;
00077 void compute_h0tilde();
00078 std::complex<T> h_tilde(Vector& K, int kx, int ky, T time) const;
00079 void compute_htilde(T time);
00080
00081 FFT_COMPLEX_TYPE *fft_in, *fft_in2;
00082 FFT_REAL_TYPE *fft_out, *fft_out2;
00083 FFT_PLAN_TYPE plan, plan2, plan3;
00084
00085 public:
00086 ocean_wave_generator(
00087 int gridsize,
00088 Vector& winddir,
00089 T windspeed = T(20.0),
00090 T waveheight = T(0.0001),
00091 T tilesize = T(100.0),
00092 T cycletime = T(10.0)
00093 );
00094
00095
00096 ocean_wave_generator(const ocean_wave_generator<T>& owg, int gridsize,
00097 int clearlowfreq = 0);
00098 void set_time(T time);
00099 void compute_heights(Water2Points& waveheights) const;
00100
00101
00102
00103
00104 void compute_displacements(const T& scalefac, Water2Points& wavedisplacements) const;
00105 ~ocean_wave_generator();
00106 };
00107
00108 template <class T>
00109 T ocean_wave_generator<T>::myrnd()
00110 {
00111 return T(rand())/RAND_MAX;
00112 }
00113
00114 template <class T>
00115 std::complex<T> ocean_wave_generator<T>::gaussrand()
00116 {
00117 T x1, x2, w;
00118 do {
00119 x1 = T(2.0) * myrnd() - T(1.0);
00120 x2 = T(2.0) * myrnd() - T(1.0);
00121 w = x1 * x1 + x2 * x2;
00122 } while ( w >= T(1.0) );
00123 w = sqrt( (T(-2.0) * log( w ) ) / w );
00124 return std::complex<T>(x1 * w, x2 * w);
00125 }
00126
00127 template <class T>
00128 T ocean_wave_generator<T>::phillips(Vector& K) const
00129 {
00130 T v2 = v * v;
00131 T k2 = K.MagnitudeSquared();
00132 if (k2 == T(0)) return T(0);
00133 T v4 = v2 * v2;
00134 T k4 = k2 * k2;
00135 T g2 = GRAVITY * GRAVITY;
00136
00137
00138
00139 T KdotW = K.dotP(W);
00140 T KdotWhat = KdotW*KdotW/k2;
00141 T eterm = exp(-g2 / (k2 * v4)) / k4;
00142 T dampfac = T(1.0/100);
00143 T l2 = v4/g2 * dampfac*dampfac;
00144 T result = a * eterm * KdotWhat * exp(-k2*l2);
00145 if (KdotW < T(0))
00146 result *= T(0.25);
00147 return result;
00148 }
00149
00150 template <class T>
00151 std::complex<T> ocean_wave_generator<T>::h0_tilde(Vector& K) const
00152 {
00153
00154
00155
00156
00157 std::complex<T> g = gaussrand();
00158 T p = sqrt(T(0.5) * phillips(K));
00159 return g * p;
00160 }
00161
00162 template <class T>
00163 void ocean_wave_generator<T>::compute_h0tilde()
00164 {
00165 const T pi2 = T(2.0*M_PI);
00166
00167
00168
00169 for (int y = 0, y2 = -N/2; y <= N; ++y, ++y2) {
00170 T Ky = pi2*y2/Lm;
00171 for (int x = 0, x2 = -N/2; x <= N; ++x, ++x2) {
00172 T Kx = pi2*x2/Lm;
00173 Vector tmp(Kx, Ky);
00174 h0tilde[y*(N+1)+x] = h0_tilde(tmp);
00175 }
00176 }
00177 }
00178
00179 template <class T>
00180 std::complex<T> ocean_wave_generator<T>::h_tilde(Vector& K, int kx, int ky, T time) const
00181 {
00182 std::complex<T> h0_tildeK = h0tilde[ky*(N+1)+kx];
00183 std::complex<T> h0_tildemKconj = conj(h0tilde[(N-ky)*(N+1)+(N-kx)]);
00184
00185 T wK = sqrt(GRAVITY * K.Magnitude());
00186 T wK2 = floor(wK/w0)*w0;
00187 T xp = wK2 * time;
00188 T cxp = cos(xp);
00189 T sxp = sin(xp);
00190 return h0_tildeK * std::complex<T>(cxp, sxp) + h0_tildemKconj * std::complex<T>(cxp, -sxp);
00191 }
00192
00193 template <class T>
00194 void ocean_wave_generator<T>::compute_htilde(T time)
00195 {
00196 const T pi2 = T(2.0*M_PI);
00197 for (int y = 0; y <= N/2; ++y) {
00198 for (int x = 0; x < N; ++x) {
00199 Vector K(pi2*(x-N/2)/Lm, pi2*(y-N/2)/Lm);
00200 htilde[y*N+x] = h_tilde(K, x, y, time);
00201 }
00202 }
00203 }
00204
00205 template <class T>
00206 ocean_wave_generator<T>::ocean_wave_generator(
00207 int gridsize,
00208 Vector& winddir,
00209 T windspeed,
00210 T waveheight,
00211 T tilesize,
00212 T cycletime )
00213 : N(int(gridsize)), W(winddir.Normalize()), v(windspeed), a(waveheight), Lm(tilesize),
00214 w0(T(2.0*M_PI)/cycletime)
00215 {
00216 h0tilde.resize((N+1)*(N+1));
00217 compute_h0tilde();
00218 htilde.resize(N*(N/2+1));
00219 fft_in = new FFT_COMPLEX_TYPE[N*(N/2+1)];
00220 fft_in2 = new FFT_COMPLEX_TYPE[N*(N/2+1)];
00221 fft_out = new FFT_REAL_TYPE[N*N];
00222 fft_out2 = new FFT_REAL_TYPE[N*N];
00223 plan = FFT_CREATE_PLAN(N, N, fft_in, fft_out, 0);
00224 plan2 = FFT_CREATE_PLAN(N, N, fft_in2, fft_out2, 0);
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 template <class T>
00277 ocean_wave_generator<T>::~ocean_wave_generator()
00278 {
00279 FFT_DELETE_PLAN(plan);
00280 FFT_DELETE_PLAN(plan2);
00281 delete[] fft_in;
00282 delete[] fft_in2;
00283 delete[] fft_out;
00284 delete[] fft_out2;
00285 }
00286
00287 template <class T>
00288 void ocean_wave_generator<T>::set_time(T time)
00289 {
00290 compute_htilde(time);
00291 }
00292
00293 template <class T>
00294 void ocean_wave_generator<T>::compute_heights(Water2Points& waveheights) const
00295 {
00296
00297
00298
00299 for (int y = 0; y <= N/2; ++y) {
00300 for (int x = 0; x < N; ++x) {
00301 const std::complex<T>& c = htilde[y*N+x];
00302 int ptr = x*(N/2+1)+y;
00303 fft_in[ptr][0] = c.real();
00304 fft_in[ptr][1] = c.imag();
00305 }
00306 }
00307
00308 FFT_EXECUTE_PLAN(plan);
00309
00310
00311
00312
00313
00314
00315 T signs[2] = { T(1), T(-1) };
00316 for (int y = 0; y < N; ++y)
00317 for (int x = 0; x < N; ++x)
00318 {
00319 Vector &point = waveheights.getPoint(x, y);
00320 point[2] = (T) fft_out[y*N+x] * signs[(x + y) & 1];
00321 }
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388 template <class T>
00389 void ocean_wave_generator<T>::compute_displacements(const T& scalefac,
00390 Water2Points& wavedisplacements) const
00391 {
00392 for (int y = 0; y <= N/2; ++y) {
00393 for (int x = 0; x < N; ++x) {
00394 const std::complex<T>& c = htilde[y*N+x];
00395 Vector K((x-N/2), (y-N/2));
00396 T k = K.Magnitude();
00397 Vector Kh;
00398 if (k != 0)
00399 Kh = K * (T(1)/k);
00400 else
00401 Kh[0] = Kh[1] = T(0);
00402 int ptr = x*(N/2+1)+y;
00403 fft_in[ptr][0] = c.imag() * Kh[0];
00404 fft_in[ptr][1] = -c.real() * Kh[0];
00405 fft_in2[ptr][0] = c.imag() * Kh[1];
00406 fft_in2[ptr][1] = -c.real() * Kh[1];
00407 }
00408 }
00409
00410 FFT_EXECUTE_PLAN(plan);
00411 FFT_EXECUTE_PLAN(plan2);
00412
00413 T signs[2] = { T(1), T(-1) };
00414 unsigned ptr = 0;
00415 for (int y = 0; y < N; ++y) {
00416 for (int x = 0; x < N; ++x) {
00417 T s = signs[(x + y) & 1];
00418
00419 Vector &point = wavedisplacements.getPoint(x, y);
00420 point[0] = (T) fft_out[ptr] * s * scalefac;
00421 point[1] = (T) fft_out2[ptr] * s * scalefac;
00422 ++ptr;
00423 }
00424 }
00425 }
00426
00427 #endif