# BH curve

Good morning community;
is there a way to implment bh curve using ngsolve :
i do somthing like
def nuf(u):
nu=CoefficientFunction(nuf)
but always i got nan values !
any tricks ?

1 Like

Dear Lahoussaine,

what is the actual model you want to implement?
“Norm(grad(u))” does not seem to a suitable model in first place, as it leads to a singular matrix for grad(u) = 0.

Best,
Matthias

I want something like
1/(B3+B2+B )
or :
3.8exp(2.17B*B)+396.2

I ve tried those expression but it gives always NAN;

TBH, I have never seen any magnetic model like that (which might not mean a lot…)

I can’t see how the first one could give something reasonable for B=0.

Before using a nonlinear model, did you have a working version for a constant “nu”?

Best

def nuf(u):
IfPos(2.28-B,(1/(-0.003995B**3+0.005956B**2+0.00646B + 0.001925)), ((1902.211B-4266.42343)/0.016694669))

Here is my formule, but it doesn’t work
The problem is whenever i put norm(B) it returns NAN
even if the formula is well defined( 1/(B**2+1) for example )

Thank you and Sorry to not be claire

Unfortunately, I still miss some information: how is “B” defined?
Where does the “Norm” enter in "1/(B**2 + 1)
Which command did you use for assembly? Where exactly do you get NaN? Already in the matrix or only after solving, i.e. in the solution?

Could you maybe post a complete minimal example that reproduces your problem?

Here is the code :

u, v = fes.TnT()
mu_r = {“air”: 1, “magnet”: 1.1, “fer1”: 1000, “fer2”: 1000}

# Definition des permeabilites -------------------------

def nuf(u):
return IfPos(B-2.28,(1/(-0.0039954722B**3+0.00595660B**2+0.00646251B + 0.001925625)), ((1902.211579B±4266.42343)/0.016694669))

nu_test = [nuf(B) if mat==“fer1” else 1.0/(mu0*mu_r[mat]) for mat in mesh.GetMaterials()]

nu = CoefficientFunction(nu_test)

a = BilinearForm(fes, symmetric=False)

Is this an oversight?
Did you call a.Assemble or a.AssembleLinearization further doen behind the dots?

Here is the reste of code

# f= LinearForm(fes)

j = CoefficientFunction(10*1e3)

a+=- (j*v) * dx(“magnet”)

def SimpleNewtonSolve(gfu, a, tol=1e-8, maxits=30): res = gfu.vec.CreateVector()
du = gfu.vec.CreateVector()
fes = gfu.space
for it in range(maxits):
print(“Iteration {:3} “.format(it), end=””)
a.Apply(gfu.vec, res)
a.AssembleLinearization(gfu.vec)
du.data = a.mat.Inverse(fes.FreeDofs()) * res
gfu.vec.data -= du

`````` # stopping criteria
stopcritval = sqrt(abs(InnerProduct(du, res)))
print("<A u", it, ", A u", it, ">_{-1}^0.5 = ", stopcritval)
if stopcritval < tol:
break
``````

# Solve the linear system

gfa = GridFunction(fes)
SimpleNewtonSolve(gfa, a)

Okay, there a couple of issues:

First of all, using grad(u) and grad(v) suggests that you are using the (auxiliary) scalar magnetic potential (H=-grad(u)). However, everywhere else you work like you’d use the vector potential (B=curl(A)), i.e. using nu=1/mu and j*v as loading term. So, to me, the whole formulation seems to be inconsistent. Could you recheck, please?

Second, when using a.AssembleLinearization(…), ngsolve will linearize the expressions it finds in the bilinear form in some way. However, the nu*grad(u)*grad(v) is a linearized form already.
When trying to linearize this term, you get NaN’s because of a division by zero (or a very small number) resulting from the norm (at B=0).
When you work with CFs representing already “linearized” forms,
they must not depend on trial functions but grid functions only.
So nu must not depend on “u” but “gfu” in this case.

Please note that I did not try to fix the example, so there may be some things that I have overlooked (also in my own explanations). I hope they are still useful though.

