Beispiel #1
0
    def __init__(self, numbers, coordinates, masses, energy, gradient, hessian, multiplicity=None, symmetry_number=None, periodic=False, title=None, graph=None, symbols=None, unit_cell=None):
        """
           Arguments:
            | ``numbers`` -- The atom numbers (integer numpy array with shape N)
            | ``coordinates`` -- the atom coordinates in Bohr (float numpy array
                                 with shape Nx3)
            | ``masses`` -- The atomic masses in atomic units (float numpy array
                            with shape N)
            | ``energy`` -- The molecular energy in Hartree
            | ``gradient`` -- The gradient of the energy, i.e. the derivatives
                              towards Cartesian coordinates, in atomic units
                              (float numpy array with shape Nx3)
            | ``hessian`` -- The hessian of the energy, i.e. the matrix with
                             second order derivatives towards Cartesian
                             coordinates, in atomic units (float numpy array
                             with shape 3Nx3N)
            | ``multiplicity`` -- The spin multiplicity of the electronic system

           Optional arguments:
            | ``symmetry_number`` -- The rotational symmetry number, None when
                                     not known or computed [default=None]
            | ``periodic`` -- True when the system is periodic in three
                              dimensions [default=False]
            | ``title`` -- The title of the system
            | ``graph`` -- The molecular graph of the system
            | ``symbols`` -- A list with atom symbols
            | ``unit_cell`` -- The unit cell vectors for periodic structures
        """
        BaseMolecule.__init__(self, numbers, coordinates, title, masses, graph, symbols, unit_cell)
        self.energy = energy
        self.gradient = gradient
        self.hessian = hessian
        self.multiplicity = multiplicity
        self.symmetry_number = symmetry_number
        self.periodic = periodic
Beispiel #2
0
    def __init__(self,
                 numbers,
                 coordinates,
                 masses,
                 energy,
                 gradient,
                 hessian,
                 multiplicity=None,
                 symmetry_number=None,
                 periodic=False,
                 title=None,
                 graph=None,
                 symbols=None,
                 unit_cell=None,
                 fixed=None):
        """
           Arguments:
            | ``numbers`` -- The atom numbers (integer numpy array with shape N)
            | ``coordinates`` -- the atom coordinates in Bohr (float numpy array
                                 with shape Nx3)
            | ``masses`` -- The atomic masses in atomic units (float numpy array
                            with shape N)
            | ``energy`` -- The molecular energy in Hartree
            | ``gradient`` -- The gradient of the energy, i.e. the derivatives
                              towards Cartesian coordinates, in atomic units
                              (float numpy array with shape Nx3)
            | ``hessian`` -- The hessian of the energy, i.e. the matrix with
                             second order derivatives towards Cartesian
                             coordinates, in atomic units (float numpy array
                             with shape 3Nx3N)
            | ``multiplicity`` -- The spin multiplicity of the electronic system

           Optional arguments:
            | ``symmetry_number`` -- The rotational symmetry number, None when
                                     not known or computed [default=None]
            | ``periodic`` -- True when the system is periodic in three
                              dimensions [default=False]
            | ``title`` -- The title of the system
            | ``graph`` -- The molecular graph of the system
            | ``symbols`` -- A list with atom symbols
            | ``unit_cell`` -- The unit cell vectors for periodic structures
            | ``fixed`` -- An array with indices of fixed atoms
        """
        BaseMolecule.__init__(self, numbers, coordinates, title, masses, graph,
                              symbols, unit_cell)
        self.energy = energy
        self.gradient = gradient
        self.hessian = hessian
        self.multiplicity = multiplicity
        self.symmetry_number = symmetry_number
        self.periodic = periodic
        self.fixed = fixed
