Skip to content

Commit 87bbfc6

Browse files
EliEli
authored andcommitted
caching for stacked_dem_fill.py , bug fix on date formats for model_time.py, accelerate laplace_smooth_data.py, optimized raster_to_nodes.py
1 parent 14f66c5 commit 87bbfc6

3 files changed

Lines changed: 348 additions & 40 deletions

File tree

schimpy/laplace_smooth_data.py

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import numpy as np
2+
import numba
23

3-
4-
def laplace_smooth_data2(mesh,data,kappa=0.05,dt=1.0,iter_total=150):
4+
def laplace_smooth_data2_slow(mesh,data,kappa=0.05,dt=1.0,iter_total=150):
55
"""
66
Apply Laplacian smoothing to a scalar field on a mesh using explicit diffusion.
7+
8+
DEPRECATED. Use the version without _slow in the name
79
810
This function performs iterative smoothing by updating each node's value
911
toward the average of its neighbors. It corresponds to forward Euler
@@ -48,6 +50,38 @@ def laplace_smooth_data2(mesh,data,kappa=0.05,dt=1.0,iter_total=150):
4850
return smooth_data
4951

5052

53+
54+
def laplace_smooth_data2(mesh,data,kappa=0.05,dt=1.0,iter_total=150):
55+
"""
56+
Apply Laplacian smoothing to a scalar field on a mesh using explicit diffusion.
57+
58+
This function performs iterative smoothing by updating each node's value
59+
toward the average of its neighbors. It corresponds to forward Euler
60+
integration of the diffusion equation.
61+
62+
Parameters
63+
----------
64+
mesh : object
65+
A mesh object with a `nodes` array and a method `get_neighbor_nodes(ndx)`
66+
returning neighbor node indices for a given node.
67+
data : ndarray of shape (N,)
68+
Initial scalar field values at each mesh node.
69+
kappa : float, optional
70+
Diffusivity coefficient. Controls the rate of smoothing. Default is 0.05.
71+
dt : float, optional
72+
Timestep used for each smoothing iteration. Default is 1.0.
73+
iter_total : int, optional
74+
Number of smoothing iterations to perform. Default is 150.
75+
76+
Returns
77+
-------
78+
smooth_data : ndarray of shape (N,)
79+
The smoothed scalar field after `iter_total` iterations.
80+
"""
81+
n_nodes = mesh.nodes.shape[0]
82+
neighbors = [np.array(mesh.get_neighbor_nodes(i), dtype=np.int32) for i in range(n_nodes)]
83+
84+
return smooth_kernel_numba(data.astype(np.float64), neighbors, kappa, dt, iter_total)
5185

5286

5387
def laplace_smooth_with_vel2(mesh,data,vel,kappa=0.05,dt=1.,iter_total=1):
@@ -152,3 +186,32 @@ def laplace_smooth_with_vel3(mesh,nlayer,data,vel,kappa=0.05,dt=1.,iter_total=1)
152186
iter += 1
153187
smooth_data = zz
154188
return smooth_data
189+
190+
191+
192+
193+
@numba.njit
194+
def smooth_kernel_numba(data, neighbors, kappa, dt, iter_total):
195+
n_nodes = len(data)
196+
result = data.copy()
197+
for _ in range(iter_total):
198+
tmp = result.copy()
199+
for i in range(n_nodes):
200+
nbs = neighbors[i]
201+
if len(nbs) == 0:
202+
continue
203+
v_ave = 0.0
204+
for j in nbs:
205+
v_ave += result[j]
206+
v_ave /= len(nbs)
207+
tmp[i] = result[i] + dt * kappa * (v_ave - result[i])
208+
result = tmp
209+
return result
210+
211+
212+
213+
214+
215+
216+
217+

schimpy/model_time.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,14 @@ def file_to_elapsed(infile, start, outpath=None, annotate=False, skip_nan=False)
263263
outfile = sys.stdout
264264
else:
265265
outfile = open(outpath, "w")
266+
267+
268+
266269
with open(infile, "r") as thfile:
267270
prev_use = False
268271
prev_outline = None
269272
no_record = True
273+
<<<<<<< HEAD
270274
for iline, line in enumerate(thfile):
271275
if (
272276
line
@@ -277,6 +281,12 @@ def file_to_elapsed(infile, start, outpath=None, annotate=False, skip_nan=False)
277281
or line.startswith("time")
278282
)
279283
):
284+
=======
285+
286+
287+
for iline,line in enumerate(thfile):
288+
if line and len(line) > 1 and not (line.startswith("#") or line.startswith("date") or line.startswith("time")):
289+
>>>>>>> ad36b55 (caching for stacked_dem_fill.py , bug fix on date formats for model_time.py, accelerate laplace_smooth_data.py, optimized raster_to_nodes.py)
280290
splitline = line.split()
281291
if skip_nan and splitline[-1] == "nan":
282292
continue
@@ -285,7 +295,8 @@ def file_to_elapsed(infile, start, outpath=None, annotate=False, skip_nan=False)
285295
timestr = splitline[0]
286296
if len(timestr) == 10:
287297
# Only got the date,not the time
288-
timestr += " %s" % splitline[1]
298+
#timestr += " %s" % splitline[1]
299+
pass
289300
use = True
290301
try:
291302
mdtm = datetime.datetime(

0 commit comments

Comments
 (0)