Skip to content

Commit 96ffcf5

Browse files
committed
feat: hermitian tests
1 parent 1d02112 commit 96ffcf5

File tree

4 files changed

+63
-63
lines changed

4 files changed

+63
-63
lines changed

fft/fft2.test.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { vec2 } from 'gl-matrix';
2-
import { abs, add, mult, Complex, complex, sub, eix, conj } from './complex';
2+
import {
3+
abs,
4+
add,
5+
mult,
6+
Complex,
7+
complex,
8+
sub,
9+
eix,
10+
conj,
11+
im,
12+
} from './complex';
313
import { dft2, fft2, idft2, ifft2 } from './fft2';
414

515
export const testDft2 = () => {
@@ -108,7 +118,7 @@ export const testFft2Combined = () => {
108118
};
109119

110120
export const testFft2Hermitian = () => {
111-
for (let pow of [3]) {
121+
for (let pow of [1, 2, 3, 4, 5, 6, 7, 8]) {
112122
// Arrange
113123
const size = 1 << pow;
114124

@@ -146,16 +156,13 @@ export const testFft2Hermitian = () => {
146156
const inverse = ifft2(spectrum);
147157

148158
// Assert
149-
150159
const ifftf = inverse.flat();
151-
console.log(ifftf);
152-
153-
// const diff = ifftf.map((v, i) => abs(sub(v, complex(s0f[i], s1f[i]))));
154-
// const closeEnougth = diff.every((v) => v <= 1.0e-5);
155-
// if (!closeEnougth) {
156-
// console.warn("testFft2Hermitian: Test don't passesd: ", diff);
157-
// return;
158-
// }
160+
const _im = ifftf.map((v) => im(v));
161+
const isReal = _im.every((v) => Math.abs(v) <= 1.0e-6);
162+
if (!isReal) {
163+
console.warn(`testFft2Hermitian: Test don't pass`);
164+
return;
165+
}
159166
}
160167

161168
console.log('testFft2Hermitian: Test passed!');

index.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import {
1010
testDisplacementFieldIfft2,
1111
testFft2Hermitian,
1212
testDisplacementFieldIfft2HermitianProperty,
13-
testFft2Combined
14-
13+
testFft2Combined,
1514
} from './test';
1615

1716
import { Simulation } from './simulation';
@@ -21,18 +20,17 @@ import { Simulation } from './simulation';
2120
// testFft();
2221
// testDft2();
2322
// testFft2();
24-
// testDisplacementFieldIfft2();
25-
testDisplacementFieldIfft2HermitianProperty();
2623
// testFft2Hermitian();
2724
// testFft2Combined();
28-
25+
// testDisplacementFieldIfft2();
26+
// testDisplacementFieldIfft2HermitianProperty();
2927

3028
const simulation = new Simulation(
3129
document.getElementById('viewport') as HTMLCanvasElement
3230
);
3331
simulation.start({
3432
size: 10,
35-
alignment: .01,
33+
alignment: 0.01,
3634
minWave: 0.001,
3735
geometryResolution: 256,
3836
resolution: 512,

wave/displacement-field.test.ts

+15-27
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,7 @@ import { vec2 } from 'gl-matrix';
22

33
import { createMockGpu } from '../graphics/gpu.mock';
44
import { DisplacementFieldFactory } from './displacement-field-factory';
5-
import {
6-
float4ToComplex2d,
7-
ifft2,
8-
fft2,
9-
abs,
10-
sub,
11-
add,
12-
areAqual,
13-
complex,
14-
Complex,
15-
mult,
16-
re,
17-
} from '../fft';
5+
import { float4ToComplex2d, ifft2, abs, sub, im } from '../fft';
186

197
export const testDisplacementFieldIfft2 = () => {
208
const gpu = createMockGpu();
@@ -107,8 +95,8 @@ export const testDisplacementFieldIfft2HermitianProperty = () => {
10795
alignment: 0.0,
10896
croppiness: -0.6,
10997
size: 100,
110-
resolution: 8,
111-
geometryResolution: 8,
98+
resolution: 512,
99+
geometryResolution: 256,
112100
wind: vec2.fromValues(28.0, 28.0),
113101
strength: 1000000,
114102
});
@@ -121,7 +109,7 @@ export const testDisplacementFieldIfft2HermitianProperty = () => {
121109
4
122110
);
123111

124-
for (let slot of [0, 1]) {
112+
for (let slot of [0, 1, 2, 3]) {
125113
for (let couple of [0, 1]) {
126114
displacementField['generateSpectrumTextures'](performance.now());
127115

@@ -148,19 +136,19 @@ export const testDisplacementFieldIfft2HermitianProperty = () => {
148136
couple * 2
149137
).flat(1);
150138

151-
console.log(actual);
152139
// Assert
153-
// const diff = actual.map((a, i) => abs(sub(a, expected[i])));
154-
// const closeEnougth = diff.every((v) => v <= 1.0e-5);
155-
// if (!closeEnougth) {
156-
// console.warn(
157-
// `testDisplacementFieldIfft2 [slot ${slot}-${couple}]: Test don't passesd: `,
158-
// diff
159-
// );
160-
// return;
161-
// }
140+
const _im = actual.map((v) => im(v));
141+
const isReal = _im.every((v) => Math.abs(v) <= 1.0e-5);
142+
if (!isReal) {
143+
console.warn(
144+
`testDisplacementFieldIfft2HermitianProperty [slot ${slot}-${couple}]: Test don't pass: [max: ${Math.max(
145+
..._im
146+
)}, min: ${Math.min(..._im)}]`
147+
);
148+
return;
149+
}
162150
console.log(
163-
`testDisplacementFieldIfft2 [slot ${slot}-${couple}]: Test passed!`
151+
`testDisplacementFieldIfft2HermitianProperty [slot ${slot}-${couple}]: Test passed!`
164152
);
165153
}
166154
}

wave/programs/hk.ts

+26-19
Original file line numberDiff line numberDiff line change
@@ -54,36 +54,43 @@ void main() {
5454
vec2 x = vec2(ivec2(gl_FragCoord.xy)) - float(resolution) * 0.5; // [-N/2, N/2]
5555
vec2 k = vec2(2.0 * PI * x.x / size, 2.0 * PI * x.y / size);
5656
float kLen = length(k);
57+
5758
if(kLen == 0.0f) {
5859
height = slope = displacement = ddisplacement = vec4(0.0f);
5960
return;
6061
}
62+
6163
float w = sqrt(g * kLen);
6264
vec4 h0Texel = texelFetch(h0Texture, ivec2(gl_FragCoord.xy), 0).rgba;
6365
6466
complex e = eix(w * t);
6567
complex h0 = complex(h0Texel.x, h0Texel.y);
6668
complex h0MinConj = complex(h0Texel.z, h0Texel.w);
67-
6869
complex hy = add(mult(h0, e), mult(h0MinConj, conj(e)));
69-
complex dx = mult(complex(0.0f, -k.x / kLen), hy);
70-
complex dz = mult(complex(0.0f, -k.y / kLen), hy);
71-
complex sx = mult(complex(0.0f, k.x), hy);
72-
complex sz = mult(complex(0.0f, k.y), hy);
73-
complex dxdx = scale(hy, k.x * k.x / kLen);
74-
complex dzdz = scale(hy, k.y * k.y / kLen);
75-
complex dxdz = scale(hy, k.y * k.x / kLen);
76-
77-
// if(
78-
// uint(gl_FragCoord.x) == 0u ||
79-
// uint(gl_FragCoord.x) == resolution - 1u ||
80-
// uint(gl_FragCoord.y) == 0u ||
81-
// uint(gl_FragCoord.y) == resolution - 1u
82-
// )
83-
// {
84-
// sx = complex(0.0f, 0.0f);
85-
// sz = complex(0.0f, 0.0f);
86-
// }
70+
complex sx = complex(0.0f, 0.0f);
71+
complex sz = complex(0.0f, 0.0f);
72+
complex dx = complex(0.0f, 0.0f);
73+
complex dz = complex(0.0f, 0.0f);
74+
75+
complex dxdx = complex(0.0f, 0.0f);
76+
complex dzdz = complex(0.0f, 0.0f);
77+
complex dxdz = complex(0.0f, 0.0f);
78+
79+
if(int(gl_FragCoord.x) != 0) {
80+
sx = mult(complex(0.0f, k.x), hy);
81+
dx = mult(complex(0.0f, -k.x / kLen), hy);
82+
dxdx = scale(hy, k.x * k.x / kLen);
83+
}
84+
85+
if(int(gl_FragCoord.y) != 0) {
86+
sz = mult(complex(0.0f, k.y), hy);
87+
dz = mult(complex(0.0f, -k.y / kLen), hy);
88+
dzdz = scale(hy, k.y * k.y / kLen);
89+
90+
if(int(gl_FragCoord.x) != 0) {
91+
dxdz = scale(hy, k.y * k.x / kLen);
92+
}
93+
}
8794
8895
height = vec4(hy.re, hy.im, dxdz.re, dxdz.im);
8996
slope = vec4(sx.re, sx.im, sz.re, sz.im);

0 commit comments

Comments
 (0)