Beispiel #3
0
def create_molecule(selected_nodes, parent=None):
    numbers = []
    coordinates = []
    atoms = list(iter_atoms(selected_nodes))
    for atom in atoms:
        numbers.append(atom.number)
        if parent is None:
            coordinates.append(atom.get_absolute_frame().t)
        else:
            coordinates.append(atom.get_frame_relative_to(parent).t)
    result = Molecule(numbers, coordinates)
    result.atoms = atoms
    return result
Beispiel #4
0
def create_molecule(selected_nodes, parent=None):
    numbers = []
    coordinates = []
    atoms = list(iter_atoms(selected_nodes))
    for atom in atoms:
        numbers.append(atom.number)
        if parent is None:
            coordinates.append(atom.get_absolute_frame().t)
        else:
            coordinates.append(atom.get_frame_relative_to(parent).t)
    result = Molecule(numbers, coordinates)
    result.atoms = atoms
    return result
Beispiel #5
0
    def collect_molecules(self, parent, universe=None):
        Atom = context.application.plugins.get_node("Atom")
        Bond = context.application.plugins.get_node("Bond")
        Frame = context.application.plugins.get_node("Frame")


        if universe==None:
            universe = parent

        atom_to_index = {}
        atoms_extra = {}
        counter = 0
        numbers = []
        coordinates = []
        for child in parent.children:
            if isinstance(child, Atom):
                atom_to_index[child] = counter
                if len(child.extra) > 0:
                    atoms_extra[counter] = child.extra
                counter += 1
                numbers.append(child.number)
                coordinates.append(child.get_frame_relative_to(universe).t)

        if len(numbers) > 0:
            molecule = Molecule(numbers, coordinates, parent.name)
            molecule.extra = parent.extra
            molecule.atoms_extra = atoms_extra
            molecule.bonds_extra = {}

            pairs = set([])
            for child in parent.children:
                if isinstance(child, Bond):
                    atoms = child.get_targets()
                    pair = frozenset([atom_to_index[atoms[0]], atom_to_index[atoms[1]]])
                    if len(child.extra) > 0:
                        molecule.bonds_extra[pair] = child.extra
                    pairs.add(pair)
            if len(pairs) > 0:
                molecule.graph = MolecularGraph(pairs, molecule.numbers)
            else:
                molecule.graph = None

            result = [molecule]
        else:
            result = []

        for child in parent.children:
            if isinstance(child, Frame):
                result.extend(self.collect_molecules(child, universe))

        return result
Beispiel #6
0
 def get_mol(self, state, root):
     if self.name == "opt":
         # load the initial geometry
         mol = Molecule.from_file("init/%s.xyz" % state.name)
     else:
         # load the optimized geometry
         fn_fchk = "%s/%s__opt/gaussian.fchk" % (root, state.name)
         mol = FCHKFile(fn_fchk, field_labels=[]).molecule
     with open("init/%s.fragments" % state.name) as f:
         mol.charge_mult = f.readline().split()
         mol.tags = f.readline().split()
     return mol
Beispiel #7
0
    def collect_molecules(self, parent, universe=None):
        Atom = context.application.plugins.get_node("Atom")
        Bond = context.application.plugins.get_node("Bond")
        Frame = context.application.plugins.get_node("Frame")

        if universe == None:
            universe = parent

        atom_to_index = {}
        atoms_extra = {}
        counter = 0
        numbers = []
        coordinates = []
        for child in parent.children:
            if isinstance(child, Atom):
                atom_to_index[child] = counter
                if len(child.extra) > 0:
                    atoms_extra[counter] = child.extra
                counter += 1
                numbers.append(child.number)
                coordinates.append(child.get_frame_relative_to(universe).t)

        if len(numbers) > 0:
            molecule = Molecule(numbers, coordinates, parent.name)
            molecule.extra = parent.extra
            molecule.atoms_extra = atoms_extra
            molecule.bonds_extra = {}

            pairs = set([])
            for child in parent.children:
                if isinstance(child, Bond):
                    atoms = child.get_targets()
                    pair = frozenset([atom_to_index[atoms[0]], atom_to_index[atoms[1]]])
                    if len(child.extra) > 0:
                        molecule.bonds_extra[pair] = child.extra
                    pairs.add(pair)
            if len(pairs) > 0:
                molecule.graph = MolecularGraph(pairs, molecule.numbers)
            else:
                molecule.graph = None

            result = [molecule]
        else:
            result = []

        for child in parent.children:
            if isinstance(child, Frame):
                result.extend(self.collect_molecules(child, universe))

        return result
