Пример #1
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
Пример #2
0
def save_field(field, fieldname, filename):
    ccode = (
        '''
#line 19 "save_field.py"
  static FILE *f = (FILE *) NULL;

  if ( close_file > 0.0 ) {
    if ( f != (FILE *) NULL) {
      (void) fclose(f);
      f = (FILE *) NULL;
    }

  } else {
    if ( f == (FILE *) NULL )
      f = fopen("'''
        + filename
        + """", "w");

    if ( f != (FILE *) NULL ) {
      if ( have_"""
        + fieldname
        + """ ) {
        double c[3] = {0.0, 0.0, 0.0};
        int i;
        for(i=0; i<((int) dim); i++) c[i] = COORDS(i);
        fprintf(f, "%g %f %f %f %g %g %g\\n",
          time, c[0], c[1], c[2], """
        + fieldname
        + """(0), """
        + fieldname
        + """(1), """
        + fieldname
        + """(2));
      }
    }
  }
  """
    )

    dim = float(field_dim(field))
    mwe = ocaml.get_mwe(field)
    fn = nfem.site_wise_applicator(["close_file", "time", "dim"], ccode, field_mwes=[mwe])

    def saver(close=False, time=0.0):
        if close:
            fn([1.0, float(time), dim], fields=[field])
        else:
            fn([-1.0, float(time), dim], fields=[field])

    return saver
Пример #3
0
def surface_interaction(m_h_list, couplings):
    """This function is used to calculate the exchange interactions between
  magnetisations defined at the same sites in the mesh."""

    mwes = []
    fields = []
    i = 1
    for m_h_couple in m_h_list:
        m, h = m_h_couple

        m_new_name = "m%d" % i
        h_new_name = "h%d" % i
        i += 1

        mwe_m = ocaml.get_mwe(m)
        mwe_mn = nfem.mwe_sibling(mwe_m, m_new_name, "renamed_m",
                                  [("m", m_new_name)])
        mn = nfem.field_alias(m, mwe_mn)

        mwe_h = ocaml.get_mwe(h)
        mwe_hn = nfem.mwe_sibling(mwe_h, h_new_name, "renamed_h",
                                  [("h_total", h_new_name)])
        hn = nfem.field_alias(h, mwe_hn)

        fields.append(mn)
        fields.append(hn)

        mwes.append(mwe_mn)
        mwes.append(mwe_hn)

    ccode = ccode_surface_interaction
    for ac in couplings:
        i1, i2, m_sat1, m_sat2, ec = ac  # ec is the exchange coupling constant
        ccode += "SURF_INTERACTION(m%d, %f, h%d, m%d, %f, h%d, %f)\n" % \
          (i1, m_sat1, i1, i2, m_sat2, i2, ec)

    original_fn = nfem.site_wise_applicator([], ccode, field_mwes=mwes)

    def fn():
        original_fn([], fields=fields)

    return fn
Пример #4
0
def surface_interaction(m_h_list, couplings):
  """This function is used to calculate the exchange interactions between
  magnetisations defined at the same sites in the mesh."""

  mwes = []
  fields = []
  i = 1
  for m_h_couple in m_h_list:
    m, h = m_h_couple

    m_new_name = "m%d" % i
    h_new_name = "h%d" % i
    i += 1

    mwe_m = ocaml.get_mwe(m)
    mwe_mn = nfem.mwe_sibling(mwe_m, m_new_name, "renamed_m", [("m", m_new_name)])
    mn = nfem.field_alias(m, mwe_mn)

    mwe_h = ocaml.get_mwe(h)
    mwe_hn = nfem.mwe_sibling(mwe_h, h_new_name, "renamed_h", [("h_total", h_new_name)])
    hn = nfem.field_alias(h, mwe_hn)

    fields.append( mn )
    fields.append( hn )

    mwes.append( mwe_mn )
    mwes.append( mwe_hn )

  ccode = ccode_surface_interaction
  for ac in couplings:
    i1, i2, m_sat1, m_sat2, ec = ac # ec is the exchange coupling constant
    ccode += "SURF_INTERACTION(m%d, %f, h%d, m%d, %f, h%d, %f)\n" % \
      (i1, m_sat1, i1, i2, m_sat2, i2, ec)

  original_fn = nfem.site_wise_applicator([], ccode, field_mwes=mwes)
  def fn():
    original_fn([], fields=fields)

  return fn
