Skip to content

Commit e2d54d6

Browse files
committed
ScatteredInterpolation gives the wrong answer, so replaced that with distance point/line
1 parent 1864854 commit e2d54d6

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

Project.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ MeshIO = "7269a6da-0436-5bbc-96c2-40638cbb6118"
2222
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
2323
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
2424
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
25-
ScatteredInterpolation = "3f865c0f-6dca-5f4d-999b-29fe1e7e3c92"
2625
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
2726
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2827
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
@@ -54,7 +53,6 @@ JLD2 = "0.4"
5453
MeshIO = "0.1 - 0.4"
5554
NearestNeighbors = "0.2 - 0.4"
5655
Parameters = "0.9 - 0.12"
57-
ScatteredInterpolation = "0.3"
5856
SpecialFunctions = "1.0, 2"
5957
StaticArrays = "1"
6058
WriteVTK = "1"

src/Setup_geometry.jl

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ using Printf
33
using Parameters # helps setting default parameters in structures
44
using SpecialFunctions: erfc
55
using GeoParams
6-
using ScatteredInterpolation
76
using StaticArrays
87

98
# Setup_geometry
@@ -1475,7 +1474,7 @@ function find_slab_distance!(ls, d, X,Y,Z, Top, Bottom, trench::Trench)
14751474
xb = Rot3D(End[1]-Start[1],End[2]-Start[2], 0.0, cosd(StrikeAngle), sind(StrikeAngle), 1.0, 0.0)
14761475

14771476
# dl
1478-
dl = Length/n_seg;
1477+
dl = trench.Length/n_seg;
14791478
l = 0 # length at the trench position
14801479
D = @SVector [Top[1,2], Bottom[1,2], Bottom[1,2],Top[1,2] ]
14811480

@@ -1510,20 +1509,11 @@ function find_slab_distance!(ls, d, X,Y,Z, Top, Bottom, trench::Trench)
15101509
# indexes of the segment
15111510
ind_seg = ind_s[ind]
15121511

1513-
# Prepare the variable to interpolate {I put here because to allow also a variation of thickness of the slab}
1514-
L = @SVector [l,l,ln,ln];
1515-
1516-
# Interpolations
1517-
points = @SMatrix [pa[1] pb[1] pc[1] pd[1]; pa[2] pb[2] pc[2] pd[2]]
1518-
1519-
itp1 = ScatteredInterpolation.interpolate(Shepard(), points, D);
1520-
itp2 = ScatteredInterpolation.interpolate(Shepard(), points, L);
1521-
15221512
# Loop over the chosen particles and interpolate the current value of L and D.
15231513
for ip in ind_seg
1524-
point_ = [Yrot[ip], Z[ip]];
1525-
d[ip] = ScatteredInterpolation.evaluate(itp1,point_)[1];
1526-
ls[ip] = ScatteredInterpolation.evaluate(itp2,point_)[1];
1514+
point_ = (Yrot[ip], Z[ip]);
1515+
d[ip] = -distance_to_linesegment(point_, pa, pd)
1516+
ls[ip] = distance_to_linesegment(point_, pb, pa) + l
15271517
end
15281518

15291519
#Update l
@@ -1532,6 +1522,40 @@ function find_slab_distance!(ls, d, X,Y,Z, Top, Bottom, trench::Trench)
15321522
end
15331523

15341524

1525+
"""
1526+
distance_to_linesegment(p::NTuple{2,_T}, v::NTuple{2,_T}, w::NTuple{2,_T})
1527+
1528+
Computes the distance normal distance from a point `p` to a line segment defined by the points `v` and `w`.
1529+
"""
1530+
function distance_to_linesegment(p::NTuple{2,_T}, v::NTuple{2,_T}, w::NTuple{2,_T}) where _T<:Number
1531+
dx = w[1] - v[1]
1532+
dy = w[2] - v[2]
1533+
l2 = dx*dx + dy*dy # i.e. |w-v|^2 - avoid a sqrt
1534+
if l2 == 0.0
1535+
dx = p[1] - v[1]
1536+
dy = p[2] - v[2]
1537+
return sqrt(dx*dx + dy*dy) # v == w case
1538+
end
1539+
# Consider the line extending the segment, parameterized as v + t (w - v).
1540+
# We find projection of point p onto the line.
1541+
# It falls where t = [(p-v) . (w-v)] / |w-v|^2
1542+
t = ((p[1] - v[1])*dx + (p[2] - v[2])*dy) / l2
1543+
if t < 0.0
1544+
dx = p[1] - v[1]
1545+
dy = p[2] - v[2]
1546+
return sqrt(dx*dx + dy*dy) # Beyond the 'v' end of the segment
1547+
elseif t > 1.0
1548+
dx = p[1] - w[1]
1549+
dy = p[2] - w[2]
1550+
return sqrt(dx*dx + dy*dy) # Beyond the 'w' end of the segment
1551+
end
1552+
projection_x = v[1] + t * dx
1553+
projection_y = v[2] + t * dy
1554+
dx = p[1] - projection_x
1555+
dy = p[2] - projection_y
1556+
return sqrt(dx*dx + dy*dy)
1557+
end
1558+
15351559
"""
15361560
addSlab!(Phase, Temp, Grid::AbstractGeneralGrid, trench::Trench; phase = ConstantPhase(1), T = nothing)
15371561

test/test_setup_geometry.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ TsHC = HalfspaceCoolingTemp(Tsurface=20.0, Tmantle=1350, Age=30, Adiabat=0.4)
237237
temp = TsHC;
238238

239239
addSlab!(Phase,Temp,Cart, t1, phase=phase, T = TsHC)
240-
@test Temp[84,84,110] 1142.25814954244
240+
@test Temp[84,84,110] 1045.1322688510577
241241
@test extrema(Phase) == (1, 4)
242242

243243
# with weak zone
@@ -264,12 +264,10 @@ phase = LithosphericPhases(Layers=[5 7 88], Phases = [2 3 4], Tlab=nothing)
264264
t1 = Trench(Start = (400.0,400.0), End = (800.0,800.0),θ_max = 90.0, direction = 1.0, n_seg = 50, Length = 600.0, Thickness = 80.0, Lb = 500.0,d_decoupling = 100.0, type_bending =:Ribe, WeakzoneThickness=10, WeakzonePhase=9)
265265

266266
addSlab!(Phase,Temp,Cart, t1, phase=phase, T = T_slab)
267-
@test Temp[84,84,110] 718.8406936737412
267+
@test Temp[84,84,110] 624.6682008876219
268268

269269
Data_Final = CartData(X,Y,Z,(Phase=Phase,Temp=Temp))
270270

271-
272-
273271
# 2D slab:
274272
nx,nz = 512,128
275273
x = range(-1000,1000, nx);

0 commit comments

Comments
 (0)