Beispiel #8
0
def test_opbend_ethene():
    mol = Molecule.from_file("input/ethene.xyz")
    c = mol.coordinates.copy()
    assert abs(ic.opbend_cos([c[0], c[5], c[4], c[3]])[0] - 1.0) < 1e-5
    assert abs(ic.opbend_angle([c[0], c[5], c[4], c[3]])[0]) < 1e-5
    for i in xrange(1000):
        angle = np.random.uniform(-np.pi/2, np.pi/2)
        radius = np.random.uniform(0, 5*angstrom)
        #offset = np.random.uniform(0, 5*angstrom)
        c[3] = [
            radius*np.cos(angle),
            0.0,
            radius*np.sin(angle),
        ]
        assert abs(ic.opbend_cos([c[0], c[5], c[4], c[3]])[0] - np.cos(angle)) < 1e-5
        assert abs(ic.opbend_angle([c[0], c[5], c[4], c[3]])[0] - angle) < 1e-5
Beispiel #9
0
def test_dihedral_ethene():
    mol = Molecule.from_file("input/ethene.xyz")
    c = mol.coordinates.copy()
    assert abs(ic.dihed_cos([c[2], c[0], c[3], c[5]])[0] - 1.0) < 1e-5
    assert abs(ic.dihed_angle([c[2], c[0], c[3], c[5]])[0]) < 1e-5
    for i in xrange(1000):
        angle = np.random.uniform(-np.pi, np.pi)
        radius = np.random.uniform(0, 5*angstrom)
        offset = np.random.uniform(0, 5*angstrom)
        c[5] = [
            offset,
            -radius*np.cos(angle),
            -radius*np.sin(angle),
        ]
        assert abs(ic.dihed_cos([c[2], c[0], c[3], c[5]])[0] - np.cos(angle)) < 1e-5
        assert abs(ic.dihed_angle([c[2], c[0], c[3], c[5]])[0] - angle) < 1e-5
Beispiel #10
0
def load_xyz(fn_xyz, temp, masses=None):
    """Create a system and state from a simple XYZ file and a temperature.

       Arguments:
        | ``fn_xyz`` -- The filename of the XYZ file.
        | ``temp`` -- The temperature for the initial velocities.

       Optional argument:
        | ``masses`` -- An array with masses to override the IUPAC 2005
                        values from the MolMod package.
    """
    molecule = Molecule.from_file(fn_xyz)
    if masses is None:
        molecule.set_default_masses()
    else:
        molecule.masses = masses
    vel = numpy.zeros(molecule.coordinates.shape, float)
    system = System(molecule.symbols, molecule.masses, molecule.numbers)
    state = State(molecule.coordinates, vel)
    set_boltzmann_velocities(temp, system, state)
    return system, state
