diff --git a/apps/symsim/ginzburg_landau/js/BrusselatorWorker.js b/apps/symsim/ginzburg_landau/js/BrusselatorWorker.js new file mode 100644 index 0000000..04ea666 --- /dev/null +++ b/apps/symsim/ginzburg_landau/js/BrusselatorWorker.js @@ -0,0 +1,130 @@ +/** + * BrusselatorWorker.js + * + * ProcessingWorker: the Brusselator, in the style of GinzburgLandauWorker. + * + * u_t = Du lap u + a - (b+1) u + u^2 v, v_t = Dv lap v + b u - u^2 v + * + * The buffer holds (u - a, v - b/a), so his noise is noise about the rest state. + * Lengths: one pixel is h, and his stencil is 1.2 h^2 lap, so D in pixels is D/(1.2 h^2). + * Time: a film's period T sets the step, dt = T/(framesPerPeriod*stepsCount), so a delay of + * tau T is a whole number of frames. T comes from the film: the stopwatch at the end of process(). + * framesPerPeriod is passed to SymmetrizationWorker on the shared buffer: it turns on the delayed lookup. + */ + +import { ParamBool, ParamFloat, ParamInt, ShaderFragments as SF, programBuilder } from './modules.js'; + +const MYNAME = 'BrusselatorWorker'; + +const stepFrag = { getName: () => MYNAME, Brusselator_step: /*glsl*/` +in vec2 vUv; +out vec4 outValue; +uniform sampler2D tSource; +uniform float a, b, Da, Db, timestep; +void main(){ + #define S(x,y) texelFetch(tSource, ivec2(x,y), 0).xy + ivec2 I = ivec2(gl_FragCoord.xy), D = textureSize(tSource, 0); + int x = I.x, y = I.y, l = (x-1+D.x)%D.x, r = (x+1)%D.x, u = (y-1+D.y)%D.y, d = (y+1)%D.y; + vec2 p = S(x,y); + vec2 lapl = 0.8*(S(l,y)+S(r,y)+S(x,d)+S(x,u)) + 0.2*(S(r,u)+S(l,d)+S(r,d)+S(l,u)) - 4.0*p; + float U = a + p.x, V = b/a + p.y, r2 = U*U*V; + outValue.xy = p + timestep*(vec2(Da,Db)*lapl + vec2(a - (b+1.)*U + r2, b*U - r2)); +} +`}; + +const programs = programBuilder({ + step: { + name: 'BrusselatorStep', + vs: { frags: [{ obj: SF, id: 'canvasVertexShader' }] }, + fs: { frags: [{ obj: stepFrag, id: 'Brusselator_step' }] }, + }, +}, false); + +function BrusselatorWorker(options = {}) { + + let mGLCtx = null, mParams = null; + const cfg = { + enabled: options.enabled ?? true, + a: 1, b: 2.3, Du: 1, Dv: 1, + h: options.h ?? 40/144, + stepsCount: options.stepsCount ?? 7, + framesPerPeriod: options.framesPerPeriod ?? 48, // a multiple of every cost's denominator + T: options.T ?? 6.3551, // the first guess: the Hopf period + stopwatch: options.stopwatch ?? true, + clockX: options.clockX ?? 0, clockY: options.clockY ?? 0, // the stopwatch's pixel, from the grid's centre: not on a mirror that costs 1/2 + }; + const clock = { t: 0, prev: 0, mean: 0, last: -1, T0: 0, px: new Float32Array(2) }; + + function init(glCtx) { + mGLCtx = glCtx; + programs.getProgram(glCtx.gl, 'step'); + } + + function process(buffer, time) { + buffer.framesPerPeriod = cfg.enabled ? cfg.framesPerPeriod : 0; + if (!cfg.enabled) return; + const gl = mGLCtx.gl, program = programs.getProgram(gl, 'step'); + const dt = cfg.T/(cfg.framesPerPeriod*cfg.stepsCount), k = 1/(1.2*cfg.h*cfg.h); + + gl.disable(gl.BLEND); + gl.viewport(0, 0, buffer.width, buffer.height); + program.bind(); + program.setUniforms({ u_aspect: buffer.height/buffer.width, u_scale: 0.5, u_center: [0.5, 0.5] }); + program.setUniforms({ a: cfg.a, b: cfg.b, Da: cfg.Du*k, Db: cfg.Dv*k, timestep: dt }); + for (let i = 0; i < cfg.stepsCount; i++) { + program.setUniforms({ tSource: buffer.read }); + program.blit(buffer.write); + buffer.swap(); + } + + // the stopwatch: T = time between two upward crossings of u past its running mean, kept if within 25% of the first guess + gl.bindFramebuffer(gl.FRAMEBUFFER, buffer.read.fbo); + gl.readPixels((buffer.width >> 1) + cfg.clockX, (buffer.height >> 1) + cfg.clockY, 1, 1, gl.RG, gl.FLOAT, clock.px); + gl.bindFramebuffer(gl.FRAMEBUFFER, null); + const u = clock.px[0], step = dt*cfg.stepsCount; + clock.T0 ||= cfg.T; + clock.t += step; + clock.mean += (u - clock.mean)*step/(5*cfg.T); + if (clock.prev < clock.mean && u >= clock.mean) { + const tc = clock.t - step*(u - clock.mean)/(u - clock.prev); + const P = tc - clock.last; + if (cfg.stopwatch && clock.last >= 0 && Math.abs(P - clock.T0) < 0.25*clock.T0) cfg.T = P; + clock.last = tc; + } + clock.prev = u; + } + + function makeParams() { + return { + enabled: ParamBool({ obj: cfg, key: 'enabled', name: 'enabled' }), + a: ParamFloat({ obj: cfg, key: 'a' }), + b: ParamFloat({ obj: cfg, key: 'b' }), + Du: ParamFloat({ obj: cfg, key: 'Du' }), + Dv: ParamFloat({ obj: cfg, key: 'Dv' }), + h: ParamFloat({ obj: cfg, key: 'h', name: 'pixel size' }), + stepsCount: ParamInt({ obj: cfg, key: 'stepsCount', min: 1, max: 10000, name: 'steps' }), + framesPerPeriod: ParamInt({ obj: cfg, key: 'framesPerPeriod', min: 3, max: 960, name: 'frames per T' }), + T: ParamFloat({ obj: cfg, key: 'T', name: 'period T' }), + stopwatch: ParamBool({ obj: cfg, key: 'stopwatch', name: 'T from the film' }), + clockX: ParamInt({ obj: cfg, key: 'clockX', min: -4096, max: 4096, name: 'clock x' }), + clockY: ParamInt({ obj: cfg, key: 'clockY', min: -4096, max: 4096, name: 'clock y' }), + }; + } + + function getParams() { + if (!mParams) mParams = makeParams(); + return mParams; + } + + return { + init, + getId: () => 'Brusselator', + getName: () => 'Brusselator', + getClassName: () => MYNAME, + getParams, + process, + }; + +} // BrusselatorWorker + +export { BrusselatorWorker }; diff --git a/apps/symsim/ginzburg_landau/js/ginzburg_landau_app.js b/apps/symsim/ginzburg_landau/js/ginzburg_landau_app.js index cccd448..4c7f4c4 100644 --- a/apps/symsim/ginzburg_landau/js/ginzburg_landau_app.js +++ b/apps/symsim/ginzburg_landau/js/ginzburg_landau_app.js @@ -22,6 +22,7 @@ import { SymmetrizationWorker } from '../../../../lib/symhublib/Symmetriza import { MaskWorker } from '../../../../lib/symhublib/MaskWorker.js'; import { GinzburgLandauWorker } from './GinzburgLandauWorker.js'; +import { BrusselatorWorker } from './BrusselatorWorker.js'; import { GinzburgLandauInitializer } from './GinzburgLandauInitializer.js'; import { GinzburgLandauUpgradeData } from './GinzburgLandauUpgradeData.js'; @@ -45,6 +46,7 @@ function makeGLWorkerFactory(getGLCtx, getBuffer, getGroup, getChildren) { infoArray: [ { name: 'GinzburgLandauInitializer', label: 'GL initialization', creator: make(GinzburgLandauInitializer) }, { name: 'GinzburgLandauWorker', label: 'GL simulation', creator: make(GinzburgLandauWorker) }, + { name: 'BrusselatorWorker', label: 'Brusselator', creator: make(BrusselatorWorker) }, { name: 'SymmetrizationWorker', label: 'Symmetrization', creator: make(SymmetrizationWorker) }, { name: 'MaskWorker', label: 'Mask', creator: make(MaskWorker) }, ], diff --git a/lib/grouplib/Group_WP.js b/lib/grouplib/Group_WP.js index 02647e7..302e407 100644 --- a/lib/grouplib/Group_WP.js +++ b/lib/grouplib/Group_WP.js @@ -14,6 +14,7 @@ import { Group, ParamChoice, ParamFloat, + ParamString, } from './modules.js'; @@ -38,6 +39,7 @@ export class Group_WP { c: getParam(opt.c,0), angle_a: getParam(opt.angle_a, 0), angle_b: getParam(opt.angle_b,90), + taus: getParam(opt.taus,''), }; this.setOptions(opt); @@ -158,6 +160,11 @@ export class Group_WP { angle_a:pm.angle_a*TORADIANS, angle_b:pm.angle_b*TORADIANS, }) + if(pm.taus) { // one time cost per side, in periods: two z-planes on its word move z by -tau + const taus = pm.taus.split(',').map(s => s.split('/').reduce((n, d) => n/d)); + const words = oldGroup.t ?? oldGroup.s.map(p => [p]); + oldGroup = { ...oldGroup, t: words.map((w, i) => [...w, iPlane([0,0,1,0]), iPlane([0,0,1,-taus[i]/2])]) }; + } return new Group(oldGroup); } @@ -218,6 +225,7 @@ export class Group_WP { min: -180, max: 180, step: inc, onChange: opc, }), + taus: ParamString({ obj: par, key: 'taus', name: 'taus', onChange: opc }), }; } diff --git a/lib/grouplib/modules.js b/lib/grouplib/modules.js index 888fda0..4d4e493 100644 --- a/lib/grouplib/modules.js +++ b/lib/grouplib/modules.js @@ -71,6 +71,7 @@ export { export { ParamChoice, ParamFloat, + ParamString, ParamInt, ParamGroup, ParamObj, diff --git a/lib/shaders/bufferToScreenColormap.glsl.mjs b/lib/shaders/bufferToScreenColormap.glsl.mjs index 75939c4..a75f500 100644 --- a/lib/shaders/bufferToScreenColormap.glsl.mjs +++ b/lib/shaders/bufferToScreenColormap.glsl.mjs @@ -25,6 +25,9 @@ uniform int uCmWrap; uniform int uInterpolation; //uniform float uColormapTransparency; uniform float uTransparency; +uniform highp sampler2DArray uHistory; +uniform int uHistFrames; +uniform int uHistNewest; #define USE_MIPMAP @@ -88,6 +91,13 @@ void main() { float mask = 1.-smoothstep(-blurWidth, blurWidth, sdb); vec4 bufValue = getTexData(uSimBuffer, tpnt, uInterpolation); + if(uHistFrames > 0) { // a tile at z != 0 shows the frame it is a copy of + int back = int(round(fract(-wpnt.z)*float(uHistFrames))) % uHistFrames; + if(back > 0) bufValue = texture(uHistory, vec3(tpnt, float((uHistNewest - back + uHistFrames) % uHistFrames))); + } else { + float phase = 6.283185307*wpnt.z; + bufValue.xy = mat2(cos(phase), sin(phase), -sin(phase), cos(phase))*bufValue.xy; + } float visValue = getDataSouceValue(bufValue, uDataSource); vec4 color = getColormapVisualization(visValue, uMinValue, uMaxValue, uColormap, uCmWrap, uCmBanding ); diff --git a/lib/shaders/symSamplerShader.glsl.mjs b/lib/shaders/symSamplerShader.glsl.mjs index 07b3fb4..6414c3b 100644 --- a/lib/shaders/symSamplerShader.glsl.mjs +++ b/lib/shaders/symSamplerShader.glsl.mjs @@ -7,6 +7,10 @@ uniform sampler2D uSource; uniform sampler2D uGroupData; uniform int uIterations; uniform float uSymMix; +uniform float uZScale; +uniform highp sampler2DArray uHistory; // the film so far, one layer per pass +uniform int uFrames; // layers per period; 0: no history, a phase instead +uniform int uHead; // the layer the current frame will be written to void tunnelPoint(inout vec3 p){ vec3 q = p - vec3(0.25,0.25, 0.); @@ -56,6 +60,13 @@ void main () { //tunnelPoint(pnt); vec4 symValue = getTexData(uSource, 0.5*pnt.xy+vec2(0.5,0.5)); + if(uFrames > 0) { // q(y,t) = q(fold y, t + z T): whole frames, because T sets the time step + int back = int(round(fract(-uZScale*pnt.z)*float(uFrames))) % uFrames; + if(back > 0) symValue = texture(uHistory, vec3(0.5*pnt.xy+vec2(0.5,0.5), float((uHead - back + uFrames) % uFrames))); + } else { + float phase = 6.283185307*uZScale*pnt.z; // z = -(sum of the costs): A(gx,t) = exp(-2 pi i tau) A(x,t) + symValue.xy = mat2(cos(phase), sin(phase), -sin(phase), cos(phase))*symValue.xy; + } // map point into [0,1] range outColor = mix(origValue, symValue, uSymMix); diff --git a/lib/symhublib/SymRenderer.js b/lib/symhublib/SymRenderer.js index 69aade9..3e62dd1 100644 --- a/lib/symhublib/SymRenderer.js +++ b/lib/symhublib/SymRenderer.js @@ -1636,6 +1636,7 @@ function SymRenderer(options) { mTransGroup = mGroup.clone(); mTransGroup.applyTransform(tr); + mTransGroup.zScale = ptc.scale; mPattern.setGroup(mTransGroup); if(false) console.log(`${MYNAME}.updateTransGroup()`, mTransGroup); diff --git a/lib/symhublib/SymmetrizationWorker.js b/lib/symhublib/SymmetrizationWorker.js index 8821bd6..9a435e1 100644 --- a/lib/symhublib/SymmetrizationWorker.js +++ b/lib/symhublib/SymmetrizationWorker.js @@ -63,6 +63,7 @@ function SymmetrizationWorker(options = {}) { let mGroup = null; let mGroupDataSampler = null; let mParams = null; + let mHistory = null, mHistoryFrames = 0, mHead = 0; const mConfig = { enabled: options.enabled ?? true, @@ -81,6 +82,22 @@ function SymmetrizationWorker(options = {}) { mGroupDataSampler = DataPacking.createGroupDataSampler(gl); } + // A new simulation starts with an empty past: the array is zero, the rest state of a worker that stores deviations. + function initialize(buffer) { mHistoryFrames = 0; } + + function ensureHistory(buffer, frames) { + if (mHistoryFrames === frames) return; + const gl = mGLCtx.gl; + gl.deleteTexture(mHistory); + mHistory = gl.createTexture(); + gl.bindTexture(gl.TEXTURE_2D_ARRAY, mHistory); + gl.texStorage3D(gl.TEXTURE_2D_ARRAY, 1, gl.RG32F, buffer.width, buffer.height, frames); + gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.LINEAR); + gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.LINEAR); + mHistoryFrames = frames; + mHead = 0; + } + // ── group ───────────────────────────────────────────────────────────────── function setGroup(group) { @@ -103,6 +120,8 @@ function SymmetrizationWorker(options = {}) { const gl = mGLCtx.gl; const program = Symm_programs.getProgram(gl, 'symSampler'); + const frames = buffer.framesPerPeriod ?? 0; + if (frames > 0) ensureHistory(buffer, frames); gl.disable(gl.BLEND); gl.viewport(0, 0, buffer.width, buffer.height); @@ -120,10 +139,23 @@ function SymmetrizationWorker(options = {}) { uGroupData: mGroupDataSampler, uSymMix: mConfig.symMix, uIterations: mConfig.symIterations, + uZScale: mGroup.zScale ?? 1, + uHistory: frames > 0 ? mHistory : { texture: null, sampler: null }, + uFrames: frames, + uHead: mHead, }); program.blit(buffer.write); buffer.swap(); + + if (frames > 0) { // this frame joins the past; the display reads the same ring + gl.bindFramebuffer(gl.FRAMEBUFFER, buffer.read.fbo); + gl.bindTexture(gl.TEXTURE_2D_ARRAY, mHistory); + gl.copyTexSubImage3D(gl.TEXTURE_2D_ARRAY, 0, 0, 0, mHead, 0, 0, buffer.width, buffer.height); + gl.bindFramebuffer(gl.FRAMEBUFFER, null); + buffer.history = { texture: mHistory, sampler: null, frames, newest: mHead }; + mHead = (mHead + 1) % frames; + } } // ── params ──────────────────────────────────────────────────────────────── @@ -150,6 +182,7 @@ function SymmetrizationWorker(options = {}) { getClassName: () => MYNAME, getParams, setGroup, + initialize, process, }; diff --git a/lib/symhublib/VisualizationColormap.js b/lib/symhublib/VisualizationColormap.js index 5d6bba9..edc080e 100644 --- a/lib/symhublib/VisualizationColormap.js +++ b/lib/symhublib/VisualizationColormap.js @@ -239,6 +239,9 @@ function VisualizationColormap(par={}){ uMaxValue: cmCfg.maxValue, uTransparency: (1. - cmCfg.opacity), uInterpolation: getInterpolationId(cmCfg.interpolation), + uHistory: dataBuffer.history ?? { texture: null, sampler: null }, + uHistFrames: dataBuffer.history?.frames ?? 0, + uHistNewest: dataBuffer.history?.newest ?? 0, }; if(cmCfg.useMipmap && mMipmapBuffer){