예제 #1
0
    def atomic_ordering_energy(self, dbe):
        """
        Return the atomic ordering contribution in symbolic form.
        Description follows Servant and Ansara, Calphad, 2001.
        """
        phase = dbe.phases[self.phase_name]
        ordered_phase_name = phase.model_hints.get('ordered_phase', None)
        disordered_phase_name = phase.model_hints.get('disordered_phase', None)
        if phase.name != ordered_phase_name:
            return S.Zero
        disordered_model = self.__class__(dbe, sorted(self.components),
                                          disordered_phase_name)
        constituents = [sorted(set(c).intersection(self.components)) \
                for c in dbe.phases[ordered_phase_name].constituents]

        # Fix variable names
        variable_rename_dict = {}
        disordered_sitefracs = [x for x in disordered_model.energy.free_symbols if isinstance(x, v.SiteFraction)]
        for atom in disordered_sitefracs:
            # Replace disordered phase site fractions with mole fractions of
            # ordered phase site fractions.
            # Special case: Pure vacancy sublattices
            all_species_in_sublattice = \
                dbe.phases[disordered_phase_name].constituents[
                    atom.sublattice_index]
            if atom.species.name == 'VA' and len(all_species_in_sublattice) == 1:
                # Assume: Pure vacancy sublattices are always last
                vacancy_subl_index = \
                    len(dbe.phases[ordered_phase_name].constituents)-1
                variable_rename_dict[atom] = \
                    v.SiteFraction(
                        ordered_phase_name, vacancy_subl_index, atom.species)
            else:
                # All other cases: replace site fraction with mole fraction
                variable_rename_dict[atom] = \
                    self.mole_fraction(
                        atom.species,
                        ordered_phase_name,
                        constituents,
                        dbe.phases[ordered_phase_name].sublattices
                        )
        # Save all of the ordered energy contributions
        # This step is why this routine must be called _last_ in build_phase
        ordered_energy = Add(*list(self.models.values()))
        self.models.clear()
        # Copy the disordered energy contributions into the correct bins
        for name, value in disordered_model.models.items():
            self.models[name] = value.xreplace(variable_rename_dict)
        # All magnetic parameters will be defined in the disordered model
        self.TC = self.curie_temperature = disordered_model.TC
        self.TC = self.curie_temperature = self.TC.xreplace(variable_rename_dict)

        molefraction_dict = {}

        # Construct a dictionary that replaces every site fraction with its
        # corresponding mole fraction in the disordered state
        ordered_sitefracs = [x for x in ordered_energy.free_symbols if isinstance(x, v.SiteFraction)]
        for sitefrac in ordered_sitefracs:
            all_species_in_sublattice = \
                dbe.phases[ordered_phase_name].constituents[
                    sitefrac.sublattice_index]
            if sitefrac.species.name == 'VA' and len(all_species_in_sublattice) == 1:
                # pure-vacancy sublattices should not be replaced
                # this handles cases like AL,NI,VA:AL,NI,VA:VA and
                # ensures the VA's don't get mixed up
                continue
            molefraction_dict[sitefrac] = \
                self.mole_fraction(sitefrac.species,
                                   ordered_phase_name, constituents,
                                   dbe.phases[ordered_phase_name].sublattices)

        return ordered_energy - ordered_energy.xreplace(molefraction_dict)
