9

trying to calculate the magnetic field of a permanent magnet using the finite element method I struggle setting up the equation system properly. In the end I want to be able to calculate the magnetic fields for any kind of shapes but I started with the easy system described bellow:

Needs["NDSolve`FEM`"]
(*Generate Geometry*)

pts = {{0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}, {0, 0, 1}, {0, 1,
1}, {1, 1, 1}, {1, 0, 1}};

pts1 = ScalingTransform[{3, 3, 3}][pts];

pts2 = TranslationTransform[{1, 1, 1}][pts];

hex = {{2, 3, 4, 1}, {1, 4, 8, 5}, {4, 3, 7, 8}, {3, 2, 6, 7}, {2, 1, 5, 6}, {5, 8, 7, 6}};

bmesh = BoundaryMeshRegion[Join[pts1, pts2], Polygon[hex], Polygon[hex + 8], MeshCellStyle -> Opacity[0.3]];

HighlightMesh[bmesh, Labeled[0, "Index"]]

bmesh = bmesh = ToBoundaryMesh[bmesh]

To generate the following output:

Region Definition

According to the documentation (Wolfram Documentation) I continued defining markers for the different parts of the region

(*Generate Markers*)
cuboidCoordinates = {{1.5, 1.5, 1.5}};

airCoordinates = {0.5, 0.5, 0.5};

markerColors = {Red, Blue}; markerCoordinates = {{cuboidCoordinates}, {airCoordinates}};

Show[bmesh["Wireframe"],

Graphics3D[

MapThread[{PointSize[0.02], #1, Point /@ #2} &, {markerColors, 

    markerCoordinates}]

] ]

Position of markers

Before I started the meshing process:

markerSpecification = {{airCoordinates, 1 }, {cuboidCoordinates[[1]], 
   2}}
mesh = ToElementMesh[bmesh, "RegionMarker" -> markerSpecification]
mesh["Wireframe"]

enter image description here

When I now start to set up the differential equation I always run into errors:

I use:

\[Mu]Air = 4 \[Pi]*10^-7;

u = {ux[x, y, z], uy[x, y, z], uz[x, y, z]};

M = Piecewise[{{{1, 0, 0}, ElementMarker == 2}}, {0, 0, 0}]; pde = Inactive[Curl][ Inactive[Dot][1/[Mu]Air, Inactive[Curl][u, {x, y, z}]] - M, {x, y, z}] == {0, 0, 0};

bcs = DirichletCondition[ u[x, y, z] == { 0, 0, 0}, {x, y, z} [NotElement] mesh]; usol = NDSolveValue[{bcs, pde}, u, {x, y, z} [Element] mesh]

Where M should be the magnetization of the permanent magnet and get the following error message: Error Message

I compared my solution to the Wolfram documentation as well as the answers in the following Question 3D FEM Vector Potential and unfortunately I can not see my mistake. So my questions are:

  1. How can I make this code work?
  2. Is my approach suitable for this type of problems?

**UPDATE: For a more advances approach to the computation of magnetic field, please have a look at the question follow up question over here **

Thanks in advance.

Best regards Tschibi2000

user21
  • 39,710
  • 8
  • 110
  • 167
Tschibi
  • 877
  • 4
  • 13
  • 2
    I think your equation is not set up correct, you dot multiply a scalar with a vector; also you dirichlet condition is not correct: you can not set u[x,y,z]={0,0,0}; you probably mean something like Thread[Equal[{ux[..].uy[],..},{0,0,0}]] – user21 Nov 19 '21 at 15:02
  • Thanks you for your input. I tried an alternative approach with bcs = {DirichletCondition[ ux[x, y, z] == 0, {x, y, z} [NotElement] mesh], DirichletCondition[ uy[x, y, z] == 0, {x, y, z} [NotElement] mesh], DirichletCondition[ uz[x, y, z] == 0, {x, y, z} [NotElement] mesh]}; but it didnt work either. Your right the blunder is within the PDE itself I will have a look at i further ... – Tschibi Nov 20 '21 at 08:26
  • What is $\mu$ for magnet? – Alex Trounev Nov 21 '21 at 08:18

1 Answers1

7

We can solve this problem using Coulomb gauge $\nabla.\vec{A}=0$ and continue approximation of magnetization instead of piecewise function from my answer here as follows

Needs["NDSolve`FEM`"]
 mesh = ToElementMesh[Cuboid[{0, 0, 0}, {3, 3, 3}], 
  MaxCellMeasure -> .001]
