Пример #1
0
def update_sigma(the_field_sigma,i):
    compute_J=nfem.prematrix_applicator(prematrix_J,
                                         field_mid=the_field_sigma)
    laplace_solver=nfem.laplace_solver(prematrix_laplace,
                                        dirichlet_bcs=[(-1,1,laplace_dbc)],
                                        mwe_mid=the_field_sigma)
    #
    field_phi = laplace_solver(cofield_drho_by_dt,
                               dbc_values=laplace_dbc_values)
    field_J = nfem.cofield_to_field(compute_J(field_phi))

   

    #print "field_J contents:", nfem.data_doftypes(field_J)
    
    # XXX NOTE: we should be able to make an applicator that does the
    # cofield_to_field conversion automatically!
    #
    #
    # Next, let us compute the new condictivity by site-wise operation on
    # field_J. We just overwrite field_sigma:
    #
    recompute_conductivity([h_x,h_y,sigma0,alpha],fields=[the_field_sigma,field_J])
    print "Refcount field_sigma:",ocaml.sys_refcount(the_field_sigma)
    return the_field_sigma
Пример #2
0
def update_sigma(the_field_sigma):
    compute_J=nfem.prematrix_applicator(prematrix_J,
                                         mwe_mid=the_field_sigma)
    laplace_solver=nfem.laplace_solver(prematrix_laplace,
                                        dirichlet_bcs=[(-1,1,laplace_dbc)],
                                        mwe_mid=the_field_sigma)
    #
    field_phi = laplace_solver(cofield_drho_by_dt,
                               dbc_values=laplace_dbc_values)
    field_J = nfem.cofield_to_field(compute_J(field_phi))
    # XXX NOTE: we should be able to make an applicator that does the
    # cofield_to_field conversion automatically!
    #
    #
    # Next, let us compute the new condictivity by site-wise operation on
    # field_J. We just overwrite field_sigma:
    #
    recompute_conductivity=nfem.site_wise_applicator(parameter_names=["H_x","H_y","sigma0","alpha"],
                                                      # all the names of extra parameters
                                                      code=code_recompute_conductivity,
                                                      fields=[the_field_sigma,field_J]
                                                      )
    #
    #
    recompute_conductivity([h_x,h_y,sigma0,alpha])
    return the_field_sigma
Пример #3
0
def update_sigma(the_field_sigma, i):
    compute_J = nfem.prematrix_applicator(prematrix_J,
                                          field_mid=the_field_sigma)
    laplace_solver = nfem.laplace_solver(prematrix_laplace,
                                         dirichlet_bcs=[(-1, 1, laplace_dbc)],
                                         mwe_mid=the_field_sigma)
    #
    field_phi = laplace_solver(cofield_drho_by_dt,
                               dbc_values=laplace_dbc_values)
    field_J = nfem.cofield_to_field(compute_J(field_phi))

    #print "field_J contents:", nfem.data_doftypes(field_J)

    # XXX NOTE: we should be able to make an applicator that does the
    # cofield_to_field conversion automatically!
    #
    #
    # Next, let us compute the new condictivity by site-wise operation on
    # field_J. We just overwrite field_sigma:
    #
    recompute_conductivity([h_x, h_y, sigma0, alpha],
                           fields=[the_field_sigma, field_J])

    nfem.visual.fields2vtkfile([field_phi, field_J, the_field_sigma],
                               'fem-rings2_all%d.vtk' % i, the_mesh)

    return the_field_sigma
Пример #4
0
def compute_total_current(the_field_sigma):
    laplace_solver=nfem.laplace_solver(prematrix_laplace,
                                        dirichlet_bcs=[(-1,1,laplace_dbc)],
                                        mwe_mid=the_field_sigma)
    compute_div_J=nfem.prematrix_applicator(prematrix_laplace,
                                             field_mid=the_field_sigma)
    field_phi=laplace_solver(cofield_drho_by_dt,
                             dbc_values=laplace_dbc_values)

    cofield_div_J=compute_div_J(field_phi)
    return accumulate_J_total([0.0,0.0,0.0,0.0],cofields=[cofield_div_J])
