Skip to content

Commit a1cfec2

Browse files
committed
Fix singularity in tetrahedron evaluation. Disabled OpenMP in micromag
1 parent b02dfe2 commit a1cfec2

File tree

5 files changed

+66
-20
lines changed

5 files changed

+66
-20
lines changed

buildMagTenseMEX.m

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function buildMagTenseMEX(USE_RELEASE,USE_CUDA,USE_CVODE)
115115
%% MagTenseLandauLifshitzSolver_mex
116116
Source_str = 'source/MagTenseMEX/MagTenseMEX/MagTenseLandauLifshitzSolver_mex.f90';
117117
mex_str = ['mex ' compiler_str ' ' Debug_flag ' ' MagTenseMicroMag_str ' ' DemagField_str ' ' TileDemagTensor_str ' ' NumericalIntegration_str ' ' CUDA_str ' ' CVODE_str ' ' MKL_str ' ' Source_str ' ' Options_str];
118-
eval(mex_str)
118+
eval_MEX(mex_str)
119119
if ~USE_RELEASE
120120
pause(pause_time)
121121
movefile(['MagTenseLandauLifshitzSolver_mex.mex' MEX_str '64.pdb'],['matlab/MEX_files/MagTenseLandauLifshitzSolver_mex.mex' MEX_str '64.pdb']);
@@ -127,7 +127,7 @@ function buildMagTenseMEX(USE_RELEASE,USE_CUDA,USE_CVODE)
127127
MagTenseMicroMag_str = ['-Lsource/MagTenseMicroMag/' build_str_NO_CUDA_MagTenseMicroMag '/ ' MagTenseMicroMag_lib_str ' -Isource/MagTenseMicroMag/' build_str_MagTenseMicroMag '/'];
128128
Source_str = 'source/MagTenseMEX/MagTenseMEX/MagTenseLandauLifshitzSolver_mex.f90';
129129
mex_str = ['mex ' compiler_str ' ' Debug_flag ' ' MagTenseMicroMag_str ' ' DemagField_str ' ' TileDemagTensor_str ' ' NumericalIntegration_str ' ' CVODE_str ' ' MKL_str ' ' Source_str ' ' Options_str];
130-
eval(mex_str)
130+
eval_MEX(mex_str)
131131
if ~USE_RELEASE
132132
pause(pause_time)
133133
movefile(['MagTenseLandauLifshitzSolver_mex.mex' MEX_str '64.pdb'],['matlab/MEX_files/MagTenseLandauLifshitzSolverNoCUDA_mex.mex' MEX_str '64.pdb']);
@@ -139,7 +139,7 @@ function buildMagTenseMEX(USE_RELEASE,USE_CUDA,USE_CVODE)
139139
%% IterateMagnetization_mex
140140
Source_str = 'source/MagTenseMEX/MagTenseMEX/IterateMagnetization_mex.f90';
141141
mex_str = ['mex ' compiler_str ' ' Debug_flag ' ' DemagField_str ' ' TileDemagTensor_str ' ' NumericalIntegration_str ' ' Source_str ' ' Options_str];
142-
eval(mex_str)
142+
eval_MEX(mex_str)
143143
if ~USE_RELEASE
144144
pause(pause_time)
145145
movefile(['IterateMagnetization_mex.mex' MEX_str '64.pdb'],['matlab/MEX_files/IterateMagnetization_mex.mex' MEX_str '64.pdb']);
@@ -150,7 +150,7 @@ function buildMagTenseMEX(USE_RELEASE,USE_CUDA,USE_CVODE)
150150
%% getHFromTiles_mex
151151
Source_str = 'source/MagTenseMEX/MagTenseMEX/getHFromTiles_mex.f90';
152152
mex_str = ['mex ' compiler_str ' ' Debug_flag ' ' DemagField_str ' ' TileDemagTensor_str ' ' NumericalIntegration_str ' ' Source_str ' ' Options_str];
153-
eval(mex_str)
153+
eval_MEX(mex_str)
154154
if ~USE_RELEASE
155155
pause(pause_time)
156156
movefile(['getHFromTiles_mex.mex' MEX_str '64.pdb'],['matlab/MEX_files/getHFromTiles_mex.mex' MEX_str '64.pdb']);
@@ -161,7 +161,7 @@ function buildMagTenseMEX(USE_RELEASE,USE_CUDA,USE_CVODE)
161161
%% getNFromTile_mex
162162
Source_str = 'source/MagTenseMEX/MagTenseMEX/getNFromTile_mex.f90';
163163
mex_str = ['mex ' compiler_str ' ' Debug_flag ' ' DemagField_str ' ' NumericalIntegration_str ' ' TileDemagTensor_str ' ' Source_str ' ' Options_str];
164-
eval(mex_str)
164+
eval_MEX(mex_str)
165165
if ~USE_RELEASE
166166
pause(pause_time)
167167
movefile(['getNFromTile_mex.mex' MEX_str '64.pdb'],['matlab/MEX_files/getNFromTile_mex.mex' MEX_str '64.pdb']);
@@ -172,7 +172,7 @@ function buildMagTenseMEX(USE_RELEASE,USE_CUDA,USE_CVODE)
172172
%% getMagForce_mex
173173
Source_str = 'source/MagTenseMEX/MagTenseMEX/getMagForce_mex.f90';
174174
mex_str = ['mex ' compiler_str ' ' Debug_flag ' ' MagneticForceIntegrator_str ' ' DemagField_str ' ' NumericalIntegration_str ' ' TileDemagTensor_str ' ' Source_str ' ' Options_str];
175-
eval(mex_str)
175+
eval_MEX(mex_str)
176176
if ~USE_RELEASE
177177
pause(pause_time)
178178
movefile(['getMagForce_mex.mex' MEX_str '64.pdb'],['matlab/MEX_files/getMagForce_mex.mex' MEX_str '64.pdb']);
@@ -181,3 +181,26 @@ function buildMagTenseMEX(USE_RELEASE,USE_CUDA,USE_CVODE)
181181
movefile(['getMagForce_mex.mex' MEX_str '64'],['matlab/MEX_files/getMagForce_mex.mex' MEX_str '64']);
182182

