Skip to content

Commit 46fb348

Browse files
Debugging action calculation
1 parent 6c50f48 commit 46fb348

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

fidimag/common/chain_method_integrators.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def __init__(self, ChainObj,
160160
# band, forces, distances, rhs_fun, action_fun,
161161
# n_images, n_dofs_image,
162162
maxSteps=1000,
163-
maxCreep=6, actionTol=1e-10, forcesTol=1e-6,
163+
maxCreep=6, actionTol=1e-2, forcesTol=1e-6,
164164
etaScale=1e-6, dEta=4., minEta=1e-6,
165165
# perturbSeed=42, perturbFactor=0.1,
166166
nTrail=13, resetMax=20
@@ -249,7 +249,7 @@ def run_for(self, n_steps):
249249
# Creep stage: minimise with a fixed eta
250250
while creepCount < self.maxCreep:
251251
# Update spin. Avoid pinned or zero-Ms sites
252-
self.band[:] = self.band_old + eta * self.etaScale * self.forces_old
252+
self.band[:] = self.band_old - eta * self.etaScale * self.forces_old
253253
normalise_spins(self.band)
254254

255255
self.trailAction
@@ -271,19 +271,23 @@ def run_for(self, n_steps):
271271
# TODO: we might use all band images, not only inner ones, although G is zero at the extrema
272272
Gnorms2 = np.sum(self.forces[INNER_DOFS].reshape(-1, 3)**2, axis=1)
273273
# Compute the root mean square per image
274-
rms_G_norms_per_image = np.sqrt(np.mean(Gnorms2.reshape(self.n_images - 2, -1), axis=1))
274+
rms_G_norms_per_image = np.sum(Gnorms2.reshape(self.n_images - 2, -1), axis=1) / self.ChainObj.n_spins
275+
rms_G_norms_per_image = np.sqrt(rms_G_norms_per_image)
275276
mean_rms_G_norms_per_image = np.mean(rms_G_norms_per_image)
276277

277278
# Average step difference between trailing action and new action
278279
deltaAction = (np.abs(self.trailAction[nStart] - self.action)) / self.nTrail
279280

280281
# print('trail Actions', self.trailAction)
281282

283+
ma = self.ChainObj.compute_min_action()
284+
282285
# Print log
283286
print(f'Step {self.i_step} ⟨RMS(G)〉= {mean_rms_G_norms_per_image:.5e} ',
284287
f'deltaAction = {deltaAction:.5e} Creep n = {creepCount:>3} resetC = {resetCount:>3} ',
285288
f'eta = {eta:>5.4e} '
286-
f'action = {self.action:>5.4e} action_old = {self.action_old:>5.4e}'
289+
f'action = {self.action:>5.4e} action_old = {self.action_old:>5.4e} '
290+
f'MIN action = {ma:>5.4e}'
287291
)
288292
# print(self.forces)
289293

fidimag/common/nebm_FS.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ def compute_tangents(self, y):
263263
nebm_clib.project_images(self.tangents, y,
264264
self.n_images, self.n_dofs_image
265265
)
266-
nebm_clib.normalise_images(self.tangents,
267-
self.n_images, self.n_dofs_image
268-
)
266+
# nebm_clib.normalise_images(self.tangents,
267+
# self.n_images, self.n_dofs_image
268+
# )
269269

270270
def compute_spring_force(self, y):
271271
"""
@@ -322,7 +322,9 @@ def compute_action(self):
322322

323323
# NOTE: Gradient here is projected in the S2^N tangent space
324324
self.gradientE.shape = (self.n_images, -1)
325-
Gnorms2 = np.sum(self.gradientE**2, axis=1) / self.n_images
325+
# NOTE: HEre we have to divide by the number of spins per image,not n_images:
326+
Gnorms2 = np.sum(self.gradientE**2, axis=1) / self.n_spins
327+
326328
# Compute the root mean square per image
327329
self.gradientENorm[:] = np.sqrt(Gnorms2)
328330
self.gradientE.shape = (-1)
@@ -333,6 +335,10 @@ def compute_action(self):
333335
# TODO: we can use a better quadrature such as Gaussian
334336
# notice that the gradient norm here is using the RMS
335337
action = spi.simpson(self.gradientENorm, x=self.path_distances)
338+
# print('E', self.energies / (self.mesh.dx * self.mesh.dy * self.mesh.dz * self.mesh.unit_length**3))
339+
# print('gradE norm', self.gradientENorm)
340+
# print('Path distance', self.path_distances)
341+
print('Images', self.band.reshape(-1, 3).reshape(self.n_images, -1))
336342

337343
# DEBUG:
338344
# print('action from gradE', action)
@@ -361,15 +367,14 @@ def compute_action(self):
361367
def compute_min_action(self):
362368
dE = self.energies[-1] - self.energies[0]
363369
minAction = np.sum(np.abs(self.energies[1:] - self.energies[:-1]))
364-
return 2 * (dE + minAction)
370+
return 2 * (dE + minAction) / (self.mesh.dx * self.mesh.dy * self.mesh.dz * self.mesh.unit_length**3) / self.path_distances[-1]
365371

366372
def nebm_step(self, y, ensure_zero_extrema=False):
367373

368374
self.compute_effective_field_and_energy(y)
369-
nebm_clib.project_images(self.gradientE, y,
370-
self.n_images, self.n_dofs_image
371-
)
375+
nebm_clib.project_images(self.gradientE, y, self.n_images, self.n_dofs_image)
372376
self.compute_tangents(y)
377+
nebm_clib.normalise_spins(self.tangents, self.n_images, self.n_dofs_image)
373378
self.compute_spring_force(y)
374379

375380
nebm_clib.compute_effective_force(self.G,

tests/test_two_particles_nebm-fs.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
# FIDIMAG:
44
from fidimag.micro import Sim
55
from fidimag.common import CuboidMesh
6-
from fidimag.micro import UniformExchange, UniaxialAnisotropy
6+
from fidimag.micro import UniformExchange, UniaxialAnisotropy, Demag
77
from fidimag.common.nebm_FS import NEBM_FS
88
import numpy as np
9+
import matplotlib.pyplot as plt
910

1011
# Material Parameters
1112
# Parameters
@@ -61,12 +62,12 @@ def relax_string(maxst, simname, init_im, interp, save_every=10000):
6162
interpolations = interp
6263

6364
nebm = NEBM_FS(sim, init_im, interpolations=interpolations, name=simname,
64-
interpolation_method='linear')
65+
interpolation_method='rotation', spring_constant=1e5)
6566

6667
# dt = integrator.stepsize means after every integrator step, the images
6768
# are rescaled. We can run more integrator steps if we decrease the
6869
# stepsize, e.g. dt=1e-3 and integrator.stepsize=1e-4
69-
nebm.integrator.maxSteps = 33
70+
nebm.integrator.maxSteps = 400
7071
nebm.integrator.run_for(maxst,
7172
# save_vtks_every=save_every,
7273
# save_npys_every=save_every,
@@ -84,7 +85,7 @@ def mid_m(pos):
8485

8586
def test_energy_barrier_2particles_string():
8687
# Initial images: we set here a rotation interpolating
87-
init_im = [(-1, 0, 0), (0.0, 0.0, 1.0), (1, 0, 0)]
88+
init_im = [(-1, 0, 0), (0.0, 0.2, 1.0), (1, 0, 0)]
8889
interp = [6, 6]
8990

9091
barriers = []

0 commit comments

Comments
 (0)