Diffuse-React Help
in
Programming Questions
•
1 year ago
I'm writing a diffuse-react algorithm and am getting odd results. Right now I only have diffusion (no reaction or chemical sources), so that I would have some intuition for whether the results were correct (as a check on the code). However, the diffusivities seem to be very sensitive to odd behavior.
If the diffusivities (diffU and diffV) are set to be 0.1-0.4, then I get normal diffusion behavior:
However, if I set them to 0.5, I get /extremely/ odd behavior that seems to cause preferential diffusion towards (0,0).
Does anyone with experience in this algorithm have any suggestions?
- int gridSize = 800;
- float[][] U = new float[gridSize][gridSize];
- float[][] V = new float[gridSize][gridSize];
- PVector diffU;
- PVector diffV;
- float h = 1;
- float k = 1;
- //-----------------Setup
- void setup(){
- size(gridSize,gridSize);
- background(0);
- smooth();
- colorMode(HSB,1.0);
- //frameRate(30);
- diffU = new PVector(0.2,0.2,0.2);
- diffV = new PVector(0.2,0.2,0.2);
- initialize();
- }
- //-----------------Main Loop
- void draw(){
- //evolve the system in time
- evolve(U,V,diffU,diffV);
- //then draw the system
- for(int i = 0; i < gridSize; i++) {
- for(int j = 0; j < gridSize; j++) {
- set(i,j,color(0.0, 0.0, (float)(U[i][j])));
- }
- }
- }
- //-----------------Interactions
- void keyPressed() {
- if (key == ' ') {
- saveFrame("####.png");
- }
- }
- //-----------------Defined Functions
- void initialize() {
- for(int i = 0; i < gridSize; i++) {
- for(int j = 0; j < gridSize; j++) {
- if((i-gridSize/2)*(i-gridSize/2) + (j-gridSize/2)*(j-gridSize/2) < 200*200) {
- U[i][j] = 1.0;
- V[i][j] = 0.0;
- }
- }
- }
- }
- int mod(int a, int b) {
- if (a >= 0) {
- return a % b;
- } else {
- return (a % b) + b;
- }
- }
- float dxx(float[][] _U, int _x, int _y, float _stepSize) {
- float dxx = (1.0 / (_stepSize*_stepSize))*(_U[mod(_x+1,gridSize)][mod(_y,gridSize)] - 2*_U[mod(_x,gridSize)][mod(_y,gridSize)] + _U[mod(_x-1,gridSize)][mod(_y,gridSize)]);
- return dxx;
- }
- float dyy(float[][] _U, int _x, int _y, float _stepSize) {
- float dyy = (1.0 / (_stepSize*_stepSize))*(_U[mod(_x,gridSize)][mod(_y+1,gridSize)] - 2*_U[mod(_x,gridSize)][mod(_y,gridSize)] + _U[mod(_x,gridSize)][mod(_y-1,gridSize)]);
- return dyy;
- }
- void evolve(float[][] _U, float[][] _V, PVector _diffU, PVector _diffV) {
- for(int i = 0; i < gridSize; i++) {
- for(int j = 0; j < gridSize; j++) {
- _U[i][j] += _diffU.x*dxx(_U,i,j,h) + _diffU.y*dyy(_U,i,j,k) - _U[i][j]*_V[i][j]*_V[i][j] + 0.03*(1 - _U[i][j]);
- _V[i][j] += _diffV.x*dxx(_V,i,j,h) + _diffV.y*dyy(_V,i,j,k) + _U[i][j]*_V[i][j]*_V[i][j] - 0.06*(_V[i][j]);
- }
- }
- }
1