@@ -193,3 +193,81 @@ def _assign_boundary_conditions(self, m2d: Maxwell2d) -> None:
193193 assignment = "Region" ,
194194 )
195195 m2d .assign_vector_potential (assignment = id_bc_az , vector_value = 0 , boundary = "A0" )
196+
197+ def add_rotor_magnet (self , m2d : Maxwell2d , mag : np .ndarray , fill_height = 0.9 , fill_width = 0.9 , segment_type = None ):
198+ modeler = m2d .modeler
199+
200+ if isinstance (mag , (list , tuple )):
201+ mag = np .concatenate ([np .asarray (m ) for m in mag ], axis = 0 )
202+
203+ pts = np .asarray (mag )
204+ center = pts .mean (axis = 0 )
205+
206+ r = np .linalg .norm (center )
207+ if r < 1e-10 :
208+ return
209+
210+ radial = center / r
211+ long_axis = np .array ([- radial [1 ], radial [0 ]])
212+
213+ pts_centered = pts - center
214+
215+ proj_long = pts_centered @ long_axis
216+ proj_short = pts_centered @ radial
217+
218+ pts_scaled = center + (np .outer (proj_long * fill_height , long_axis ) + np .outer (proj_short * fill_width , radial ))
219+
220+ points = [[f"{ x } mm" , f"{ y } mm" , "0mm" ] for x , y in pts_scaled ]
221+
222+ mag_id = modeler .create_polyline (
223+ points = points ,
224+ close_surface = True ,
225+ cover_surface = True ,
226+ name = "Magnet" ,
227+ )
228+ cs_angle_deg = np .degrees (np .arctan2 (radial [1 ], radial [0 ]))
229+
230+ self .assign_magnet_cs (m2d , mag_id , cs_angle_deg )
231+ mag_id .material_name = self .magnet
232+ mag_id .solve_inside = True
233+ mag_id .color = (255 , 0 , 0 )
234+ mag_id .transparency = 0.0
235+
236+ modeler .set_working_coordinate_system ("Global" )
237+
238+ def _create_magnet_cs (self , m2d : Maxwell2d , name : str , angle_deg : float ) -> None :
239+ m2d .modeler .create_coordinate_system (
240+ origin = [0 , 0 , 0 ],
241+ name = name ,
242+ mode = "axis" ,
243+ x_pointing = [float (np .cos (np .radians (angle_deg ))), float (np .sin (np .radians (angle_deg ))), 0 ],
244+ y_pointing = [float (- np .sin (np .radians (angle_deg ))), float (np .cos (np .radians (angle_deg ))), 0 ],
245+ )
246+
247+ def assign_magnet_cs (self , m2d : Maxwell2d , magnet_name : str , angle_deg : float ) -> None :
248+ cs_name = f"CS_{ magnet_name } "
249+ self ._create_magnet_cs (m2d , cs_name , angle_deg )
250+ m2d .modeler [magnet_name ].part_coordinate_system = cs_name
251+
252+ def _create_material (
253+ self ,
254+ m2d : Maxwell2d ,
255+ name : str ,
256+ permeability : float ,
257+ conductivity : float ,
258+ density : float ,
259+ coercivity : float | None = None ,
260+ coercivity_dir : tuple [float , float , float ] = (0.0 , 0.0 , 0.0 ),
261+ ):
262+ if name .lower () in m2d .materials .material_keys :
263+ return m2d .materials [name ]
264+
265+ mat = m2d .materials .add_material (name )
266+ mat .permeability = permeability
267+ mat .conductivity = conductivity
268+ mat .mass_density = density
269+
270+ if coercivity is not None :
271+ mat .set_magnetic_coercivity (value = coercivity , x = coercivity_dir [0 ], y = coercivity_dir [1 ], z = coercivity_dir [2 ])
272+
273+ return mat
0 commit comments