# User query: Can I have an applied field changing both in time and in space?¶

You can simulate an applied field which both changes in space and time: this may be useful to mimic the effect of a write head on the magnetic grains of an hard disk while the head is moving.

The way we do this is by changing the applied field every delta_t picoseconds. This means that the applied field won't change continuously in time: it will be piecewise constant in time (but, in general, it can be non uniform in space).

You can do something like that:

```
1 import math
2
3 def set_H(sim):
4 width = 10.0 # nm
5 v = 100.0 # nm/ns == m/s
6 H_amplitude = 0.5e6 # A/m
7
8 t = float(sim.time/SI(1e-9, 's')) # get the time in ns
9 center = (v*t, 0, 0) # center of the applied field region
```**10** def H(r):
11 x, y, z = [ri/1e-9 - ci for ri, ci in zip(r, center)]
12 factor = H_amplitude*math.exp(-(x*x + y*y + z*z)/(width*width))
13 return [factor, factor, factor]
14
15 sim.set_H_ext(H, unit=SI('A/m'))
16
17 sim.relax(do=[(set_H, every('time', SI(50e-12, 's'))),
18 ('exit', at('time', SI(1000e-12, 's')))])

The function set_H is called every 50 ps and does the following: it sets a new field from the function H(r).

This function sets a field which directed along the direction [1, 1, 1] and almost vanishes outside a sphere with radius ~ 30.0 nm.

The center of this sphere moves along the direction [1, 0, 0] with velocity 100 nm/ns, thus simulating the motion of a write head in a hard disk.

Obviously the piece of code is not complete, it shows only the technique in order to have a field changing in time and space.

For a complete example see the next section.

## Complete example: simple moving write-head example¶

Here is a simulation of five cubes made of cobalt and a write-head which moves on the top of the cubes and applies a time-varying field in order to change their magnetisation. At the beginning the magnetisation of all the cubes is pointing in the [0, 0, 1] direction. After the write-head has passed over the cubes, the magnetisation of cube 1, 3 and 5 are switched in the opposite direction, while cube 2 and 4 have unchanged magnetisation.

This is possible because the write-head field, which is space-dependent (being intense only inside a sphere of radius 15-20 nm), changes also in time. It indeed translates in space, but also change in intensity, being directed in the [0, 0, -1] direction when the sphere is at the center of cube 1, 3 and 5 and in the [0, 0, 1] direction when the center of the sphere is in cube 2 and 4.

Here is the geo file used to generate the mesh (Netgen):

algebraic3d # cubes solid cube1 = orthobrick ( 0, 0, 0; 20.0, 20.0, 20.0) -maxh = 2; solid cube2 = orthobrick ( 30.0, 0, 0; 50.0, 20.0, 20.0) -maxh = 2; solid cube3 = orthobrick ( 60.0, 0, 0; 80.0, 20.0, 20.0) -maxh = 2; solid cube4 = orthobrick ( 90.0, 0, 0; 110.0, 20.0, 20.0) -maxh = 2; solid cube5 = orthobrick (120.0, 0, 0; 140.0, 20.0, 20.0) -maxh = 2; tlo cube1; tlo cube2; tlo cube3; tlo cube4; tlo cube5;

And here is the full listing of the example:

```
1 from nmag.common import *
2 import math
3
4 # Define magnetic material (data from OOMMF materials file)
5 mat_Co = MagMaterial(name="Co",
6 Ms=SI(1400e3, "A/m"),
7 exchange_coupling=SI(30e-12, "J/m"),
8 anisotropy=uniaxial_anisotropy(axis=[0, 0, 1],
9 K1=SI(520e3, "J/m^3")))
```**10** sim = Simulation()
11 sim.load_mesh("cubes.nmesh.h5",
12 [('cube1', mat_Co), ('cube2', mat_Co), ('cube3', mat_Co),
13 ('cube4', mat_Co), ('cube5', mat_Co)],
14 unit_length=SI(1e-9, 'm'))
15
16 sim.set_m([0, 0, 1])
17
18 sim.relax(save=[('fields', at('convergence'))])
19
**20** t0 = [sim.time]
21
22 def set_H(sim):
23 t = float((sim.time - t0[0])/SI(1e-9, 's')) # get time in ns
24 width = 10.0 # nm
25 v = 25.0 # nm/ns = m/s
26 H_amplitude = 4.0e6*math.sin(math.pi*t) # A/m
27 center = (v*t, 20, 10)
28 print "CENTER IN", center
29 def H(r):
**30** x, y, z = [ri/1e-9 - ci for ri, ci in zip(r, center)]
31 factor = H_amplitude*math.exp(-(x*x + y*y + z*z)/(width*width))
32 return [0, 0, -factor]
33
34 sim.set_H_ext(H, unit=SI('A/m'))
35
36 set_H(sim)
37
38 sim.set_params(stopping_dm_dt=0*degrees_per_ns)
39
**40** sim.relax(save=[('fields', every('time', SI(200e-12, 's'), first=t0[0]))],
41 do=[(set_H, every('time', SI(50e-12, 's'), first=t0[0])),
42 ('exit', at('time', SI(6000e-12, 's')))])

Here is the magnetisation at the beginning of the simulation, after the first relax command (whose purpose is just to find the zero field magnetisation configuration):

and here is the magnetisation after the write-head has passed over the cubes: