1111from LoopStructural .utils import getLogger
1212
1313logger = getLogger (__name__ )
14+
15+
1416def compute_weighting (grid_points , gradient_constraint_points , alpha = 10.0 , sigma = 1.0 ):
1517 """
1618 Compute weights for second derivative regularization based on proximity to gradient constraints.
@@ -29,12 +31,13 @@ def compute_weighting(grid_points, gradient_constraint_points, alpha=10.0, sigma
2931
3032 # Find the distance from each grid point to the nearest gradient constraint
3133 distances , _ = tree .query (grid_points , k = 1 )
32-
34+
3335 # Compute weighting function (higher weight for nearby points)
3436 weights = 1 + alpha * np .exp (- (distances ** 2 ) / (2 * sigma ** 2 ))
3537
3638 return weights
3739
40+
3841class FiniteDifferenceInterpolator (DiscreteInterpolator ):
3942 def __init__ (self , grid , data = {}):
4043 """
@@ -67,6 +70,7 @@ def __init__(self, grid, data={}):
6770
6871 self .type = InterpolatorType .FINITE_DIFFERENCE
6972 self .use_regularisation_weight_scale = False
73+
7074 def setup_interpolator (self , ** kwargs ):
7175 """
7276
@@ -294,11 +298,11 @@ def add_gradient_constraints(self, w=1.0):
294298 self .add_constraints_to_least_squares (A , B , idc [inside , :], w = w , name = "gradient" )
295299 A = np .einsum ("ij,ijk->ik" , dip_vector .T , T )
296300 self .add_constraints_to_least_squares (A , B , idc [inside , :], w = w , name = "gradient" )
297- self .regularisation_scale += compute_weighting (
298- self .support .nodes ,
299- points [inside , : self .support .dimension ],
300- sigma = self .support .nsteps [0 ] * 10 ,
301- )
301+ self .regularisation_scale += compute_weighting (
302+ self .support .nodes ,
303+ points [inside , : self .support .dimension ],
304+ sigma = self .support .nsteps [0 ] * 10 ,
305+ )
302306 if np .sum (inside ) <= 0 :
303307 logger .warning (
304308 f" { np .sum (~ inside )} \
@@ -356,7 +360,7 @@ def add_norm_constraints(self, w=1.0):
356360 # indexes = self.support.global_node_indices(indexes)
357361 # self.regularisation_scale[indexes] =10
358362
359- self .regularisation_scale += compute_weighting (
363+ self .regularisation_scale += compute_weighting (
360364 self .support .nodes ,
361365 points [inside , : self .support .dimension ],
362366 sigma = self .support .nsteps [0 ] * 10 ,
@@ -499,7 +503,11 @@ def assemble_inner(self, operator, w, name='regularisation'):
499503 a [inside , :],
500504 B [inside ],
501505 idc [inside , :],
502- w = self .regularisation_scale [idc [inside , 13 ].astype (int )] * w if self .use_regularisation_weight_scale else w ,
506+ w = (
507+ self .regularisation_scale [idc [inside , 13 ].astype (int )] * w
508+ if self .use_regularisation_weight_scale
509+ else w
510+ ),
503511 name = name ,
504512 )
505513 return
0 commit comments