pyviewfactor 0.1.14__tar.gz → 0.1.15__tar.gz

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.
Files changed (31) hide show
  1. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/PKG-INFO +1 -1
  2. pyviewfactor-0.1.15/pyviewfactor/__init__.py +34 -0
  3. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/pyviewfactor/_version.py +1 -1
  4. pyviewfactor-0.1.15/pyviewfactor/pvf_geometry_preprocess.py +249 -0
  5. pyviewfactor-0.1.15/pyviewfactor/pvf_integrators.py +283 -0
  6. pyviewfactor-0.1.15/pyviewfactor/pvf_plot.py +77 -0
  7. pyviewfactor-0.1.15/pyviewfactor/pvf_viewfactor_computations.py +320 -0
  8. pyviewfactor-0.1.15/pyviewfactor/pvf_visibility_obstruction.py +316 -0
  9. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/pyviewfactor.egg-info/PKG-INFO +1 -1
  10. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/pyviewfactor.egg-info/SOURCES.txt +5 -9
  11. pyviewfactor-0.1.14/pyviewfactor/11pyviewfactor.py +0 -1166
  12. pyviewfactor-0.1.14/pyviewfactor/OLD.py +0 -984
  13. pyviewfactor-0.1.14/pyviewfactor/__init__.py +0 -5
  14. pyviewfactor-0.1.14/pyviewfactor/pyviewfactor.py +0 -1205
  15. pyviewfactor-0.1.14/pyviewfactor/pyviewfactor_906.py +0 -1647
  16. pyviewfactor-0.1.14/pyviewfactor/pyviewfactor_BKP12-06.py +0 -1341
  17. pyviewfactor-0.1.14/pyviewfactor/pyviewfactor_integration.py +0 -1242
  18. pyviewfactor-0.1.14/pyviewfactor/pyviewfactor_v1.py +0 -1008
  19. pyviewfactor-0.1.14/pyviewfactor/pyviewfactor_vis_obs_OK.py +0 -1206
  20. pyviewfactor-0.1.14/pyviewfactor/pyviewfactorv2.py +0 -1719
  21. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/LICENSE.txt +0 -0
  22. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/README.md +0 -0
  23. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/pyviewfactor.egg-info/dependency_links.txt +0 -0
  24. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/pyviewfactor.egg-info/requires.txt +0 -0
  25. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/pyviewfactor.egg-info/top_level.txt +0 -0
  26. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/setup.cfg +0 -0
  27. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/setup.py +0 -0
  28. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/test/__init__.py +0 -0
  29. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/test/test_utils.py +0 -0
  30. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/test/test_view_factor.py +0 -0
  31. {pyviewfactor-0.1.14 → pyviewfactor-0.1.15}/test/test_visibility_obstruction.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pyviewfactor
3
- Version: 0.1.14
3
+ Version: 0.1.15
4
4
  Summary: A python library to calculate numerically exact radiation view factors between planar faces.
5
5
  Home-page: https://gitlab.com/arep-dev/pyViewFactor/
6
6
  Author: Mateusz BOGDAN, Edouard WALTHER, Marc ALECIAN, Mina CHAPON
@@ -0,0 +1,34 @@
1
+ """
2
+ .. include:: ../README.md
3
+ """
4
+ from ._version import __version__
5
+ from .pvf_geometry_preprocess import (
6
+ ProcessedGeometry,
7
+ FaceMeshPreprocessor,
8
+ fc_unstruc2poly,
9
+ face_to_array,
10
+ polygon_area,
11
+ polygon_centroid,
12
+ face_normal_numpy,
13
+ tri_overlaps_aabb
14
+ )
15
+ from .pvf_visibility_obstruction import (
16
+ get_visibility,
17
+ get_visibility_from_cache,
18
+ get_obstruction,
19
+ get_obstruction_from_cache
20
+ )
21
+
22
+ from .pvf_integrators import (
23
+ set_quadrature_order,
24
+ )
25
+
26
+ from .pvf_viewfactor_computations import (
27
+ compute_viewfactor,
28
+ batch_compute_viewfactors,
29
+ compute_viewfactor_matrix
30
+ )
31
+
32
+ from .pvf_plot import (
33
+ plot_viewfactor
34
+ )
@@ -1,5 +1,5 @@
1
1
  """Version info for pyViewFactor"""