Beispiel #11
0
    def __init__(self, filename):
        """
           Arguments:
            | ``filename`` -- The file to load.
        """
        self.filename = filename

        # auxiliary skip function
        def skip_to(f, linestart):
            while True:
                line = f.readline()
                if line.startswith(linestart):
                    return line
                if len(line) == 0:
                    return

        with open(filename) as f:
            # Unit cell parameters
            line = skip_to(f, ' DIRECT LATTICE VECTOR COMPONENTS (BOHR)')
            if line is None:
                raise FileFormatError('Could not find the lattice vectors')
            f.readline()
            f.readline()
            vectors = []
            for i in range(3):
                line = f.readline()
                vectors.append([float(word) for word in line.split()[1:]])
            vectors = np.array(vectors)
            self.unit_cell = UnitCell(vectors.transpose())
            # Atomic numbers and coordinates
            line = skip_to(f, ' ATOM CARTESIAN COORDINATES (BOHR)')
            if line is None:
                raise FileFormatError('Could not find the atomic coordinates')
            f.readline()
            f.readline()
            f.readline()
            numbers = []
            symbols = []
            coordinates = []
            while True:
                line = f.readline()
                if line.startswith(' *****'):
                    break
                words = line.split()
                numbers.append(int(words[1]))
                symbols.append(words[2])
                coordinates.append(
                    [float(words[3]),
                     float(words[4]),
                     float(words[5])])
            self.mol = Molecule(np.array(numbers),
                                np.array(coordinates),
                                symbols=symbols,
                                unit_cell=self.unit_cell)
            # Basis set specification
            line = skip_to(f, ' VARIATIONAL BASIS SET')
            if line is None:
                raise FileFormatError('Could not find the basis set')
            f.readline()
            f.readline()
            f.readline()
            f.readline()
            f.readline()
            f.readline()
            self.basisset = {}
            last_basis = None
            last_contraction = None
            while True:
                line = f.readline()
                if line.startswith(' *****'):
                    break
                if line.startswith('         '):
                    # add info to the last atomic basis set
                    assert last_basis is not None
                    subshell = line[36:40].strip()
                    if len(subshell) == 0:
                        subshell = last_contraction[0]
                        # add a primitive to the contraction
                        exponent = float(line[40:50])
                        if subshell == 'S':
                            values = exponent, float(line[50:60])
                        elif subshell == 'SP':
                            values = exponent, float(line[50:60]), float(
                                line[60:70])
                        else:
                            values = exponent, float(line[70:80])
                        last_contraction[1].append(values)
                    else:
                        # define a new contraction
                        last_contraction = (subshell, [])
                        last_basis.append(last_contraction)
                else:
                    # add new atoms
                    symbol = line.split()[1]
                    if symbol not in self.basisset:
                        last_basis = []
                        self.basisset[symbol] = last_basis
            # Compute the total number of basis functions (and orbitals).
            self.num_basis = 0
            subshell_counts = {'S': 1, 'P': 3, 'SP': 4, 'D': 5, 'F': 7, 'G': 9}
            for symbol, basis in self.basisset.items():
                symbol_count = symbols.count(symbol)
                for subshell, contraction in basis:
                    self.num_basis += symbol_count * subshell_counts[subshell]
            # Density matrix.
            line = skip_to(f, ' DENSITY MATRIX DIRECT LATTICE')
            if line is None:
                raise FileFormatError('Could not find the density matrix')
            f.readline()
            f.readline()
            f.readline()
            self.density_matrix = np.zeros((self.num_basis, self.num_basis),
                                           float)
            j0 = 0
            while j0 < self.num_basis:
                f.readline()
                f.readline()
                f.readline()
                for i in range(self.num_basis):
                    line = f.readline()
                    words = line.split()[1:]
                    for j1, word in enumerate(words):
                        self.density_matrix[i, j0 + j1] = float(word)
                j0 += 10
