radia 1.3.3__py3-none-any.whl → 1.3.5__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.
@@ -0,0 +1,293 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ radia_ngsolve_utils.py - Utilities for Radia-NGSolve integration
4
+
5
+ This module provides high-level utilities for working with Radia and NGSolve together,
6
+ including mesh import and VTK export functionality.
7
+
8
+ Author: Radia Development Team
9
+ Created: 2025-11-22
10
+ Version: 1.0.0
11
+
12
+ Key Features
13
+ ------------
14
+ - Import Netgen/OCC tetrahedral meshes to Radia
15
+ - Export Radia geometry to VTK for visualization
16
+ - Export NGSolve solutions to VTK
17
+ - Unified interface for both solvers
18
+
19
+ Functions
20
+ ---------
21
+ create_radia_from_mesh : Import Netgen mesh to Radia with VTK export
22
+ export_ngsolve_vtk : Export NGSolve solution to VTK
23
+ export_radia_vtk : Export Radia geometry to VTK
24
+
25
+ Example
26
+ -------
27
+ >>> import radia as rad
28
+ >>> from ngsolve import Mesh
29
+ >>> from netgen.occ import Box, OCCGeometry
30
+ >>> from radia_ngsolve_utils import create_radia_from_mesh, export_radia_vtk
31
+ >>>
32
+ >>> # Setup
33
+ >>> rad.FldUnits('m')
34
+ >>>
35
+ >>> # Create mesh
36
+ >>> geo = OCCGeometry(Box((-0.5, -0.5, -0.5), (0.5, 0.5, 0.5)))
37
+ >>> mesh = Mesh(geo.GenerateMesh(maxh=0.2))
38
+ >>>
39
+ >>> # Import to Radia and export VTK
40
+ >>> mag = create_radia_from_mesh(
41
+ ... mesh,
42
+ ... material={'magnetization': [0, 0, 1.2]},
43
+ ... vtk_filename='magnet'
44
+ ... )
45
+ >>>
46
+ >>> # Apply material and solve
47
+ >>> rad.MatApl(mag, rad.MatStd('NdFeB', 1.2))
48
+ >>> rad.Solve(mag, 0.0001, 10000)
49
+ >>>
50
+ >>> # Export final geometry
51
+ >>> export_radia_vtk(mag, 'magnet_solved')
52
+ """
53
+
54
+ import os
55
+ import radia as rad
56
+ from netgen_mesh_import import netgen_mesh_to_radia
57
+ from radia_vtk_export import exportGeometryToVTK
58
+
59
+
60
+ def create_radia_from_mesh(mesh, material=None, units='m', combine=True,
61
+ verbose=False, material_filter=None, vtk_filename=None):
62
+ """
63
+ Import Netgen mesh to Radia and optionally export to VTK.
64
+
65
+ This is a convenience wrapper around netgen_mesh_to_radia that adds
66
+ automatic VTK export functionality.
67
+
68
+ Parameters
69
+ ----------
70
+ mesh : ngsolve.Mesh
71
+ NGSolve mesh object to import
72
+ material : dict, optional
73
+ Material properties. Should contain 'magnetization' key with [Mx, My, Mz] in Tesla.
74
+ Default: {'magnetization': [0, 0, 0]}
75
+ units : str, optional
76
+ Length units: 'm' (meters) or 'mm' (millimeters)
77
+ Default: 'm' (recommended for NGSolve integration)
78
+ combine : bool, optional
79
+ If True, combine all tetrahedra into single container
80
+ If False, return list of individual tetrahedra
81
+ Default: True
82
+ verbose : bool, optional
83
+ Print detailed import information
84
+ Default: False
85
+ material_filter : str, optional
86
+ Filter mesh by material name (e.g., 'magnetic', 'air')
87
+ If None, import all materials
88
+ Default: None
89
+ vtk_filename : str, optional
90
+ If provided, export geometry to VTK file with this name (without .vtk extension)
91
+ Default: None (no VTK export)
92
+
93
+ Returns
94
+ -------
95
+ int or list
96
+ Radia object ID (if combine=True) or list of object IDs (if combine=False)
97
+
98
+ Example
99
+ -------
100
+ >>> from netgen.occ import Box, OCCGeometry
101
+ >>> from ngsolve import Mesh
102
+ >>> import radia as rad
103
+ >>> from radia_ngsolve_utils import create_radia_from_mesh
104
+ >>>
105
+ >>> rad.FldUnits('m')
106
+ >>> geo = OCCGeometry(Box((0, 0, 0), (0.1, 0.1, 0.1)))
107
+ >>> mesh = Mesh(geo.GenerateMesh(maxh=0.03))
108
+ >>>
109
+ >>> magnet = create_radia_from_mesh(
110
+ ... mesh,
111
+ ... material={'magnetization': [0, 0, 1.2]},
112
+ ... vtk_filename='my_magnet'
113
+ ... )
114
+ >>> # Creates my_magnet.vtk in current directory
115
+ """
116
+ # Import mesh to Radia
117
+ radia_obj = netgen_mesh_to_radia(
118
+ mesh=mesh,
119
+ material=material,
120
+ units=units,
121
+ combine=combine,
122
+ verbose=verbose,
123
+ material_filter=material_filter
124
+ )
125
+
126
+ # Export to VTK if requested
127
+ if vtk_filename is not None:
128
+ export_radia_vtk(radia_obj, vtk_filename)
129
+
130
+ return radia_obj
131
+
132
+
133
+ def export_radia_vtk(radia_obj, filename):
134
+ """
135
+ Export Radia geometry to VTK file.
136
+
137
+ Parameters
138
+ ----------
139
+ radia_obj : int
140
+ Radia object ID
141
+ filename : str
142
+ Output filename without .vtk extension
143
+
144
+ Returns
145
+ -------
146
+ str
147
+ Full path to created VTK file
148
+
149
+ Example
150
+ -------
151
+ >>> import radia as rad
152
+ >>> from radia_ngsolve_utils import export_radia_vtk
153
+ >>>
154
+ >>> magnet = rad.ObjRecMag([0, 0, 0], [10, 10, 10], [0, 0, 1])
155
+ >>> vtk_path = export_radia_vtk(magnet, 'my_magnet')
156
+ >>> print(f"VTK exported to: {vtk_path}")
157
+ """
158
+ # Ensure .vtk extension
159
+ if not filename.endswith('.vtk'):
160
+ base_filename = filename
161
+ filename = filename + '.vtk'
162
+ else:
163
+ base_filename = filename[:-4]
164
+
165
+ # Export using radia_vtk_export
166
+ exportGeometryToVTK(radia_obj, base_filename)
167
+
168
+ # Get absolute path
169
+ vtk_path = os.path.abspath(filename)
170
+
171
+ return vtk_path
172
+
173
+
174
+ def export_ngsolve_vtk(mesh, gridfunction, filename, field_name='solution'):
175
+ """
176
+ Export NGSolve solution to VTK file.
177
+
178
+ Uses NGSolve's built-in VTK export functionality.
179
+
180
+ Parameters
181
+ ----------
182
+ mesh : ngsolve.Mesh
183
+ NGSolve mesh
184
+ gridfunction : ngsolve.GridFunction
185
+ Solution to export (e.g., H-field, B-field, potential)
186
+ filename : str
187
+ Output filename without .vtk extension
188
+ field_name : str, optional
189
+ Name for the field in VTK file
190
+ Default: 'solution'
191
+
192
+ Returns
193
+ -------
194
+ str
195
+ Full path to created VTK file
196
+
197
+ Example
198
+ -------
199
+ >>> from ngsolve import *
200
+ >>> from netgen.occ import Box, OCCGeometry
201
+ >>> from radia_ngsolve_utils import export_ngsolve_vtk
202
+ >>>
203
+ >>> geo = OCCGeometry(Box((0, 0, 0), (1, 1, 1)))
204
+ >>> mesh = Mesh(geo.GenerateMesh(maxh=0.2))
205
+ >>> fes = H1(mesh, order=2)
206
+ >>> gfu = GridFunction(fes)
207
+ >>> # ... solve for gfu ...
208
+ >>>
209
+ >>> vtk_path = export_ngsolve_vtk(mesh, gfu, 'solution', 'phi')
210
+ >>> print(f"NGSolve VTK exported to: {vtk_path}")
211
+ """
212
+ from ngsolve import VTKOutput
213
+
214
+ # Ensure .vtk extension
215
+ if filename.endswith('.vtk'):
216
+ filename = filename[:-4]
217
+
218
+ # Create VTK output
219
+ vtk = VTKOutput(
220
+ ma=mesh,
221
+ coefs=[gridfunction],
222
+ names=[field_name],
223
+ filename=filename,
224
+ subdivision=2
225
+ )
226
+ vtk.Do()
227
+
228
+ # Get absolute path
229
+ vtk_path = os.path.abspath(filename + '.vtk')
230
+
231
+ return vtk_path
232
+
233
+
234
+ def export_comparison_vtk(mesh_ngsolve, gfu_ngsolve, radia_obj,
235
+ prefix='comparison', ngsolve_field_name='H_ngsolve'):
236
+ """
237
+ Export both NGSolve and Radia results to VTK for comparison.
238
+
239
+ Creates two VTK files:
240
+ - {prefix}_ngsolve.vtk : NGSolve solution
241
+ - {prefix}_radia.vtk : Radia geometry
242
+
243
+ Parameters
244
+ ----------
245
+ mesh_ngsolve : ngsolve.Mesh
246
+ NGSolve mesh
247
+ gfu_ngsolve : ngsolve.GridFunction
248
+ NGSolve solution
249
+ radia_obj : int
250
+ Radia object ID
251
+ prefix : str, optional
252
+ Prefix for output filenames
253
+ Default: 'comparison'
254
+ ngsolve_field_name : str, optional
255
+ Name for NGSolve field in VTK
256
+ Default: 'H_ngsolve'
257
+
258
+ Returns
259
+ -------
260
+ tuple of str
261
+ (ngsolve_vtk_path, radia_vtk_path)
262
+
263
+ Example
264
+ -------
265
+ >>> from radia_ngsolve_utils import export_comparison_vtk
266
+ >>>
267
+ >>> # After solving both NGSolve and Radia...
268
+ >>> ngsolve_vtk, radia_vtk = export_comparison_vtk(
269
+ ... mesh, gfu, radia_system,
270
+ ... prefix='cube_benchmark',
271
+ ... ngsolve_field_name='H_field'
272
+ ... )
273
+ >>> print(f"NGSolve: {ngsolve_vtk}")
274
+ >>> print(f"Radia: {radia_vtk}")
275
+ """
276
+ ngsolve_filename = f"{prefix}_ngsolve"
277
+ radia_filename = f"{prefix}_radia"
278
+
279
+ ngsolve_vtk = export_ngsolve_vtk(mesh_ngsolve, gfu_ngsolve,
280
+ ngsolve_filename, ngsolve_field_name)
281
+ radia_vtk = export_radia_vtk(radia_obj, radia_filename)
282
+
283
+ return ngsolve_vtk, radia_vtk
284
+
285
+
286
+ # Module-level constants
287
+ __version__ = '1.0.0'
288
+ __all__ = [
289
+ 'create_radia_from_mesh',
290
+ 'export_radia_vtk',
291
+ 'export_ngsolve_vtk',
292
+ 'export_comparison_vtk'
293
+ ]