Пример #5
0
def compute_total_current(the_field_sigma):
    laplace_solver = nfem.laplace_solver(prematrix_laplace,
                                         dirichlet_bcs=[(-1, 1, laplace_dbc)],
                                         mwe_mid=the_field_sigma)
    compute_div_J = nfem.prematrix_applicator(prematrix_laplace,
                                              field_mid=the_field_sigma)
    field_phi = laplace_solver(cofield_drho_by_dt,
                               dbc_values=laplace_dbc_values)

    cofield_div_J = compute_div_J(field_phi)
    return accumulate_J_total([0.0, 0.0, 0.0, 0.0], cofields=[cofield_div_J])
Пример #6
0
def compute_total_current(the_field_sigma):
    laplace_solver=nfem.laplace_solver(prematrix_laplace,
                                        dirichlet_bcs=[(-1,1,laplace_dbc)],
                                        mwe_mid=the_field_sigma)
    compute_div_J=nfem.prematrix_applicator(prematrix_laplace,
                                             mwe_mid=the_field_sigma)
    field_phi=laplace_solver(cofield_drho_by_dt,
                             dbc_values=laplace_dbc_values)
    print "OK 1 - field_phi!\n"
    sys.stdout.flush()
    compute_div_J(field_phi, target=_cofield_div_J)
    print "OK 2 - div_J!\n"
    sys.stdout.flush()
    return _accumulate_J_total([0.0,0.0,0.0,0.0])
Пример #7
0
        print "unused laplace_dbc at x =", x, "and y =", y
        raise StandardError, "this place can't be reached"


cofield_drho_by_dt = nfem.make_cofield(mwe_drho_by_dt)

print "1"

prematrix_laplace = nfem.prematrix(
    diffop_laplace, mwe_drho_by_dt, mwe_phi, mwe_mid=mwe_sigma)

print "2"
prematrix_J = nfem.prematrix(diffop_J, mwe_J, mwe_phi, mwe_mid=mwe_sigma)

print "3"
compute_J = nfem.prematrix_applicator(prematrix_J, field_mid=field_sigma)
print "4"
laplace_solver = nfem.laplace_solver(
    prematrix_laplace,
    dirichlet_bcs=[(-1, 1, laplace_dbc)],
    mwe_mid=field_sigma)

print "5"
field_phi = laplace_solver(cofield_drho_by_dt, dbc_values=laplace_dbc_values)
print "6"
field_J = nfem.cofield_to_field(compute_J(field_phi))

nfem.visual.fields2vtkfile([field_phi, field_J], 'result.vtk', the_mesh)

print "Try to visualise with "
print "mayavi -d run_bug-fem2-halfring/result.vtk -m SurfaceMap -m VelocityVector"
Пример #8
0
#This is " $\ laplace -phi = =rho_M$
prematrix_laplace = nfem.prematrix(diffop_laplace, mwe_rho_M, mwe_phi_M)
prematrix_div_M = nfem.prematrix(diffop_div_M,
                                 mwe_rho_M,
                                 mwe_M,
                                 ignore_jumps=False)
prematrix_grad_phi = nfem.prematrix(diffop_grad_phi, mwe_H, mwe_phi_M)

log.info("OK 3!")

solve_bem = nfem.laplace_solver_bem(prematrix_laplace, inside_regions=[1])

log.info("OK 4!")

compute_div_M = nfem.prematrix_applicator(prematrix_div_M,
                                          interface_coeffs=[(1, -1, 1.0)])
compute_grad_phi = nfem.prematrix_applicator(prematrix_grad_phi)

log.info("OK 5!")

field_M0 = nfem.make_field(mwe_M, fun_M0)

cofield_div_M = compute_div_M(field_M0)

# DDD
ddd_field_div_M = nfem.cofield_to_field(cofield_div_M)
print "DDD div M: ", nfem.probe_field(
    ddd_field_div_M, [2.7, 0.2, 0.0])  # we may add: ,name_stem="rho_M")
# END DDD

field_phi_M = solve_bem(cofield_div_M)
Пример #9
0
diffop_grad_phi=nfem.diffop("<H(j)||d/dxj phi_M>, j:3")

log.info("OK 2!")

#This is " $\ laplace -phi = =rho_M$
prematrix_laplace=nfem.prematrix(diffop_laplace,mwe_rho_M,mwe_phi_M)
prematrix_div_M=nfem.prematrix(diffop_div_M,mwe_rho_M,mwe_M,ignore_jumps=False)
prematrix_grad_phi=nfem.prematrix(diffop_grad_phi,mwe_H,mwe_phi_M)

log.info("OK 3!")

solve_bem=nfem.laplace_solver_bem(prematrix_laplace,inside_regions=[1])

log.info("OK 4!")

compute_div_M=nfem.prematrix_applicator(prematrix_div_M,interface_coeffs=[(1,-1,1.0)])
compute_grad_phi=nfem.prematrix_applicator(prematrix_grad_phi)

log.info("OK 5!")

field_M0=nfem.make_field(mwe_M,fun_M0)

cofield_div_M=compute_div_M(field_M0)

# DDD
ddd_field_div_M=nfem.cofield_to_field(cofield_div_M)
print "DDD div M: ",nfem.probe_field(ddd_field_div_M,[2.7,0.2,0.0]) # we may add: ,name_stem="rho_M")
# END DDD

field_phi_M=solve_bem(cofield_div_M)
Пример #10
0

# Note that this magnetization is "mostly zero":
def fun_M0(dof_name_indices, position):
    if dof_name_indices[1][0] == 1:  # y-direction
        x = position[0]
        return 1.0 / (1.0 + (x * x / 4.0))
    else:
        return 0.0


field_M = nfem.make_field(mwe_M, fun_M0)

prematrix_v_laplace = nfem.prematrix(diffop_v_laplace, mwe_H, mwe_M)

v_laplace = nfem.prematrix_applicator(prematrix_v_laplace)

field_H = nfem.cofield_to_field(v_laplace(field_M))

range_i = 100

# nfem.field_print_contents(field_H)

for i in range(0, range_i):
    pos = [-4.5 + 9.0 * i / (0.0 + range_i), 0.0, 0.0]
    field_val = nfem.probe_field(field_H, "H", pos)
    print pos[0], " ", field_val[1]

#for i in range(0,range_i):
#    pos=[-4.5+9.0*i/(0.0+range_i),0.0,0.0]
#    print pos, "=> ", nfem.probe_field(field_H,"H",pos)
Пример #11
0
diffop_v_laplace = nfem.diffop("-<d/dxj H(k) || d/dxj M(k)>, j:3, k:3")

# Note that this magnetization is "mostly zero":
def fun_M0(dof_name_indices,position):
    if dof_name_indices[1][0]==1: # y-direction
        x=position[0]
        return 1.0/(1.0+(x*x/4.0))
    else:
        return 0.0


field_M=nfem.make_field(mwe_M,fun_M0)

prematrix_v_laplace=nfem.prematrix(diffop_v_laplace,mwe_H,mwe_M)

v_laplace=nfem.prematrix_applicator(prematrix_v_laplace)

field_H=nfem.cofield_to_field(v_laplace(field_M))

range_i=100

# nfem.field_print_contents(field_H)

for i in range(0,range_i):
    pos=[-4.5+9.0*i/(0.0+range_i),0.0,0.0]
    field_val=nfem.probe_field(field_H,"H",pos)
    print pos[0], " ", field_val[1]


#for i in range(0,range_i):
#    pos=[-4.5+9.0*i/(0.0+range_i),0.0,0.0]
Пример #12
0
elem2d_rho_M = nfem.make_element("rho_M",[],dim=2);
elem2d_phi_M = nfem.make_element("phi_M",[],dim=2);

mwe2d_M      = nfem.make_mwe("mwe2d_M",     [(1,elem2d_M)])
mwe2d_H      = nfem.make_mwe("mwe2d_H",     [(1,elem2d_H)])
mwe2d_rho_M  = nfem.make_mwe("mwe2d_rho_M", [(1,elem2d_rho_M)])
mwe2d_phi_M  = nfem.make_mwe("mwe2d_phi_M", [(1,elem2d_phi_M)])

#This is " $\ laplace -phi = =rho_M$
pmx2d_laplace=nfem.prematrix(diffop_laplace,mwe2d_rho_M,mwe2d_phi_M)
pmx2d_div_M=nfem.prematrix(diffop_div_M,mwe2d_rho_M,mwe2d_M,ignore_jumps=False)
pmx2d_grad_phi=nfem.prematrix(diffop_grad_phi,mwe2d_H,mwe2d_phi_M)

solve_bem_2d=nfem.laplace_solver_bem(pmx2d_laplace,inside_regions=[1], thickness=thickness2d)

compute_div_M_2d=nfem.prematrix_applicator(pmx2d_div_M,interface_coeffs=[(1,-1,1.0)])
compute_grad_phi_2d=nfem.prematrix_applicator(pmx2d_grad_phi)

field2d_M0=nfem.make_field(mwe2d_M,fun_M0)

cofield2d_div_M=compute_div_M_2d(field2d_M0)

field2d_phi_M=solve_bem_2d(cofield2d_div_M)

field2d_H=nfem.cofield_to_field(compute_grad_phi_2d(field2d_phi_M))

# DDDDDD

print "phi_M left: ",nfem.probe_field(field2d_phi_M,[-3.8,0.0])
print "phi_M right: ",nfem.probe_field(field2d_phi_M,[3.8,0.0])
Пример #13
0
    def setup(self):
        '''This function should be called after the method 'set' to setup
       the simulation (create the fields, the operators and so on)'''

        # Should not do initializizations more than once
        if self.is_ready: return

        mwe_m, field_m = self.new_mwe_and_field(
            "m", [3], initial_values=self.initial_mag)
        mwe_h_total, field_h_total = self.new_mwe_and_field("h_total", [3])

        if self.features["include_demag"]:
            self.new_field("h_demag", indices=[3])
        if self.features["include_exchange"]:
            self.new_field("h_exch", indices=[3])
        if self.features["external_field"]:
            h0 = self.features["external_field"]
            self.new_field("h_ext", indices=[3], initial_values=h0)

        # The demag field
        if self.features["include_demag"]:
            if self.mesh.dim != 3:
                raise "Sorry, the demag-calculation is implemented only for 3-D space."

            mwe_h_demag = self.mwes["h_demag"]
            field_h_demag = self.fields["h_demag"]

            mwe_scalar = self.new_mwe("scalar")
            mwe_rho_m = nfem.mwe_sibling(mwe_scalar, "mwe_rho_m",
                                         "renamed_scalar",
                                         [("scalar", "rho_m")])
            mwe_phi_m = nfem.mwe_sibling(mwe_scalar, "mwe_phi_m",
                                         "renamed_scalar",
                                         [("scalar", "phi_m")])

            field_div_m = nfem.make_field(mwe_rho_m)
            field_phi_m = nfem.make_field(mwe_phi_m)

            diffop_div_m_str = "%f <rho_m||d/dxj m(j)>, j:3" % self.m_sat
            print diffop_div_m_str
            compute_div_m = \
             nfem.diffop_applicator(diffop_div_m_str,
                                    mwe_rho_m, mwe_m,
                                    interface_coeffs=[(-2,-2,1.0)],
                                    petsc_name="mumag_div_m")

            prematrix_laplace = \
             nfem.prematrix("-<d/dxj rho_m||d/dxj phi_m>, j:3", mwe_rho_m, mwe_phi_m)

            solve_bem = \
             nfem.laplace_solver_bem(prematrix_laplace, inside_regions=self.where)

            compute_grad_phi = \
             nfem.diffop_applicator("<h_demag(j)||d/dxj phi_m>, j:3",
                                    mwe_h_demag, mwe_phi_m, result="field")

            cofield_div_m = compute_div_m(self.fields["m"])
            solve_bem(cofield_div_m, target=field_phi_m)
            compute_grad_phi(field_phi_m, target=field_h_demag)

            def calculate_h_demag():
                compute_div_m(self.fields["m"], target=cofield_div_m)
                solve_bem(cofield_div_m, target=field_phi_m)
                compute_grad_phi(field_phi_m, target=field_h_demag)

            self.calculate_h_demag = calculate_h_demag

        # Now we add the exchange and demag fields if needed
        if self.features["include_exchange"]:
            if not self.features["exchange_coupling"]:
                raise "You want to include exchange interaction, " + \
                 "but you did not specify the exchange coupling constant!"
            ec = self.features["exchange_coupling"]
            if ec < 0.0:
                raise "Error: you specified a negative exchange coupling constant."
            mwe_h_exch = self.mwes["h_exch"]
            field_h_exch = self.fields["h_exch"]
            exch_factor = -2.0 * ec / self.m_sat_mu0
            op_str = "%f <d/dxi h_exch(j) || d/dxi m(j)>, i:%d, j:3" % (
                exch_factor, self.mesh.dim)
            op_h_exch = nfem.diffop(op_str)
            p = nfem.prematrix(op_h_exch, mwe_h_exch, mwe_m, ignore_jumps=True)
            compute_h_exch = nfem.prematrix_applicator(p)
            h_exch_cofield = compute_h_exch(field_m)
            nfem.cofield_to_field(h_exch_cofield, target=field_h_exch)

            def calculate_h_exch():
                compute_h_exch(field_m, target=h_exch_cofield)
                nfem.cofield_to_field(h_exch_cofield, target=field_h_exch)

            self.calculate_h_exch = calculate_h_exch

        # Create the C-functions which performs the different parts
        # of the computation

        some_names = ["m", "h_total"]
        some_mwes = self.mwe_list(some_names)
        some_fields = self.field_list(some_names)

        if self.uniaxial_anis:
            args = ["m_sat_mu0", "axis_x", "axis_y", "axis_z", "k1", "k2"]
            c_uniaxial = nfem.site_wise_applicator(args,
                                                   ccode_uniaxial,
                                                   field_mwes=some_mwes)

            def calculate_uniaxial_anis():
                for ua in self.uniaxial_anis:
                    axis, k1, k2 = ua
                    axis_x, axis_y, axis_z = axis
                    args_values = [
                        self.m_sat_mu0, axis_x, axis_y, axis_z, k1, k2
                    ]
                    c_uniaxial(args_values, fields=some_fields)

            self.calculate_uniaxial_anis = calculate_uniaxial_anis

        if self.cubic_anis:
            args = [
                "m_sat_mu0", "axis1_x", "axis1_y", "axis1_z", "axis2_x",
                "axis2_y", "axis2_z", "k1", "k2", "k3"
            ]
            c_cubic = nfem.site_wise_applicator(args,
                                                ccode_cubic,
                                                field_mwes=some_mwes)

            def calculate_cubic_anis():
                for ca in self.cubic_anis:
                    axis1, axis2, k1, k2, k3 = ca
                    axis1_x, axis1_y, axis1_z = axis1
                    axis2_x, axis2_y, axis2_z = axis2
                    args_values = [
                        self.m_sat_mu0, axis1_x, axis1_y, axis1_z, axis2_x,
                        axis2_y, axis2_z, k1, k2, k3
                    ]
                    c_cubic(args_values, fields=some_fields)

            self.calculate_cubic_anis = calculate_cubic_anis

        more_names = ["h_total", "h_ext", "h_demag", "h_exch"]
        more_mwes = self.mwe_list(more_names)
        more_fields = self.field_list(more_names)
        add_fields = nfem.site_wise_applicator([],
                                               ccode_add_fields,
                                               field_mwes=more_mwes)

        def add_ext_demag_exch():
            add_fields([], fields=more_fields)

        self.add_ext_demag_exch = add_ext_demag_exch

        if self.features["calculate_energy"]:
            swa_calculate_energy = \
             nfem.site_wise_applicator(["energy"], ccode_calculate_energy,
                                       field_mwes=[mwe_m],cofield_mwes=[mwe_h_total])
            cofield_h_total = nfem.field_to_cofield(field_h_total)

            def calculate_energy():
                nfem.field_to_cofield(field_h_total, target=cofield_h_total)
                energy = swa_calculate_energy([0.0],
                                              fields=[field_m],
                                              cofields=[cofield_h_total])
                return -self.m_sat_mu0 * energy[0]

            self.__calculate_energy = calculate_energy

        self.is_ready = True
Пример #14
0
cofield_drho_by_dt=nfem.make_cofield(mwe_drho_by_dt)

print "1"

prematrix_laplace=nfem.prematrix(diffop_laplace,
                                  mwe_drho_by_dt,mwe_phi,
                                  mwe_mid=mwe_sigma)

print "2"
prematrix_J=nfem.prematrix(diffop_J,
                                   mwe_J,mwe_phi,
                                   mwe_mid=mwe_sigma)

print "3"
compute_J=nfem.prematrix_applicator(prematrix_J,
                                     field_mid=field_sigma)
print "4"
laplace_solver=nfem.laplace_solver(prematrix_laplace,
                                    dirichlet_bcs=[(-1,1,laplace_dbc)],
                                    mwe_mid=field_sigma)

print "5"
field_phi = laplace_solver(cofield_drho_by_dt,
                           dbc_values=laplace_dbc_values)
print "6"
field_J = nfem.cofield_to_field(compute_J(field_phi))

nfem.visual.fields2vtkfile([field_phi,field_J],'result.vtk',the_mesh)

print "Try to visualise with "
print "mayavi -d run_bug-fem2-halfring/result.vtk -m SurfaceMap -m VelocityVector"
Пример #15
0
# surface outflow of electrical current!


code_recompute_conductivity="""
double len2_J, sprod_HJ, cos2;
double len2_H=H_x*H_x+H_y*H_y;

len2_J=J(0)*J(0)+J(1)*J(1);
sprod_HJ=H_x*J(0)+H_y*J(1);

cos2=(fabs(len2_J)<1e-8)?0.0:(sprod_HJ*sprod_HJ/(len2_H*len2_J));
sigma = sigma0 + alpha * cos2;
"""


compute_J=nfem.prematrix_applicator(prematrix_J,
                                     mwe_mid=field_sigma)

compute_laplace=nfem.prematrix_applicator(prematrix_laplace,mwe_mid=field_sigma)

laplace_solver=nfem.laplace_solver(prematrix_laplace,
                                    dirichlet_bcs=[(-1,1,laplace_dbc)],
                                    mwe_mid=field_sigma)

field_phi = laplace_solver(cofield_drho_by_dt,
                           dbc_values=laplace_dbc_values)