예제 #2
0
    def atomic_ordering_energy(self, dbe):
        """
        Return the atomic ordering contribution in symbolic form.
        Description follows Servant and Ansara, Calphad, 2001.
        """
        phase = dbe.phases[self.phase_name]
        ordered_phase_name = phase.model_hints.get('ordered_phase', None)
        disordered_phase_name = phase.model_hints.get('disordered_phase', None)
        if phase.name != ordered_phase_name:
            return S.Zero
        disordered_model = self.__class__(dbe, sorted(self.components),
                                          disordered_phase_name)
        constituents = [sorted(set(c).intersection(self.components)) \
                for c in dbe.phases[ordered_phase_name].constituents]

        # Fix variable names
        variable_rename_dict = {}
        disordered_sitefracs = [x for x in disordered_model.energy.free_symbols if isinstance(x, v.SiteFraction)]
        for atom in disordered_sitefracs:
            # Replace disordered phase site fractions with mole fractions of
            # ordered phase site fractions.
            # Special case: Pure vacancy sublattices
            all_species_in_sublattice = \
                dbe.phases[disordered_phase_name].constituents[
                    atom.sublattice_index]
            if atom.species.name == 'VA' and len(all_species_in_sublattice) == 1:
                # Assume: Pure vacancy sublattices are always last
                vacancy_subl_index = \
                    len(dbe.phases[ordered_phase_name].constituents)-1
                variable_rename_dict[atom] = \
                    v.SiteFraction(
                        ordered_phase_name, vacancy_subl_index, atom.species)
            else:
                # All other cases: replace site fraction with mole fraction
                variable_rename_dict[atom] = \
                    self.mole_fraction(
                        atom.species,
                        ordered_phase_name,
                        constituents,
                        dbe.phases[ordered_phase_name].sublattices
                        )
        # Save all of the ordered energy contributions
        # This step is why this routine must be called _last_ in build_phase
        ordered_energy = Add(*list(self.models.values()))
        self.models.clear()
        # Copy the disordered energy contributions into the correct bins
        for name, value in disordered_model.models.items():
            self.models[name] = value.xreplace(variable_rename_dict)
        # All magnetic parameters will be defined in the disordered model
        self.TC = self.curie_temperature = disordered_model.TC
        self.TC = self.curie_temperature = self.TC.xreplace(variable_rename_dict)

        molefraction_dict = {}

        # Construct a dictionary that replaces every site fraction with its
        # corresponding mole fraction in the disordered state
        ordered_sitefracs = [x for x in ordered_energy.free_symbols if isinstance(x, v.SiteFraction)]
        for sitefrac in ordered_sitefracs:
            all_species_in_sublattice = \
                dbe.phases[ordered_phase_name].constituents[
                    sitefrac.sublattice_index]
            if sitefrac.species.name == 'VA' and len(all_species_in_sublattice) == 1:
                # pure-vacancy sublattices should not be replaced
                # this handles cases like AL,NI,VA:AL,NI,VA:VA and
                # ensures the VA's don't get mixed up
                continue
            molefraction_dict[sitefrac] = \
                self.mole_fraction(sitefrac.species,
                                   ordered_phase_name, constituents,
                                   dbe.phases[ordered_phase_name].sublattices)

        return ordered_energy - ordered_energy.xreplace(molefraction_dict)
