## Defining Current Density for Magnetic Analysis

The graphical user interface of Elmer
dvarx
Posts: 6
Joined: 01 Jan 2019, 20:57
Antispam: Yes
Location: Switzerland

### Defining Current Density for Magnetic Analysis

Hello everyone,

I would like to do a simulation of a static Magnetic Field which is generated by a static current density J. So I create a Magnetodynamic Equation (MgHarm) with the following body force:

Code: Select all

``````Body Force 1
Name = "Current Density"
Current Density 2 = 0
Current Density Im 1 = 0
Current Density 1 = 0
Current Density Im 3 = 0
Current Density Im 2 = 0
Current Density 3 = 0
End``````
My question is how can I define the current density components as functions of the spatial coordinates (x,y,z)? For example, assume I want to define the current density in the following way (assuming a cylindrical coordinate system):
CodeCogsEqn (2).gif (2.13 KiB) Viewed 952 times
Could I use an expression similar to this one (C-Syntax) in order to define the x-Component of the Current Density?

Code: Select all

``````		Current Density 1 = (abs(z)<h0&&sqrt(x*x+y*y)<rho_outer&&sqrt(x*x+y*y)>rho_inner)?(1/sqrt(x*x+y*y)*(-y)*J0):0
Current Density Im 1 = 0
``````

raback
Posts: 3453
Joined: 22 Aug 2009, 11:57
Antispam: Yes
Location: Espoo, Finland
Contact:

### Re: Defining Current Density for Magnetic Analysis

Hi

I would rather redommend making writing this as a small Fortran routine as it will be a lot faster and easier to debug. MATC makes string conversions back and forth making it painfully slow. I personally only use it for BCs and for testing.

-Peter

dvarx
Posts: 6
Joined: 01 Jan 2019, 20:57
Antispam: Yes
Location: Switzerland

### Re: Defining Current Density for Magnetic Analysis

raback wrote:
02 Jan 2019, 00:58
Hi

I would rather redommend making writing this as a small Fortran routine as it will be a lot faster and easier to debug. MATC makes string conversions back and forth making it painfully slow. I personally only use it for BCs and for testing.

-Peter

Is there a template for such Fortran routines (or the source code of a working example)? I have been looking in the non-gui tutorial but I couldn't find the Fortran source code for the examples which use Fortran routines.

raback
Posts: 3453
Joined: 22 Aug 2009, 11:57
Antispam: Yes
Location: Espoo, Finland
Contact:

### Re: Defining Current Density for Magnetic Analysis

Hi

Well there are plenty of UDFs among the tests. Any Real valued keyword can usually be MATC expression, UDF etc. This would be one of the simplest examples (untested):

Code: Select all

``````FUNCTION CurrDensX( model, n, args) RESULT( f )
USE DefUtils

IMPLICIT NONE
TYPE(Model_t) :: model
INTEGER :: n
REAL(KIND=dp) :: args(3), f

REAL(KIND=dp) :: x,y,z,r
REAL(KIND=dp), PARAMETER :: H0 = 1.23, Rin = 1.0, Rout = 1.2, J0 = 4.56

x = args(1)
y = args(2)
z = args(3)
r = SQRT( x**2 + y**2 )

IF( ABS( z ) < H0 .AND. ( Rout - r ) * ( r - Rin ) > 0.0_dp ) THEN
f = -y * J0 / r
ELSE
f = 0.0_dp
END IF
END FUNCTION CurrDensX
``````
And you would compile it as

Code: Select all

``````elmerf90 -o CurrDens.so CurrDens.F90
``````
and call it in code as

Code: Select all

``````Current Density 1 = Variable "coordinate"
Real Procedure "CurrDens" "CurrDensX"
``````
Also place a similar CurrDensY in the same module.

Note that this is a step function so it does not integrate accurately. Rather define a body that defines the coil cross-section to have a clean step.

No need to define zeros as that's the default.

-Peter

dvarx
Posts: 6
Joined: 01 Jan 2019, 20:57
Antispam: Yes
Location: Switzerland

### Re: Defining Current Density for Magnetic Analysis

Hello

Thanks for your answer. I am trying a simple test problem with a spherical charge density:
CodeCogsEqn.gif (892 Bytes) Viewed 923 times
I have the following file ChargeDensity.f90:

Code: Select all

``````FUNCTION ChargeDensity( model, n, args) RESULT( f )
USE DefUtils

IMPLICIT NONE
TYPE(Model_t) :: model
INTEGER :: n
REAL(KIND=dp) :: args(3), f

REAL(KIND=dp) :: x,y,z,r
REAL(KIND=dp), PARAMETER :: R0=0.01

x = args(1)
y = args(2)
z = args(3)
r = SQRT( x**2 + y**2 + z**2)

IF(r<R0) THEN
f = 1
ELSE
f = 0
END IF
END FUNCTION ChargeDensity``````
and I compile it using:

Code: Select all

``elmerf90 -o ChargeDensityLib.so ChargeDensity.f90``
This results in the following compilation error (ElmerIceSolvers.so and ElmerIceUDF.so not found):

Code: Select all

``````dvarx@dvarx-ThinkPad-T550:~/Desktop/fem_simulations/electrostatic_example\$ elmerf90 -o ChargeDensityLib.so ChargeDensity.f90
with elmerice
/usr/bin/f95 -o ChargeDensityLib.so ChargeDensity.f90 -DCONTIG=,CONTIGUOUS -DHAVE_EXECUTECOMMANDLINE -DUSE_ISO_C_BINDINGS -DUSE_ARPACK -O2 -g -DNDEBUG -fPIC -shared -I/home/dvarx/elmer/install//share/elmersolver/include -L/home/dvarx/elmer/install//lib/elmersolver /home/dvarx/elmer/install//lib/elmersolver/../../share/elmersolver/lib/ElmerIceSolvers.so /home/dvarx/elmer/install//lib/elmersolver/../../share/elmersolver/lib/ElmerIceUSF.so -shared -lelmersolver
f95: error: /home/dvarx/elmer/install//lib/elmersolver/../../share/elmersolver/lib/ElmerIceSolvers.so: No such file or directory
f95: error: /home/dvarx/elmer/install//lib/elmersolver/../../share/elmersolver/lib/ElmerIceUSF.so: No such file or directory``````
Do I need to change anything else (I built elmer using https://www.github.com/ElmerCSC/elmerfem)?

mzenker
Posts: 1946
Joined: 07 Dec 2009, 11:49
Location: Germany

### Re: Defining Current Density for Magnetic Analysis

Hi,

I cannot help with the compilation error, but I would like to add for the records that there is a chapter "basic programming" in the Solver manual, and there is also a Programmer's tutorial.

Matthias

dvarx
Posts: 6
Joined: 01 Jan 2019, 20:57
Antispam: Yes
Location: Switzerland

### Re: Defining Current Density for Magnetic Analysis

mzenker wrote:
04 Jan 2019, 11:08
Hi,

I cannot help with the compilation error, but I would like to add for the records that there is a chapter "basic programming" in the Solver manual, and there is also a Programmer's tutorial.

Matthias
Ok, I got it to work no by removing the ElmerICE libraries from the compile command.

Thanks!