Beispiel #1
0
    def get_forces(self, cell_with_disp):
        """
        Calculate the forces of a supercell using tinker
        :param cell_with_disp: supercell (PhonopyAtoms) from which determine the forces
        :return array: numpy array matrix with forces of atoms [Natoms x 3]
        """

        import tempfile
        import subprocess
        import os
        from subprocess import PIPE

        temp_file_name = tempfile.gettempdir() + '/tinker_temp' + '_' + str(
            os.getpid())

        # temp_file_name = 'test_calc'

        supercell_wd = rebuild_connectivity_tinker(self._structure,
                                                   cell_with_disp,
                                                   self._supercell_matrix)

        tinker_input_file = open(temp_file_name + '.txyz', mode='w')
        tinker_input_file.write(generate_tinker_txyz_file(supercell_wd))

        tinker_key_file = open(temp_file_name + '.key', mode='w')
        tinker_key_file.write(generate_tinker_key_file(supercell_wd))

        tinker_input_file.close()
        tinker_key_file.close()

        tinker_command = './testgrad ' + tinker_input_file.name + \
                         ' ' + self.force_field + ' Y N N ' + ' -k ' + tinker_key_file.name

        tinker_process = subprocess.Popen(tinker_command,
                                          stdin=PIPE,
                                          stderr=PIPE,
                                          stdout=PIPE,
                                          shell=True)
        (output, err) = tinker_process.communicate()
        tinker_process.wait()

        if len(err.split()) != 0:
            print(err)
            print('Something wrong in forces calculation!')
            exit()

        # print(output)
        os.unlink(tinker_input_file.name)
        os.unlink(tinker_key_file.name)

        forces = parse_tinker_forces(output) * unit_factors[self.units]

        return forces
    from subprocess import PIPE

    force_field = 'mm3'
    temp_file_name = tempfile.gettempdir() + '/tinker_temp'+ '_' + str(os.getpid())


    tinker_input_file = open(temp_file_name + '.txyz',mode='w')
    tinker_input_file.write(generate_tinker_txyz_file(supercell_wd))

    tinker_key_file = open(temp_file_name + '.key',mode='w')
    tinker_key_file.write(generate_tinker_key_file(supercell_wd))

    tinker_input_file.close()
    tinker_key_file.close()


    print('filename', tinker_input_file)
    tinker_command = './testgrad ' + tinker_input_file.name + \
                     ' ' + force_field + ' Y N N ' + ' -k ' + tinker_key_file.name

    tinker_process = subprocess.Popen(tinker_command, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)
    (output, err) = tinker_process.communicate()
    tinker_process.wait()

    os.unlink(tinker_input_file.name)
    os.unlink(tinker_key_file.name)

    forces = parse_tinker_forces(output)
    print(forces)
    print(forces.shape)