Beispiel #12
0
    def from_file(cls, *fns, **user_kwargs):
        """Construct a new System instance from one or more files

           **Arguments:**

           fn1, fn2, ...
                A list of filenames that are read in order. Information in later
                files overrides information in earlier files.

           **Optional arguments:**

           Any argument from the default constructor ``__init__``. These must be
           given with keywords.

           **Supported file formats**

           .xyz
                Standard Cartesian coordinates file (in angstroms). Atomic
                positions and atomic numbers are read from this file. If the
                title consists of 3, 6 or 9 numbers, each group of three numbers
                is interpreted as a cell vector (in angstroms). A guess of the
                bonds will be made based on inter-atomic distances.

           .psf
                Atom types and bonds are read from this file

           .chk
                Internal text-based checkpoint format. It just contains a
                dictionary with the constructor arguments.
        """
        with log.section('SYS'):
            kwargs = {}
            for fn in fns:
                if fn.endswith('.xyz'):
                    from molmod import Molecule
                    mol = Molecule.from_file(fn)
                    kwargs['numbers'] = mol.numbers.copy()
                    kwargs['pos'] = mol.coordinates.copy()
                elif fn.endswith('.psf'):
                    from molmod.io import PSFFile
                    psf = PSFFile(fn)
                    kwargs['ffatypes'] = psf.atom_types
                    kwargs['bonds'] = np.array(psf.bonds, copy=False)
                    kwargs['charges'] = np.array(psf.charges, copy=False)
                elif fn.endswith('.chk'):
                    from molmod.io import load_chk
                    allowed_keys = [
                        'numbers', 'pos', 'scopes', 'scope_ids', 'ffatypes',
                        'ffatype_ids', 'bonds', 'rvecs', 'charges', 'radii',
                        'dipoles','radii2','masses',
                    ]
                    for key, value in load_chk(fn).iteritems():
                        if key in allowed_keys:
                            kwargs.update({key: value})
                elif fn.endswith('.h5'):
                    with h5.File(fn, 'r') as f:
                        return cls.from_hdf5(f)
                else:
                    raise IOError('Can not read from file \'%s\'.' % fn)
                if log.do_high:
                    log('Read system parameters from %s.' % fn)
            kwargs.update(user_kwargs)
        return cls(**kwargs)