Best,
Matthias

Thank you for your time, I will just try to clarify my problem one last time.
You will find below the code of the Linear solution.

Concerning the linear solution, the weak formulation has been simplified, so the equation might be not clear, sorry for that. However, this simulation has been validated both with a commercial solver and another FEA api.

My problem is not about the weak formulation but the way to carry on a NonLinear simulation, the NonLinearity coming from the nu expression which depend on B and thus on u.
The Nonlinear simulation is thus the same as the linear one but with nu(B)

The question is : How would you perform a Non linear algorithm iterating on nu ?

You’ll find below the linear code that worked for me …

stator = Circle(center=(0, 0), radius=10, bc=“bc_rect_ext” )
magnet_f = magnet1-rotor
airgap = circle_airgap-(magnet1)
stator_f = stator-circle_airgap

# change domain name and maxh

magnet_f.Mat(“magnet”).Maxh(0.5)
rotor.Mat(“fer1”).Maxh(0.5)
stator_f.Mat(“fer2”).Maxh(0.5)
airgap.Mat(“air”).Maxh(0.5)

mesh = Mesh(geo.GenerateMesh(maxh=0.1))
Draw(mesh)

# FEM -------------------------------------------------

fes = H1(mesh, order=3, dirichlet=‘bc_rect_ext’)
u, v = fes.TnT()
mu_r = {“air”: 1, “magnet”: 1.1, “fer1”: 1000, “fer2”: 1000}

# Definition des permeabilites -------------------------

mu0 = 4pi1e-7
nu_coef = [1/(mu0mu_r[mat]) for mat in mesh.GetMaterials()]
nu = CoefficientFunction(nu_coef)
a = BilinearForm(fes)
a += nu
M=80000
f = LinearForm(fes)
j = CoefficientFunction(10
1e3)
Mx = CoefficientFunction(M
cos(atan2(y,x))*1e0)
My = CoefficientFunction(0)

a.Assemble()
f.Assemble()

# Solve the linear system

## gfa = GridFunction(fes) gfa.vec.data = a.mat.Inverse(fes.FreeDofs()) * f.vec

1 Like

In my previous post, I might have had a confusion of the meaning of nu. Is the your nu “H(B)” or “dH/dB” evaluated for some B? I am used to work with “incremental” coefficients, ie “dH/dB”. So if you do not so, then my second “issue” in my previous response is not valid.

Assuming that your nu if such that H = nu B, then a.AssembleLinearization(gfa.vec) will linearized it for you.
However, linearizing Norm(B) when B is zero, results in a division by zero. You can work around that as follows (see comments in the code below, in particular around “def nuf”):

fes = H1(mesh, order=3, dirichlet=‘bc_rect_ext’)
u, v = fes.TnT()
mu_r = {“air”: 1, “magnet”: 1.1, “fer1”: 1000, “fer2”: 1000}

# Definition des permeabilites

mu0 = 4pi1e-7

def nuf(u):
# You need to avoid division by zero in the linearization! Either by perturbing the norm of by providing
# the linearized norm for the norm being close to zero.

``````# Perturbation

# manual linearization
return IfPos(B-2.28,
(1/(-0.0039954722*B**3+0.00595660*B**2+0.00646251*B + 0.001925625)),
IfPos(B - 1e-8,
(1902.211579*B+-4266.42343)/0.016694669,
1902.211579/0.016694669) # linearized nu (dh/dB) for small B
)
``````

nu_coef = [nuf(u) if mat==“fer1” else 1.0/(mu0*mu_r[mat]) for mat in mesh.GetMaterials()]

nu = CoefficientFunction(nu_coef)
a = BilinearForm(fes)
M=80000
f = LinearForm(fes)
j = CoefficientFunction(10
1e3)
Mx = CoefficientFunction(M
cos(atan2(y,x))*1e0)
My = CoefficientFunction(0)

# define gfa upfront

gfa = GridFunction(fes)

``````a.AssembleLinearization(gfa.vec)