Пример #1
0
 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]
Пример #2
0
 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]
Пример #3
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
Пример #4
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