2
- version_info = 0, 1, 14
2
+ version_info = 0, 1, 15
3
3
 
4
4
  # Nice string for the version
5
5
  __version__ = ".".join(map(str, version_info))
@@ -0,0 +1,249 @@
1
+ """
2
+ file: pvf_geometry.py
3
+ """
4
+
5
+ import numpy as np
6
+ import pyvista as pv
7
+ from numba import njit
8
+
9
+ def fc_unstruc2poly(mesh_unstruc):
10
+ """Convenience conversion function from UnstructuredGrid to PolyData
11
+
12
+ Parameters
13
+ ----------
14
+ * **mesh_unstruc** : *pyvista.UnstructuredGrid*
15
+
16
+ > Unstructured Pyvista Grid.
17
+
18
+ Returns
19
+ -------
20
+ * *pyvista.PolyData*
21
+
22
+ > The same mesh converted to a surface pyvista.PolyData.
23
+
24
+ Examples
25
+ --------
26
+ >>> import pyviewfactor as pvf
27
+ >>> import pyvista as pv
28
+ >>> sphere = pv.Sphere(radius=0.5, center=(0, 0, 0))
29
+ >>> subset = sphere.extract_cells(10)
30
+ >>> subsetPoly = fc_unstruc2poly(subset)
31
+ >>> subsetPoly
32
+ PolyData (0x1fdd9786040)
33
+ N Cells: 1
34
+ N Points: 3
35
+ X Bounds: -5.551e-17, 3.617e-02
36
+ Y Bounds: 0.000e+00, 4.682e-02
37
+ Z Bounds: -5.000e-01, -4.971e-01
38
+ N Arrays: 0
39
+
40
+ """
41
+
42
+ # Get the points and cells
43
+ points = mesh_unstruc.points
44
+ faces = mesh_unstruc.cells
45
+ # Return the same geometry as a pv.PolyData mesh
46
+ return pv.PolyData(points, faces)
47
+
48
+
49
+ def face_to_array(face):
50
+ """Extract and return the vertices of a PyVista PolyData face as ndarray."""
51
+ faces = face.faces
52
+ n_pts = int(faces[0])
53
+ idxs = faces[1:1 + n_pts]
54
+ return np.array(face.points[idxs], dtype=np.float64)
55
+
56
+
57
+ def polygon_area(pts):
58
+ # Compute normal
59
+ n = face_normal_numpy(pts)
60
+ # Use polygon centroid and cross product trick
61
+ area = 0.0
62
+ n_pts = pts.shape[0]
63
+ for i in range(n_pts):
64
+ v0 = pts[i]
65
+ v1 = pts[(i + 1) % n_pts]
66
+ area += np.dot(n, np.cross(v0, v1))
67
+ return 0.5 * np.abs(area)
68
+
69
+
70
+ def polygon_centroid(pts: np.ndarray) -> np.ndarray:
71
+ """
72
+ Compute the area centroid ( area-weighted centroid) of a planar polygon in 3D.
73
+
74
+ Uses fan triangulation around pts[0]. If polygon is degenerate,
75
+ returns the arithmetic mean of pts.
76
+
77
+ Parameters
78
+ ----------
79
+ pts : ndarray, shape (n,3)
80
+ Ordered vertex coordinates.
81
+
82
+ Returns
83
+ -------
84
+ ndarray, shape (3,)
85
+ Area centroid.
86
+ """
87
+ p0, p1, p2 = pts[0], pts[1], pts[2]
88
+ v1 = p1 - p0
89
+ v2 = p2 - p0
90
+ normal = np.cross(v1, v2)
91
+ normal /= np.linalg.norm(normal)
92
+ u = v1 / np.linalg.norm(v1)
93
+ w = np.cross(normal, u)
94
+ proj = np.array([[np.dot(p - p0, u), np.dot(p - p0, w)] for p in pts])
95
+ A = 0.0
96
+ Cx = 0.0
97
+ Cy = 0.0
98
+ N = proj.shape[0]
99
+ for i in range(N):
100
+ x0, y0 = proj[i]
101
+ x1, y1 = proj[(i + 1) % N]
102
+ cross = x0 * y1 - x1 * y0
103
+ A += cross
104
+ Cx += (x0 + x1) * cross
105
+ Cy += (y0 + y1) * cross
106
+ A *= 0.5
107
+ if np.abs(A) < 1e-12:
108
+ return np.mean(pts, axis=0)
109
+ centroid_2D = np.array([Cx / (6 * A), Cy / (6 * A)])
110
+ return p0 + centroid_2D[0] * u + centroid_2D[1] * w
111
+
112
+
113
+ @njit
114
+ def face_normal_numpy(pts):
115
+ """
116
+ Compute a unit normal for a planar polygon with vertex coords in pts (n×3).
117
+ Returns the *right-handed* normal. Handles n > 3.
118
+ If degenerate, returns [0,0,1].
119
+ """
120
+ n = pts.shape[0]
121
+ if n < 3:
122
+ return np.array([0.0, 0.0, 1.0])
123
+ # Newell's method (robust for convex planar polygons)
124
+ nx = 0.0
125
+ ny = 0.0
126
+ nz = 0.0
127
+ for i in range(n):
128
+ j = (i + 1) % n
129
+ nx += (pts[i,1] - pts[j,1]) * (pts[i,2] + pts[j,2])
130
+ ny += (pts[i,2] - pts[j,2]) * (pts[i,0] + pts[j,0])
131
+ nz += (pts[i,0] - pts[j,0]) * (pts[i,1] + pts[j,1])
132
+ norm = np.sqrt(nx*nx + ny*ny + nz*nz)
133
+ if norm < 1e-12:
134
+ return np.array([0.0, 0.0, 1.0])
135
+ return np.array([nx, ny, nz]) / norm
136
+
137
+
138
+ @njit
139
+ def tri_overlaps_aabb(tri_pts, aabb_min, aabb_max):
140
+ """
141
+ JIT‐compatible AABB overlap test without using axis keywords.
142
+ """
143
+ # initialize with the first vertex
144
+ tri_min0 = tri_pts[0, 0]
145
+ tri_min1 = tri_pts[0, 1]
146
+ tri_min2 = tri_pts[0, 2]
147
+ tri_max0 = tri_min0
148
+ tri_max1 = tri_min1
149
+ tri_max2 = tri_min2
150
+
151
+ # find true min/max over the three vertices
152
+ for i in range(1, tri_pts.shape[0]):
153
+ x, y, z = tri_pts[i, 0], tri_pts[i, 1], tri_pts[i, 2]
154
+ if x < tri_min0: tri_min0 = x
155
+ if y < tri_min1: tri_min1 = y
156
+ if z < tri_min2: tri_min2 = z
157
+ if x > tri_max0: tri_max0 = x
158
+ if y > tri_max1: tri_max1 = y
159
+ if z > tri_max2: tri_max2 = z
160
+
161
+ # now test overlap
162
+ return (
163
+ (tri_max0 >= aabb_min[0]) and (tri_min0 <= aabb_max[0]) and
164
+ (tri_max1 >= aabb_min[1]) and (tri_min1 <= aabb_max[1]) and
165
+ (tri_max2 >= aabb_min[2]) and (tri_min2 <= aabb_max[2])
166
+ )
167
+
168
+
169
+ class ProcessedGeometry:
170
+ """
171
+ A lightweight structure to cache face geometry (points, normals, centroids, area)
172
+ for high-performance view factor computation.
173
+ """
174
+ def __init__(self, mesh, rounding_decimal=8):
175
+ self.rounding_decimal = rounding_decimal
176
+ self.N = mesh.n_cells
177
+ self.points = []
178
+ self.normals = np.zeros((self.N, 3), dtype=np.float64)
179
+ self.centroids = np.zeros((self.N, 3), dtype=np.float64)
180
+ self.areas = np.zeros(self.N, dtype=np.float64)
181
+ self.sizes = np.zeros(self.N, dtype=np.float64)
182
+
183
+ for i in range(self.N):
184
+ raw = mesh.extract_cells(i)
185
+ face = fc_unstruc2poly(raw)
186
+ faces_i = face.faces
187
+ n_i = int(faces_i[0])
188
+ idxs = faces_i[1:1 + n_i]
189
+ pts = np.round(face.points[idxs], rounding_decimal)
190
+
191
+ self.points.append(pts)
192
+ self.centroids[i] = polygon_centroid(pts)
193
+
194
+ v1 = pts[1] - pts[0]
195
+ v2 = pts[2] - pts[0]
196
+ norm = np.cross(v1, v2)
197
+ self.normals[i] = norm / np.linalg.norm(norm)
198
+
199
+ self.areas[i] = polygon_area(np.round(face.points, rounding_decimal))
200
+ edges = np.roll(pts, -1, axis=0) - pts
201
+ edge_lengths = np.linalg.norm(edges, axis=1)
202
+ self.sizes[i] = np.mean(edge_lengths)
203
+
204
+ def get_face(self, i):
205
+ return self.points[i]
206
+
207
+ def get_normal(self, i):
208
+ return self.normals[i]
209
+
210
+ def get_centroid(self, i):
211
+ return self.centroids[i]
212
+
213
+ def get_area(self, i):
214
+ return self.areas[i]
215
+
216
+ def get_size(self, i):
217
+ return self.sizes[i]
218
+
219
+
220
+ class FaceMeshPreprocessor:
221
+ def __init__(self, mesh: pv.PolyData):
222
+ if not mesh.is_all_triangles:
223
+ mesh = mesh.triangulate()
224
+ self.points = mesh.points.astype(np.float64)
225
+ # cells array is [n, i0, i1, i2, n, ...]
226
+ cells = mesh.faces.reshape(-1,4)[:,1:]
227
+ self.faces = cells # shape (n_cells,3)
228
+ self.triangles = np.array([self.points[c] for c in cells], dtype=np.float64)
229
+
230
+ def aabb_filter(self, aabb_min, aabb_max):
231
+ kept = []
232
+ for tri in self.triangles: # tri.shape == (3,3)
233
+ if tri_overlaps_aabb(tri, aabb_min, aabb_max):
234
+ kept.append(tri)
235
+ return np.array(kept, dtype=np.float64)
236
+
237
+ def exclude_exact_match(self, pts1, pts2, tris):
238
+ # build frozensets of each vertex array
239
+ vs1 = frozenset(sorted(tuple(np.round(p,8)) for p in pts1))
240
+ vs2 = frozenset(sorted(tuple(np.round(p,8)) for p in pts2))
241
+ out = []
242
+ for tri in tris:
243
+ tri_set = frozenset(sorted(tuple(np.round(p,8)) for p in tri))
244
+ if not tri_set.isdisjoint(vs1) or not tri_set.isdisjoint(vs2):
245
+ continue
246
+ out.append(tri)
247
+ return np.array(out, dtype=np.float64)
248
+
249
+
@@ -0,0 +1,283 @@
1
+ """
2
+ file: pvf_integrators.py
3
+ """
4
+
5
+ import numpy as np
6
+ from numpy.polynomial.legendre import leggauss
7
+ import scipy.integrate
8
+ from numba import njit
9
+
10
+ # #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
11
+ # SCIPY DBLQUAD INTEGRATION
12
+
13
+ @njit
14
+ def _integrand_dblquad(x, y, norm_q_carree, norm_p_carree, scal_qpq,
15
+ scal_qpp, scal_pq, norm_qp_carree):
16
+ """
17
+ Represents the logarithmic integrand of the contour form‐factor integral
18
+ for one edge‐pair between two facets.
19
+
20
+ Used in the *compute_viewfactor* function.
21
+
22
+ Core integrand function for SciPy dblquad contour integration.
23
+
24
+ Parameters
25
+ ----------
26
+ x, y : float
27
+ Quadrature parameters in [0,1].
28
+ norm_q_carree : float
29
+ Squared length of one directed edge vector (facet1).
30
+ norm_p_carree : float
31
+ Squared length of the other directed edge vector (facet2).
32
+ scal_qpq : float
33
+ Dot(edge1, edge2) term coupling the two edges in the log.
34
+ scal_qpp : float
35
+ Dot(edge2, vector between vertices).
36
+ scal_pq : float
37
+ Dot(edge1, vector between vertices); also multiplies the log.
38
+ norm_qp_carree : float
39
+ Squared length of the inter‐vertex vector.
40
+
41
+ Returns
42
+ -------
43
+ float
44
+ V
45
+
46
+ """
47
+ integrand_function = np.log(y**2 * norm_q_carree
48
+ + x**2 * norm_p_carree
49
+ - 2 * y * scal_qpq
50
+ + 2 * x * scal_qpp
51
+ - 2 * x * y * scal_pq
52
+ + norm_qp_carree
53
+ ) * scal_pq
54
+ return integrand_function
55
+
56
+
57
+ def _compute_viewfactor_dblquad(pts1, pts2, constante):
58
+ """
59
+ Robust fallback integrator using SciPy’s dblquad for contour integration.
60
+
61
+ This method loops over every pair of directed edges from two facets
62
+ and evaluates the exact contour integral via `scipy.integrate.dblquad`.
63
+
64
+ It is used only in cases of shared edges or vertices
65
+
66
+ Parameters
67
+ ----------
68
+ pts1 : ndarray, shape (n1, 3)
69
+ Rounded vertex coordinates of the *receiving* facet.
70
+ pts2 : ndarray, shape (n2, 3)
71
+ Rounded (and possibly epsilon-shifted) vertex coordinates of the
72
+ *emitting* facet.
73
+ constante : float
74
+ Normalization constant = 4π × area of the emitting facet.
75
+
76
+ Returns
77
+ -------
78
+ float
79
+ The computed view factor from emitting→receiving (non-negative).
80
+
81
+ Notes
82
+ -----
83
+ If the raw integral comes out negative due to numerical noise,
84
+ it is clamped to zero.
85
+ """
86
+ total = 0.0
87
+ n1, n2 = len(pts1), len(pts2)
88
+ v1 = np.roll(pts1, -1, axis=0) - pts1
89
+ v2 = np.roll(pts2, -1, axis=0) - pts2
90
+ norm1 = np.sum(v1 * v1, axis=1)
91
+ norm2 = np.sum(v2 * v2, axis=1)
92
+ scal = v1 @ v2.T
93
+
94
+ for i in range(n1):
95
+ nq = norm1[i]
96
+ e1 = v1[i]
97
+ for j in range(n2):
98
+ np_ = norm2[j]
99
+ e2 = v2[j]
100
+ d = pts2[j] - pts1[i]
101
+ nqp = np.dot(d, d)
102
+ sqpq = np.dot(d, e1)
103
+ sqpp = np.dot(d, e2)
104
+ spq = scal[i, j]
105
+ val, _ = scipy.integrate.dblquad(
106
+ _integrand_dblquad,
107
+ 0.0, 1.0,
108
+ lambda xx: 0.0, lambda xx: 1.0,
109
+ args=(nq, np_, sqpq, sqpp, spq, nqp)
110
+ )
111
+ total += val
112
+ vf = total / constante
113
+ if vf > 0:
114
+ return vf
115
+ else:
116
+ return 0.0
117
+
118
+
119
+ # #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
120
+ # GAUSS LEGENDRE INTEGRATION
121
+
122
+ # Default settings.
123
+ # Precompute 30×30 nodes & weights once
124
+ _GL_ORDER = 30
125
+ _nodes, _weights = leggauss(_GL_ORDER)
126
+ # map from [-1,1] → [0,1]
127
+ _GL_X = 0.5 * (_nodes + 1.0)
128
+ _GL_W = 0.5 * _weights
129
+
130
+
131
+ def set_quadrature_order(order: int):
132
+ """
133
+ Change the fixed‐order Gauss–Legendre quadrature rule at runtime.
134
+ Must be called **before** any `compute_viewfactor()` calls.
135
+
136
+ The default value before calling this function is `_GL_ORDER=30`.
137
+
138
+ If modified during a session, `pvf` needs to be reloaded:
139
+ ```python
140
+ import importlib
141
+ importlib.reload(pvf)
142
+ ```
143
+
144
+ Parameters
145
+ ----------
146
+ * **order**: *int*
147
+
148
+ > The order of Gauss–Legendre integration
149
+
150
+ Returns
151
+ -------
152
+ * *Nothing*
153
+
154
+ > Changes internal variables.
155
+
156
+ """
157
+ global _GL_ORDER, _GL_X, _GL_W
158
+ from numpy.polynomial.legendre import leggauss
159
+ _GL_ORDER = order
160
+ nodes, weights = leggauss(order)
161
+ _GL_X = 0.5 * (nodes + 1.0)
162
+ _GL_W = 0.5 * weights
163
+
164
+
165
+
166
+ @njit
167
+ def _integrand_gauss_legendre(norm_q, norm_p, scal_qpq, scal_qpp, scal_pq, norm_qp):
168
+ """
169
+ Fixed‐order Gauss–Legendre quadrature kernel for one edge‐pair.
170
+
171
+ Internally used by `_compute_viewfactor_gauss_legendre` to sum
172
+ over a 10×10 tensor product of nodes/weights on [0,1]×[0,1].
173
+
174
+ Parameters
175
+ ----------
176
+ norm_q : float
177
+ Squared length of the directed edge from the receiving facet.
178
+ norm_p : float
179
+ Squared length of the directed edge from the emitting facet.
180
+ scal_qpq : float
181
+ Dot(edge_q, vector between vertices).
182
+ scal_qpp : float
183
+ Dot(edge_p, vector between vertices).
184
+ scal_pq : float
185
+ Dot(edge_q, edge_p).
186
+ norm_qp : float
187
+ Squared length of the vector between a vertex on facet1 and a vertex on facet2.
188
+
189
+ Returns
190
+ -------
191
+ float
192
+ Contribution of this edge‐pair to the contour integral,
193
+ already multiplied by the Gauss–Legendre weights.
194
+ """
195
+ total = 0.0
196
+ for i in range(_GL_ORDER):
197
+ xi = _GL_X[i]
198
+ wi = _GL_W[i]
199
+ for j in range(_GL_ORDER):
200
+ yj = _GL_X[j]
201
+ wj = _GL_W[j]
202
+ expr = (yj * yj * norm_q
203
+ + xi * xi * norm_p
204
+ - 2.0 * yj * scal_qpq
205
+ + 2.0 * xi * scal_qpp
206
+ - 2.0 * xi * yj * scal_pq
207
+ + norm_qp)
208
+ if expr > 0.0:
209
+ total += wi * wj * np.log(expr) * scal_pq
210
+ return total
211
+
212
+
213
+
214
+ @njit
215
+ def _compute_viewfactor_gauss_legendre(pts1, pts2, constante):
216
+ """
217
+ Fast Numba‐JITed contour integrator using fixed‐order Gauss–Legendre.
218
+
219
+ This routine evaluates the view‐factor contour integral by looping
220
+ over every pair of directed edges on two convex planar facets,
221
+ approximating the double integral with a 10×10 Gauss–Legendre rule
222
+ (or _GL_ORDER value is modified)
223
+ .
224
+ Parameters
225
+ ----------
226
+ pts1 : ndarray, shape (n1,3)
227
+ Ordered vertex coordinates of the *receiving* facet.
228
+ pts2 : ndarray, shape (n2,3)
229
+ Ordered (and possibly shifted) vertex coordinates of the *emitting* facet.
230
+ constante : float
231
+ Normalization factor = 4π × area of the emitting facet.
232
+
233
+ Returns
234
+ -------
235
+ float
236
+ The view factor F<sub>2→1</sub>, clamped to ≥0.
237
+
238
+ Notes
239
+ -----
240
+ - Uses precomputed nodes & weights `_GL_X`, `_GL_W` of length `_GL_ORDER=10`.
241
+ - Requires no external dependencies once jitted; extremely fast.
242
+ """
243
+ n1 = pts1.shape[0]
244
+ n2 = pts2.shape[0]
245
+ v1 = np.empty((n1, 3))
246
+ for k in range(n1 - 1):
247
+ v1[k, 0] = pts1[k + 1, 0] - pts1[k, 0]
248
+ v1[k, 1] = pts1[k + 1, 1] - pts1[k, 1]
249
+ v1[k, 2] = pts1[k + 1, 2] - pts1[k, 2]
250
+ # last edge back to first point
251
+ v1[n1 - 1, 0] = pts1[0, 0] - pts1[n1 - 1, 0]
252
+ v1[n1 - 1, 1] = pts1[0, 1] - pts1[n1 - 1, 1]
253
+ v1[n1 - 1, 2] = pts1[0, 2] - pts1[n1 - 1, 2]
254
+
255
+ v2 = np.empty((n2, 3))
256
+ for k in range(n2 - 1):
257
+ v2[k, 0] = pts2[k + 1, 0] - pts2[k, 0]
258
+ v2[k, 1] = pts2[k + 1, 1] - pts2[k, 1]
259
+ v2[k, 2] = pts2[k + 1, 2] - pts2[k, 2]
260
+ v2[n2 - 1, 0] = pts2[0, 0] - pts2[n2 - 1, 0]
261
+ v2[n2 - 1, 1] = pts2[0, 1] - pts2[n2 - 1, 1]
262
+ v2[n2 - 1, 2] = pts2[0, 2] - pts2[n2 - 1, 2]
263
+ # precompute norms & dot‐products
264
+ norm1 = np.sum(v1 * v1, axis=1)
265
+ norm2 = np.sum(v2 * v2, axis=1)
266
+ scal = v1 @ v2.T
267
+
268
+ total = 0.0
269
+ for i in range(n1):
270
+ nq = norm1[i]
271
+ e1 = v1[i]
272
+ for j in range(n2):
273
+ np_ = norm2[j]
274
+ e2 = v2[j]
275
+ d = pts2[j] - pts1[i]
276
+ nqp = np.dot(d, d)
277
+ sqpq = np.dot(d, e1)
278
+ sqpp = np.dot(d, e2)
279
+ spq = scal[i, j]
280
+ total += _integrand_gauss_legendre(nq, np_, sqpq, sqpp, spq, nqp)
281
+ vf = total / constante
282
+ return vf
283
+
@@ -0,0 +1,77 @@
1
+ # plot.py
2
+ import pyvista as pv
3
+ import numpy as np
4
+ # Helper function to plot a mesh with view factor from one of the cells
5
+ # to all others.
6
+ def plot_viewfactor(mesh: pv.PolyData,
7
+ F: np.ndarray,
8
+ cell_id: int,
9
+ cmap: str = "viridis",
10
+ show: bool = True) -> pv.Plotter:
11
+ """
12
+ Render a mesh colored by the view‐factor distribution from one source cell.
13
+
14
+ This helper attaches the row F[cell_id, :] as a new cell‐array
15
+ `'ViewFactor'` and displays it via PyVista.
16
+
17
+ Parameters
18
+ ----------
19
+ mesh : pyvista.PolyData
20
+ Closed or open mesh whose cells correspond to both rows and columns of F.
21
+ F : ndarray, shape (n_cells, n_cells)
22
+ Precomputed view‐factor matrix.
23
+ cell_id : int
24
+ Index of the source cell whose F → others will be plotted.
25
+ cmap : str, optional
26
+ Name of a Matplotlib colormap (default=`"viridis"`).
27
+ show : bool, optional
28
+ If `True`, calls `pl.show()` before returning.
29
+ If `False`, returns the `Plotter` for further customization.
30
+
31
+ Returns
32
+ -------
33
+ pv.Plotter
34
+ The PyVista Plotter instance with the view‐factor plot.
35
+
36
+ Raises
37
+ ------
38
+ ValueError
39
+ If `F.shape != (mesh.n_cells, mesh.n_cells)`.
40
+
41
+ Examples
42
+ --------
43
+ >>> import pyvista as pv
44
+ >>> import numpy as np
45
+ >>> import pyviewfactor as pvf
46
+ >>> sphere = pv.Sphere().triangulate()
47
+ >>> F = np.random.rand(sphere.n_cells, sphere.n_cells)
48
+ >>> pl = pvf.plot_viewfactor(sphere, F, cell_id=0, show=False)
49
+ >>> pl.add_title("View factors from cell 0")
50
+ >>> pl.show()
51
+ """
52
+ n = mesh.n_cells
53
+ if F.shape != (n, n):
54
+ raise ValueError(f"Expected F shape {(n,n)}, got {F.shape}")
55
+
56
+ # Make a copy so we don’t overwrite the original cell_data
57
+ mesh_pf = mesh.copy()
58
+ mesh_pf.cell_data["ViewFactor"] = F[cell_id, :]
59
+
60
+ pl = pv.Plotter()
61
+ pl.add_mesh(
62
+ mesh_pf,
63
+ scalars="ViewFactor",
64
+ cmap=cmap,
65
+ show_edges=False,
66
+ scalar_bar_args={"title": f"F from cell {cell_id}"}
67
+ )
68
+ pl.add_title(f"View Factors from cell {cell_id}")
69
+
70
+ if show:
71
+ pl.show()
72
+ return pl
73
+
74
+
75
+
76
+
77
+ """ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End Of File ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """