<commit_before>import os
import unittest
import matscipytest

import matscipy.dislocation as sd
import numpy as np

from ase.calculators.lammpslib import LAMMPSlib
from ase.io import write
from scipy.optimize import minimize


class TestDislocation(matscipytest.MatSciPyTestCase):
    """Class to store test for dislocation.py module."""

    def test_core_position(self):
        """Make screw dislocation and fit a core position to it.

        Tests that the fitted core postion is same as created
        """
        print("Fitting the core, takes about 10 seconds")
        dft_alat = 3.19

        dft_C11 = 488
        dft_C12 = 200
        dft_C44 = 137

        dft_elastic_param = [dft_alat,
                             dft_C11,
                             dft_C12,
                             dft_C44]

        cent_x = np.sqrt(6.0)*dft_alat/3.0
        center = np.array((cent_x, 0.0, 0.0))

        # make the cell with dislocation core not in center
        disloc, bulk, u = sd.make_screw_cyl(dft_alat, dft_C11,
                                            dft_C12, dft_C44,
                                            cylinder_r=40,
                                            center=center)

        #  initial guess is the center of the cell
        initial_guess = np.diagonal(disloc.cell).copy()[:2] / 2.0

        res = minimize(sd.cost_function,
                       initial_guess,
                       args=(disloc,
                             bulk,
                             40,
                             dft_elastic_param,
                             False, False),
                       method='Nelder-Mead')

        self.assertArrayAlmostEqual(res.x, initial_guess + center[:2], tol=1e-4)

    # This function tests the lammpslib and LAMMPS installation and thus skipped during automated testing
    # Comment the line below to run it.
    @unittest.skip("LAMMPS installation is required and thus is not good for automated testing")
    def test_elastic_constants_lammpslib(self):
        """Test the get_elastic_constants() function using lammpslib.

        If lammps crashes, check "lammps.log" file in the tests directory
        See lammps and ASE documentation on how to make lammpslib work
        """
        print("WARNING: In case lammps crashes no error message is printed: ",
              "check 'lammps.log' file in test folder")
        target_values = np.array([3.14339177996466,  # alat
                                  523.0266819809012,  # C11<commit_msg>added test_elastic_constants_EAM() using matscipy EAM calculator<commit_after>import os
import unittest
import matscipytest

import matscipy.dislocation as sd
import numpy as np

from ase.calculators.lammpslib import LAMMPSlib
from ase.io import write
from scipy.optimize import minimize
from matscipy.calculators.eam import EAM

class TestDislocation(matscipytest.MatSciPyTestCase):
    """Class to store test for dislocation.py module."""

    def test_core_position(self):
        """Make screw dislocation and fit a core position to it.

        Tests that the fitted core postion is same as created
        """
        print("Fitting the core, takes about 10 seconds")
        dft_alat = 3.19

        dft_C11 = 488
        dft_C12 = 200
        dft_C44 = 137

        dft_elastic_param = [dft_alat,
                             dft_C11,
                             dft_C12,
                             dft_C44]

        cent_x = np.sqrt(6.0)*dft_alat/3.0
        center = np.array((cent_x, 0.0, 0.0))

        # make the cell with dislocation core not in center
        disloc, bulk, u = sd.make_screw_cyl(dft_alat, dft_C11,
                                            dft_C12, dft_C44,
                                            cylinder_r=40,
                                            center=center)

        #  initial guess is the center of the cell
        initial_guess = np.diagonal(disloc.cell).copy()[:2] / 2.0

        res = minimize(sd.cost_function,
                       initial_guess,
                       args=(disloc,
                             bulk,
                             40,
                             dft_elastic_param,
                             False, False),
                       method='Nelder-Mead')

        self.assertArrayAlmostEqual(res.x, initial_guess + center[:2], tol=1e-4)

    def test_elastic_constants_EAM(self):
        """Test the get_elastic_constants() function using matscipy EAM calculator."""
        target_values = np.array([3.14339177996466,  # alat
                                  523.0266819809012,  # C11
                                  202.1786296941397,  # C12
                                  160.88179872237012])  # C44 for eam4

        pot_name = "w_eam4.fs"

        calc_EAM = EAM(pot_name)

        obtained_values = sd.get_elastic_constants(calculator=calc_EAM,
                                                   delta=1.0e-3)

        self.assertArrayAlmostEqual(obtained_values, target_values, tol=1e-4)

    # This function tests the lammpslib and LAMMPS installation and thus skipped during automated testing
    # Comment the line below to run it.
    @unittest.skip("LAMMPS installation is required and thus is not good for automated testing")
    def test_elastic_constants_lammpslib(self):
        """Test the get_elastic_constants() function using lammpslib.

        If lammps crashes, check "lammps.log" file in the tests directory
        See lammps and ASE documentation on how to make lammpslib work
        """
        print("WARNING: In case lammps crashes no error message is printed: ",
              "check 'lammps.log' file in test folder")
        target_values = np.array([3.14339177996466,  # alat
                                  523.0266819809012,  # C11
{'fraction_non_alphanumeric': 0.0683819720255569, 'fraction_numerical': 0.04196166465204628, 'mean_word_length': 1.676524953789279, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1651044', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1626, 'n_words': 457}