handy-diffusion 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ADI.js +41 -11
- package/index.d.ts +118 -0
- package/initArrays.js +7 -7
- package/package.json +2 -1
package/ADI.js
CHANGED
|
@@ -8,7 +8,8 @@ let modifiedUpperDiagonal2, modifiedRightHandSide2, solution2;
|
|
|
8
8
|
let intermediateConcentration;
|
|
9
9
|
let a1, b1, c1, d1;
|
|
10
10
|
let a2, b2, c2, d2;
|
|
11
|
-
let alpha, halfDeltaT,
|
|
11
|
+
let alpha, halfDeltaT, scaledSources;
|
|
12
|
+
let gamma;
|
|
12
13
|
|
|
13
14
|
export const setADIProperties = (
|
|
14
15
|
width,
|
|
@@ -16,7 +17,7 @@ export const setADIProperties = (
|
|
|
16
17
|
diffusionCoefficient,
|
|
17
18
|
deltaX,
|
|
18
19
|
deltaT,
|
|
19
|
-
|
|
20
|
+
decayRates = 0
|
|
20
21
|
) => {
|
|
21
22
|
WIDTH = width;
|
|
22
23
|
HEIGHT = height;
|
|
@@ -38,11 +39,39 @@ export const setADIProperties = (
|
|
|
38
39
|
d2,
|
|
39
40
|
alpha,
|
|
40
41
|
halfDeltaT,
|
|
41
|
-
oneMinus2AlphaMinusGamma,
|
|
42
42
|
scaledSources,
|
|
43
|
-
|
|
43
|
+
gamma,
|
|
44
|
+
} = initADIArrays(WIDTH, HEIGHT, diffusionCoefficient, deltaX, deltaT));
|
|
44
45
|
};
|
|
45
46
|
|
|
47
|
+
export const updateSinksAndSources = (sinks, sources) => {
|
|
48
|
+
for (let i = 0; i < WIDTH * HEIGHT; i++) {
|
|
49
|
+
gamma[i] = (sinks[i] * halfDeltaT) / 4;
|
|
50
|
+
scaledSources[i] = sources[i] * halfDeltaT;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const updateMainDiagonalXstep = (yCoord) => {
|
|
54
|
+
//update b1
|
|
55
|
+
for (let i = 0; i < WIDTH; i++) {
|
|
56
|
+
const idx = yCoord * WIDTH + i;
|
|
57
|
+
b1[i] = 1 + 2 * alpha + gamma[idx];
|
|
58
|
+
}
|
|
59
|
+
//boundary conditions
|
|
60
|
+
b1[0] = 1 + alpha + gamma[yCoord * WIDTH + 0];
|
|
61
|
+
b1[WIDTH - 1] = 1 + alpha + gamma[yCoord * WIDTH + (WIDTH - 1)];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const updateMainDiagonalYstep = (xCoord) => {
|
|
65
|
+
//update b2
|
|
66
|
+
for (let j = 0; j < HEIGHT; j++) {
|
|
67
|
+
const idx = j * WIDTH + xCoord;
|
|
68
|
+
b2[j] = 1 + 2 * alpha + gamma[idx];
|
|
69
|
+
}
|
|
70
|
+
//boundary conditions
|
|
71
|
+
b2[0] = 1 + alpha + gamma[0 * WIDTH + xCoord];
|
|
72
|
+
b2[HEIGHT - 1] = 1 + alpha + gamma[(HEIGHT - 1) * WIDTH + xCoord];
|
|
73
|
+
}
|
|
74
|
+
|
|
46
75
|
export const ADI = (
|
|
47
76
|
concentrationData,
|
|
48
77
|
sources,
|
|
@@ -72,10 +101,11 @@ export const ADI = (
|
|
|
72
101
|
|
|
73
102
|
d1[i] =
|
|
74
103
|
alpha * bottom +
|
|
75
|
-
|
|
104
|
+
(1 - 2 * alpha - gamma[idx]) * center +
|
|
76
105
|
alpha * top +
|
|
77
106
|
scaledSources[idx];
|
|
78
107
|
}
|
|
108
|
+
updateMainDiagonalXstep(j);
|
|
79
109
|
|
|
80
110
|
thomasAlgorithm(
|
|
81
111
|
a1,
|
|
@@ -104,7 +134,7 @@ export const ADI = (
|
|
|
104
134
|
|
|
105
135
|
d1[i] =
|
|
106
136
|
alpha * bottom +
|
|
107
|
-
|
|
137
|
+
(1 - 2 * alpha - gamma[idx]) * center +
|
|
108
138
|
alpha * top +
|
|
109
139
|
scaledSources[idx];
|
|
110
140
|
}
|
|
@@ -133,7 +163,7 @@ export const ADI = (
|
|
|
133
163
|
|
|
134
164
|
d1[i] =
|
|
135
165
|
alpha * bottom +
|
|
136
|
-
|
|
166
|
+
(1 - 2 * alpha - gamma[idx]) * center +
|
|
137
167
|
alpha * top +
|
|
138
168
|
scaledSources[idx];
|
|
139
169
|
}
|
|
@@ -165,11 +195,11 @@ export const ADI = (
|
|
|
165
195
|
|
|
166
196
|
d2[j] =
|
|
167
197
|
alpha * left +
|
|
168
|
-
|
|
198
|
+
(1 - 2 * alpha - gamma[idx]) * center +
|
|
169
199
|
alpha * right +
|
|
170
200
|
scaledSources[idx];
|
|
171
201
|
}
|
|
172
|
-
|
|
202
|
+
updateMainDiagonalYstep(i);
|
|
173
203
|
thomasAlgorithm(
|
|
174
204
|
a2,
|
|
175
205
|
b2,
|
|
@@ -201,7 +231,7 @@ export const ADI = (
|
|
|
201
231
|
|
|
202
232
|
d2[j] =
|
|
203
233
|
alpha * left +
|
|
204
|
-
|
|
234
|
+
(1 - 2 * alpha - gamma[idx]) * center +
|
|
205
235
|
alpha * right +
|
|
206
236
|
scaledSources[idx];
|
|
207
237
|
}
|
|
@@ -234,7 +264,7 @@ export const ADI = (
|
|
|
234
264
|
|
|
235
265
|
d2[j] =
|
|
236
266
|
alpha * left +
|
|
237
|
-
|
|
267
|
+
(1 - 2 * alpha - gamma[idx]) * center +
|
|
238
268
|
alpha * right +
|
|
239
269
|
scaledSources[idx];
|
|
240
270
|
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for handy-diffusion
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Set properties for the ADI (Alternating Direction Implicit) method
|
|
7
|
+
* @param width - Grid width
|
|
8
|
+
* @param height - Grid height
|
|
9
|
+
* @param diffusionCoefficient - Diffusion coefficient
|
|
10
|
+
* @param deltaX - Spatial step size
|
|
11
|
+
* @param deltaT - Time step size
|
|
12
|
+
* @param decayRate - Decay rate constant (default: 0)
|
|
13
|
+
*/
|
|
14
|
+
export function setADIProperties(
|
|
15
|
+
width: number,
|
|
16
|
+
height: number,
|
|
17
|
+
diffusionCoefficient: number,
|
|
18
|
+
deltaX: number,
|
|
19
|
+
deltaT: number,
|
|
20
|
+
decayRate?: number
|
|
21
|
+
): void;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Solve 2D diffusion equation using ADI method
|
|
25
|
+
* @param concentrationData - Concentration array (width * height)
|
|
26
|
+
* @param sources - Source terms array
|
|
27
|
+
* @param totalNumberOfIterations - Number of time steps
|
|
28
|
+
* @param allowNegativeValues - Whether to allow negative concentrations (default: false)
|
|
29
|
+
*/
|
|
30
|
+
export function ADI(
|
|
31
|
+
concentrationData: Float64Array | number[],
|
|
32
|
+
sources: Float64Array | number[],
|
|
33
|
+
totalNumberOfIterations: number,
|
|
34
|
+
allowNegativeValues?: boolean
|
|
35
|
+
): void;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Set properties for the Crank-Nicolson method (1D)
|
|
39
|
+
* @param length - Number of grid points
|
|
40
|
+
* @param diffusionCoefficient - Diffusion coefficient
|
|
41
|
+
* @param deltaX - Spatial step size
|
|
42
|
+
* @param deltaT - Time step size
|
|
43
|
+
* @param decayRate - Decay rate constant (default: 0)
|
|
44
|
+
*/
|
|
45
|
+
export function setCNProperties(
|
|
46
|
+
length: number,
|
|
47
|
+
diffusionCoefficient: number,
|
|
48
|
+
deltaX: number,
|
|
49
|
+
deltaT: number,
|
|
50
|
+
decayRate?: number
|
|
51
|
+
): void;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Solve 1D diffusion equation using Crank-Nicolson method
|
|
55
|
+
* @param concentrationData - Initial concentration values
|
|
56
|
+
* @param sources - Source terms
|
|
57
|
+
* @param totalNumberOfIterations - Number of time steps
|
|
58
|
+
* @param allowNegativeValues - Whether to allow negative concentrations (default: false)
|
|
59
|
+
*/
|
|
60
|
+
export function CrankNicolson(
|
|
61
|
+
concentrationData: Float64Array | number[],
|
|
62
|
+
sources: Float64Array | number[],
|
|
63
|
+
totalNumberOfIterations: number,
|
|
64
|
+
allowNegativeValues?: boolean
|
|
65
|
+
): void;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Calculate analytic steady-state solution
|
|
69
|
+
* @param WIDTH - Grid width
|
|
70
|
+
* @param HEIGHT - Grid height
|
|
71
|
+
* @param DIFFUSION_RATE - Diffusion rate
|
|
72
|
+
* @param DECAY_RATE - Decay rate
|
|
73
|
+
* @param deltaX - Spatial step size
|
|
74
|
+
* @param sources - Source terms array
|
|
75
|
+
* @param maxMode - Maximum mode for series expansion
|
|
76
|
+
* @returns Steady-state concentration array
|
|
77
|
+
*/
|
|
78
|
+
export function analyticSteadyState(
|
|
79
|
+
WIDTH: number,
|
|
80
|
+
HEIGHT: number,
|
|
81
|
+
DIFFUSION_RATE: number,
|
|
82
|
+
DECAY_RATE: number,
|
|
83
|
+
deltaX: number,
|
|
84
|
+
sources: Float64Array | number[],
|
|
85
|
+
maxMode: number
|
|
86
|
+
): Float64Array;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Calculate effective influence of sources
|
|
90
|
+
* @param width - Grid width
|
|
91
|
+
* @param height - Grid height
|
|
92
|
+
* @param sources - Source terms array
|
|
93
|
+
* @param lambda - Length scale parameter
|
|
94
|
+
* @param scale - Scaling factor
|
|
95
|
+
* @returns Effective influence array
|
|
96
|
+
*/
|
|
97
|
+
export function efectiveInfluence(
|
|
98
|
+
width: number,
|
|
99
|
+
height: number,
|
|
100
|
+
sources: Float64Array | number[],
|
|
101
|
+
lambda: number,
|
|
102
|
+
scale: number
|
|
103
|
+
): Float64Array;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Solve tridiagonal system using Thomas algorithm
|
|
107
|
+
* @param a - Sub-diagonal coefficients
|
|
108
|
+
* @param b - Main diagonal coefficients
|
|
109
|
+
* @param c - Super-diagonal coefficients
|
|
110
|
+
* @param d - Right-hand side values
|
|
111
|
+
* @returns Solution array
|
|
112
|
+
*/
|
|
113
|
+
export function thomasAlgorithm(
|
|
114
|
+
a: Float64Array | number[],
|
|
115
|
+
b: Float64Array | number[],
|
|
116
|
+
c: Float64Array | number[],
|
|
117
|
+
d: Float64Array | number[]
|
|
118
|
+
): Float64Array;
|
package/initArrays.js
CHANGED
|
@@ -12,7 +12,7 @@ const generateDiagonals = (length, alpha, gamma) => {
|
|
|
12
12
|
return { lowerDiagonal, mainDiagonal, upperDiagonal, rightHandSide };
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
export const initADIArrays = (WIDTH, HEIGHT, DIFFUSION_RATE, deltaX, deltaT
|
|
15
|
+
export const initADIArrays = (WIDTH, HEIGHT, DIFFUSION_RATE, deltaX, deltaT) => {
|
|
16
16
|
const modifiedUpperDiagonal1 = new Float64Array(WIDTH);
|
|
17
17
|
const modifiedRightHandSide1 = new Float64Array(WIDTH);
|
|
18
18
|
const solution1 = new Float64Array(WIDTH);
|
|
@@ -21,23 +21,23 @@ export const initADIArrays = (WIDTH, HEIGHT, DIFFUSION_RATE, deltaX, deltaT, dec
|
|
|
21
21
|
const solution2 = new Float64Array(HEIGHT);
|
|
22
22
|
const intermediateConcentration = new Float64Array(WIDTH * HEIGHT);
|
|
23
23
|
const scaledSources = new Float64Array(WIDTH * HEIGHT);
|
|
24
|
+
const gamma = new Float64Array(WIDTH * HEIGHT).fill(0);
|
|
24
25
|
|
|
25
26
|
const alpha = (DIFFUSION_RATE * deltaT) / (2 * deltaX * deltaX);
|
|
26
|
-
const
|
|
27
|
+
const gammaPoint = 0;
|
|
27
28
|
const {
|
|
28
29
|
lowerDiagonal: a1,
|
|
29
30
|
mainDiagonal: b1,
|
|
30
31
|
upperDiagonal: c1,
|
|
31
32
|
rightHandSide: d1,
|
|
32
|
-
} = generateDiagonals(WIDTH, alpha,
|
|
33
|
+
} = generateDiagonals(WIDTH, alpha, gammaPoint);
|
|
33
34
|
const {
|
|
34
35
|
lowerDiagonal: a2,
|
|
35
36
|
mainDiagonal: b2,
|
|
36
37
|
upperDiagonal: c2,
|
|
37
38
|
rightHandSide: d2,
|
|
38
|
-
} = generateDiagonals(HEIGHT, alpha,
|
|
39
|
+
} = generateDiagonals(HEIGHT, alpha, gammaPoint);
|
|
39
40
|
const halfDeltaT = deltaT / 2;
|
|
40
|
-
const oneMinus2AlphaMinusGamma = 1 - 2 * alpha - gamma;
|
|
41
41
|
return {
|
|
42
42
|
modifiedUpperDiagonal1,
|
|
43
43
|
modifiedRightHandSide1,
|
|
@@ -56,8 +56,8 @@ export const initADIArrays = (WIDTH, HEIGHT, DIFFUSION_RATE, deltaX, deltaT, dec
|
|
|
56
56
|
d2,
|
|
57
57
|
alpha,
|
|
58
58
|
halfDeltaT,
|
|
59
|
-
oneMinus2AlphaMinusGamma,
|
|
60
59
|
scaledSources,
|
|
61
|
-
|
|
60
|
+
gamma,
|
|
62
61
|
};
|
|
63
62
|
};
|
|
63
|
+
|
package/package.json
CHANGED