Пример #5
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

        self.damping = self.features["damping"]
        self.gamma_G = self.features["gamma_G"]
        self.gamma_LL = self.features["gamma_LL"]
        self.absolute_step_error = self.features["absolute_step_error"]
        self.relative_step_error = self.features["relative_step_error"]
        self.step_headroom = self.features["step_headroom"]
        self.stopping_time = self.features["stopping_time"]
        self.stopping_dm_dt = self.features["stopping_dm_dt"]
        self.max_iterations = self.features["max_iterations"]
        self.max_rejections = self.features["max_rejections"]
        self.initial_dt = self.features["initial_dt"]
        self.max_dt_ratio = self.features["max_dt_ratio"]
        self.initial_time = self.features["initial_time"]
        self.time = 0.0

        MumagCore.setup(self)

        # self.llg_gamma_LL == None if llg_gamma_G is given by the user
        if self.gamma_LL == None:
            self.gamma_LL = self.gamma_G / (1.0 + self.damping**2)

        self.new_field("old_m", indices=[3], initial_values=self.initial_mag)
        self.new_field("dm_dt", indices=[3])
        self.new_field("old_dm_dt", indices=[3])
        self.new_field("delta_t", indices=[])

        # Just some shorthands for what follows
        mwe_m = self.mwes["m"]
        field_m = self.fields["m"]

        # The auxiliary arguments passed from python to C
        args_names = [
            "time", "dt", "old_max_norm_dm_dt", "llg_damping", "llg_gamma_LL",
            "absolute_step_error", "relative_step_error", "step_headroom",
            "m_sat"
        ]

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

        # The fields which will be passed to 'step_make' and 'step_check'
        some_names = [
            "m", "old_m", "dm_dt", "old_dm_dt", "delta_t", "h_ext", "h_total",
            "h_demag", "h_exch"
        ]
        some_mwes = self.mwe_list(some_names)
        some_fields = self.field_list(some_names)

        # Called to execute the euler step (calculate the new m and the
        # value of old_norm_dm_dt needed for the computation of the error)
        c_step_make = nfem.site_wise_applicator(args_names,
                                                ccode_step_make,
                                                field_mwes=some_mwes)

        def step_make(time, dt):
            self.old_max_norm_dm_dt = 0.0
            aux_arg_list = [
                time, dt, self.old_max_norm_dm_dt, self.damping, self.gamma_LL,
                self.absolute_step_error, self.relative_step_error,
                self.step_headroom, self.m_sat
            ]
            modified_args = c_step_make(aux_arg_list, fields=some_fields)
            self.old_max_norm_dm_dt = modified_args[2]

        #----------------------End of definition of the function step_make
        self.step_make = step_make

        c_step_check = nfem.site_wise_applicator(args_names,
                                                 ccode_step_check,
                                                 field_mwes=some_mwes)

        def step_check(time, dt):
            aux_arg_list = [
                time, dt, self.old_max_norm_dm_dt, self.damping, self.gamma_LL,
                self.absolute_step_error, self.relative_step_error,
                self.step_headroom, self.m_sat
            ]
            c_step_check(aux_arg_list, fields=some_fields)

        #----------------------End of definition of the function step_check
        self.step_check = step_check

        self.c_calculate_next_dt = nfem.site_wise_applicator(
            ["accepted_step", "previous_dt"],
            ccode_calculate_next_dt,
            field_mwes=[self.mwes["delta_t"]])

        self.step_calculate_h_total = self.calculate_h_total
        self.is_ready = True
        self.next_stage(time=self.initial_time)
Пример #6
0
else
{
  double j=drho_by_dt;

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

_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_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)
Пример #7
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

    self.damping = self.features["damping"]
    self.gamma_G = self.features["gamma_G"]
    self.gamma_LL = self.features["gamma_LL"]
    self.absolute_step_error = self.features["absolute_step_error"]
    self.relative_step_error = self.features["relative_step_error"]
    self.step_headroom = self.features["step_headroom"]
    self.stopping_time = self.features["stopping_time"]
    self.stopping_dm_dt = self.features["stopping_dm_dt"]
    self.max_iterations = self.features["max_iterations"]
    self.max_rejections = self.features["max_rejections"]
    self.initial_dt = self.features["initial_dt"]
    self.max_dt_ratio = self.features["max_dt_ratio"]
    self.initial_time = self.features["initial_time"]
    self.time = 0.0


    MumagCore.setup(self)

    # self.llg_gamma_LL == None if llg_gamma_G is given by the user
    if self.gamma_LL == None:
      self.gamma_LL = self.gamma_G/(1.0+self.damping**2)

    self.new_field("old_m", indices=[3], initial_values=self.initial_mag)
    self.new_field("dm_dt", indices=[3])
    self.new_field("old_dm_dt", indices=[3])
    self.new_field("delta_t", indices=[])

    # Just some shorthands for what follows
    mwe_m = self.mwes["m"]
    field_m = self.fields["m"]

    # The auxiliary arguments passed from python to C
    args_names = ["time", "dt", "old_max_norm_dm_dt",
      "llg_damping", "llg_gamma_LL",
      "absolute_step_error", "relative_step_error", "step_headroom",
      "m_sat"]

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

    # The fields which will be passed to 'step_make' and 'step_check'
    some_names = ["m", "old_m", "dm_dt", "old_dm_dt", "delta_t", "h_ext",
     "h_total", "h_demag", "h_exch"]
    some_mwes = self.mwe_list(some_names)
    some_fields = self.field_list(some_names)

    # Called to execute the euler step (calculate the new m and the
    # value of old_norm_dm_dt needed for the computation of the error)
    c_step_make = nfem.site_wise_applicator(args_names,
      ccode_step_make, field_mwes=some_mwes)
    def step_make(time, dt):
      self.old_max_norm_dm_dt = 0.0
      aux_arg_list = [time, dt, self.old_max_norm_dm_dt, self.damping,
        self.gamma_LL, self.absolute_step_error, self.relative_step_error,
        self.step_headroom, self.m_sat]
      modified_args = c_step_make(aux_arg_list, fields=some_fields)
      self.old_max_norm_dm_dt = modified_args[2]
    #----------------------End of definition of the function step_make
    self.step_make = step_make

    c_step_check = nfem.site_wise_applicator(args_names,
      ccode_step_check, field_mwes=some_mwes)

    def step_check(time, dt):
      aux_arg_list = [time, dt, self.old_max_norm_dm_dt, self.damping,
       self.gamma_LL, self.absolute_step_error, self.relative_step_error,
       self.step_headroom, self.m_sat]
      c_step_check(aux_arg_list, fields=some_fields)
    #----------------------End of definition of the function step_check
    self.step_check = step_check

    self.c_calculate_next_dt = nfem.site_wise_applicator(
      ["accepted_step", "previous_dt"], ccode_calculate_next_dt,
      field_mwes=[self.mwes["delta_t"]])

    self.step_calculate_h_total = self.calculate_h_total
    self.is_ready = True
    self.next_stage(time=self.initial_time)
Пример #8
0
nfem.plot_scalar_field(nfem.cofield_to_field(cofield2d_div_M),
                        "rho_M","/tmp/plot-rho-M.ps",
                        plot_edges=False,
                        color_scheme=[(-2.0,[0.1,0.1,1.0]),
                                      (0.0,[0.1,1.0,0.1]),
                                      (2.0,[1.0,0.1,0.1])])


# End DDDDDD

# we still have to correct the Z-field component. For now, we do this
# with a site-wise applicator...

adjust_H_z_2d=nfem.site_wise_applicator(parameter_names=[],
                                         # all the names of extra parameters
                                         code="H(2)=-M(2);",
                                         field_mwes=[mwe2d_M,mwe2d_H]
                                         )
adjust_H_z_2d([],fields=[field2d_M0,field2d_H])

# nfem.field_print_contents(field2d_H) # DDD

print "*** The 2.5-dimensional result ***"

for i in range(0,76):
    pos=[-3.8+i*0.1,0.0]
    print "Position ",pos," H: ",nfem.probe_field(field2d_H,pos)

# === The 3d computation ===

nfem.set_default_mesh(mesh_3d)
Пример #9
0
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;
/* printf(\"cos2=%f sigma = %8.6f\\n \",cos2, 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,
                                                  field_mwes=[mwe_sigma,mwe_J]
                                                  )

# computing the total current through the sample:

code_integrate_div_J="""
if(coords(1)>2.45)
{
  total_current_up+=drho_by_dt;
}
else if(coords(1)<-2.45)
{
  total_current_down+=drho_by_dt;
}
else
Пример #10
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
Пример #11
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
Пример #12
0
}
"""

#check_div_J=nfem.site_wise_applicator(parameter_names=[],
#                                       # all the names of extra parameters
#                                       code=code_check_div_J,
#                                       cofields=[cofield_div_J],
#                                       position_name="coords"
#                                       )
#
#check_div_J([])

integrate_div_J = 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])

print "div J integrated: ", integrate_div_J([0.0, 0.0, 0.0, 0.0])

sys.exit()

# This example shows that we indeed are on the right track and the
# artefacts I encuntered yesterday were just a product of working too
# long... The important thing to remember is:
# DO NOT COMPUTE div(sigma*grad(phi)), compute Laplace(phi)
# (with modified laplace operator) to get div j. The div-grad-thingy will
# do strange things when taking the gradient of phi at the boundary.