polyhedral-gravity 3.4__cp315-cp315-win_arm64.whl

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.
@@ -0,0 +1,13 @@
1
+ """""" # start delvewheel patch
2
+ def _delvewheel_patch_1_13_1():
3
+ import os
4
+ if os.path.isdir(libs_dir := os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, 'polyhedral_gravity.libs'))):
5
+ os.add_dll_directory(libs_dir)
6
+
7
+
8
+ _delvewheel_patch_1_13_1()
9
+ del _delvewheel_patch_1_13_1
10
+ # end delvewheel patch
11
+
12
+ from ._core import *
13
+ from ._core import __version__, __parallelization__, __commit__, __logging__
@@ -0,0 +1,252 @@
1
+ """
2
+ Differentiable polyhedral gravity model - PyTorch interface.
3
+
4
+ Implements the Tsoulis (2012) line-integral formula in pure PyTorch, making
5
+ the gravitational potential and acceleration differentiable with respect to
6
+ vertex positions and density. Supports CPU and CUDA execution.
7
+ """
8
+
9
+ try:
10
+ import torch
11
+ from torch import Tensor
12
+ except ImportError as _err:
13
+ raise ImportError(
14
+ "polyhedral_gravity.torch requires PyTorch. Install it with: pip install torch"
15
+ ) from _err
16
+
17
+ import math
18
+
19
+ G_SI: float = 6.67430e-11 # m^3 kg^-1 s^-2
20
+
21
+
22
+ def evaluate(
23
+ vertices: Tensor,
24
+ faces: Tensor,
25
+ density: float,
26
+ computation_points: Tensor,
27
+ gravitational_constant: float = G_SI,
28
+ ) -> tuple[Tensor, Tensor, Tensor]:
29
+ """
30
+ Gravitational potential, acceleration, and gradient tensor for a polyhedron,
31
+ differentiable w.r.t. vertex positions and density.
32
+
33
+ Args:
34
+ vertices: (N, 3) vertex positions [m].
35
+ faces: (F, 3) triangle vertex indices, integer dtype.
36
+ density: Constant density [kg/m^3].
37
+ computation_points: (Q, 3) evaluation positions [m].
38
+ gravitational_constant: Gravitational constant, defaults to 6.67430e-11.
39
+
40
+ Returns:
41
+ potential: (Q,) gravitational potential [m^2/s^2].
42
+ acceleration: (Q, 3) gravitational acceleration [m/s^2].
43
+ tensor: (Q, 6) gravity gradient tensor [1/s^2],
44
+ components ordered as [Vxx, Vyy, Vzz, Vxy, Vxz, Vyz].
45
+ """
46
+
47
+ eps = 1e-30
48
+
49
+ # face vertex coords: (F, 3, 3)
50
+ face_vertices = vertices[faces] # [f, vertex_idx, xyz]
51
+
52
+ # shift so each query point is at the origin: (Q, F, 3, 3)
53
+ face_vertices_rel = face_vertices[None] - computation_points[:, None, None, :]
54
+
55
+ v0 = face_vertices_rel[:, :, 0] # (Q, F, 3)
56
+ v1 = face_vertices_rel[:, :, 1]
57
+ v2 = face_vertices_rel[:, :, 2]
58
+
59
+ # ------------------------------------------------------------------ #
60
+ # Edge vectors G_pq and outward unit normal (face_normal)
61
+ # ------------------------------------------------------------------ #
62
+ edge0 = v1 - v0 # (Q, F, 3)
63
+ edge1 = v2 - v1
64
+ edge2 = v0 - v2
65
+
66
+ edges = torch.stack([edge0, edge1, edge2], dim=2) # (Q, F, 3, 3) [q,f,seg,xyz]
67
+ segment_start_points = torch.stack(
68
+ [v0, v1, v2], dim=2
69
+ ) # (Q, F, 3, 3) segment start vertices
70
+
71
+ cross_01 = torch.linalg.cross(edge0, edge1) # (Q, F, 3)
72
+ face_normal = cross_01 / cross_01.norm(dim=-1, keepdim=True).clamp(min=eps)
73
+
74
+ # ------------------------------------------------------------------ #
75
+ # Segment unit normals n_pq = (G_pq x face_normal) / |...|
76
+ # ------------------------------------------------------------------ #
77
+ face_normal_expanded = face_normal[:, :, None].expand_as(edges) # (Q, F, 3, 3)
78
+ n_pq = torch.linalg.cross(edges, face_normal_expanded)
79
+ n_pq = n_pq / n_pq.norm(dim=-1, keepdim=True).clamp(min=eps)
80
+
81
+ # ------------------------------------------------------------------ #
82
+ # Signed plane distance, plane normal orientation, projection P'
83
+ # ------------------------------------------------------------------ #
84
+ hp_signed = (face_normal * v0).sum(dim=-1) # (Q, F) = N_p . v0
85
+ h_p = hp_signed.abs()
86
+ sigma_p = hp_signed.sign() # sigma_p
87
+
88
+ plane_projection = (
89
+ hp_signed[..., None] * face_normal
90
+ ) # (Q, F, 3) P' = hp_signed * face_normal
91
+
92
+ # ------------------------------------------------------------------ #
93
+ # Segment normal orientations sigma_pq and edge distances h_pq
94
+ #
95
+ # h_pq_signed = -(n_pq . (P' - v_start)) is the signed distance from
96
+ # P' to the edge line within the face plane. Deriving both sigma_pq
97
+ # and h_pq from this single expression keeps d(n_pq)/d(v) in the
98
+ # gradient graph; the equivalent norm-based formula (||P''-P'||) shares
99
+ # the same value but its graph misses that contribution.
100
+ # ------------------------------------------------------------------ #
101
+ plane_projection_expanded = plane_projection[:, :, None].expand_as(
102
+ segment_start_points
103
+ ) # (Q, F, 3, 3)
104
+ h_pq_raw = -(n_pq * (plane_projection_expanded - segment_start_points)).sum(
105
+ dim=-1
106
+ ) # (Q, F, 3)
107
+
108
+ # Threshold relative to edge length: floating-point noise in
109
+ # normalised-vector arithmetic can produce |h_pq_raw| / edge_scale ~ 1e-16
110
+ # for algebraically-zero distances. Zeroing these before .sign() prevents
111
+ # misclassifying the singularity case (all_inside vs on_edge/on_vertex),
112
+ # which would corrupt singularity_scalar by 2-4x.
113
+ edge_length = edges.norm(dim=-1) # (Q, F, 3)
114
+ edge_scale = edge_length.amax(dim=-1, keepdim=True) # (Q, F, 1)
115
+ h_pq_signed = torch.where(
116
+ h_pq_raw.abs() < 1e-10 * edge_scale,
117
+ torch.zeros_like(h_pq_raw),
118
+ h_pq_raw,
119
+ )
120
+ sigma_pq = h_pq_signed.sign() # (Q, F, 3)
121
+ h_pq = h_pq_signed.abs() # (Q, F, 3)
122
+
123
+ # ------------------------------------------------------------------ #
124
+ # Projection parameter t along each edge (needed for s1, s2)
125
+ # ------------------------------------------------------------------ #
126
+ edge_length_sq = (edges * edges).sum(dim=-1).clamp(min=eps) # (Q, F, 3)
127
+ t_pq = ((plane_projection_expanded - segment_start_points) * edges).sum(
128
+ dim=-1
129
+ ) / edge_length_sq # (Q, F, 3)
130
+
131
+ # ------------------------------------------------------------------ #
132
+ # Distances from P (origin) to segment endpoints
133
+ # ------------------------------------------------------------------ #
134
+ segment_end_points = torch.roll(
135
+ segment_start_points, -1, dims=2
136
+ ) # v_end = v_{(q+1)%3}
137
+ l1 = segment_start_points.norm(dim=-1) # (Q, F, 3) |v_start|
138
+ l2 = segment_end_points.norm(dim=-1) # (Q, F, 3) |v_end|
139
+
140
+ # signed 1-D distances along the edge: s1 = -t*|G|, s2 = (1-t)*|G|
141
+ s1 = -t_pq * edge_length
142
+ s2 = (1.0 - t_pq) * edge_length
143
+
144
+ # ------------------------------------------------------------------ #
145
+ # Transcendental expressions LN_pq and AN_pq (Tsoulis Eqs 14-15)
146
+ # ------------------------------------------------------------------ #
147
+ ln_num = (s2 + l2).clamp(min=eps)
148
+ ln_den = (s1 + l1).clamp(min=eps)
149
+ log_term = torch.log(ln_num / ln_den)
150
+
151
+ h_p_e = h_p[:, :, None].expand_as(h_pq) # (Q, F, 3)
152
+ degenerate = (h_p_e < 1e-15) | (h_pq < 1e-15)
153
+ arctan_term = torch.atan(h_p_e * s2 / (h_pq * l2).clamp(min=eps)) - torch.atan(
154
+ h_p_e * s1 / (h_pq * l1).clamp(min=eps)
155
+ )
156
+ arctan_term = torch.where(degenerate, torch.zeros_like(arctan_term), arctan_term)
157
+
158
+ # ------------------------------------------------------------------ #
159
+ # Per-face sums S1, S2, singularity correction (Tsoulis Eqs 11-12)
160
+ # ------------------------------------------------------------------ #
161
+ sum1 = (h_pq_signed * log_term).sum(dim=-1) # (Q, F)
162
+ sum2 = (sigma_pq * arctan_term).sum(dim=-1) # (Q, F)
163
+ sum1_tensor = (n_pq * log_term[..., None]).sum(
164
+ dim=-2
165
+ ) # (Q, F, 3) for gradient tensor
166
+
167
+ # Singularity correction - three cases detected from the sigma_pq pattern:
168
+ # all +1 -> P' strictly inside face -> -2*pi*h_p
169
+ # two +1, one 0 -> P' on a face edge -> -pi*h_p
170
+ # one +1, two 0 -> P' at a face vertex -> -theta_v*h_p
171
+ n_pos = (sigma_pq > 0).sum(dim=-1) # (Q, F)
172
+ n_zero = (sigma_pq == 0).sum(dim=-1) # (Q, F)
173
+
174
+ all_inside = n_pos == 3
175
+ on_edge = (n_pos == 2) & (n_zero == 1)
176
+ on_vertex = (n_pos == 1) & (n_zero == 2)
177
+
178
+ edge_unit_vector = edges / edge_length[..., None].clamp(min=eps) # (Q, F, 3, 3)
179
+ cos_t0 = -(edge_unit_vector[..., 0, :] * edge_unit_vector[..., 2, :]).sum(
180
+ dim=-1
181
+ ) # (Q, F)
182
+ cos_t1 = -(edge_unit_vector[..., 1, :] * edge_unit_vector[..., 0, :]).sum(dim=-1)
183
+ cos_t2 = -(edge_unit_vector[..., 2, :] * edge_unit_vector[..., 1, :]).sum(dim=-1)
184
+ theta_0 = torch.acos(cos_t0.clamp(-1.0, 1.0))
185
+ theta_1 = torch.acos(cos_t1.clamp(-1.0, 1.0))
186
+ theta_2 = torch.acos(cos_t2.clamp(-1.0, 1.0))
187
+ theta_v = (
188
+ theta_2 * (sigma_pq[..., 0] > 0).to(h_p.dtype)
189
+ + theta_0 * (sigma_pq[..., 1] > 0).to(h_p.dtype)
190
+ + theta_1 * (sigma_pq[..., 2] > 0).to(h_p.dtype)
191
+ ) # (Q, F)
192
+
193
+ singularity_scalar = torch.where(
194
+ all_inside,
195
+ -2.0 * math.pi * h_p,
196
+ torch.where(
197
+ on_edge,
198
+ -math.pi * h_p,
199
+ torch.where(on_vertex, -theta_v * h_p, torch.zeros_like(h_p)),
200
+ ),
201
+ )
202
+
203
+ singularity_vector = torch.where(
204
+ all_inside[..., None],
205
+ -2.0 * math.pi * sigma_p[..., None] * face_normal,
206
+ torch.where(
207
+ on_edge[..., None],
208
+ -math.pi * sigma_p[..., None] * face_normal,
209
+ torch.where(
210
+ on_vertex[..., None],
211
+ -theta_v[..., None] * sigma_p[..., None] * face_normal,
212
+ torch.zeros_like(face_normal),
213
+ ),
214
+ ),
215
+ ) # (Q, F, 3)
216
+
217
+ face_sum = sum1 + h_p * sum2 + singularity_scalar # (Q, F) per-face scalar sum
218
+
219
+ # ------------------------------------------------------------------ #
220
+ # Sum over faces -> potential and acceleration
221
+ # ------------------------------------------------------------------ #
222
+ potential_terms = sigma_p * h_p * face_sum # (Q, F)
223
+ g_contributions = face_normal * face_sum[..., None] # (Q, F, 3)
224
+
225
+ potential = (gravitational_constant * density / 2.0) * potential_terms.sum(dim=-1)
226
+ acceleration = -gravitational_constant * density * g_contributions.sum(dim=-2)
227
+
228
+ # ------------------------------------------------------------------ #
229
+ # Gradient tensor (Tsoulis Eq. 13)
230
+ # ------------------------------------------------------------------ #
231
+ tensor_subterm = (
232
+ sum1_tensor + face_normal * (sigma_p * sum2)[..., None] + singularity_vector
233
+ ) # (Q, F, 3)
234
+
235
+ # diagonal components: Vxx, Vyy, Vzz -> face_normal[i] * tensor_subterm[i]
236
+ diag = face_normal * tensor_subterm # (Q, F, 3)
237
+ # off-diagonal: Vxy=N[0]*sub[1], Vxz=N[0]*sub[2], Vyz=N[1]*sub[2]
238
+ offdiag_normal = torch.stack(
239
+ [face_normal[..., 0], face_normal[..., 0], face_normal[..., 1]], dim=-1
240
+ )
241
+ offdiag_subterm = torch.stack(
242
+ [tensor_subterm[..., 1], tensor_subterm[..., 2], tensor_subterm[..., 2]], dim=-1
243
+ )
244
+ offdiag = offdiag_normal * offdiag_subterm # (Q, F, 3)
245
+
246
+ tensor = (
247
+ gravitational_constant
248
+ * density
249
+ * torch.cat([diag, offdiag], dim=-1).sum(dim=-2)
250
+ ) # (Q, 6)
251
+
252
+ return potential, acceleration, tensor
@@ -0,0 +1,2 @@
1
+ Version: 1.13.1
2
+ Arguments: ['C:\\Users\\runneradmin\\AppData\\Local\\Temp\\cibw-run-kvvg6l26\\cp315-win_arm64\\build\\venv\\Scripts\\delvewheel', 'repair', '-w', 'C:\\Users\\runneradmin\\AppData\\Local\\Temp\\cibw-run-kvvg6l26\\cp315-win_arm64\\repaired_wheel', '-v', 'C:\\Users\\runneradmin\\AppData\\Local\\Temp\\cibw-run-kvvg6l26\\cp315-win_arm64\\built_wheel\\polyhedral_gravity-3.4-cp315-cp315-win_arm64.whl']