Beispiel #13
0
    def run_GCMC(self, N_iterations, N_sample):

        A = Acceptance()

        if rank == 0:
            if not (os.path.isdir('results')):
                try:
                    os.mkdir('results')
                except:pass

            if self.write_traj:
                ftraj = open('results/traj_%.8f.xyz'%(self.P/bar), 'w')

        e = 0
        t_it = time()

        N_samples = []
        E_samples = []
        pressures = []
        traj = []
        q0s = []

        if rank == 0:
            print('\n Iteration  inst. N    inst. E    inst. V     time [s]')
            print('--------------------------------------------------------')

        for iteration in range(N_iterations+1):

            if self.ads_ei:
                sfac_init = deepcopy(self.sfac)
            pos_init = deepcopy(self.pos)
            rvecs_init = deepcopy(self.rvecs)
            rvecs_flat_init = deepcopy(self.rvecs_flat)
            V_init = self.V
            e_el_real_init = self.e_el_real
            e_vdw_init = self.e_vdw
            switch = np.random.rand()
            acc = 0

            # Insertion / deletion
            if(switch < self.prob[0] and not self.Z_ads == self.fixed_N):

                if(switch < self.prob[0]/2):

                    new_pos = random_ads(self.pos_ads, self.rvecs)
                    e_new = self.insertion(new_pos)

                    exp_value = self.beta * (-e_new + e)
                    if(exp_value > 100):
                        acc = 1
                    elif(exp_value < -100):
                        acc = 0
                    else:
                        acc = min(1, self.V*self.beta*self.fugacity/self.Z_ads * np.exp(exp_value))

                    # Reject monte carlo move
                    if np.random.rand() > acc:
                        self.pos = pos_init
                        if self.ads_ei:
                            self.sfac = sfac_init
                        self.e_el_real = e_el_real_init
                        self.e_vdw = e_vdw_init
                        self.Z_ads -= 1
                    else:
                        e = e_new

                elif(self.Z_ads > 0):

                    deleted_coord, e_new = self.deletion()

                    exp_value = -self.beta * (e_new - e)
                    if(exp_value > 100):
                        acc = 1
                    else:
                        acc = min(1, (self.Z_ads+1)/self.V/self.beta/self.fugacity * np.exp(exp_value))

                    # Reject monte carlo move
                    if np.random.rand() > acc:
                        self.pos = pos_init
                        if self.ads_ei:
                            self.sfac = sfac_init
                        self.e_el_real = e_el_real_init
                        self.e_vdw = e_vdw_init
                        self.Z_ads += 1
                    else:
                        e = e_new

            elif(switch < self.prob[1]):

                if self.Z_ads != 0:

                    trial = np.random.randint(self.Z_ads)

                    if((switch < self.prob[0] + (self.prob[1]-self.prob[0])/2) or self.nads == 1):

                        # Calculate translation energy as deletion + insertion of molecule
                        deleted_coord, e_new = self.deletion()
                        deleted_coord += self.step * (np.random.rand(3) - 0.5)
                        e_new = self.insertion(deleted_coord)

                    else:

                        # Calculate rotation energy as deletion + insertion of molecule
                        deleted_coord, e_new = self.deletion()
                        deleted_coord = random_rot(deleted_coord, circlefrac=0.1)
                        e_new = self.insertion(deleted_coord)

                    exp_value = -self.beta * (e_new - e)
                    if(exp_value > 0):
                        exp_value = 0
                    acc = min(1, np.exp(exp_value))

                    # Reject monte carlo move
                    if np.random.rand() > acc:
                        self.pos = pos_init
                        if self.ads_ei:
                            self.sfac = sfac_init
                        self.e_el_real = e_el_real_init
                        self.e_vdw = e_vdw_init
                    else:
                        e = e_new

            else:

                # Construct system and forcefield class for the MD engine
                from yaff import System, ForceField, XYZWriter, VerletScreenLog, MTKBarostat, \
                       NHCThermostat, TBCombination, VerletIntegrator, HDF5Writer, log
                log.set_level(0)

                n = np.append(self.data.numbers_MOF, np.tile(self.data.numbers_ads, self.Z_ads))

                ffa_MOF = self.data.system.ffatypes[self.data.system.ffatype_ids]
                ffa_ads = self.data.system_ads.ffatypes[self.data.system_ads.ffatype_ids]
                ffa = np.append(ffa_MOF, np.tile(ffa_ads, self.Z_ads))
                assert len(self.pos) == len(ffa)

                s = System(n, self.pos, ffatypes = ffa, rvecs=self.rvecs)
                s.detect_bonds()

                ff = ForceField.generate(s, self.ff_file,
                                            rcut=self.rcut,
                                            alpha_scale=self.alpha_scale,
                                            gcut_scale=self.gcut_scale,
                                            tailcorrections=True)

                ff_lammps = swap_noncovalent_lammps(ff, fn_system='system_%.8f.dat'%(self.P/bar),
                                        fn_table='table.dat',
                                        nrows=5000,
                                        kspace='pppm',
                                        kspace_accuracy=1e-7,
                                        scalings_ei = [1.0, 1.0, 1.0],
                                        move_central_cell=False,
                                        fn_log="none",
                                        overwrite_table=False, comm=comm)

                # Setup and NPT MD run
                if rank == 0:
                    vsl = VerletScreenLog(step=50)

                    if self.write_h5s:
                        if self.fixed_N:
                            hdf5_writer = HDF5Writer(h5.File('results/temp_%d.h5'%self.fixed_N, mode='w'), step=101)
                        else:
                            hdf5_writer = HDF5Writer(h5.File('results/temp_%.8f.h5'%(self.P/bar), mode='w'), step=101)

       	       	ensemble_hook = NHCThermostat(temp=self.T, timecon=100*femtosecond, chainlength=3)
                if self.barostat:
                    mtk = MTKBarostat(ff_lammps, temp=self.T, press=self.P, \
                        timecon=1000*femtosecond, vol_constraint = self.vol_constraint, anisotropic = True)
                    ensemble_hook = TBCombination(ensemble_hook, mtk)

                if self.meta:
                    cv = CVVolume(ff_lammps.system)
                    sigma = 1000*angstrom**3
                    K = 20*kjmol
                    step = 498

                # Run MD
                t = time()
                if self.write_h5s:
                    if rank == 0:
                        verlet = VerletIntegrator(ff_lammps, self.timestep, hooks=[ensemble_hook, vsl, hdf5_writer], temp0=self.T)
                    else:
                        verlet = VerletIntegrator(ff_lammps, self.timestep, hooks=[ensemble_hook], temp0=self.T)
                else:
                    if rank == 0:
                        hooks = [ensemble_hook, vsl]
                        if self.meta:
                            meta = MTDHook(ff_lammps, cv, sigma, K, start=step, step=step)
                            for q0 in q0s:
                                meta.hills.add_hill(q0, K)
                            hooks.append(meta)
                        verlet = VerletIntegrator(ff_lammps, self.timestep, hooks=hooks, temp0=self.T)
                    else:
                        hooks = [ensemble_hook]
                        if self.meta:
                            meta = MTDHook(ff_lammps, cv, sigma, K, start=step, step=step) 
                            for q0 in q0s:
                                meta.hills.add_hill(q0, K)
                            hooks.append(meta)
                        verlet = VerletIntegrator(ff_lammps, self.timestep, hooks=hooks, temp0=self.T)

                e0_tot = verlet._compute_ekin() + ff_lammps.compute()
                verlet.run(600)
                ef_tot = verlet._compute_ekin() + ff_lammps.compute()

                if not self.vol_constraint:
                    Vn = np.linalg.det(ff_lammps.system.cell.rvecs)
                    exp_value = -self.beta * (ef_tot - e0_tot + self.P * (Vn - self.V) - len(self.pos)/self.beta * np.log(Vn/self.V))
                else:
                    exp_value = -self.beta * (ef_tot - e0_tot)

                if(exp_value > 0):
                    exp_value = 0
                acc = min(1, np.exp(exp_value))

                # Accept monte carlo move
                if np.random.rand() < acc:

                    if self.write_h5s:
                        # Append MD data to previous data
                        self.append_h5()

                    # Rebuild data for MC
                    pos_total = ff_lammps.system.pos
                    self.pos = pos_total[:self.N_frame]
                    pos_molecules = pos_total[self.N_frame:]

                    self.rvecs = ff_lammps.system.cell.rvecs
                    self.rvecs_flat = self.rvecs.reshape(9)
                    self.V = np.linalg.det(self.rvecs)
                    if self.meta:
                        q0s.append(self.V)
                    if self.ads_ei:
                        self.sfac = Sfac(self.pos, self.N_frame, self.rvecs_flat, \
                                            self.charges, self.alpha, self.gcut)
                    self.e_el_real = 0
                    self.e_vdw = 0

                    if self.Z_ads > 0:
                        for p in np.split(pos_molecules, self.Z_ads):
                            e_new = self.insertion(p)
                            self.Z_ads -= 1
                        e = e_new
                    else:
                        e = 0
                else:

                    self.pos = pos_init
                    self.rvecs = rvecs_init
                    self.rvecs_flat = rvecs_flat_init
                    self.V = V_init

                if rank == 0:
                    log.set_level(log.medium)

            if(iteration % N_sample == 0 and iteration > 0):
                eprint = e
                if np.abs(eprint) < 1e-10:
                    eprint = 0
                if rank == 0:
                    print(' {:7.7}       {:7.7} {:7.7} {:7.7}    {:7.4}'.format(
                          str(iteration),str(self.Z_ads),str(eprint/kjmol),str(self.V/angstrom**3),time()-t_it)
                          )
                t_it = time()
                N_samples.append(self.Z_ads)
                E_samples.append(e)
                if self.Z_ads == self.fixed_N:
                    traj.append(self.pos)

                if rank == 0 and self.write_traj:

                    natom = self.N_frame + self.nads * self.Z_ads
                    rv = self.rvecs_flat/angstrom
                    ffa_MOF = self.data.system.ffatypes[self.data.system.ffatype_ids]
                    ffa_ads = self.data.system_ads.ffatypes[self.data.system_ads.ffatype_ids]
                    ffa = np.append(ffa_MOF, np.tile(ffa_ads, self.Z_ads))

                    ftraj.write('%d\n%f %f %f %f %f %f %f %f %f\n'%(natom, rv[0], rv[1], rv[2], rv[3], rv[4], rv[5], rv[6], rv[7], rv[8]))
                    for s, p in zip(ffa, self.pos/angstrom):
                        ftraj.write('%s %f %f %f\n'%(s, p[0], p[1], p[2]))


        if rank == 0:
            print('Average N: %.3f'%np.average(N_samples))
            if self.fixed_N:
                np.save('results/N_%d.npy'%self.fixed_N, np.array(N_samples))
                np.save('results/E_%d.npy'%self.fixed_N, np.array(E_samples))
            else:
                np.save('results/N_%.8f.npy'%(self.P/bar), np.array(N_samples))
                np.save('results/E_%.8f.npy'%(self.P/bar), np.array(E_samples))

            if self.fixed_N:

                from yaff import System
                n = np.append(self.data.numbers_MOF, np.tile(self.data.numbers_ads, self.Z_ads))
                s = System(n, self.pos, rvecs=self.rvecs)
                s.to_file('results/end_%d.xyz'%self.fixed_N)

                mol = Molecule.from_file('results/end_%d.xyz'%self.fixed_N)
                symbols = mol.symbols
                self.write_traj(traj, symbols)
                os.remove('results/end_%d.xyz'%self.fixed_N)

            if self.write_traj:
                ftraj.close()
