Need help with RangeError

Rudy Matt
Rudy Matt Posts: 1
edited December 2022 in Programming #1

I'm trying to prepare animation of heatness spreading. But problem is that I can only calculate heat for matrixes with size 12x12x12. When I'm trying to use bigger values I receive: RangeError: execution timeout.

I'm trying to generate simulation on table (using hitTestResult from WoldMeshController).

createMatrix function creates a 3D array to store objects representing every cell and store it in global.matrix

_global.heatness is 3D array which stores only values of the heat (these ones are used to calculate heat in every step of algorithm)

global.heatnessSource_X/Y/Z are coords of heat source

Function for calculating heat for whole 3D matrix:

global.startVisualization = function() {

var centerOfScreenPos = new vec2(0.5, 0.5);
if (global.WorldMeshController.isInitialize) {
    var rayCastRes = global.WorldMeshController.getHitTestResult(centerOfScreenPos);
    if (!rayCastRes.isValid()) {
        return;
    } else if (rayCastRes.getClassification() == 4) {
        var pos = rayCastRes.getWorldPos();
        var substractValue = global.matrixShape/2 * 3.5;
        createMatrix(pos.x-substractValue, pos.y, pos.z-substractValue, global.matrixShape);

        global.heatness[global.heatnessSource_X][global.heatnessSource_Y][global.heatnessSource_Z] = global.heatnessValue;
        for (var t = 0; t < script.time; t++) {
                global.heatness = spreadHeatness(script.h, script.dt, script.e);
        } 
    } else {
        global.logToScreen("Wrong suface: " + rayCastRes.getClassification());
    }
}

}

Function to calculate heat in every algorithm step:

function spreadHeatness(h, dt, e) {
var newHeatness = global.heatness.map(function(arr) {
return arr.slice();
});

for (var x = 1; x < global.heatness.length-1; x++) {
    for (var y = 1; y < global.heatness.length-1; y++) {
        for (var z = 1; z < global.heatness.length-1; z++) {
            dx2 = e * ((global.heatness[x+1][y][z] - 2*global.heatness[x][y][z] + global.heatness[x-1][y][z]) / (h*h));
            dy2 = e * ((global.heatness[x][y+1][z] - 2*global.heatness[x][y][z] + global.heatness[x][y-1][z]) / (h*h)); 
            dz2 = e * ((global.heatness[x][y][z+1] - 2*global.heatness[x][y][z] + global.heatness[x][y][z-1]) / (h*h)); 

            var new_color = global.heatness[x][y][z] + dt * (dx2 + dy2 + dz2);

            if (x == global.heatnessSource_X && y == global.heatnessSource_Y && z == global.heatnessSource_Z) {                        
                new_color = new_color + global.heatnessValue;
            }
            newHeatness[x][y][z] = new_color;

        }
    }
}

return newHeatness;

}
So as I mentioned earlier, for matrixShape > 12 I'm getting RangeError: execution timeout. I do not know why, but I would like to generate simulations for much bigger 3D matrix. JS is not my main advantage so description of this error from documentation is not saying much to me.

This error is happing every time in different line, but always in spreadHeatness function.

I'll be glad for every help, Thanks!!!

Answers

  • Bakari Mustafa
    Bakari Mustafa Posts: 174 🔥🔥🔥

    There are a few things you can try to improve the performance of your simulation:

    • Optimize the code: You could try pre-allocating the newHeatness array, rather than creating a new copy of the global.heatness array on each iteration of the loop.
    • Use a smaller time step: The time step (dt) determines how quickly the heat spreads through the matrix. If you use a smaller time step, the simulation will take longer to run, but it will be more accurate. You could try using a smaller time step to see if it improves the performance of the simulation.
    • Use a smaller matrix size: If you are able to achieve the desired results with a smaller matrix size, you can try using a smaller matrix size to improve the performance of the simulation.