nfem.plot_scalar_field(field_phi,
                        "phi","/tmp/plot-phi.ps",
                        plot_edges=False,
                        color_scheme=[(-1.2,[0.1,0.1,0.1]),
                                      (-1.1,[0.9,0.9,0.9]),
Пример #16
0
  total_current_up+=drho_by_dt;
}
else if(coords(1)<-2.45)
{
  total_current_down+=drho_by_dt;
}
else
{
  double j=drho_by_dt;

  if(j>0)total_bad_current_plus+=j;
  else   total_bad_current_minus+=j;
}
"""

compute_div_J=nfem.prematrix_applicator(prematrix_div_J,
                                         interface_coeffs=[(-1,1,1.0)])

# These definitions are to be regarded as internal to compute_total_current:
_cofield_div_J=nfem.make_cofield(mwe_drho_by_dt)

_accumulate_J_total=nfem.site_wise_applicator(parameter_names=["total_current_up",
                                                                "total_current_down",
                                                                "total_bad_current_plus",
                                                                "total_bad_current_minus"
                                                                ],
                                               code=code_integrate_div_J,
                                               position_name="coords",
                                               cofields=[_cofield_div_J])

def compute_total_current(the_field_J):
    compute_div_J(the_field_J,target=_cofield_div_J)
Пример #17
0
  def setup(self):
    '''This function should be called after the method 'set' to setup
       the simulation (create the fields, the operators and so on)'''

    # Should not do initializizations more than once
    if self.is_ready: return

    mwe_m, field_m = self.new_mwe_and_field("m", [3], initial_values=self.initial_mag)
    mwe_h_total, field_h_total = self.new_mwe_and_field("h_total", [3])

    if self.features["include_demag"]:
      self.new_field("h_demag", indices=[3])
    if self.features["include_exchange"]:
      self.new_field("h_exch", indices=[3])
    if self.features["external_field"]:
      h0 = self.features["external_field"]
      self.new_field("h_ext", indices=[3], initial_values=h0)

    # The demag field
    if self.features["include_demag"]:
      if self.mesh.dim != 3:
        raise "Sorry, the demag-calculation is implemented only for 3-D space."

      mwe_h_demag = self.mwes["h_demag"]
      field_h_demag = self.fields["h_demag"]

      mwe_scalar = self.new_mwe("scalar")
      mwe_rho_m = nfem.mwe_sibling(mwe_scalar, "mwe_rho_m", "renamed_scalar", [("scalar", "rho_m")])
      mwe_phi_m = nfem.mwe_sibling(mwe_scalar, "mwe_phi_m", "renamed_scalar", [("scalar", "phi_m")])

      field_div_m = nfem.make_field(mwe_rho_m)
      field_phi_m = nfem.make_field(mwe_phi_m)

      diffop_div_m_str = "%f <rho_m||d/dxj m(j)>, j:3" % self.m_sat
      print diffop_div_m_str
      compute_div_m = \
       nfem.diffop_applicator(diffop_div_m_str,
                              mwe_rho_m, mwe_m,
                              interface_coeffs=[(-2,-2,1.0)],
                              petsc_name="mumag_div_m")

      prematrix_laplace = \
       nfem.prematrix("-<d/dxj rho_m||d/dxj phi_m>, j:3", mwe_rho_m, mwe_phi_m)

      solve_bem = \
       nfem.laplace_solver_bem(prematrix_laplace, inside_regions=self.where)

      compute_grad_phi = \
       nfem.diffop_applicator("<h_demag(j)||d/dxj phi_m>, j:3",
                              mwe_h_demag, mwe_phi_m, result="field")

      cofield_div_m = compute_div_m(self.fields["m"])
      solve_bem(cofield_div_m, target=field_phi_m)
      compute_grad_phi(field_phi_m, target=field_h_demag)

      def calculate_h_demag():
        compute_div_m(self.fields["m"], target=cofield_div_m)
        solve_bem(cofield_div_m, target=field_phi_m)
        compute_grad_phi(field_phi_m, target=field_h_demag)

      self.calculate_h_demag = calculate_h_demag

    # Now we add the exchange and demag fields if needed
    if self.features["include_exchange"]:
      if not self.features["exchange_coupling"]:
        raise "You want to include exchange interaction, " + \
         "but you did not specify the exchange coupling constant!"
      ec = self.features["exchange_coupling"]
      if ec < 0.0:
        raise "Error: you specified a negative exchange coupling constant."
      mwe_h_exch = self.mwes["h_exch"]
      field_h_exch = self.fields["h_exch"]
      exch_factor  = -2.0*ec/self.m_sat_mu0
      op_str = "%f <d/dxi h_exch(j) || d/dxi m(j)>, i:%d, j:3" % (exch_factor, self.mesh.dim)
      op_h_exch = nfem.diffop(op_str)
      p = nfem.prematrix(op_h_exch, mwe_h_exch, mwe_m, ignore_jumps=True)
      compute_h_exch = nfem.prematrix_applicator(p)
      h_exch_cofield = compute_h_exch(field_m)
      nfem.cofield_to_field(h_exch_cofield, target=field_h_exch)
      def calculate_h_exch():
        compute_h_exch(field_m, target=h_exch_cofield)
        nfem.cofield_to_field(h_exch_cofield, target=field_h_exch)

      self.calculate_h_exch = calculate_h_exch

    # Create the C-functions which performs the different parts
    # of the computation

    some_names = ["m", "h_total"]
    some_mwes = self.mwe_list(some_names)
    some_fields = self.field_list(some_names)

    if self.uniaxial_anis:
      args = ["m_sat_mu0", "axis_x", "axis_y", "axis_z", "k1", "k2"]
      c_uniaxial = nfem.site_wise_applicator(args, ccode_uniaxial, field_mwes=some_mwes)
      def calculate_uniaxial_anis():
        for ua in self.uniaxial_anis:
          axis, k1, k2 = ua
          axis_x, axis_y, axis_z = axis
          args_values = [self.m_sat_mu0, axis_x, axis_y, axis_z, k1, k2]
          c_uniaxial(args_values, fields=some_fields)

      self.calculate_uniaxial_anis = calculate_uniaxial_anis

    if self.cubic_anis:
      args = ["m_sat_mu0", "axis1_x", "axis1_y", "axis1_z",
       "axis2_x", "axis2_y", "axis2_z", "k1", "k2", "k3"]
      c_cubic = nfem.site_wise_applicator(args, ccode_cubic, field_mwes=some_mwes)
      def calculate_cubic_anis():
        for ca in self.cubic_anis:
          axis1, axis2, k1, k2, k3 = ca
          axis1_x, axis1_y, axis1_z = axis1
          axis2_x, axis2_y, axis2_z = axis2
          args_values = [self.m_sat_mu0, axis1_x, axis1_y, axis1_z,
          axis2_x, axis2_y, axis2_z, k1, k2, k3]
          c_cubic(args_values, fields=some_fields)
      self.calculate_cubic_anis = calculate_cubic_anis

    more_names = ["h_total", "h_ext", "h_demag", "h_exch"]
    more_mwes = self.mwe_list(more_names)
    more_fields = self.field_list(more_names)
    add_fields = nfem.site_wise_applicator([], ccode_add_fields, field_mwes=more_mwes)
    def add_ext_demag_exch():
      add_fields([], fields=more_fields)
    self.add_ext_demag_exch = add_ext_demag_exch

    if self.features["calculate_energy"]:
      swa_calculate_energy = \
       nfem.site_wise_applicator(["energy"], ccode_calculate_energy,
                                 field_mwes=[mwe_m],cofield_mwes=[mwe_h_total])
      cofield_h_total = nfem.field_to_cofield(field_h_total)
      def calculate_energy():
        nfem.field_to_cofield(field_h_total, target=cofield_h_total)
        energy = swa_calculate_energy([0.0], fields=[field_m], cofields=[cofield_h_total])
        return -self.m_sat_mu0*energy[0]
      self.__calculate_energy = calculate_energy

    self.is_ready = True