Beispiel #14
0
    def from_file(cls, *fns, **user_kwargs):
        """Construct a new System instance from one or more files

           **Arguments:**

           fn1, fn2, ...
                A list of filenames that are read in order. Information in later
                files overrides information in earlier files.

           **Optional arguments:**

           Any argument from the default constructor ``__init__``. These must be
           given with keywords.

           **Supported file formats**

           .xyz
                Standard Cartesian coordinates file (in angstroms). Atomic
                positions and atomic numbers are read from this file. If the
                title consists of 3, 6 or 9 numbers, each group of three numbers
                is interpreted as a cell vector (in angstroms). A guess of the
                bonds will be made based on inter-atomic distances.

           .psf
                Atom types and bonds are read from this file

           .chk
                Internal text-based checkpoint format. It just contains a
                dictionary with the constructor arguments.
        """
        with log.section('SYS'):
            kwargs = {}
            for fn in fns:
                if fn.endswith('.xyz'):
                    from molmod import Molecule
                    mol = Molecule.from_file(fn)
                    kwargs['numbers'] = mol.numbers.copy()
                    kwargs['pos'] = mol.coordinates.copy()
                elif fn.endswith('.psf'):
                    from molmod.io import PSFFile
                    psf = PSFFile(fn)
                    kwargs['ffatypes'] = psf.atom_types
                    kwargs['bonds'] = np.array(psf.bonds, copy=False)
                    kwargs['charges'] = np.array(psf.charges, copy=False)
                elif fn.endswith('.chk'):
                    from molmod.io import load_chk
                    allowed_keys = [
                        'numbers',
                        'pos',
                        'scopes',
                        'scope_ids',
                        'ffatypes',
                        'ffatype_ids',
                        'bonds',
                        'rvecs',
                        'charges',
                        'radii',
                        'valence_charges',
                        'dipoles',
                        'radii2',
                        'masses',
                    ]
                    for key, value in load_chk(fn).items():
                        if key in allowed_keys:
                            kwargs.update({key: value})
                elif fn.endswith('.h5'):
                    with h5.File(fn, 'r') as f:
                        return cls.from_hdf5(f)
                else:
                    raise IOError('Can not read from file \'%s\'.' % fn)
                if log.do_high:
                    log('Read system parameters from %s.' % fn)
            kwargs.update(user_kwargs)
        return cls(**kwargs)