183183
end
184+
185+
function eval_MEX(mex_str)
186+
187+
try
188+
eval(mex_str);
189+
catch ME
190+
if (strcmp(ME.message(91:117),'mt : general error c101008d'))
191+
fail_mex = true;
192+
while fail_mex
193+
try
194+
disp('Microsoft manifest tool error - retrying')
195+
eval(mex_str);
196+
fail_mex = false;
197+
catch
198+
continue
199+
end
200+
end
201+
else
202+
disp(ME.message)
203+
rethrow(ME)
204+
end
205+
end
206+
end

source/DemagField/DemagField/IterateMagnetSolution.f90

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ subroutine iterateMagnetization( tiles, n, stateFunction, n_stf, T, err_max, max
204204

205205
pts = pts + tiles(i)%offset !< Finally add the offset to translate the circ piece
206206

207+
case(tileTypeTetrahedron)
208+
!! No rotation is needed as the center of the tetrahedron is with respect to the vertices
209+
pts(1) = (tiles(i)%vert(1,1) + tiles(i)%vert(1,2) + tiles(i)%vert(1,3) + tiles(i)%vert(1,4))/4.0
210+
pts(2) = (tiles(i)%vert(2,1) + tiles(i)%vert(2,2) + tiles(i)%vert(2,3) + tiles(i)%vert(2,4))/4.0
211+
pts(3) = (tiles(i)%vert(3,1) + tiles(i)%vert(3,2) + tiles(i)%vert(3,3) + tiles(i)%vert(3,4))/4.0
212+
207213

208214
case default
209215

source/MagTenseMicroMag/LandauLifshitzEquationSolver.f90

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ subroutine updateDemagfield( problem, solution)
699699
beta = 0.0
700700
!Hmx = Kxx * Mx
701701
call gemv( problem%Kxx, solution%Mx_s, solution%HmX, alpha, beta )
702-
702+
703703
beta = 1.0
704704
!Hmx = Hmx + Kxy * My
705705
call gemv( problem%Kxy, solution%My_s, solution%HmX, alpha, beta )
@@ -914,8 +914,9 @@ subroutine ComputeDemagfieldTensor( problem )
914914

915915
CALL SYSTEM_CLOCK(c1)
916916

917-
call mkl_set_num_threads(problem%nThreadsMatlab)
918-
call omp_set_num_threads(problem%nThreadsMatlab)
917+
!call mkl_set_num_threads(problem%nThreadsMatlab)
918+
!call omp_set_num_threads(problem%nThreadsMatlab)
919+
call omp_set_num_threads(1)
919920

920921
if ( problem%grid%gridType .eq. gridTypeUniform ) then
921922

@@ -984,10 +985,10 @@ subroutine ComputeDemagfieldTensor( problem )
984985
call displayGUIMessage( 'Averaging the N_tensor not supported for this tile type' )
985986
endif
986987

987-
!$OMP PARALLEL shared(problem)
988-
!$omp do private(ind, tile, H, Nout)
989-
988+
!$OMP PARALLEL shared(problem) private(ind, indx_ele, tile, H, Nout, pts_arr, i)
989+
990990
!for each element find the tensor for all evaluation points (i.e. all elements)
991+
!$omp do
991992
do i=1,nx
992993
!Setup template tile
993994
tile(1)%tileType = 5 !(for tetrahedron)
@@ -1001,7 +1002,13 @@ subroutine ComputeDemagfieldTensor( problem )
10011002
allocate(Nout(1,ntot,3,3))
10021003
allocate(H(ntot,3))
10031004

1004-
call getFieldFromTiles( tile, H, problem%grid%pts, 1, ntot, Nout, .false. )
1005+
allocate(pts_arr(ntot,3))
1006+
pts_arr(:,1) = problem%grid%pts(:,1)
1007+
pts_arr(:,2) = problem%grid%pts(:,2)
1008+
pts_arr(:,3) = problem%grid%pts(:,3)
1009+
1010+
!call getFieldFromTiles( tile, H, problem%grid%pts, 1, ntot, Nout, .false. )
1011+
call getFieldFromTiles( tile, H, pts_arr, 1, ntot, Nout, .false. )
10051012

10061013
!Copy Nout into the proper structure used by the micro mag model
10071014
ind = i
@@ -1022,13 +1029,15 @@ subroutine ComputeDemagfieldTensor( problem )
10221029
problem%Kzz(:,ind) = sngl(Nout(1,:,3,3))
10231030

10241031
!Clean up
1032+
deallocate(pts_arr)
10251033
deallocate(Nout)
10261034
deallocate(H)
10271035
enddo
1028-
10291036
!$omp end do
1037+
10301038
!$OMP END PARALLEL
1031-
1039+
1040+
10321041
elseif ( problem%grid%gridType .eq. gridTypeUnstructuredPrisms ) then
10331042

10341043
!$OMP PARALLEL shared(problem)
@@ -1129,8 +1138,7 @@ subroutine ComputeDemagfieldTensor( problem )
11291138

11301139
!call displayGUIMessage( 'Kxx(1,1):' )
11311140
!write (prog_str,'(f10.3)') problem%Kxx(1,1)
1132-
!call displayGUIMessage( prog_str )
1133-
1141+
!call displayGUIMessage( prog_str )
11341142

11351143
!Write the demag tensors to disk if asked to do so
11361144
if ( problem%demagTensorReturnState .gt. DemagTensorReturnMemory ) then

source/MagTenseMicroMag/MicroMagParameters.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ module MicroMagParameters
105105
integer :: demag_approximation !> Flag for how to approximate the demagnetization tensor as specified in the parameters below
106106
integer :: demagTensorReturnState !> Flag describing how or if the demag tensor should be returned
107107
integer :: demagTensorLoadState !> Flag describing how or if to load the demag tensor (from disk e.g.)
108-
integer*4 :: nThreadsMatlab !> Number of threads to use in the OpenMP demag tensor allocation
108+
integer :: nThreadsMatlab !> Number of threads to use in the OpenMP demag tensor allocation
109109
integer,dimension(3) :: N_ave !> Number of points to average the demag tensor in in the recieving tile, N_ave(1) = N_x etc
110110
character*256 :: demagTensorFileOut, demagTensorFileIn !> Filename (including path) for output (input) of demag tensor if it is to be returned as a file (demagTensorReturnState >2 and the value is equal to the length of the file including path)
111111

source/TileDemagTensor/TileDemagTensor/TileTriangle.f90

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module TileTriangle
22
use SPECIALFUNCTIONS
3+
use, intrinsic :: ieee_arithmetic
34
implicit none
45

56
real,parameter :: pi=3.14159265359
@@ -169,8 +170,8 @@ function Nxz( r, l, h )
169170

170171
! Returns the Nxz tensor component in the local coordinate system
171172
Nxz = -1./(4.*pi) * ( F_Nxz(r,h,l,h) - F_Nxz(r,0.,l,h) - ( G_Nxz(r,h) - G_Nxz(r,0.) ) )
172-
173-
end function Nxz
173+
174+
end function Nxz
174175

175176
function F_Nxz( r, yp, l, h )
176177
real :: F_Nxz
@@ -189,6 +190,10 @@ function G_Nxz( r, yp )
189190
real,dimension(3),intent(in) :: r
190191
real,intent(in) :: yp
191192
G_Nxz = atanh( ( r(2)-yp )/ (sqrt(r(1)**2+(r(2)-yp)**2+r(3)**2)) )
193+
194+
if (.not.ieee_is_finite(G_Nxz)) then ! variable is not finite
195+
G_Nxz = 0;
196+
endif
192197
end function G_Nxz
193198

194199
!---------------------------------------------------------------------------
@@ -223,6 +228,10 @@ function L_Nyz( r, xp )
223228
real,dimension(3),intent(in) :: r
224229
real,intent(in) :: xp
225230
L_Nyz = atanh( (r(1) - xp) / (sqrt((r(1)-xp)**2+r(2)**2+r(3)**2)) )
231+
232+
if (.not.ieee_is_finite(L_Nyz)) then ! variable is not finite
233+
L_Nyz = 0;
234+
endif
226235
end function L_Nyz
227236

228237
!---------------------------------------------------------------------------

0 commit comments

Comments
 (0)