예제 #3
0
def search_error(v_str, GF_step=4, use_latex=True):
    global a
    global x

    g = x ** 10 + x ** 8 + x ** 5 + x ** 4 + x ** 2 + x + 1
    p = x ** 4 + x + 1

    string = ''
    degree_of_symbol_x, log_table, exp_table = get_tables_log_exp(GF_step, p, use_latex)

    degree_of_symbol_x_pd = pd.DataFrame(degree_of_symbol_x, columns=['Mult', 'Ans', 'Bin'])
    if use_latex:
        string += "\\noindent\n\\begin{minipage}[c]{60mm}\n\\parindent=3em"
        table = degree_of_symbol_x_pd.to_latex(index=False, escape=False)
        table = table.replace('toprule', 'hline').replace('midrule', 'hline').replace('bottomrule', 'hline')
        string += table
        string += "\n\\end{minipage}"
        string += "\\hfill"
    else:
        string += '\n'
        string += degree_of_symbol_x_pd.to_string()

    log_table_pd = pd.DataFrame(log_table, columns=['a', 'log(a)'])
    if use_latex:
        string += "\n\\begin{minipage}[c]{30mm}\n"
        table = log_table_pd.to_latex(index=False, escape=False)
        table = table.replace('toprule', 'hline').replace('midrule', 'hline').replace('bottomrule', 'hline')
        string += table
        string += "\n\\end{minipage}"
        string += "\\hfill"
    else:
        string += '\n'
        string += log_table_pd.to_string()

    exp_table_pd = pd.DataFrame(exp_table, columns=['k', r'$\alpha^k$'])
    if use_latex:
        string += "\n\\begin{minipage}[c]{30mm}\n"
        table = exp_table_pd.to_latex(index=False, escape=False)
        table = table.replace('toprule', 'hline').replace('midrule', 'hline').replace('bottomrule', 'hline')
        string += table
        string += "\n\\end{minipage}"
        string += "\\hfill"
        string += '\n'
    else:
        string += '\n'
        string += exp_table_pd.to_string()

    string += '~\\\\ \n ~\\\\ \n\n'
    v_vec = [int(i) for i in list(v_str)]
    v = Add()
    for i in range(len(v_vec)):
        v += v_vec[i] * x ** i
    if use_latex:
        string += '{tab}\n\n$v = {}${newline}\n\n'.format(latex(v), tab=r'~\\', newline=r'\\')
    else:
        string += '\n\nv = {}\n\n'.format(v)

    S1, ind, calc = calc_S(v_vec, 1, GF_step, degree_of_symbol_x, use_latex)
    if use_latex:
        string += '$S1 = {} = {} = {}$\n\n'.format(calc, degree_of_symbol_x[ind][2], latex(S1), GF=GF_step)
    else:
        string += 'S1 = {} = {} = {}\n'.format(calc, degree_of_symbol_x[ind][2], S1, GF=GF_step)

    S3, ind, calc = calc_S(v_vec, 3, GF_step, degree_of_symbol_x, use_latex)
    if use_latex:
        string += '$S3 = {} = {} = {}$\n\n'.format(calc, degree_of_symbol_x[ind][2], latex(S3), GF=GF_step)
    else:
        string += 'S3 = {} = {} = {}\n'.format(calc, degree_of_symbol_x[ind][2], S3, GF=GF_step)

    S5, ind, calc = calc_S(v_vec, 5, GF_step, degree_of_symbol_x, use_latex)
    if use_latex:
        string += '$S5 = {} = {} = {}$\\\\\n\n'.format(calc, degree_of_symbol_x[ind][2], latex(S5), GF=GF_step)
    else:
        string += 'S5 = {} = {} = {}\n\n'.format(calc, degree_of_symbol_x[ind][2], S5, GF=GF_step)

    Lambda1 = S1

    success3, Lambda2, Lambda3 = get_lambda_3(S1, S3, S5, degree_of_symbol_x, GF_step)
    if not success3:
        success2, Lanbda2, Lambda3 = get_lambda_2(S1, S3, degree_of_symbol_x, GF_step)

    if success3:
        if use_latex:
            string += '${slash}Lambda_0 = 1$\n\n'.format(slash='\\')
            string += '${slash}Lambda_1 = S_1 = {}$\n\n'.format(latex(Lambda1), slash='\\')
            string += '${slash}Lambda_2 = (S_1^2 {slash}cdot S_3 + S_5) {slash}cdot {one} = {}$\n\n'. \
                format(latex(Lambda2), slash='\\', one=r'{(S_1^3 + S_3)}^{-1}')
            string += '${slash}Lambda_3 = (S_1^3 + S_3) + S_1{slash}cdot{slash}Lambda_2 = {}$\\\\\n\n'. \
                format(latex(Lambda3), slash='\\')
        else:
            string += 'Lambda0 = 1\n'
            string += 'Lambda1 = S1 = {}\n'.format(Lambda1)
            string += 'Lambda2 = (S1^2*S3 + S5)*(S1^3 + S3)^-1 = {}\n'.format(Lambda2)
            string += 'Lambda3 = (S1^3 + S3) + S1*Lambda2 = {}\n\n'.format(Lambda3)
    elif success2:
        if use_latex:
            string += '$\\Lambda_0 = 1$\n\n'
            string += '$\\Lambda_1 = S_1 = {}$\n\n'.format(latex(Lambda1))
            string += '$\\Lambda_2 = (S_3 + S_1^3){one} = {}$\n\n'. \
                format(latex(Lambda2), one=r'S1^{-1}')
        else:
            string += 'Lambda0 = 1\n'
            string += 'Lambda1 = S1 = {}\n'.format(Lambda1)
            string += 'Lambda2 = (S3 + S1^3)S1^-1 = {}\n\n'.format(Lambda2)
    else:
        if use_latex:
            string += '$\\Lambda_0 = 1$\n\n'
            string += '$\\Lambda_1 = S_1 = {}$\n\n'.format(latex(Lambda1))
        else:
            string += 'Lambda0 = 1\n'
            string += 'Lambda1 = S1 = {}\n\n'.format(Lambda1)

    Lambda = Add() + 1 + Lambda1 * x + Lambda2 * (x ** 2) + Lambda3 * (x ** 3)
    max_count = 0
    if Lambda1 != 0:
        max_count = 1
    if Lambda2 != 0:
        max_count = 2
    if Lambda3 != 0:
        max_count = 3
    if use_latex:
        string += '\n$\\Lambda = {}$\\\\\n\n'.format(latex(Lambda))
    else:
        string += 'Lambda = {}\n\n'.format(Lambda)

    Lambda_vec = np.zeros((2 ** GF_step - 1, 2), dtype=object)
    count = 0
    stop = 0
    for i in range(2 ** GF_step - 1):
        xreplace = Lambda.xreplace({x: a ** i}).as_poly(a, domain='GF(2)').args[0]
        Lambda_vec[i][0] = degree_mod(xreplace, 2 ** GF_step - 1)
        Lambda_vec[i][1] = sum_alpha(Lambda_vec[i][0], degree_of_symbol_x)
        if Lambda_vec[i][1] == 0:
            count += 1
        if xreplace == Lambda_vec[i][0]:
            if use_latex:
                string += '$\\Lambda({}) = {} = {}$\n\n'.format(latex(a ** i), latex(Lambda_vec[i][0]),
                                                                '{:>0{GF}b}'.format(Lambda_vec[i][1], GF=GF_step))
            else:
                string += 'Lambda({}) = {} = {}\n'.format(a ** i, Lambda_vec[i][0],
                                                          '{:>0{GF}b}'.format(Lambda_vec[i][1], GF=GF_step))
        else:
            if use_latex:
                string += '$\\Lambda({}) = {} = {} = {}$\n\n'.format(latex(a ** i), latex(xreplace),
                                                                     latex(Lambda_vec[i][0]),
                                                                     '{:>0{GF}b}'.format(Lambda_vec[i][1], GF=GF_step))
            else:
                string += 'Lambda({}) = {} = {} = {}\n'.format(a ** i, xreplace, Lambda_vec[i][0],
                                                               '{:>0{GF}b}'.format(Lambda_vec[i][1], GF=GF_step))

        if count == max_count:
            stop = i + 1
            break

    ans = np.where(Lambda_vec[:stop, 1] == 0)[0]
    ans = [a ** int(an) for an in ans]
    if use_latex:
        string += '~\\\\\n\n'
        string += "\\noindent\n\\begin{minipage}[c]{40mm}\n\\parindent=3em"
    else:
        string += '\n'
    for i in range(len(ans)):
        if use_latex:
            string += '$x_{} = {}${end}'.format(i, latex(ans[i]), end='\n~\\\\\n' if i == (len(ans)) else '\n\n')
        else:
            string += 'x{} = {}\n'.format(i, ans[i])
    if use_latex:
        string += "\n\\end{minipage}"
        string += "\\hfill"
        string += "\n\\begin{minipage}[c]{40mm}\n"
        # string += '\n\n'
    else:
        string += '\n'
    ans = [degree_mod(an ** (-1), 2 ** GF_step - 1) for an in ans]
    for i in range(len(ans)):
        if use_latex:
            string += '$x_{}^{one} = {}${end}'.format(i, latex(ans[i]), one='{-1}',
                                                      end='\n~\\\\\n' if i == (len(ans)) else '\n\n')
        else:
            string += 'x{}^-1 = {}\n'.format(i, ans[i], one='{-1}')
    if use_latex:
        string += "\n\\end{minipage}"
        string += "\\hfill"
        string += "\n\\begin{minipage}[c]{40mm}\n"
        # string += '\n\n'
    else:
        string += '\n'
    ans = [0 if an == 1 else 1 if an == a else int(an.args[1]) for an in ans]
    for i in range(len(ans)):
        if use_latex:
            string += '$\\log(x_{}^{one}) = {}$\n\n'.format(i, ans[i], one='{-1}')
        else:
            string += 'log(x{}^-1) = {}\n'.format(i, ans[i])

    if use_latex:
        string += "\n\\end{minipage}"
        string += "\\hfill"

    ans.sort()
    string += '\n~\\\\\n\nError in: {}'.format(ans)

    return string, ans