-
Notifications
You must be signed in to change notification settings - Fork 12
/
simulator.ts
596 lines (493 loc) · 18.6 KB
/
simulator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
82
83
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
115
116
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import { Regl, Framebuffer2D, DrawCommand } from "regl"
import * as k from "./kernels/simulation"
import { DrawInfo, DrawShape } from "./drawing"
export type MaterialType = "permittivity" | "permeability" | "conductivity"
function clamp(min: number, max: number, value: number) {
return Math.max(min, Math.min(max, value))
}
function snapToGrid(relativePoint: [number, number], gridSize: [number, number]) {
const relativeCellSize = [1 / gridSize[0], 1 / gridSize[1]]
const residual = [relativePoint[0] % relativeCellSize[0], relativePoint[1] % relativeCellSize[1]]
const gridPoint = [
relativePoint[0] - residual[0] + 0.5 * relativeCellSize[0],
relativePoint[1] - residual[1] + 0.5 * relativeCellSize[1]
]
return gridPoint
}
export function combineMaterialMaps(permittivity: number[][],
permeability: number[][], conductivity: number[][]): number[][][] {
const material: number[][][] = [];
const width = permittivity[0].length;
const height = permittivity.length;
// TODO: Verify same dims
for (let y = 0; y < height; y++) {
const row: number[][] = []
for (let x = 0; x < width; x++) {
row.push([
clamp(0, 255, 128 + 4 * permittivity[y][x]),
clamp(0, 255, 128 + 4 * permeability[y][x]),
clamp(0, 255, 128 + 4 * conductivity[y][x]),
])
}
material.push(row)
}
return material
}
class DoubleFramebuffer2D {
current: Framebuffer2D
previous: Framebuffer2D
constructor(current: Framebuffer2D, previous: Framebuffer2D) {
this.current = current
this.previous = previous
}
swap() {
const oldCurrent = this.current
this.current = this.previous
this.previous = oldCurrent
}
}
export type SimulationData = {
time: number
electricField: DoubleFramebuffer2D
magneticField: DoubleFramebuffer2D
material: DoubleFramebuffer2D
alphaBetaField: Framebuffer2D
electricSourceField: DoubleFramebuffer2D
}
/**
* Simulates the electromagnetic field with materials.
*/
export interface Simulator {
/**
* Performs the update step for the electric field.
* @param dt Time step size
*/
stepElectric: (dt: number) => void
/**
* Performs the update step for the magnetic field.
* @param dt Time step size
*/
stepMagnetic: (dt: number) => void
/**
* Resets the electric, magnetic and source fields
* and sets the time back to `0`.
*/
resetFields: () => void
/**
* Resets the materials to their default values of
* `1` for permittivity and permeability and `0` for
* conductivity.
*/
resetMaterials: () => void
/**
* Injects a value into the electric source field.
* @param drawInfo Draw info that specifies where and
* what values to inject.
* @param dt Time step size
*/
injectSignal: (drawInfo: DrawInfo, dt: number) => void
/**
* Returns the simulation data containing the frame-buffers
* for the fields and materials.
*/
getData: () => SimulationData
/**
* Returns the size of a cell.
*/
getCellSize: () => number
/**
* Sets the size of a cell.
* @param cellSize Size of a cell
*/
setCellSize: (cellSize: number) => void
/**
* Returns the width and height of the grid in
* number of cells.
*/
getGridSize: () => [number, number]
/**
* Sets the width and height of the grid in
* number of cells.
* @param reflectiveBoundary Width and height of the grid in
* number of cells.
*/
setGridSize: (gridSize: [number, number]) => void
/**
* Returns whether the grid boundary is reflective.
*/
getReflectiveBoundary: () => boolean
/**
* Sets whether the grid boundary is reflective.
* @param reflectiveBoundary Whether the grid boundary is reflective
*/
setReflectiveBoundary: (reflectiveBoundary: boolean) => void
/**
* Draws values onto a material.
* @param materialType Material to draw onto
* @param drawInfo Draw info that specifies where and
* what values to draw.
*/
drawMaterial: (materialType: MaterialType, drawInfo: DrawInfo) => void
/**
* Loads a material from a 3D array.
* @param material Material of shape `[height, width, 3]`.
*
* Channel 0: Permittivity
*
* Channel 1: Permeability
*
* Channel 2: Conductivity
*/
loadMaterial: (material: number[][][]) => void
/**
* Loads a material from arrays.
* @param permittivity Array of shape `[height, width]`
* specifying the permittivity at every cell.
* @param permeability Array of shape `[height, width]`
* specifying the permeability at every cell.
* @param conductivity Array of shape `[height, width]`
* specifying the conductivity at every cell.
*/
loadMaterialFromComponents: (permittivity: number[][], permeability: number[][], conductivity: number[][]) => void
/**
* Returns the material as a 3D array.
* @param material Material of shape `[height, width, 3]`.
*
* Channel 0: Permittivity
*
* Channel 1: Permeability
*
* Channel 2: Conductivity
*/
getMaterial: () => number[][][]
}
export class FDTDSimulator implements Simulator {
private data: SimulationData
private updateMagnetic: DrawCommand
private updateElectric: DrawCommand
private updateAlphaBeta: DrawCommand
private injectSource: DrawCommand
private decaySource: DrawCommand
private drawOnTexture: { [shape: string]: DrawCommand }
private copyUint8ToFloat16: DrawCommand
private copyFloat16ToUint8: DrawCommand
private frameBuffers: Framebuffer2D[]
private alphaBetaDt: number // dt that the alpha beta values were calculated for
constructor(readonly regl: Regl, private gridSize: [number, number], private cellSize: number, public reflectiveBoundary: boolean, private dt: number) {
this.alphaBetaDt = dt
this.frameBuffers = []
const makeFrameBuffer = () => {
// Create half-precision texture at grid size
const fbo = regl.framebuffer({
color: regl.texture({
width: gridSize[0],
height: gridSize[1],
wrap: "clamp",
type: "float16",
format: "rgba",
min: "nearest",
mag: "nearest",
}),
depthStencil: false
})
// Keep track of all fbos so we can resize them
// together if the grid size changes.
this.frameBuffers.push(fbo)
return fbo
}
const makeField = () => {
return new DoubleFramebuffer2D(
makeFrameBuffer(),
makeFrameBuffer()
)
}
this.data = {
time: 0,
electricField: makeField(),
magneticField: makeField(),
electricSourceField: makeField(),
material: makeField(),
alphaBetaField: makeFrameBuffer()
}
const makeFragFn = <T>(frag: string, fbos: { current: Framebuffer2D }, uniforms: T) => {
return regl({
frag: frag,
framebuffer: () => fbos.current,
uniforms: uniforms,
attributes: {
position: [
[1, -1],
[1, 1],
[-1, -1],
[-1, 1]
]
},
vert: k.vert,
count: 4,
primitive: "triangle strip",
depth: { enable: false }
})
}
const makeFragWithFboPropFn = (frag: string, uniforms: any) => {
return regl({
frag: frag,
framebuffer: (_: any, prop: any) => prop.fbo,
uniforms: uniforms,
attributes: {
position: [
[1, -1],
[1, 1],
[-1, -1],
[-1, 1]
]
},
vert: k.vert,
count: 4,
primitive: "triangle strip",
depth: { enable: false }
})
}
this.updateAlphaBeta = makeFragFn(k.updateAlphaBeta, { current: this.data.alphaBetaField }, {
dt: (_: any, props: any) => props.dt,
cellSize: (_: any, props: any) => props.cellSize,
material: (_: any, props: any) => props.material,
})
this.updateElectric = makeFragFn(k.updateElectric, this.data.electricField, {
electricField: (_: any, props: any) => props.electricField,
magneticField: (_: any, props: any) => props.magneticField,
alphaBetaField: (_: any, props: any) => props.alphaBetaField,
relativeCellSize: (_: any, props: any) => props.relativeCellSize,
reflectiveBoundary: (_: any, props: any) => props.reflectiveBoundary,
})
this.updateMagnetic = makeFragFn(k.updateMagnetic, this.data.magneticField, {
electricField: (_: any, props: any) => props.electricField,
magneticField: (_: any, props: any) => props.magneticField,
alphaBetaField: (_: any, props: any) => props.alphaBetaField,
relativeCellSize: (_: any, props: any) => props.relativeCellSize,
reflectiveBoundary: (_: any, props: any) => props.reflectiveBoundary,
})
this.injectSource = makeFragFn(k.injectSource, this.data.electricField, {
sourceField: (_: any, props: any) => props.sourceField,
field: (_: any, props: any) => props.field,
dt: (_: any, props: any) => props.dt,
})
this.decaySource = makeFragFn(k.decaySource, this.data.electricSourceField, {
sourceField: (_: any, props: any) => props.sourceField,
dt: (_: any, props: any) => props.dt,
})
this.drawOnTexture = {
[DrawShape.Ellipse]: makeFragWithFboPropFn(k.drawEllipse, {
texture: (_: any, props: any) => props.texture,
pos: (_: any, props: any) => props.pos,
value: (_: any, props: any) => props.value,
radius: (_: any, props: any) => props.radius,
keep: (_: any, props: any) => props.keep,
}),
[DrawShape.Square]: makeFragWithFboPropFn(k.drawSquare, {
texture: (_: any, props: any) => props.texture,
pos: (_: any, props: any) => props.pos,
value: (_: any, props: any) => props.value,
size: (_: any, props: any) => props.size,
keep: (_: any, props: any) => props.keep,
}),
}
this.copyUint8ToFloat16 = makeFragFn(k.copyUint8ToFloat16, this.data.material, {
texture: (_: any, props: any) => props.texture
})
this.copyFloat16ToUint8 = makeFragWithFboPropFn(k.copyFloat16ToUint8, {
texture: (_: any, props: any) => props.texture
})
this.resetFields()
this.resetMaterials()
}
updateAlphaBetaFromMaterial(dt: number) {
this.alphaBetaDt = dt
this.updateAlphaBeta({
material: this.data.material.current,
dt: dt,
cellSize: this.cellSize
})
}
getGridSize = () => this.gridSize;
setGridSize = (gridSize: [number, number]) => {
this.gridSize = gridSize
this.frameBuffers.forEach(frameBuffer => frameBuffer.resize(gridSize[0], gridSize[1]))
// TODO: Copy old data ?
this.resetFields()
}
getCellSize = () => this.cellSize;
setCellSize = (cellSize: number) => {
this.cellSize = cellSize
this.resetFields()
}
getReflectiveBoundary = () => this.reflectiveBoundary;
setReflectiveBoundary = (reflectiveBoundary: boolean) => {
this.reflectiveBoundary = reflectiveBoundary
}
stepElectric = (dt: number) => {
if (this.alphaBetaDt !== dt) {
this.updateAlphaBetaFromMaterial(dt)
}
this.data.electricField.swap()
this.data.electricSourceField.swap()
// Writes to E current
this.injectSource({
sourceField: this.data.electricSourceField.previous,
field: this.data.electricField.previous,
dt: dt
})
this.data.electricField.swap()
// Writes to S current
this.decaySource({
sourceField: this.data.electricSourceField.previous,
dt: dt
})
// Writes to E current
this.updateElectric({
electricField: this.data.electricField.previous,
magneticField: this.data.magneticField.current,
alphaBetaField: this.data.alphaBetaField,
relativeCellSize: [1 / this.gridSize[0], 1 / this.gridSize[1]],
reflectiveBoundary: this.reflectiveBoundary,
})
this.data.time += dt / 2
}
stepMagnetic = (dt: number) => {
if (this.alphaBetaDt !== dt) {
this.updateAlphaBetaFromMaterial(dt)
}
this.data.magneticField.swap()
this.updateMagnetic({
electricField: this.data.electricField.current,
magneticField: this.data.magneticField.previous,
alphaBetaField: this.data.alphaBetaField,
relativeCellSize: [1 / this.gridSize[0], 1 / this.gridSize[1]],
reflectiveBoundary: this.reflectiveBoundary,
})
this.data.time += dt / 2
}
resetFields = () => {
this.data.time = 0
this.regl.clear({
color: [0, 0, 0, 0],
framebuffer: this.data.electricField.current
})
this.regl.clear({
color: [0, 0, 0, 0],
framebuffer: this.data.magneticField.current
})
this.regl.clear({
color: [0, 0, 0, 0],
framebuffer: this.data.electricSourceField.current
})
}
resetMaterials = () => {
this.regl.clear({
color: [1, 1, 0, 0],
framebuffer: this.data.material.current
})
this.updateAlphaBetaFromMaterial(this.alphaBetaDt)
}
drawMaterial = (materialType: MaterialType, drawInfo: DrawInfo) => {
this.data.material.swap()
const value = [0, 0, 0, 0]
const keep = [1, 1, 1, 1]
if (materialType === "permittivity") {
value[0] = drawInfo.value
keep[0] = 0
} else if (materialType === "permeability") {
value[1] = drawInfo.value
keep[1] = 0
} else if (materialType === "conductivity") {
value[2] = drawInfo.value
keep[2] = 0
}
const uniforms: any = {
pos: snapToGrid(drawInfo.center, this.gridSize),
value: value,
keep: keep,
texture: this.data.material.previous,
fbo: this.data.material.current
}
if (drawInfo.drawShape === DrawShape.Ellipse) {
uniforms.radius = drawInfo.radius
} else if (drawInfo.drawShape === DrawShape.Square) {
uniforms.size = drawInfo.halfSize
}
this.drawOnTexture[drawInfo.drawShape](uniforms)
this.updateAlphaBetaFromMaterial(this.alphaBetaDt)
}
injectSignal = (drawInfo: DrawInfo, dt: number) => {
this.data.electricSourceField.swap()
const uniforms: any = {
pos: snapToGrid(drawInfo.center, this.gridSize),
value: [0, 0, drawInfo.value * dt, 0],
keep: [1, 1, 1, 1],
texture: this.data.electricSourceField.previous,
fbo: this.data.electricSourceField.current
}
if (drawInfo.drawShape === DrawShape.Ellipse) {
uniforms.radius = drawInfo.radius
} else if (drawInfo.drawShape === DrawShape.Square) {
uniforms.size = drawInfo.halfSize
}
this.drawOnTexture[drawInfo.drawShape](uniforms)
}
loadMaterial = (material: number[][][]) => {
const materialTexture = this.regl.texture({
data: material,
format: "rgb",
type: "uint8",
min: "nearest",
mag: "nearest",
})
this.copyUint8ToFloat16({
texture: materialTexture
})
materialTexture.destroy()
this.updateAlphaBetaFromMaterial(this.alphaBetaDt)
}
loadMaterialFromComponents = (permittivity: number[][], permeability: number[][], conductivity: number[][]) => {
this.loadMaterial(
combineMaterialMaps(permittivity, permeability, conductivity)
)
}
getData = () => this.data
getMaterial = () => {
const fbo = this.regl.framebuffer({
color: this.regl.texture({
width: this.gridSize[0],
height: this.gridSize[1],
wrap: "clamp",
type: "uint8",
format: "rgba",
min: "nearest",
mag: "nearest",
}),
depthStencil: false
})
this.copyFloat16ToUint8({
fbo: fbo,
texture: this.data.material.current
})
const materialData = this.regl.read({
framebuffer: fbo
})
fbo.destroy()
// Uint8 to float with correct scaling
const materialFloats: number[][][] = []
for (let y = 0; y < this.gridSize[1]; y++) {
const row: number[][] = []
for (let x = 0; x < this.gridSize[0]; x++) {
row.push([
(materialData[y * this.gridSize[0] * 4 + x * 4 + 0] - 127) / 4,
(materialData[y * this.gridSize[0] * 4 + x * 4 + 1] - 127) / 4,
(materialData[y * this.gridSize[0] * 4 + x * 4 + 2] - 127) / 4,
// +3 is unused
])
}
materialFloats.push(row)
}
return materialFloats
}
}