mesh["Wireframe"] 
u = {ux[x, y, z], uy[x, y, z], uz[x, y, z]}; appro = 
 With[{k = 2. 10^4}, ArcTan[k #]/Pi + 1/2 &];
mx = Simplify`PWToUnitStep@
   PiecewiseExpand@
    If[1 <= x <= 2 && 1 <= y <= 2 && 1 <= z <= 2, 1, 0] /. 
  UnitStep -> appro; bmx[x_, y_, z_] := Curl[{mx, 0, 0}, {x, y, z}]

pde = Inactivate[Laplacian[u, {x, y, z}], Laplacian];

bcs = DirichletCondition[{ux[x, y, z] == 0, uy[x, y, z] == 0, uz[x, y, z] == 0}, True]; {Ax, Ay, Az} = NDSolveValue[{bcs, Table[Activate[pde][[i]] == -bmx[x, y, z][[i]], {i, 3}]}, {ux, uy, uz}, {x, y, z} [Element] mesh]

B = Evaluate[ Curl[{Ax[x, y, z], Ay[x, y, z], Az[x, y, z]}, {x, y, z}]];

Visualization of vector potential and magnetic field

{VectorPlot3D[{Ax[x, y, z], Ay[x, y, z], 
   Az[x, y, z]}, {x, y, z} \[Element] mesh, 
  VectorStyle -> Arrowheads[0.01], VectorPoints -> Fine], 
 StreamPlot[{Ay[1.5, y, z], Az[1.5, y, z]}, {y, 0, 3}, {z, 0, 3}]}

Figure 1

{VectorPlot3D[B, {x, y, z} \[Element] mesh, 
  VectorStyle -> Arrowheads[0.01], VectorPoints -> 10], 
 StreamPlot[{B[[1]], B[[3]]} /. y -> 1.5, {x, .5, 2.5}, {z, .50, 2.5},
   VectorPoints -> Fine]}

Figure 2

With this method we can also solve more complex geometry including cylinder magnet and 2 iron bars:

Needs["NDSolve`FEM`"]
bmesh = Region[
  RegionUnion[Cylinder[{{0, 0, -0.25}, {0, 0, .25}}, 1.0], 
   Cuboid[{3.0, -1.5, -0.25}, {3.5, 1.5, .25}], 
   Cuboid[{-3.5, -1.5, 0}, {-3.0, 1.5, .5}]]]; Region[bmesh]

mesh = ToElementMesh[Cuboid[{-6, -3, -2}, {6, 3, 2}], MaxCellMeasure -> .005] m = mesh["Wireframe"]

Show[m, bmesh]

Figure 3

Properties of magnet and iron bar

u = {ux[x, y, z], uy[x, y, z], uz[x, y, z]}; appro = 
 With[{k = 2. 10^4}, ArcTan[k #]/Pi + 1/2 &];
mz = Simplify`PWToUnitStep@
   PiecewiseExpand@If[x^2 + y^2 <= 1 && -.25 <= z <= .25, 1, 0] /. 
  UnitStep -> appro; nu1 = 
 Simplify`PWToUnitStep@
   PiecewiseExpand@
    If[-3.5 <= x <= -3. && -1.5 <= y <= 1.5 && -.25 <= z <= .25, 
     1/1000, 1] /. UnitStep -> appro; nu2 = 
 Simplify`PWToUnitStep@
   PiecewiseExpand@
    If[3. <= x <= 3.5 && -1.5 <= y <= 1.5 && -.25 <= z <= .25, 1/1000,
      1] /. UnitStep -> appro; 
bmz[x_, y_, z_] := Curl[{0, 0, mz}, {x, y, z}]

PDEs, boundary condition and solution

pde = Inactivate[(nu1 + nu2) Laplacian[u, {x, y, z}] - 
   Cross[Grad[nu1 + nu2, {x, y, z}], Curl[u, {x, y, z}]], 
  Laplacian | Grad | Curl]; bcs = 
 DirichletCondition[{ux[x, y, z] == 0, uy[x, y, z] == 0, 
   uz[x, y, z] == 0}, True];

{Ax, Ay, Az} = NDSolveValue[{bcs, Table[Activate[pde][[i]] == -bmz[x, y, z][[i]], {i, 3}]}, {ux, uy, uz}, {x, y, z} [Element] mesh]

Visualization of magnetic field

B = Evaluate[Curl[{Ax[x, y, z], Ay[x, y, z], Az[x, y, z]}, {x, y, z}]];

{VectorPlot3D[B, {x, y, z} [Element] mesh, VectorStyle -> Arrowheads[0.01], VectorPoints -> 10], StreamPlot[{B[[2]], B[[3]]} /. x -> 0, {y, -2.5, 2.5}, {z, -1.5, 1.5}, VectorPoints -> Fine]}

Figure 4

Alex Trounev
  • 44,369
  • 3
  • 48
  • 106
  • Thank you for your help. That was a big step in the right direction :-) Just some questions regarding your approach: Would it be possible to use the "ElementMarker" mechanism to define the magnetization of the permanent magnet? Could other elements with varying permeability be also defined and added? – Tschibi Nov 22 '21 at 07:39
  • My plan is to extend my program to that point that I can adapt to more complex geometries combining permanent magnets and metal parts. – Tschibi Nov 22 '21 at 07:49
  • @Tschibi Yes, it is possible to use "ElementMarker", but your function M = Piecewise[{{{1, 0, 0}, ElementMarker == 2}}, {0, 0, 0}] is not working. This problem discussed on https://mathematica.stackexchange.com/questions/230282/3d-fem-vector-potential/231375?noredirect=1#comment586679_231375 – Alex Trounev Nov 22 '21 at 14:42
  • @Tschibi Could you show geometry of your magnet with all parts and permeability? – Alex Trounev Nov 23 '21 at 03:44
  • Yes of course. What would be the best way to share? Via STL File? – Tschibi Nov 24 '21 at 06:11
  • As I can not upload files directly I used the following code: bmesh = Region[ RegionUnion[ Cylinder[{{0, 0, 0}, {0, 0, 5}}, 10], Cuboid[{30, -15, 0}, {35, 15, 5}], Cuboid[{-35, -15, 0}, {-30, 15, 5}]]]

    This would be a typical problem I have to work on. In the middle we find a permanent magnet with the field strength of 1 T and the two bars on the side are iron with a µ of 1000 (in other Problems the bars can be magnetized to, so I have to be flexible defining my system if I want to use it in an productive enviroment) Update: The gemeometry is given in millimeters

    – Tschibi Nov 24 '21 at 06:23
  • @Tschibi Please, see update to my answer. – Alex Trounev Nov 24 '21 at 16:17
  • Great Job. Again, thank you very much. I was able to adapt the solution and play with it. The last thing I was trying to do was simplifying the definition of magnetisation and permeability. I splitted them in components for every part and used the If[] condition to add values to them. Anyway as upcoming Geometries wont be as easy as the ones we have here. I probably will struggle to define the If[] statements. I tried If[{x, y, z} [Element] Cylinder[{{0, 0, -0.25}, {0, 0, .25}}, 1.0], 1, 0] but could not get it working ... – Tschibi Nov 25 '21 at 09:24
  • @Tschibi It could be to use implicit region definition in If, like in my example mz = SimplifyPWToUnitStep@PiecewiseExpand@If[x^2 + y^2 <= 1 && -.25 <= z <= .25, 1, 0] /. UnitStep -> appro;` – Alex Trounev Nov 25 '21 at 13:53
  • My Problem is, that not all geometries can be Translated to an implicit region, Therefore RegionConvert fails as soon as it gets for example an imported STL geometry ... Is there a way to talk more in detail about the problem, like with a chat? – Tschibi Nov 25 '21 at 14:15
  • @Tschibi We can discussed this problem in a chat, but I have no solution for STL geometry yet. – Alex Trounev Nov 27 '21 at 14:42
  • Sorry for the late reply. I send the question to Wolfram Support. When I get an answer I will share it with you. – Tschibi Dec 14 '21 at 14:06
  • @Tschibi Thank you very much! – Alex Trounev Dec 14 '21 at 14:50
  • Hello, sorry for the late answer. I tried to solve this problem several times within the last months but I had no luck. From my point of view PiecewiseExpand[] is not able to deal with ElementMarker or Region definitions given by STL. Wolfram Support does not answer and is only telling me, that it is possible to set ElementMarkers. While this is true it does not help me with the solution of my problem: Deriving the magnetic field of a permanent magnet with geometry given by STL. Shall I open a new Question here? Do you have any new insights to that problem? Best Regards – Tschibi Aug 02 '22 at 09:19
  • Ok! Can you start a new topic with STL file attached? – Alex Trounev Aug 02 '22 at 09:52
  • New topic is started at: https://mathematica.stackexchange.com/questions/271650/calculating-the-3d-magnetic-vector-field-of-a-permanent-magnet-with-shape-given – Tschibi Aug 04 '22 at 12:09
  • @ Alex Trounev Together with user21 we found a solution for the .STL task. It is a more general approach but we still have some open questions to solve and we would politely invite you to join the discussion in the corresponding chat: https://chat.stackexchange.com/rooms/139042/discussion-between-tschibi-and-user21 – Tschibi Sep 08 '22 at 14:19