pyviewfactor 0.1.0__py3-none-any.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.
- pyviewfactor/__init__.py +5 -0
- pyviewfactor/_version.py +5 -0
- pyviewfactor/pyviewfactor.py +776 -0
- pyviewfactor-0.1.0.dist-info/LICENSE.txt +21 -0
- pyviewfactor-0.1.0.dist-info/METADATA +374 -0
- pyviewfactor-0.1.0.dist-info/RECORD +8 -0
- pyviewfactor-0.1.0.dist-info/WHEEL +5 -0
- pyviewfactor-0.1.0.dist-info/top_level.txt +1 -0
pyviewfactor/__init__.py
ADDED
pyviewfactor/_version.py
ADDED
|
@@ -0,0 +1,776 @@
|
|
|
1
|
+
""" PVF main functions"""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# Imports
|
|
5
|
+
from numba import njit
|
|
6
|
+
import pyvista as pv
|
|
7
|
+
import pandas as pd
|
|
8
|
+
import scipy.integrate
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# Functions
|
|
13
|
+
def fc_unstruc2poly(mesh_unstruc):
|
|
14
|
+
"""Convenience conversion function from UnstructuredGrid to PolyData
|
|
15
|
+
|
|
16
|
+
Parameters
|
|
17
|
+
----------
|
|
18
|
+
* **mesh_unstruc** : *pyvista.UnstructuredGrid*
|
|
19
|
+
|
|
20
|
+
> Unstructured Pyvista Grid.
|
|
21
|
+
|
|
22
|
+
Returns
|
|
23
|
+
-------
|
|
24
|
+
* *pyvista.PolyData*
|
|
25
|
+
|
|
26
|
+
> The same mesh converted to a surface pyvista.PolyData.
|
|
27
|
+
|
|
28
|
+
Examples
|
|
29
|
+
--------
|
|
30
|
+
>>> import pyviewfactor as pvf
|
|
31
|
+
>>> import pyvista as pv
|
|
32
|
+
>>> sphere = pv.Sphere(radius=0.5, center=(0, 0, 0))
|
|
33
|
+
>>> subset = sphere.extract_cells(10)
|
|
34
|
+
>>> subsetPoly = fc_unstruc2poly(subset)
|
|
35
|
+
>>> subsetPoly
|
|
36
|
+
PolyData (0x1fdd9786040)
|
|
37
|
+
N Cells: 1
|
|
38
|
+
N Points: 3
|
|
39
|
+
X Bounds: -5.551e-17, 3.617e-02
|
|
40
|
+
Y Bounds: 0.000e+00, 4.682e-02
|
|
41
|
+
Z Bounds: -5.000e-01, -4.971e-01
|
|
42
|
+
N Arrays: 0
|
|
43
|
+
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
# Get the points and cells
|
|
47
|
+
points = mesh_unstruc.points
|
|
48
|
+
faces = mesh_unstruc.cells
|
|
49
|
+
# Return the same geometry as a pv.PolyData mesh
|
|
50
|
+
return pv.PolyData(points, faces)
|
|
51
|
+
|
|
52
|
+
def trunc(values, decs=0):
|
|
53
|
+
"""
|
|
54
|
+
Truncate numeric values to a fixed number of decimals.
|
|
55
|
+
|
|
56
|
+
Parameters
|
|
57
|
+
----------
|
|
58
|
+
values : float or ndarray
|
|
59
|
+
Float or array of floats to truncate.
|
|
60
|
+
decs : int, optional
|
|
61
|
+
Number of decimals to keep (default=0).
|
|
62
|
+
|
|
63
|
+
Returns
|
|
64
|
+
-------
|
|
65
|
+
float or ndarray
|
|
66
|
+
Truncated values.
|
|
67
|
+
|
|
68
|
+
Examples
|
|
69
|
+
--------
|
|
70
|
+
>>> trunc(1.234567, 2)
|
|
71
|
+
1.23
|
|
72
|
+
>>> import pyvista as pv
|
|
73
|
+
>>> tri = pv.Triangle([[0.111111, 1.111111, 1.111111],
|
|
74
|
+
... [1.222222, 1.222222, 1.222222],
|
|
75
|
+
... [1.333333, 0.333333, 1.333333]])
|
|
76
|
+
>>> trunc(tri.points, 2)
|
|
77
|
+
array([[0.11, 1.11, 1.11],
|
|
78
|
+
[1.22, 1.22, 1.22],
|
|
79
|
+
[1.33, 0.33, 1.33]])
|
|
80
|
+
"""
|
|
81
|
+
factor = 10 ** decs
|
|
82
|
+
return np.trunc(values * factor) / factor
|
|
83
|
+
|
|
84
|
+
def get_visibility(c1, c2, strict=False, print_warning=False, rounding_decimal=1):
|
|
85
|
+
"""Facets visibility:
|
|
86
|
+
|
|
87
|
+
A test to check if two facets can "see" each other, taking the normals
|
|
88
|
+
into consideration (no obstruction tests, only normals orientations).
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
[UPDATE 23-05-2025]
|
|
92
|
+
* Use cell_normals[0] and cell_centers().points[0] directly
|
|
93
|
+
* Replace triple product logic with vectorized dot product half-space test
|
|
94
|
+
* Support arbitrary convex planar polygons (not just triangles)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
Parameters
|
|
98
|
+
----------
|
|
99
|
+
* **c1** : *pyvista.PolyData*
|
|
100
|
+
|
|
101
|
+
> PolyData facet (pyvista format).
|
|
102
|
+
|
|
103
|
+
* **c2** : *pyvista.PolyData*
|
|
104
|
+
|
|
105
|
+
> PolyData facet (pyvista format).
|
|
106
|
+
|
|
107
|
+
* **strict** : *Bool*
|
|
108
|
+
|
|
109
|
+
> If *True*, checks all the points are able to see each other
|
|
110
|
+
(considering the face normal) and then continue. If some points are
|
|
111
|
+
"begind the other faces, it will return *False*,
|
|
112
|
+
> Else, compute the centroids visibility, and might print a warning if
|
|
113
|
+
some points are "behind".
|
|
114
|
+
|
|
115
|
+
* **print_warning** : *Bool*
|
|
116
|
+
|
|
117
|
+
> If *True*, warning messages will be printed in addition to be returned
|
|
118
|
+
|
|
119
|
+
* **rounding_decimal** : *Int*
|
|
120
|
+
|
|
121
|
+
> Number of decimals at which rounding shall be done for the points
|
|
122
|
+
coordinates. This is to avoid numeric issues when the points
|
|
123
|
+
coordinates are given as for instance 1.234e-13 instead of 0.
|
|
124
|
+
|
|
125
|
+
Returns
|
|
126
|
+
-------
|
|
127
|
+
* **bool**
|
|
128
|
+
|
|
129
|
+
> True when the facets "see" each other, False else.
|
|
130
|
+
|
|
131
|
+
* **str**
|
|
132
|
+
|
|
133
|
+
> Warning message if any (empty string if no warning).
|
|
134
|
+
|
|
135
|
+
Examples
|
|
136
|
+
--------
|
|
137
|
+
>>> import pyvista as pv
|
|
138
|
+
>>> import pyviewfactor as pvf
|
|
139
|
+
>>> tri1 = pv.Triangle([[0.0, 1.0, 1.0],[1.0, 1.0, 1.0],[1.0, 0.0, 1.0]])
|
|
140
|
+
>>> tri2 = pv.Triangle([[1.0, 0.0, 0.0],[1.0, 1.0, 0.0],[0.0, 1.0, 0.0]])
|
|
141
|
+
>>> pvf.get_visibility(tri1, tri2,strict=False, print_warning=True)
|
|
142
|
+
"""
|
|
143
|
+
center1 = c1.cell_centers().points[0]
|
|
144
|
+
center2 = c2.cell_centers().points[0]
|
|
145
|
+
v21 = center1 - center2
|
|
146
|
+
|
|
147
|
+
n1 = c1.cell_normals[0]
|
|
148
|
+
n2 = c2.cell_normals[0]
|
|
149
|
+
|
|
150
|
+
pos_dot_prod = np.dot(v21, n2)
|
|
151
|
+
neg_dot_prod = np.dot(v21, n1)
|
|
152
|
+
|
|
153
|
+
if not (pos_dot_prod > 0 and neg_dot_prod < 0):
|
|
154
|
+
return False, ""
|
|
155
|
+
|
|
156
|
+
if strict:
|
|
157
|
+
for cel_i, cel_j in [(c1, c2), (c2, c1)]:
|
|
158
|
+
base_center = cel_j.cell_centers().points[0]
|
|
159
|
+
normal = cel_j.cell_normals[0]
|
|
160
|
+
vectors = np.round(cel_i.points - base_center, rounding_decimal)
|
|
161
|
+
dot_products = np.dot(vectors, normal)
|
|
162
|
+
if np.any(dot_products > 0) and np.any(dot_products < 0):
|
|
163
|
+
warning_str = (
|
|
164
|
+
"[PVF-Warning-1] strict argument is set to True, "
|
|
165
|
+
"cells are considered not visible although they partially are"
|
|
166
|
+
)
|
|
167
|
+
if print_warning:
|
|
168
|
+
print(warning_str)
|
|
169
|
+
return False, warning_str
|
|
170
|
+
else:
|
|
171
|
+
for cel_i, cel_j in [(c1, c2), (c2, c1)]:
|
|
172
|
+
base_center = cel_j.cell_centers().points[0]
|
|
173
|
+
normal = cel_j.cell_normals[0]
|
|
174
|
+
vectors = np.round(cel_i.points - base_center, rounding_decimal)
|
|
175
|
+
dot_products = np.dot(vectors, normal)
|
|
176
|
+
if np.any(dot_products > 0) and np.any(dot_products < 0):
|
|
177
|
+
warning_str = (
|
|
178
|
+
"[PVF-Warning-2] strict argument is set to False, "
|
|
179
|
+
"cells are considered visible although they are only partially"
|
|
180
|
+
)
|
|
181
|
+
if print_warning:
|
|
182
|
+
print(warning_str)
|
|
183
|
+
return True, warning_str
|
|
184
|
+
|
|
185
|
+
return True, ""
|
|
186
|
+
|
|
187
|
+
@njit
|
|
188
|
+
def batch_ray_obstruction(ray_starts, ray_ends, triangles, eps=1e-6):
|
|
189
|
+
"""
|
|
190
|
+
Batch-ray intersection test (Möller–Trumbore) for multiple rays.
|
|
191
|
+
|
|
192
|
+
Parameters
|
|
193
|
+
----------
|
|
194
|
+
ray_starts : ndarray, shape (nrays,3)
|
|
195
|
+
Ray origin points.
|
|
196
|
+
ray_ends : ndarray, shape (nrays,3)
|
|
197
|
+
Ray end points.
|
|
198
|
+
triangles : ndarray, shape (ntri,3,3)
|
|
199
|
+
Vertex coords for each triangle.
|
|
200
|
+
eps : float, optional
|
|
201
|
+
Epsilon to skip near-origin hits (default=1e-6).
|
|
202
|
+
|
|
203
|
+
Returns
|
|
204
|
+
-------
|
|
205
|
+
mask : ndarray(bool), shape (nrays,)
|
|
206
|
+
True if unobstructed; False if any intersection.
|
|
207
|
+
|
|
208
|
+
Examples
|
|
209
|
+
--------
|
|
210
|
+
>>> starts = np.array([[0,0,0]])
|
|
211
|
+
>>> ends = np.array([[0,0,1]])
|
|
212
|
+
>>> tri = np.array([[[0,0,0.5],[1,0,0.5],[0,1,0.5]]])
|
|
213
|
+
>>> batch_ray_obstruction(starts, ends, tri)
|
|
214
|
+
array([False])
|
|
215
|
+
"""
|
|
216
|
+
n_rays = ray_starts.shape[0]
|
|
217
|
+
n_tri = triangles.shape[0]
|
|
218
|
+
mask = np.ones(n_rays, dtype=np.bool_)
|
|
219
|
+
for r in range(n_rays):
|
|
220
|
+
s = ray_starts[r]
|
|
221
|
+
d = ray_ends[r] - s
|
|
222
|
+
L = np.linalg.norm(d)
|
|
223
|
+
for t in range(n_tri):
|
|
224
|
+
v0,v1,v2 = triangles[t]
|
|
225
|
+
e1 = v1-v0; e2 = v2-v0
|
|
226
|
+
h = np.cross(d,e2)
|
|
227
|
+
a = np.dot(e1,h)
|
|
228
|
+
if abs(a)<eps: continue
|
|
229
|
+
f = 1.0/a
|
|
230
|
+
sv = s-v0
|
|
231
|
+
u = f*np.dot(sv,h)
|
|
232
|
+
if u<0 or u>1: continue
|
|
233
|
+
q = np.cross(sv,e1)
|
|
234
|
+
v = f*np.dot(d,q)
|
|
235
|
+
if v<0 or u+v>1: continue
|
|
236
|
+
th = f*np.dot(e2,q)
|
|
237
|
+
if eps<th< L-eps:
|
|
238
|
+
mask[r] = False
|
|
239
|
+
break
|
|
240
|
+
return mask
|
|
241
|
+
|
|
242
|
+
def is_ray_obstructed(start, end, obstacle, eps=1e-6, print_debug=False):
|
|
243
|
+
"""
|
|
244
|
+
Test if a single ray intersects a triangulated mesh.
|
|
245
|
+
|
|
246
|
+
Parameters
|
|
247
|
+
----------
|
|
248
|
+
start : array-like, shape (3,)
|
|
249
|
+
Ray origin.
|
|
250
|
+
end : array-like, shape (3,)
|
|
251
|
+
Ray end.
|
|
252
|
+
obstacle : pyvista.PolyData
|
|
253
|
+
Triangulated occluder.
|
|
254
|
+
eps : float, optional
|
|
255
|
+
Skip intersections within eps from origin.
|
|
256
|
+
print_debug : bool, optional
|
|
257
|
+
Print intersection info.
|
|
258
|
+
|
|
259
|
+
Returns
|
|
260
|
+
-------
|
|
261
|
+
bool
|
|
262
|
+
True if obstructed, False otherwise.
|
|
263
|
+
|
|
264
|
+
Examples
|
|
265
|
+
--------
|
|
266
|
+
>>> mesh = pv.Cube().triangulate()
|
|
267
|
+
>>> is_ray_obstructed([0,0,-1],[0,0,1],mesh)
|
|
268
|
+
True
|
|
269
|
+
"""
|
|
270
|
+
if not obstacle.is_all_triangles:
|
|
271
|
+
obstacle.triangulate(inplace=True)
|
|
272
|
+
d = np.array(end) - np.array(start)
|
|
273
|
+
L = np.linalg.norm(d)
|
|
274
|
+
for idx in range(obstacle.n_cells):
|
|
275
|
+
v0,v1,v2 = obstacle.get_cell(idx).points
|
|
276
|
+
e1 = v1-v0; e2=v2-v0
|
|
277
|
+
h = np.cross(d,e2)
|
|
278
|
+
a = np.dot(e1,h)
|
|
279
|
+
if abs(a)<eps: continue
|
|
280
|
+
f=1.0/a
|
|
281
|
+
sv=np.array(start)-v0
|
|
282
|
+
u=f*np.dot(sv,h)
|
|
283
|
+
if u<0 or u>1: continue
|
|
284
|
+
q=np.cross(sv,e1)
|
|
285
|
+
v=f*np.dot(d,q)
|
|
286
|
+
if v<0 or u+v>1: continue
|
|
287
|
+
t=f*np.dot(e2,q)
|
|
288
|
+
if eps<t< L-eps:
|
|
289
|
+
if print_debug:
|
|
290
|
+
print(f"Obstructed by triangle {idx}, t={t}")
|
|
291
|
+
return True
|
|
292
|
+
return False
|
|
293
|
+
|
|
294
|
+
def get_obstruction(face1, face2, obstacle, strict=False, print_debug=False):
|
|
295
|
+
"""
|
|
296
|
+
Determine if face1 and face2 are obstructed by a mesh using Möller–Trumbore algorithm.
|
|
297
|
+
|
|
298
|
+
[UPDATE 23/05/2025]
|
|
299
|
+
* Only Möller Trumbore algorithm
|
|
300
|
+
* Addition of a strict argument,
|
|
301
|
+
* strict = False > centroid-to-centroid check
|
|
302
|
+
* strict = True > points of cell1 to point of cell2
|
|
303
|
+
*
|
|
304
|
+
|
|
305
|
+
Parameters
|
|
306
|
+
----------
|
|
307
|
+
face1 : pyvista.PolyData
|
|
308
|
+
face2 : pyvista.PolyData
|
|
309
|
+
obstacle : pyvista.PolyData
|
|
310
|
+
strict : bool, optional
|
|
311
|
+
If True, checks all vertex-to-vertex rays between the two faces.
|
|
312
|
+
If False, only the centroid-to-centroid ray is tested.
|
|
313
|
+
print_debug : bool, optional
|
|
314
|
+
|
|
315
|
+
Returns
|
|
316
|
+
-------
|
|
317
|
+
visible : bool
|
|
318
|
+
warning_str : str
|
|
319
|
+
Warning if strict=False and result is approximate.
|
|
320
|
+
"""
|
|
321
|
+
if not obstacle.is_all_triangles:
|
|
322
|
+
obstacle.triangulate(inplace=True)
|
|
323
|
+
eps = 1e-6
|
|
324
|
+
# --- strict mode: test every vertex‐to‐vertex ray ---
|
|
325
|
+
if strict:
|
|
326
|
+
# build a (N_tri,3,3) array of triangles
|
|
327
|
+
tri_array = np.array([
|
|
328
|
+
obstacle.get_cell(i).points
|
|
329
|
+
for i in range(obstacle.n_cells)
|
|
330
|
+
])
|
|
331
|
+
# build all (m×n) start‐end pairs
|
|
332
|
+
m = face1.points.shape[0]
|
|
333
|
+
n = face2.points.shape[0]
|
|
334
|
+
m = face1.points.shape[0]
|
|
335
|
+
n = face2.points.shape[0]
|
|
336
|
+
tri_array = np.array([obstacle.get_cell(i).points
|
|
337
|
+
for i in range(obstacle.n_cells)])
|
|
338
|
+
ray_starts = np.repeat(face1.points, n, axis=0)
|
|
339
|
+
ray_ends = np.tile(face2.points, (m, 1))
|
|
340
|
+
mask = batch_ray_obstruction(ray_starts, ray_ends, tri_array, eps=eps)
|
|
341
|
+
|
|
342
|
+
print(mask)
|
|
343
|
+
# if *all* rays clear → fully visible
|
|
344
|
+
if mask.all():
|
|
345
|
+
return True, ""
|
|
346
|
+
|
|
347
|
+
# otherwise: strictly we call that "obstructed", but check centroid
|
|
348
|
+
cent_s = face1.cell_centers().points[0]
|
|
349
|
+
cent_e = face2.cell_centers().points[0]
|
|
350
|
+
cent_blocked = is_ray_obstructed(
|
|
351
|
+
cent_s, cent_e, obstacle, eps=eps, print_debug=print_debug
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
if not cent_blocked:
|
|
355
|
+
warning = (
|
|
356
|
+
"[PVF-Warning-4] strict=True: vertices indicate partial "
|
|
357
|
+
"visibility, but strict mode treats as obstructed."
|
|
358
|
+
)
|
|
359
|
+
if print_debug:
|
|
360
|
+
print(warning)
|
|
361
|
+
return False, warning
|
|
362
|
+
|
|
363
|
+
# centroid also blocked → just obstructed, no warning
|
|
364
|
+
return False, ""
|
|
365
|
+
|
|
366
|
+
# --- non‐strict mode: only centroid ray ---
|
|
367
|
+
start = face1.cell_centers().points[0]
|
|
368
|
+
end = face2.cell_centers().points[0]
|
|
369
|
+
blocked = is_ray_obstructed(start, end,
|
|
370
|
+
obstacle, eps=eps,
|
|
371
|
+
print_debug=print_debug)
|
|
372
|
+
visible = not blocked
|
|
373
|
+
return visible, ""
|
|
374
|
+
|
|
375
|
+
def trunc(values, decs=0):
|
|
376
|
+
"""
|
|
377
|
+
Truncate numeric values to a fixed number of decimals.
|
|
378
|
+
|
|
379
|
+
Parameters
|
|
380
|
+
----------
|
|
381
|
+
values : float or ndarray
|
|
382
|
+
Float or array of floats to truncate.
|
|
383
|
+
decs : int, optional
|
|
384
|
+
Number of decimals to keep (default=0).
|
|
385
|
+
|
|
386
|
+
Returns
|
|
387
|
+
-------
|
|
388
|
+
float or ndarray
|
|
389
|
+
Truncated values.
|
|
390
|
+
|
|
391
|
+
Examples
|
|
392
|
+
--------
|
|
393
|
+
>>> trunc(1.234567, 2)
|
|
394
|
+
1.23
|
|
395
|
+
>>> import pyvista as pv
|
|
396
|
+
>>> tri = pv.Triangle([[0.111111, 1.111111, 1.111111],
|
|
397
|
+
... [1.222222, 1.222222, 1.222222],
|
|
398
|
+
... [1.333333, 0.333333, 1.333333]])
|
|
399
|
+
>>> trunc(tri.points, 2)
|
|
400
|
+
array([[0.11, 1.11, 1.11],
|
|
401
|
+
[1.22, 1.22, 1.22],
|
|
402
|
+
[1.33, 0.33, 1.33]])
|
|
403
|
+
"""
|
|
404
|
+
factor = 10 ** decs
|
|
405
|
+
return np.trunc(values * factor) / factor
|
|
406
|
+
|
|
407
|
+
@njit
|
|
408
|
+
def fast_integrand(norm_q_carree, norm_p_carree, scal_qpq,
|
|
409
|
+
scal_qpp, scal_pq, norm_qp_carree):
|
|
410
|
+
"""
|
|
411
|
+
Compute the contour integral kernel for view factor between two edges.
|
|
412
|
+
|
|
413
|
+
Uses 10-point Gauss–Legendre quadrature over [0,1]^2.
|
|
414
|
+
|
|
415
|
+
Parameters
|
|
416
|
+
----------
|
|
417
|
+
norm_q_carree : float
|
|
418
|
+
Squared length of edge vector q.
|
|
419
|
+
norm_p_carree : float
|
|
420
|
+
Squared length of edge vector p.
|
|
421
|
+
scal_qpq : float
|
|
422
|
+
Dot(q, p).
|
|
423
|
+
scal_qpp : float
|
|
424
|
+
Dot(q, p_next).
|
|
425
|
+
scal_pq : float
|
|
426
|
+
Dot(p, q).
|
|
427
|
+
norm_qp_carree : float
|
|
428
|
+
Squared distance between edge origins.
|
|
429
|
+
|
|
430
|
+
Returns
|
|
431
|
+
-------
|
|
432
|
+
float
|
|
433
|
+
The weighted sum of ln(expr)*scal_pq over quadrature points.
|
|
434
|
+
|
|
435
|
+
Examples
|
|
436
|
+
--------
|
|
437
|
+
>>> fast_integrand(1.0, 1.0, 0.5, 0.5, 0.25, 2.0)
|
|
438
|
+
0.0 # example output
|
|
439
|
+
"""
|
|
440
|
+
nodes = np.array([
|
|
441
|
+
-0.9739065285, -0.8650633667, -0.6794095682, -0.4333953941, -0.1488743390,
|
|
442
|
+
0.1488743390, 0.4333953941, 0.6794095682, 0.8650633667, 0.9739065285
|
|
443
|
+
])
|
|
444
|
+
weights = np.array([
|
|
445
|
+
0.0666713443, 0.1494513491, 0.2190863625, 0.2692667193, 0.2955242247,
|
|
446
|
+
0.2955242247, 0.2692667193, 0.2190863625, 0.1494513491, 0.0666713443
|
|
447
|
+
])
|
|
448
|
+
x = 0.5 * (nodes + 1.0)
|
|
449
|
+
w = 0.5 * weights
|
|
450
|
+
result = 0.0
|
|
451
|
+
for i in range(10):
|
|
452
|
+
xi = x[i]
|
|
453
|
+
wi = w[i]
|
|
454
|
+
for j in range(10):
|
|
455
|
+
yj = x[j]
|
|
456
|
+
wj = w[j]
|
|
457
|
+
expr = (yj*yj * norm_q_carree + xi*xi * norm_p_carree
|
|
458
|
+
- 2*yj*scal_qpq + 2*xi*scal_qpp
|
|
459
|
+
- 2*xi*yj*scal_pq + norm_qp_carree)
|
|
460
|
+
if expr > 0.0:
|
|
461
|
+
result += wi * wj * np.log(expr) * scal_pq
|
|
462
|
+
return result
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
@njit
|
|
466
|
+
def compute_viewfactor_core(cell_1_points, cell_2_points,
|
|
467
|
+
vect_intra_1, vect_intra_2,
|
|
468
|
+
constante):
|
|
469
|
+
"""
|
|
470
|
+
Core Numba kernel: sum contributions of all edge pairs between two facets.
|
|
471
|
+
|
|
472
|
+
Parameters
|
|
473
|
+
----------
|
|
474
|
+
cell_1_points : ndarray, shape (n1,3)
|
|
475
|
+
Vertex coordinates of facet 1.
|
|
476
|
+
cell_2_points : ndarray, shape (n2,3)
|
|
477
|
+
Vertex coordinates of facet 2.
|
|
478
|
+
vect_intra_1 : ndarray, shape (n1,3)
|
|
479
|
+
Edge vectors of facet 1.
|
|
480
|
+
vect_intra_2 : ndarray, shape (n2,3)
|
|
481
|
+
Edge vectors of facet 2.
|
|
482
|
+
constante : float
|
|
483
|
+
Normalization constant = 4*pi*Area(facet2).
|
|
484
|
+
|
|
485
|
+
Returns
|
|
486
|
+
-------
|
|
487
|
+
float
|
|
488
|
+
View factor from facet 2 to facet 1.
|
|
489
|
+
|
|
490
|
+
Examples
|
|
491
|
+
--------
|
|
492
|
+
>>> pts1 = np.array([[0,0,0],[1,0,0],[1,1,0]])
|
|
493
|
+
>>> pts2 = pts1 + [0,0,1]
|
|
494
|
+
>>> v1 = np.vstack([pts1[1:]-pts1[:-1], pts1[0]-pts1[-1]])
|
|
495
|
+
>>> v2 = np.vstack([pts2[1:]-pts2[:-1], pts2[0]-pts2[-1]])
|
|
496
|
+
>>> constant = 4*np.pi*1.0
|
|
497
|
+
>>> compute_viewfactor_core(pts1, pts2, v1, v2, constant)
|
|
498
|
+
0.0
|
|
499
|
+
"""
|
|
500
|
+
n1 = cell_1_points.shape[0]
|
|
501
|
+
n2 = cell_2_points.shape[0]
|
|
502
|
+
matL = cell_2_points[None,:,:] - cell_1_points[:,None,:]
|
|
503
|
+
norm1 = np.sum(vect_intra_1*vect_intra_1,axis=1)
|
|
504
|
+
norm2 = np.sum(vect_intra_2*vect_intra_2,axis=1)
|
|
505
|
+
scal = np.dot(vect_intra_1, vect_intra_2.T)
|
|
506
|
+
total = 0.0
|
|
507
|
+
for i in range(n1):
|
|
508
|
+
nqi = norm1[i]
|
|
509
|
+
for j in range(n2):
|
|
510
|
+
npj = norm2[j]
|
|
511
|
+
l = matL[i,j]
|
|
512
|
+
nl = np.dot(l,l)
|
|
513
|
+
sqpq = np.dot(l, vect_intra_1[i])
|
|
514
|
+
sqpp = np.dot(l, vect_intra_2[j])
|
|
515
|
+
spq = scal[i,j]
|
|
516
|
+
val = fast_integrand(nqi, npj, sqpq, sqpp, spq, nl)
|
|
517
|
+
total += round(val/constante,11)
|
|
518
|
+
return total if total>0 else 0.0
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
def compute_viewfactor(cell_1, cell_2, epsilon=0.001, rounding_decimal=6):
|
|
523
|
+
"""
|
|
524
|
+
Compute the radiation view factor from cell_2 to cell_1 using contour integration.
|
|
525
|
+
|
|
526
|
+
This function casts each face to an unstructured grid, extracts and rounds
|
|
527
|
+
its vertices, applies a small displacement if cells share vertices, and
|
|
528
|
+
then calls a fast Numba-compiled core to evaluate the contour integral
|
|
529
|
+
via Gauss-Legendre quadrature.
|
|
530
|
+
|
|
531
|
+
Parameters
|
|
532
|
+
----------
|
|
533
|
+
cell_1 : pyvista.PolyData
|
|
534
|
+
The receiving facet (must be a single face PolyData).
|
|
535
|
+
cell_2 : pyvista.PolyData
|
|
536
|
+
The emitting facet (must be a single face PolyData).
|
|
537
|
+
epsilon : float, optional
|
|
538
|
+
Small displacement applied when facets share vertices to avoid singularities
|
|
539
|
+
(default=0.001).
|
|
540
|
+
rounding_decimal : int, optional
|
|
541
|
+
Number of decimal places to round vertex coordinates for numerical stability
|
|
542
|
+
(default=6).
|
|
543
|
+
|
|
544
|
+
Returns
|
|
545
|
+
-------
|
|
546
|
+
float
|
|
547
|
+
View factor from cell_2 onto cell_1 (dimensionless, between 0 and 1).
|
|
548
|
+
|
|
549
|
+
Examples
|
|
550
|
+
--------
|
|
551
|
+
>>> import pyvista as pv
|
|
552
|
+
>>> tri1 = pv.Triangle([[0,0,0],[1,0,0],[0,1,0]])
|
|
553
|
+
>>> tri2 = pv.Triangle([[0,0,1],[1,0,1],[0,1,1]])
|
|
554
|
+
>>> vf = compute_viewfactorNew(tri1, tri2)
|
|
555
|
+
>>> isinstance(vf, float)
|
|
556
|
+
True
|
|
557
|
+
"""
|
|
558
|
+
# prepare points and edge vectors
|
|
559
|
+
c1 = cell_1.cast_to_unstructured_grid().get_cell(0).points
|
|
560
|
+
c1 = np.round(c1, rounding_decimal)
|
|
561
|
+
c2 = cell_2.cast_to_unstructured_grid().get_cell(0).points
|
|
562
|
+
c2 = np.round(c2, rounding_decimal)
|
|
563
|
+
v1 = np.roll(c1, -1, axis=0) - c1
|
|
564
|
+
v2 = np.roll(c2, -1, axis=0) - c2
|
|
565
|
+
# area and constant
|
|
566
|
+
A2 = cell_2.compute_cell_sizes(area=True)['Area'][0]
|
|
567
|
+
C = 4*np.pi*A2
|
|
568
|
+
# virtual displacement if shared vertices
|
|
569
|
+
shared = np.argwhere((c2[:,None,:]==c1[None,:,:]).all(axis=2))
|
|
570
|
+
if shared.size>0:
|
|
571
|
+
cent = cell_2.cell_centers().points - cell_1.cell_centers().points
|
|
572
|
+
cent = cent/np.linalg.norm(cent)
|
|
573
|
+
c2 += epsilon*cent[0]
|
|
574
|
+
return compute_viewfactor_core(c1, c2, v1, v2, C)
|
|
575
|
+
|
|
576
|
+
def compute_viewfactor_matrix(
|
|
577
|
+
mesh,
|
|
578
|
+
obstacle=None,
|
|
579
|
+
use_reciprocity=True,
|
|
580
|
+
visibility_kwargs=None,
|
|
581
|
+
obstruction_kwargs=None,
|
|
582
|
+
n_jobs=1
|
|
583
|
+
):
|
|
584
|
+
"""
|
|
585
|
+
Compute the view factor matrix for all faces in a mesh, skipping
|
|
586
|
+
pairs that are not mutually visible or are obstructed.
|
|
587
|
+
|
|
588
|
+
Parameters
|
|
589
|
+
----------
|
|
590
|
+
mesh : pyvista.PolyData
|
|
591
|
+
Input mesh with planar faces (triangles or polygons).
|
|
592
|
+
obstacle : pyvista.PolyData, list of PolyData, or None
|
|
593
|
+
Obstruction geometry. Can be a single mesh or a list of meshes. If None,
|
|
594
|
+
no obstruction testing is applied.
|
|
595
|
+
use_reciprocity : bool
|
|
596
|
+
If True, applies reciprocity: F[j,i] = (A_i/A_j) * F[i,j].
|
|
597
|
+
visibility_kwargs : dict or None
|
|
598
|
+
Keyword arguments passed to get_visibility().
|
|
599
|
+
obstruction_kwargs : dict or None
|
|
600
|
+
Keyword arguments passed to get_obstruction().
|
|
601
|
+
n_jobs : int, optional
|
|
602
|
+
Number of parallel jobs to launch (using joblib). 1 means sequential.
|
|
603
|
+
|
|
604
|
+
Returns
|
|
605
|
+
-------
|
|
606
|
+
F : ndarray, shape (n_faces, n_faces)
|
|
607
|
+
Symmetric view factor matrix.
|
|
608
|
+
|
|
609
|
+
Examples
|
|
610
|
+
--------
|
|
611
|
+
Basic usage without obstruction or custom kwargs:
|
|
612
|
+
|
|
613
|
+
F = compute_viewfactor_matrix(my_mesh)
|
|
614
|
+
|
|
615
|
+
Using a separate obstacle mesh or list of meshes:
|
|
616
|
+
|
|
617
|
+
occluder = pv.read("building_block.vtk")
|
|
618
|
+
F = compute_viewfactor_matrix(
|
|
619
|
+
my_mesh,
|
|
620
|
+
obstacle=[occluder, other_obstacle_mesh],
|
|
621
|
+
visibility_kwargs={'strict': False},
|
|
622
|
+
obstruction_kwargs={'strict': True},
|
|
623
|
+
n_jobs=4
|
|
624
|
+
)
|
|
625
|
+
|
|
626
|
+
Showing progress bar on sequential run (n_jobs=1):
|
|
627
|
+
|
|
628
|
+
F = compute_viewfactor_matrix(
|
|
629
|
+
my_mesh,
|
|
630
|
+
n_jobs=1 # tqdm progress bar shown
|
|
631
|
+
)
|
|
632
|
+
"""
|
|
633
|
+
import pyvista as pv
|
|
634
|
+
from joblib import Parallel, delayed
|
|
635
|
+
from tqdm import tqdm
|
|
636
|
+
|
|
637
|
+
# Prepare faces and areas
|
|
638
|
+
faces = []
|
|
639
|
+
areas = []
|
|
640
|
+
for i in range(mesh.n_cells):
|
|
641
|
+
face = mesh.extract_cells(i)
|
|
642
|
+
faces.append(face)
|
|
643
|
+
areas.append(face.compute_cell_sizes(area=True)['Area'][0])
|
|
644
|
+
n = len(faces)
|
|
645
|
+
F = np.zeros((n, n), dtype=np.float64)
|
|
646
|
+
|
|
647
|
+
visibility_kwargs = visibility_kwargs or {}
|
|
648
|
+
obstruction_kwargs = obstruction_kwargs or {}
|
|
649
|
+
|
|
650
|
+
# Normalize obstacle to list
|
|
651
|
+
obstacles = []
|
|
652
|
+
if obstacle is None:
|
|
653
|
+
obstacles = []
|
|
654
|
+
elif isinstance(obstacle, (list, tuple)):
|
|
655
|
+
obstacles = obstacle
|
|
656
|
+
else:
|
|
657
|
+
obstacles = [obstacle]
|
|
658
|
+
|
|
659
|
+
def _is_blocked(f1, f2):
|
|
660
|
+
for obs in obstacles:
|
|
661
|
+
vis_obs, _ = get_obstruction(f1, f2, obs, **obstruction_kwargs)
|
|
662
|
+
if not vis_obs:
|
|
663
|
+
return True
|
|
664
|
+
return False
|
|
665
|
+
|
|
666
|
+
# Helper to compute a single pair
|
|
667
|
+
def _compute_pair(i, j):
|
|
668
|
+
f_i, f_j = faces[i], faces[j]
|
|
669
|
+
# visibility
|
|
670
|
+
vis, _ = get_visibility(f_i, f_j, **visibility_kwargs)
|
|
671
|
+
if not vis:
|
|
672
|
+
return (i, j, 0.0)
|
|
673
|
+
# obstruction over all obstacles
|
|
674
|
+
if obstacles and _is_blocked(f_i, f_j):
|
|
675
|
+
return (i, j, 0.0)
|
|
676
|
+
# compute view factor
|
|
677
|
+
F_ij = compute_viewfactor(f_i, f_j)
|
|
678
|
+
if use_reciprocity:
|
|
679
|
+
F_ji = (areas[i] / areas[j]) * F_ij
|
|
680
|
+
else:
|
|
681
|
+
F_ji = compute_viewfactor(f_j, f_i)
|
|
682
|
+
return (i, j, F_ij, F_ji)
|
|
683
|
+
|
|
684
|
+
# Prepare all unique pairs
|
|
685
|
+
pairs = [(i, j) for i in range(n) for j in range(i+1, n)]
|
|
686
|
+
|
|
687
|
+
# Compute in parallel or sequential
|
|
688
|
+
if n_jobs and n_jobs != 1:
|
|
689
|
+
results = Parallel(n_jobs=n_jobs)(
|
|
690
|
+
delayed(_compute_pair)(i, j) for (i, j) in pairs
|
|
691
|
+
)
|
|
692
|
+
else:
|
|
693
|
+
results = []
|
|
694
|
+
for (i, j) in tqdm(pairs, desc="Computing view factors", total=len(pairs)):
|
|
695
|
+
results.append(_compute_pair(i, j))
|
|
696
|
+
|
|
697
|
+
# Fill matrix
|
|
698
|
+
for res in results:
|
|
699
|
+
if len(res) == 3:
|
|
700
|
+
i, j, val = res
|
|
701
|
+
F[i, j] = val
|
|
702
|
+
else:
|
|
703
|
+
i, j, fij, fji = res
|
|
704
|
+
F[i, j] = fij
|
|
705
|
+
F[j, i] = fji
|
|
706
|
+
return F
|
|
707
|
+
|
|
708
|
+
def plot_viewfactor(mesh: pv.PolyData,
|
|
709
|
+
F: np.ndarray,
|
|
710
|
+
cell_id: int,
|
|
711
|
+
cmap: str = "viridis",
|
|
712
|
+
show: bool = True) -> pv.Plotter:
|
|
713
|
+
"""
|
|
714
|
+
Visualize the view factor distribution on a mesh from a given cell.
|
|
715
|
+
pl = plot_viewfactor(mesh, F, cell_id=5, cmap='plasma', show=False)
|
|
716
|
+
pl.show()
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
Parameters
|
|
720
|
+
----------
|
|
721
|
+
mesh : pyvista.PolyData
|
|
722
|
+
The input mesh whose cells correspond to rows/columns of F.
|
|
723
|
+
F : ndarray, shape (n_cells, n_cells)
|
|
724
|
+
Precomputed view‐factor matrix.
|
|
725
|
+
cell_id : int
|
|
726
|
+
Index of the source cell whose view factors to plot.
|
|
727
|
+
cmap : str, optional
|
|
728
|
+
A matplotlib colormap name (default: 'viridis').
|
|
729
|
+
show : bool, optional
|
|
730
|
+
If True (default), immediately renders the window via `pl.show()`.
|
|
731
|
+
If False, returns the Plotter so you can customize further.
|
|
732
|
+
|
|
733
|
+
Returns
|
|
734
|
+
-------
|
|
735
|
+
pv.Plotter
|
|
736
|
+
The PyVista Plotter instance with the mesh colored by view factors.
|
|
737
|
+
|
|
738
|
+
Raises
|
|
739
|
+
------
|
|
740
|
+
ValueError
|
|
741
|
+
If `F`’s shape doesn’t match the number of cells in `mesh`.
|
|
742
|
+
|
|
743
|
+
Examples
|
|
744
|
+
--------
|
|
745
|
+
>>> import pyvista as pv
|
|
746
|
+
>>> import numpy as np
|
|
747
|
+
>>> # Suppose mesh.n_cells == 5 and F is 5×5
|
|
748
|
+
>>> mesh = pv.Sphere().triangulate()
|
|
749
|
+
>>> F = np.random.rand(mesh.n_cells, mesh.n_cells)
|
|
750
|
+
>>> pl = plot_viewfactor(mesh, F, cell_id=2, cmap='plasma', show=False)
|
|
751
|
+
>>> pl.add_scalar_bar("View Factor")
|
|
752
|
+
>>> pl.show()
|
|
753
|
+
"""
|
|
754
|
+
n = mesh.n_cells
|
|
755
|
+
if F.shape != (n, n):
|
|
756
|
+
raise ValueError(f"Expected F shape {(n,n)}, got {F.shape}")
|
|
757
|
+
|
|
758
|
+
# Make a copy so we don’t overwrite the original cell_data
|
|
759
|
+
mesh_pf = mesh.copy()
|
|
760
|
+
mesh_pf.cell_data["ViewFactor"] = F[cell_id, :]
|
|
761
|
+
|
|
762
|
+
pl = pv.Plotter()
|
|
763
|
+
pl.add_mesh(
|
|
764
|
+
mesh_pf,
|
|
765
|
+
scalars="ViewFactor",
|
|
766
|
+
cmap=cmap,
|
|
767
|
+
show_edges=False,
|
|
768
|
+
scalar_bar_args={"title": f"F from cell {cell_id}"}
|
|
769
|
+
)
|
|
770
|
+
pl.add_title(f"View Factors from cell {cell_id}")
|
|
771
|
+
|
|
772
|
+
if show:
|
|
773
|
+
pl.show()
|
|
774
|
+
return pl
|
|
775
|
+
|
|
776
|
+
""" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End Of File ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 AREP
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyviewfactor
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A python library to calculate numerically exact radiation view factors between planar faces.
|
|
5
|
+
Home-page: https://gitlab.com/arep-dev/pyViewFactor/
|
|
6
|
+
Author: Mateusz BOGDAN, Edouard WALTHER, Marc ALECIAN, Mina CHAPON
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: homepage, https://gitlab.com/arep-dev/pyViewFactor/
|
|
9
|
+
Project-URL: Documentation, https://arep-dev.gitlab.io/pyViewFactor/
|
|
10
|
+
Project-URL: Tracker, https://gitlab.com/arep-dev/pyViewFactor/-/issues
|
|
11
|
+
Project-URL: Source, https://gitlab.com/arep-dev/pyViewFactor/
|
|
12
|
+
Keywords: radiative-transfer radiation vtk geometry ViewFactor STL visibility
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE.txt
|
|
15
|
+
Requires-Dist: pandas
|
|
16
|
+
Requires-Dist: pyvista
|
|
17
|
+
Requires-Dist: numba
|
|
18
|
+
Requires-Dist: numpy
|
|
19
|
+
|
|
20
|
+
# PyViewFactor
|
|
21
|
+
[](https://gitlab.com/arep-dev/pyViewFactor/-/releases)
|
|
22
|
+
[](https://opensource.org/licenses/MIT)
|
|
23
|
+
[](https://pypi.org/project/pyviewfactor/)
|
|
24
|
+
[](https://pypi.org/project/pyviewfactor/)
|
|
25
|
+
|
|
26
|
+
A python library to compute exact view factors between planar faces ([Documentation](https://arep-dev.gitlab.io/pyViewFactor/)).
|
|
27
|
+
|
|
28
|
+
This code computes the radiation view factor between polygons using the double contour integral method described in [(Mazumder and Ravishankar 2012)](https://www.academia.edu/download/77459051/Viewfactor_paper_IJHMT.pdf) and [(Schmid 2016)](https://hal.archives-ouvertes.fr/tel-01734545/).
|
|
29
|
+
|
|
30
|
+
It uses the handy [Pyvista](https://docs.pyvista.org/) package to deal with geometrical aspects of such problems.
|
|
31
|
+
|
|
32
|
+
## How does it work?
|
|
33
|
+
|
|
34
|
+
- [x] Use [pyvista](https://docs.pyvista.org/index.html) to import your geometry (*.stl, *.vtk, *.obj, ...) or alternately draw it with the same package.
|
|
35
|
+
- [x] Optionally check that the faces can "see" each other with `get_visibility(face1, face2)`
|
|
36
|
+
- [x] Optionally check that no obstruction lies between them `get_visibility_obstruction(face1, face2, obstacle)`
|
|
37
|
+
- [x] Compute the view factor from `face2` to `face1` with `compute_view_factor(face1, face2)`: Done!
|
|
38
|
+
|
|
39
|
+
## Minimum working example : facet to facet view factor computation
|
|
40
|
+
|
|
41
|
+
Suppose we want to compute the radiation view factor between a triangle and a rectangle.
|
|
42
|
+
|
|
43
|
+
<img src="https://gitlab.com/arep-dev/pyViewFactor/-/raw/64e19ed126bbfd1bc2bff1197b1dee23d35c7836/img/mwe.png?raw=true" alt="Triangle and rectangle configuration" width="260"/>
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
You are now 18 lines of code away from your first view factor computation:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
import pyvista as pv
|
|
50
|
+
import pyviewfactor as pvf
|
|
51
|
+
|
|
52
|
+
# first define a rectangle...
|
|
53
|
+
pointa = [1, 0, 0]
|
|
54
|
+
pointb = [1, 1, 0]
|
|
55
|
+
pointc = [0, 1, 0]
|
|
56
|
+
pointd = [0, 0, 0]
|
|
57
|
+
rectangle = pv.Rectangle([pointa, pointb, pointc, pointd])
|
|
58
|
+
|
|
59
|
+
# ... then a triangle
|
|
60
|
+
pointa = [1, 0, 1]
|
|
61
|
+
pointb = [1, 1, 1]
|
|
62
|
+
pointc = [0, 1, 1]
|
|
63
|
+
liste_pts = [pointa, pointb, pointc]
|
|
64
|
+
liste_pts.reverse() # let us put the normal the other way around (facing the rectangle)
|
|
65
|
+
triangle = pv.Triangle(liste_pts) # ... done with geometry.
|
|
66
|
+
|
|
67
|
+
# preliminary check for visibility
|
|
68
|
+
if pvf.get_visibility(rectangle , triangle):
|
|
69
|
+
F = pvf.compute_viewfactor(rectangle, triangle)
|
|
70
|
+
print("View factor from triangle to rectangle = ", F)
|
|
71
|
+
else:
|
|
72
|
+
print("Not facing each other")
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
You usually get your geometry from a different format? (*.idf, *.dat, ...)
|
|
76
|
+
|
|
77
|
+
Check pyvista's documentation on [how to generate a PolyData facet from points](https://docs.pyvista.org/examples/00-load/create-poly.html).
|
|
78
|
+
|
|
79
|
+
## Example with a closed geometry and the VTK file format
|
|
80
|
+
|
|
81
|
+
We will now compute the view factors within a more complex geometry: a closed sphere (clipped in half below), with inwards facing normals, so the faces can "see" each other. Note that the face-to-face visibility is unobstructed (for obstructed geometries, see next section).
|
|
82
|
+
|
|
83
|
+
The field of view factors from one facet to all others will be computed and stored in a VTK file, which you can explore with the open source [Paraview software](https://www.paraview.org/download/).
|
|
84
|
+
|
|
85
|
+
<img src="https://gitlab.com/arep-dev/pyViewFactor/-/raw/73249e2093b207d5030d9b6637603c4b77b2374c/img/demi_sphere.png?raw=true" alt="Sphere" width="350"/>
|
|
86
|
+
|
|
87
|
+
Following snippet can be reused as a kick-start for your own purposes:
|
|
88
|
+
```python
|
|
89
|
+
import pyvista as pv
|
|
90
|
+
import numpy as np
|
|
91
|
+
from pyviewfactor import compute_viewfactor, fc_unstruc2poly # viewfactor + a useful conversion function
|
|
92
|
+
from tqdm import tqdm # for a fancy progress bar
|
|
93
|
+
|
|
94
|
+
# create a raw sphere with pyvista
|
|
95
|
+
sphere = pv.Sphere(radius=50, center=(0, 0, 0), direction=(0, 0, 1),
|
|
96
|
+
theta_resolution=9, phi_resolution=9)
|
|
97
|
+
# and put the normals inwards please
|
|
98
|
+
sphere.flip_normals()
|
|
99
|
+
|
|
100
|
+
# let us chose a cell to compute view factors from
|
|
101
|
+
chosen_face = sphere.extract_cells(10)
|
|
102
|
+
# convert the face from UnstructuredGrid to PolyData
|
|
103
|
+
chosen_face = fc_unstruc2poly(chosen_face)
|
|
104
|
+
# "one array to contain them all" -> the results will be stored there
|
|
105
|
+
F = np.zeros(sphere.n_cells)
|
|
106
|
+
|
|
107
|
+
# now let us compute the view factor to all other faces
|
|
108
|
+
# (with a fancy progress bar, iterating over the mesh's faces)
|
|
109
|
+
for i in tqdm(range(sphere.n_cells), total=sphere.n_cells):
|
|
110
|
+
face = sphere.extract_cells(i) # other facet
|
|
111
|
+
face = fc_unstruc2poly(face) # convert to PolyData
|
|
112
|
+
F[i] = compute_viewfactor(face, chosen_face) # compute VF
|
|
113
|
+
|
|
114
|
+
print("Complementarity check: \n (e.g. is \sum_{i=0}^n F_i =? 1)", np.sum(F))
|
|
115
|
+
|
|
116
|
+
# put the scalar values in the geometry
|
|
117
|
+
sphere.cell_data["F"] = F
|
|
118
|
+
sphere.save("./sphere.vtk") # ... and save.
|
|
119
|
+
|
|
120
|
+
# let us have a look in 3D with an interactive window...
|
|
121
|
+
p=pv.Plotter() # instantiate 3D window
|
|
122
|
+
p.add_mesh(sphere, scalars='F', cmap='jet') # add mesh with a nice color scheme
|
|
123
|
+
p.show() # plot
|
|
124
|
+
```
|
|
125
|
+
The results look as per following images showing the view factor from the chosen cell to all others.
|
|
126
|
+
|
|
127
|
+
<img src="https://gitlab.com/arep-dev/pyViewFactor/-/raw/3ac896fe2f420443c7c96eaa9dbbb955474e80d5/img/F_sphere.png?raw=true" alt="VF to other faces inside the sphere" width="240"/>
|
|
128
|
+
<img src="https://gitlab.com/arep-dev/pyViewFactor/-/raw/3ac896fe2f420443c7c96eaa9dbbb955474e80d5/img/F_sphere_clip.png?raw=true" alt="Clipped result" width="240"/>
|
|
129
|
+
|
|
130
|
+
## View factors of an individual with a wall
|
|
131
|
+
|
|
132
|
+
For comfort computations, it may be useful to determine heat transfer between an invidivual and a wall. We will use here PyVista's [doorman example](https://docs.pyvista.org/api/examples/_autosummary/pyvista.examples.downloads.download_doorman.html?highlight=example+man#pyvista.examples.downloads.download_doorman) as a basis for the human geometry.
|
|
133
|
+
|
|
134
|
+
<img src="https://gitlab.com/arep-dev/pyViewFactor/-/raw/efeae65b20630978972a94de2cffaa4ab5e427dc/img/Fground.png?raw=true" alt="View factors of the doorman's faces to the ground" width="380"/>
|
|
135
|
+
|
|
136
|
+
The following code and VTK file of the doorman example are available in the [./examples/](https://gitlab.com/arep-dev/pyViewFactor/-/tree/main/examples) folder.
|
|
137
|
+
```python
|
|
138
|
+
# -*- coding: utf-8 -*-
|
|
139
|
+
from tqdm import tqdm
|
|
140
|
+
import numpy as np
|
|
141
|
+
import pyvista as pv
|
|
142
|
+
import pyviewfactor as pvf
|
|
143
|
+
|
|
144
|
+
def fc_Fwall(nom_vtk):
|
|
145
|
+
mesh=pv.read(nom_vtk)
|
|
146
|
+
|
|
147
|
+
# find all types of walls
|
|
148
|
+
wall_types=list(np.unique(mesh['names']))
|
|
149
|
+
# remove the individual from the list (still named 'cylinder'...)
|
|
150
|
+
wall_types.remove('cylinder\r') # with dirty trailing character
|
|
151
|
+
# where is the doorman in the list?
|
|
152
|
+
index_doorman=np.where(mesh['names']=='cylinder\r')[0]
|
|
153
|
+
# prepare storage for the different walls in a dict
|
|
154
|
+
dict_F={}
|
|
155
|
+
# loop over wall types
|
|
156
|
+
for type_wall in wall_types:
|
|
157
|
+
# prepare for storing doorman to wall view factor
|
|
158
|
+
F=np.zeros(mesh.n_cells)
|
|
159
|
+
# get the indices of this type of wall
|
|
160
|
+
indices=np.where(mesh['names']==type_wall )[0]
|
|
161
|
+
# loop over
|
|
162
|
+
for i in indices:
|
|
163
|
+
wall=mesh.extract_cells(i)
|
|
164
|
+
wall=pvf.fc_unstruc2poly(wall) # convert for normals
|
|
165
|
+
# ... for each facet of the individual
|
|
166
|
+
for idx in tqdm(index_doorman):
|
|
167
|
+
face = mesh.extract_cells(idx)
|
|
168
|
+
face =pvf.fc_unstruc2poly(face) # convert for normals
|
|
169
|
+
# check if faces can "see" each other
|
|
170
|
+
if pvf.get_visibility(wall,face):
|
|
171
|
+
Ffp=pvf.compute_viewfactor(wall,face) # compute face2wall view factor
|
|
172
|
+
else:
|
|
173
|
+
Ffp=0
|
|
174
|
+
F[idx]=Ffp
|
|
175
|
+
#store array F in e.g. dict_F['F_ceiling']
|
|
176
|
+
dict_F['F_'+type_wall.replace('\r','')]=F
|
|
177
|
+
return dict_F
|
|
178
|
+
|
|
179
|
+
# download it directly from https://gitlab.com/arep-dev/pyViewFactor/-/blob/main/examples/example_doorman.vtk ...
|
|
180
|
+
# ...or get it from this repository's examples
|
|
181
|
+
file="./example_doorman.vtk"
|
|
182
|
+
# compute the view factors or the doorman to the different wall types in the scene
|
|
183
|
+
dict_F=fc_Fwall(file)
|
|
184
|
+
|
|
185
|
+
# re-read and store
|
|
186
|
+
mesh=pv.read(file)
|
|
187
|
+
# loop over what is in the dictionary of view factors
|
|
188
|
+
for elt in dict_F.keys():
|
|
189
|
+
mesh[elt.replace('\r','')]=dict_F[elt] # name the field
|
|
190
|
+
mesh.save(file) # store in the intial VTK
|
|
191
|
+
|
|
192
|
+
# have a look without paraview with fancy colors
|
|
193
|
+
mesh.plot(cmap='jet',lighting=False)
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
More details and view abacuses in [this page](https://lhypercube.arep.fr/en/confort/calcul-de-la-temperature-radiante/). Is the computation time a bit lengthy? Learn how to [go parallel!](https://eddes.github.io/#go-parallel).
|
|
197
|
+
|
|
198
|
+
## Computing the view factors of a wall in its built environment
|
|
199
|
+
|
|
200
|
+
For building simulation purposes, it may prove to be useful to compute the ground and sky view factors of a given wall, or the view factor of the wall to other walls in the built environment. In following example (available in the "examples" folder), we compute the view factors of the environment of the purple wall depicted below.
|
|
201
|
+
|
|
202
|
+
<img src="https://gitlab.com/arep-dev/pyViewFactor/-/raw/3ac896fe2f420443c7c96eaa9dbbb955474e80d5/img/wall_view_factors.png?raw=true" alt="View factors in built environment" width="430"/>
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
import numpy as np
|
|
206
|
+
import pyvista as pv
|
|
207
|
+
from tqdm import tqdm
|
|
208
|
+
import pyviewfactor as pvf
|
|
209
|
+
|
|
210
|
+
# read the geometry
|
|
211
|
+
mesh = pv.read("./built_envmt.vtk")
|
|
212
|
+
meshpoly = pvf.fc_unstruc2poly(mesh) # convert to polydata for obstruction check
|
|
213
|
+
|
|
214
|
+
# identify who is who
|
|
215
|
+
i_wall = np.where(mesh["wall_names"]=='wall')[0]
|
|
216
|
+
i_sky = np.where(mesh["wall_names"]=='sky')[0]
|
|
217
|
+
i_building1 = np.where(mesh["wall_names"]=='building1')[0]
|
|
218
|
+
i_building2 = np.where(mesh["wall_names"]=='building2')[0]
|
|
219
|
+
|
|
220
|
+
# get the different elements
|
|
221
|
+
wall = mesh.extract_cells(i_wall)
|
|
222
|
+
sky = mesh.extract_cells(i_sky)
|
|
223
|
+
building1 = mesh.extract_cells(i_building1)
|
|
224
|
+
building2 = mesh.extract_cells(i_building2)
|
|
225
|
+
|
|
226
|
+
# convert to polydata
|
|
227
|
+
wall = pvf.fc_unstruc2poly(wall)
|
|
228
|
+
|
|
229
|
+
Fsky = 0
|
|
230
|
+
# for all cells constituting the ensemble
|
|
231
|
+
for patch in tqdm(i_sky):
|
|
232
|
+
sky = mesh.extract_cells(patch) # extract one cell
|
|
233
|
+
sky = pvf.fc_unstruc2poly(sky) # convert to polydata
|
|
234
|
+
if pvf.get_visibility(sky, wall): # if the can see each other...
|
|
235
|
+
if pvf.get_visibility_raytrace(sky, wall, meshpoly): # ... if no obstruction
|
|
236
|
+
Fsky += pvf.compute_viewfactor(sky, wall) # compute and increment view factor : F_i->(j+k) = F_i->j + F_i->k
|
|
237
|
+
|
|
238
|
+
# same for building 1
|
|
239
|
+
Fbuilding1 = 0
|
|
240
|
+
for patch in tqdm(i_building1):
|
|
241
|
+
bldng1 = mesh.extract_cells(patch)
|
|
242
|
+
bldng1 = pvf.fc_unstruc2poly(bldng1)
|
|
243
|
+
if pvf.get_visibility(bldng1, wall):
|
|
244
|
+
if pvf.get_visibility_raytrace(bldng1, wall, meshpoly):
|
|
245
|
+
Fbuilding1 += pvf.compute_viewfactor(bldng1, wall)
|
|
246
|
+
|
|
247
|
+
# same for building 2
|
|
248
|
+
Fbuilding2 = 0
|
|
249
|
+
for patch in tqdm(i_building2):
|
|
250
|
+
bldng2 = mesh.extract_cells(patch)
|
|
251
|
+
bldng2 = pvf.fc_unstruc2poly(bldng2)
|
|
252
|
+
if pvf.get_visibility(bldng2, wall):
|
|
253
|
+
if pvf.get_visibility_raytrace(bldng2, wall, meshpoly):
|
|
254
|
+
Fbuilding2 += pvf.compute_viewfactor(bldng2, wall)
|
|
255
|
+
|
|
256
|
+
# complementarity implies \sigma F_i = 1 : compute viewfactor to ground
|
|
257
|
+
Fground = 1-Fbuilding1-Fbuilding2-Fsky
|
|
258
|
+
|
|
259
|
+
print('\n----------------------')
|
|
260
|
+
print('Wall to environment view factors :')
|
|
261
|
+
print('\tSky ', round(Fsky, 4))
|
|
262
|
+
print('\tBuilding 1 ', round(Fbuilding1, 4))
|
|
263
|
+
print('\tBuilding 2 ', round(Fbuilding2, 4))
|
|
264
|
+
print('Ground view factor :')
|
|
265
|
+
print('\tGround ', round(Fground, 4))
|
|
266
|
+
```
|
|
267
|
+
The code yields following view factors :
|
|
268
|
+
```math
|
|
269
|
+
F_{\text{sky}} = 0.345 \\
|
|
270
|
+
F_{\text{ground}} = 0.373 \\
|
|
271
|
+
F_{\text{building1}} = 0.251 \\
|
|
272
|
+
F_{\text{building2}} = 0.031 \\
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Understanding the obstruction check function
|
|
276
|
+
|
|
277
|
+
In real life problems, obstacles may well hinder the radiation heat transfer between surfaces. We make use here of [pyvista's raytrace function](https://docs.pyvista.org/examples/01-filter/poly-ray-trace.html) to perform obstruction tests, as per following example, much inspired from pyvista's online documentation.
|
|
278
|
+
|
|
279
|
+
<img src="https://gitlab.com/arep-dev/pyViewFactor/-/raw/3ac896fe2f420443c7c96eaa9dbbb955474e80d5/img/intersection_simple.png?raw=true" alt="Obstruction check between rectangles" width="350"/>
|
|
280
|
+
|
|
281
|
+
The code snippet below shows how the ray tracing function works and allows to understand its usage in the pyviewfactor `get_visibility_raytrace` function.
|
|
282
|
+
```python
|
|
283
|
+
import pyvista as pv
|
|
284
|
+
from pyviewfactor import get_visibility_raytrace
|
|
285
|
+
# let us first create two rectangles
|
|
286
|
+
pointa = [1, 0, 0]
|
|
287
|
+
pointb = [1, 1, 0]
|
|
288
|
+
pointc = [0, 1, 0]
|
|
289
|
+
pointd = [0, 0, 0]
|
|
290
|
+
rectangle_down = pv.Rectangle([pointa, pointb, pointc, pointd])
|
|
291
|
+
pointa = [1, 0, 1]
|
|
292
|
+
pointb = [1, 1, 1]
|
|
293
|
+
pointc = [0, 1, 1]
|
|
294
|
+
pointd = [0, 0, 1]
|
|
295
|
+
rectangle_up = pv.Rectangle([pointa, pointb, pointc, pointd])
|
|
296
|
+
|
|
297
|
+
# a circle will be the obstruction
|
|
298
|
+
z_translation, r = 0.5, 2
|
|
299
|
+
obstacle = pv.Circle(radius=r, resolution=10)
|
|
300
|
+
# we translate the obstruction right between both rectangles
|
|
301
|
+
obstacle.translate([0, 0, z_translation], inplace=True)
|
|
302
|
+
# Define line segment
|
|
303
|
+
start = rectangle_down.cell_centers().points[0]
|
|
304
|
+
stop = rectangle_up.cell_centers().points[0]
|
|
305
|
+
# Perform ray trace
|
|
306
|
+
points, ind = obstacle.ray_trace(start, stop)
|
|
307
|
+
|
|
308
|
+
# Create geometry to represent ray trace
|
|
309
|
+
ray = pv.Line(start, stop)
|
|
310
|
+
intersection = pv.PolyData(points)
|
|
311
|
+
|
|
312
|
+
# Render the result
|
|
313
|
+
p = pv.Plotter(notebook=True)
|
|
314
|
+
p.add_mesh(obstacle, show_edges=True, opacity=0.5, color="red", lighting=False, label="obstacle")
|
|
315
|
+
p.add_mesh(rectangle_up, color="blue", line_width=5, opacity=0.5, label="rect up")
|
|
316
|
+
p.add_mesh(rectangle_down, color="yellow", line_width=5,opacity=0.5, label="rect down")
|
|
317
|
+
p.add_mesh(ray, color="green", line_width=5, label="ray trace")
|
|
318
|
+
|
|
319
|
+
# if any intersection
|
|
320
|
+
if intersection.n_cells > 0:
|
|
321
|
+
p.add_mesh(intersection, color="green", point_size=25, label="Intersection Points")
|
|
322
|
+
p.add_legend()
|
|
323
|
+
p.show(cpos="yz")
|
|
324
|
+
|
|
325
|
+
#now a call to the obstruction check function
|
|
326
|
+
print(get_visibility_raytrace(rectangle_up, rectangle_down, obstacle))
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
More complex scenes can then be treated with the function `get_visibility_raytrace`.
|
|
330
|
+
|
|
331
|
+
<img src="https://gitlab.com/arep-dev/pyViewFactor/-/raw/3ac896fe2f420443c7c96eaa9dbbb955474e80d5/img/intersection.png?raw=true" alt="Obstruction within an enclosure" width="350"/>
|
|
332
|
+
|
|
333
|
+
## Installation
|
|
334
|
+
pyViewFactor can be installed from [PyPi](https://pypi.org/project/pyviewfactor/) using `pip` on Python >= 3.7:
|
|
335
|
+
```
|
|
336
|
+
pip install pyviewfactor
|
|
337
|
+
```
|
|
338
|
+
You can also visit [PyPi](https://pypi.org/project/pyviewfactor/) or [Gitlab](https://gitlab.com/arep-dev/pyViewFactor) to download the sources.
|
|
339
|
+
|
|
340
|
+
Requirements:
|
|
341
|
+
```
|
|
342
|
+
numpy==1.17.4
|
|
343
|
+
pandas==1.4.2
|
|
344
|
+
pyvista==0.35.2
|
|
345
|
+
scipy==1.8.1
|
|
346
|
+
numba>=0.55.2
|
|
347
|
+
```
|
|
348
|
+
The code will probably work with lower versions of the required packages, however this has not been tested.
|
|
349
|
+
|
|
350
|
+
__Note__ - If you are alergic to `numba`, you may `pip install pyviewfactor==0.0.10` that works (and give up the x2 speed-up in view factor computation).
|
|
351
|
+
|
|
352
|
+
## Authors and acknowledgment
|
|
353
|
+
Mateusz BOGDAN, Edouard WALTHER, Marc ALECIAN, Mina CHAPON
|
|
354
|
+
|
|
355
|
+
## Citation
|
|
356
|
+
There is even a [conference paper](https://www.researchgate.net/publication/360835982_Calcul_des_facteurs_de_forme_entre_polygones_-Application_a_la_thermique_urbaine_et_aux_etudes_de_confort), showing analytical validations :
|
|
357
|
+
> Mateusz BOGDAN, Edouard WALTHER, Marc ALECIAN and Mina CHAPON. _Calcul des facteurs de forme entre polygones - Application à la thermique urbaine et aux études de confort_. IBPSA France 2022, Châlons-en-Champagne.
|
|
358
|
+
|
|
359
|
+
Bibtex entry:
|
|
360
|
+
``` latex
|
|
361
|
+
@inproceedings{pyViewFactor22bogdan,
|
|
362
|
+
authors = "Mateusz BOGDAN and Edouard WALTHER and Marc ALECIAN and Mina CHAPON",
|
|
363
|
+
title = "Calcul des facteurs de forme entre polygones - Application à la thermique urbaine et aux études de confort",
|
|
364
|
+
year = "2022",
|
|
365
|
+
organization = "IBPSA France",
|
|
366
|
+
venue = "Châlons-en-Champagne, France"
|
|
367
|
+
note = "IBPSA France 2022",
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
## License
|
|
372
|
+
MIT License - Copyright (c) AREP 2022
|
|
373
|
+
|
|
374
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
pyviewfactor/__init__.py,sha256=_5V864bwi7Ve90aeTjT06wAfrTCh9niglmO6dcVfUZA,108
|
|
2
|
+
pyviewfactor/_version.py,sha256=RNI1tvOfu8z02dsP4x4cr_ZcOGU0a_Hplab8gXfI7ms,137
|
|
3
|
+
pyviewfactor/pyviewfactor.py,sha256=6c4E_g3iq8zcBh7IQQE8L_dkppI4gkMPSz-EpKom4Eo,23972
|
|
4
|
+
pyviewfactor-0.1.0.dist-info/LICENSE.txt,sha256=IH0BqC-YAOlXBRkBCWdV-XVMtk3QxCQtnyU7SkF2YtY,1061
|
|
5
|
+
pyviewfactor-0.1.0.dist-info/METADATA,sha256=baveALBy-N064xK9TxU7jcSNLgVnasQDM0PtD8IprtU,17105
|
|
6
|
+
pyviewfactor-0.1.0.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
|
|
7
|
+
pyviewfactor-0.1.0.dist-info/top_level.txt,sha256=FOOzkgn9sXmoGuzjb2pC747QeXA-XP2xRjcr9nDvgBs,13
|
|
8
|
+
pyviewfactor-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyviewfactor
|