FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
FEAScript-core/src/visualization/plotlyPlot.js at main · FEAScript/FEAScript-core · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
FEAScript
/
FEAScript-core
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
13
Star
68
Code
Issues
7
Pull requests
0
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
FEAScript-core
/
src
/
visualization
/
plotlyPlot.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
458 lines (421 loc) · 15.7 KB
Breadcrumbs
FEAScript-core
/
src
/
visualization
/
plotlyPlot.js
Copy path
File metadata and controls
458 lines (421 loc) · 15.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/**
* ════════════════════════════════════════════════════════════════
* FEAScript Core Library
* Lightweight Finite Element Simulation in JavaScript
* Version: 0.3.0 (RC) | https://feascript.com
* MIT License © 2023–2026 FEAScript
* ════════════════════════════════════════════════════════════════
*/
// Internal imports
import
{
prepareMesh
,
pointInsideQuadrilateral
,
computeNodeNeighbors
,
getBoundarySegments
,
}
from
"../mesh/meshUtils.js"
;
import
{
BasisFunctions
}
from
"../mesh/basisFunctions.js"
;
import
{
basicLog
,
debugLog
,
errorLog
}
from
"../utilities/logging.js"
;
/**
* Function to create plots of the solution vector
*
@param
{
object
} result - Object containing solution vector and mesh information
*
@param
{
object
} model - Object containing model properties
*
@param
{
string
} plotType - The type of plot
*
@param
{
string
} plotDivId - The id of the div where the plot will be rendered
*/
export
function
plotSolution
(
model
,
result
,
plotType
,
plotDivId
)
{
console
.
time
(
"plottingTime"
)
;
const
{
nodesXCoordinates
,
nodesYCoordinates
}
=
result
.
nodesCoordinates
;
const
solutionVector
=
result
.
solutionVector
;
const
solverConfig
=
model
.
solverConfig
;
const
meshDimension
=
model
.
meshConfig
.
meshDimension
;
const
meshData
=
prepareMesh
(
model
.
meshConfig
)
;
// Retrieve mesh connectivity details (used in splitQuadrilateral)
if
(
meshDimension
===
"1D"
&&
plotType
===
"line"
)
{
// Check if solutionVector is a nested array
let
yData
;
if
(
solutionVector
.
length
>
0
&&
Array
.
isArray
(
solutionVector
[
0
]
)
)
{
yData
=
solutionVector
.
map
(
(
arr
)
=>
arr
[
0
]
)
;
}
else
{
yData
=
solutionVector
;
}
let
xData
=
Array
.
from
(
nodesXCoordinates
)
;
let
lineData
=
{
x
:
nodesXCoordinates
,
y
:
yData
,
mode
:
"lines"
,
type
:
"scatter"
,
line
:
{
color
:
"rgb(219, 64, 82)"
,
width
:
2
}
,
name
:
"Solution"
,
}
;
let
maxWindowWidth
=
Math
.
min
(
window
.
innerWidth
,
700
)
;
let
plotWidth
=
Math
.
min
(
maxWindowWidth
,
600
)
;
let
plotHeight
=
300
;
let
layout
=
{
title
:
`line plot -
${
solverConfig
}
`
,
width
:
plotWidth
,
height
:
plotHeight
,
xaxis
:
{
title
:
"x"
}
,
yaxis
:
{
title
:
"Solution"
}
,
margin
:
{
l
:
50
,
r
:
50
,
t
:
50
,
b
:
50
}
,
}
;
Plotly
.
newPlot
(
plotDivId
,
[
lineData
]
,
layout
,
{
responsive
:
true
}
)
;
console
.
timeEnd
(
"plottingTime"
)
;
}
else
if
(
meshDimension
===
"2D"
&&
plotType
===
"contour"
)
{
// Check if solutionVector is a nested array
let
zData
;
if
(
Array
.
isArray
(
solutionVector
[
0
]
)
)
{
zData
=
solutionVector
.
map
(
(
val
)
=>
val
[
0
]
)
;
}
else
{
zData
=
solutionVector
;
}
// Plot sizing parameters
let
maxWindowWidth
=
Math
.
min
(
window
.
innerWidth
,
700
)
;
let
minX
=
Math
.
min
(
...
nodesXCoordinates
)
;
let
maxX
=
Math
.
max
(
...
nodesXCoordinates
)
;
let
minY
=
Math
.
min
(
...
nodesYCoordinates
)
;
let
maxY
=
Math
.
max
(
...
nodesYCoordinates
)
;
let
lengthX
=
maxX
-
minX
;
let
lengthY
=
maxY
-
minY
;
let
aspectRatio
=
lengthY
/
lengthX
;
let
plotWidth
=
Math
.
min
(
maxWindowWidth
,
600
)
;
let
plotHeight
=
plotWidth
*
aspectRatio
;
// Layout properties
let
layout
=
{
title
:
`
${
plotType
}
plot -
${
solverConfig
}
`
,
width
:
plotWidth
,
height
:
plotHeight
,
xaxis
:
{
title
:
"x"
}
,
yaxis
:
{
title
:
"y"
,
scaleanchor
:
"x"
,
scaleratio
:
1
,
}
,
margin
:
{
l
:
50
,
r
:
50
,
t
:
50
,
b
:
50
}
,
hovermode
:
"closest"
,
}
;
// Create the plot
let
contourData
=
{
x
:
nodesXCoordinates
,
y
:
nodesYCoordinates
,
z
:
zData
,
type
:
"contour"
,
line
:
{
smoothing
:
0.85
,
}
,
contours
:
{
coloring
:
"heatmap"
,
showlabels
:
false
,
}
,
//colorscale: 'Viridis',
colorbar
:
{
title
:
"Solution"
,
}
,
name
:
"Solution Field"
,
}
;
Plotly
.
newPlot
(
plotDivId
,
[
contourData
]
,
layout
,
{
responsive
:
true
}
)
;
console
.
timeEnd
(
"plottingTime"
)
;
}
}
/**
* Function to generate a dense visualization grid and interpolate the FEM solution on it
*
@param
{
object
} result - Object containing solution vector and mesh information
*
@param
{
object
} model - Object containing model properties
*
@param
{
string
} plotType - The type of plot
*
@param
{
string
} plotDivId - The id of the div where the plot will be rendered
*/
export
function
plotInterpolatedSolution
(
model
,
result
,
plotType
,
plotDivId
)
{
console
.
time
(
"plottingTime"
)
;
const
{
nodesXCoordinates
,
nodesYCoordinates
}
=
result
.
nodesCoordinates
;
// TODO: Check if we should place it inside the 2D block
const
meshDimension
=
model
.
meshConfig
.
meshDimension
;
const
meshData
=
prepareMesh
(
model
.
meshConfig
)
;
// Retrieve mesh connectivity details
// Initialize BasisFunctions once here to avoid creating it inside the loop
const
basisFunctions
=
new
BasisFunctions
(
{
meshDimension
:
model
.
meshConfig
.
meshDimension
,
elementOrder
:
model
.
meshConfig
.
elementOrder
,
}
)
;
if
(
meshDimension
===
"1D"
&&
plotType
===
"line"
)
{
// 1D plot region
}
else
if
(
meshDimension
===
"2D"
&&
plotType
===
"contour"
)
{
const
visNodeXCoordinates
=
[
]
;
const
visNodeYCoordinates
=
[
]
;
const
lengthX
=
Math
.
max
(
...
nodesXCoordinates
)
-
Math
.
min
(
...
nodesXCoordinates
)
;
const
lengthY
=
Math
.
max
(
...
nodesYCoordinates
)
-
Math
.
min
(
...
nodesYCoordinates
)
;
const
visPoinsPerUnit
=
50
;
// Number of nodes per one length unit of the visualization grid
const
visNodesX
=
Math
.
round
(
lengthX
*
visPoinsPerUnit
)
;
// Number of nodes along the x-axis of the visualization grid
const
visNodesY
=
Math
.
round
(
lengthY
*
visPoinsPerUnit
)
;
// Number of nodes along the y-axis of the visualization grid
const
deltavisX
=
lengthX
/
(
visNodesX
-
1
)
;
const
deltavisY
=
lengthY
/
(
visNodesY
-
1
)
;
let
visSolution
=
[
]
;
visNodeXCoordinates
[
0
]
=
Math
.
min
(
...
nodesXCoordinates
)
;
visNodeYCoordinates
[
0
]
=
Math
.
min
(
...
nodesYCoordinates
)
;
for
(
let
visNodeIndexY
=
1
;
visNodeIndexY
<
visNodesY
;
visNodeIndexY
++
)
{
visNodeXCoordinates
[
visNodeIndexY
]
=
visNodeXCoordinates
[
0
]
;
visNodeYCoordinates
[
visNodeIndexY
]
=
visNodeYCoordinates
[
0
]
+
visNodeIndexY
*
deltavisY
;
}
for
(
let
visNodeIndexX
=
1
;
visNodeIndexX
<
visNodesX
;
visNodeIndexX
++
)
{
const
nnode
=
visNodeIndexX
*
visNodesY
;
visNodeXCoordinates
[
nnode
]
=
visNodeXCoordinates
[
0
]
+
visNodeIndexX
*
deltavisX
;
visNodeYCoordinates
[
nnode
]
=
visNodeYCoordinates
[
0
]
;
for
(
let
visNodeIndexY
=
1
;
visNodeIndexY
<
visNodesY
;
visNodeIndexY
++
)
{
visNodeXCoordinates
[
nnode
+
visNodeIndexY
]
=
visNodeXCoordinates
[
nnode
]
;
visNodeYCoordinates
[
nnode
+
visNodeIndexY
]
=
visNodeYCoordinates
[
nnode
]
+
visNodeIndexY
*
deltavisY
;
}
}
const
visNodeCoordinates
=
{
visNodeXCoordinates
,
visNodeYCoordinates
}
;
// Initialize visSolution with null for all visualization nodes
visSolution
=
new
Array
(
visNodesX
*
visNodesY
)
.
fill
(
null
)
;
// Get boundary segments for ray casting
const
boundarySegments
=
getBoundarySegments
(
meshData
)
;
// Perform adjacency-based search to find which element contains a given point (quick search)
const
{
nodeNeighbors
,
neighborCount
}
=
computeNodeNeighbors
(
meshData
)
;
let
lastParentElement
=
0
;
for
(
let
visNodeIndex
=
0
;
visNodeIndex
<
visNodesX
*
visNodesY
;
visNodeIndex
++
)
{
// Ray casting check
if
(
!
pointInsidePolygon
(
visNodeXCoordinates
[
visNodeIndex
]
,
visNodeYCoordinates
[
visNodeIndex
]
,
boundarySegments
,
)
)
{
continue
;
}
let
found
=
false
;
for
(
let
localNodeIndex
=
0
;
localNodeIndex
<
meshData
.
nop
[
lastParentElement
]
.
length
;
localNodeIndex
++
)
{
let
globalNodeIndex
=
meshData
.
nop
[
lastParentElement
]
[
localNodeIndex
]
-
1
;
for
(
let
neighborElementsIndex
=
0
;
neighborElementsIndex
<
neighborCount
[
globalNodeIndex
]
;
neighborElementsIndex
++
)
{
let
currentElement
=
nodeNeighbors
[
globalNodeIndex
]
[
neighborElementsIndex
]
;
const
searchResult
=
pointSearch
(
model
,
meshData
,
result
,
currentElement
,
visNodeXCoordinates
[
visNodeIndex
]
,
visNodeYCoordinates
[
visNodeIndex
]
,
basisFunctions
,
)
;
if
(
searchResult
.
inside
)
{
lastParentElement
=
currentElement
;
visSolution
[
visNodeIndex
]
=
searchResult
.
value
;
found
=
true
;
break
;
}
}
if
(
found
)
break
;
}
// Scan all elements to find which element contains a given point (slow search)
if
(
!
found
)
{
for
(
let
currentElement
=
0
;
currentElement
<
meshData
.
nop
.
length
;
currentElement
++
)
{
const
searchResult
=
pointSearch
(
model
,
meshData
,
result
,
currentElement
,
visNodeXCoordinates
[
visNodeIndex
]
,
visNodeYCoordinates
[
visNodeIndex
]
,
basisFunctions
,
)
;
if
(
searchResult
.
inside
)
{
lastParentElement
=
currentElement
;
visSolution
[
visNodeIndex
]
=
searchResult
.
value
;
found
=
true
;
break
;
}
}
}
}
// Plot sizing parameters
let
maxWindowWidth
=
Math
.
min
(
window
.
innerWidth
,
700
)
;
let
aspectRatio
=
lengthY
/
lengthX
;
let
plotWidth
=
Math
.
min
(
maxWindowWidth
,
600
)
;
let
plotHeight
=
plotWidth
*
aspectRatio
;
// Layout properties
let
layout
=
{
title
:
`
${
plotType
}
plot (interpolated) -
${
model
.
solverConfig
}
`
,
width
:
plotWidth
,
height
:
plotHeight
,
xaxis
:
{
title
:
"x"
}
,
yaxis
:
{
title
:
"y"
,
scaleanchor
:
"x"
,
scaleratio
:
1
,
}
,
margin
:
{
l
:
50
,
r
:
50
,
t
:
50
,
b
:
50
}
,
hovermode
:
"closest"
,
}
;
// Create the plot
let
contourData
=
{
x
:
visNodeXCoordinates
,
y
:
visNodeYCoordinates
,
z
:
visSolution
,
type
:
"contour"
,
connectgaps
:
false
,
hoverongaps
:
false
,
line
:
{
smoothing
:
0.85
,
}
,
contours
:
{
coloring
:
"heatmap"
,
showlabels
:
false
,
}
,
//colorscale: 'Viridis',
colorbar
:
{
title
:
"Solution"
,
}
,
name
:
"Interpolated Solution Field"
,
}
;
Plotly
.
newPlot
(
plotDivId
,
[
contourData
]
,
layout
,
{
responsive
:
true
}
)
;
console
.
timeEnd
(
"plottingTime"
)
;
}
}
/**
* Function to search if a point is inside an element and interpolate the solution
*
@param
{
object
} model - Object containing model properties
*
@param
{
object
} meshData - Object containing mesh data
*
@param
{
object
} result - Object containing solution vector and mesh information
*
@param
{
number
} currentElement - Index of the element to check
*
@param
{
number
} visNodeXCoordinate - X-coordinate of the point
*
@param
{
number
} visNodeYCoordinate - Y-coordinate of the point
*
@param
{
object
} basisFunctions - Instance of BasisFunctions class
*
@returns
{
object
} Object containing inside boolean and interpolated value
*/
function
pointSearch
(
model
,
meshData
,
result
,
currentElement
,
visNodeXCoordinate
,
visNodeYCoordinate
,
basisFunctions
,
)
{
const
{
nodesXCoordinates
,
nodesYCoordinates
}
=
result
.
nodesCoordinates
;
const
nodesPerElement
=
meshData
.
nop
[
currentElement
]
.
length
;
if
(
nodesPerElement
===
4
)
{
// Linear quadrilateral element
let
vertices
=
[
[
nodesXCoordinates
[
meshData
.
nop
[
currentElement
]
[
0
]
-
1
]
,
nodesYCoordinates
[
meshData
.
nop
[
currentElement
]
[
0
]
-
1
]
,
]
,
[
nodesXCoordinates
[
meshData
.
nop
[
currentElement
]
[
1
]
-
1
]
,
nodesYCoordinates
[
meshData
.
nop
[
currentElement
]
[
1
]
-
1
]
,
]
,
[
nodesXCoordinates
[
meshData
.
nop
[
currentElement
]
[
2
]
-
1
]
,
nodesYCoordinates
[
meshData
.
nop
[
currentElement
]
[
2
]
-
1
]
,
]
,
[
nodesXCoordinates
[
meshData
.
nop
[
currentElement
]
[
3
]
-
1
]
,
nodesYCoordinates
[
meshData
.
nop
[
currentElement
]
[
3
]
-
1
]
,
]
,
]
;
const
pointCheck
=
pointInsideQuadrilateral
(
visNodeXCoordinate
,
visNodeYCoordinate
,
vertices
)
;
if
(
pointCheck
.
inside
)
{
return
{
inside
:
true
,
value
:
solutionInterpolation
(
model
,
meshData
,
result
,
currentElement
,
pointCheck
.
ksi
,
pointCheck
.
eta
,
basisFunctions
,
)
,
}
;
}
}
else
if
(
nodesPerElement
===
9
)
{
// Quadratic quadrilateral element
let
vertices
=
[
[
nodesXCoordinates
[
meshData
.
nop
[
currentElement
]
[
0
]
-
1
]
,
nodesYCoordinates
[
meshData
.
nop
[
currentElement
]
[
0
]
-
1
]
,
]
,
[
nodesXCoordinates
[
meshData
.
nop
[
currentElement
]
[
2
]
-
1
]
,
nodesYCoordinates
[
meshData
.
nop
[
currentElement
]
[
2
]
-
1
]
,
]
,
[
nodesXCoordinates
[
meshData
.
nop
[
currentElement
]
[
6
]
-
1
]
,
nodesYCoordinates
[
meshData
.
nop
[
currentElement
]
[
6
]
-
1
]
,
]
,
[
nodesXCoordinates
[
meshData
.
nop
[
currentElement
]
[
8
]
-
1
]
,
nodesYCoordinates
[
meshData
.
nop
[
currentElement
]
[
8
]
-
1
]
,
]
,
]
;
const
pointCheck
=
pointInsideQuadrilateral
(
visNodeXCoordinate
,
visNodeYCoordinate
,
vertices
)
;
if
(
pointCheck
.
inside
)
{
return
{
inside
:
true
,
value
:
solutionInterpolation
(
model
,
meshData
,
result
,
currentElement
,
pointCheck
.
ksi
,
pointCheck
.
eta
,
basisFunctions
,
)
,
}
;
}
}
// TODO: Add also triangular element cases
return
{
inside
:
false
,
value
:
null
}
;
}
/**
* Function to interpolate the solution at a specific point (ksi, eta) within an element
*
@param
{
object
} model - Object containing model properties
*
@param
{
object
} meshData - Object containing mesh data
*
@param
{
object
} result - Object containing solution vector and mesh information
*
@param
{
number
} elementIndex - Index of the element containing the point
*
@param
{
number
} ksi - First natural coordinate (ksi)
*
@param
{
number
} eta - Second natural coordinate (eta)
*
@param
{
object
} basisFunctions - Instance of BasisFunctions class
*
@returns
{
number
} Interpolated solution value
*/
function
solutionInterpolation
(
model
,
meshData
,
result
,
elementIndex
,
ksi
,
eta
,
basisFunctions
)
{
// Initialize FEA components
const
solutionVector
=
result
.
solutionVector
;
const
nodesPerElement
=
meshData
.
nop
[
elementIndex
]
.
length
;
// Get basis functions for the current point
const
basisFunctionsAndDerivatives
=
basisFunctions
.
getBasisFunctions
(
ksi
,
eta
)
;
let
basisFunction
=
basisFunctionsAndDerivatives
.
basisFunction
;
// Check if solutionVector is a nested array
let
zData
;
if
(
Array
.
isArray
(
solutionVector
[
0
]
)
)
{
zData
=
solutionVector
.
map
(
(
val
)
=>
val
[
0
]
)
;
}
else
{
zData
=
solutionVector
;
}
// Interpolate solution
let
solutionInterpolationValue
=
0
;
for
(
let
localNodeIndex
=
0
;
localNodeIndex
<
nodesPerElement
;
localNodeIndex
++
)
{
solutionInterpolationValue
+=
zData
[
meshData
.
nop
[
elementIndex
]
[
localNodeIndex
]
-
1
]
*
basisFunction
[
localNodeIndex
]
;
}
return
solutionInterpolationValue
;
}
/**
* Function to check if a point is inside a polygon using ray casting algorithm
*
@param
{
number
} x - X-coordinate of the point
*
@param
{
number
} y - Y-coordinate of the point
*
@param
{
array
} segments - Array of boundary segments
*
@returns
{
boolean
} True if the point is inside the polygon
*/
function
pointInsidePolygon
(
x
,
y
,
segments
)
{
let
inside
=
false
;
for
(
let
i
=
0
;
i
<
segments
.
length
;
i
++
)
{
const
[
[
x1
,
y1
]
,
[
x2
,
y2
]
]
=
segments
[
i
]
;
const
intersect
=
y1
>
y
!==
y2
>
y
&&
x
<
(
(
x2
-
x1
)
*
(
y
-
y1
)
)
/
(
y2
-
y1
)
+
x1
;
if
(
intersect
)
inside
=
!
inside
;
}
return
inside
;
}
Back
|
FazBrowse Home
|
New Git URL