def calculate(self, atoms=None, properties=['energy'], system_changes=all_changes): Calculator.calculate(self, atoms, properties, system_changes) self.write_input(self.atoms, properties, system_changes) if self.command is None: raise CalculatorSetupError('Please set ${} environment variable '. format('ASE_' + self.name.upper() + '_COMMAND') + 'or supply the command keyword') command = self.command if 'PREFIX' in command: command = command.replace('PREFIX', self.prefix) try: proc = subprocess.Popen(command, shell=True, cwd=self.directory) except OSError as err: # Actually this may never happen with shell=True, since # probably the shell launches successfully. But we soon want # to allow calling the subprocess directly, and then this # distinction (failed to launch vs failed to run) is useful. msg = 'Failed to execute "{}"'.format(command) raise EnvironmentError(msg) from err errorcode = proc.wait() if errorcode: path = os.path.abspath(self.directory) msg = ('Calculator "{}" failed with command "{}" failed in ' '{} with error code {}'.format(self.name, command, path, errorcode)) raise CalculationFailed(msg) self.read_results()
def calculate(self, atoms=None, properties=['energy'], system_changes=all_changes): Calculator.calculate(self, atoms, properties, system_changes) self.write_input(self.atoms, properties, system_changes) if self.command is None: raise CalculatorSetupError( 'Please set ${} environment variable ' .format('ASE_' + self.name.upper() + '_COMMAND') + 'or supply the command keyword') command = self.command.replace('PREFIX', self.prefix) errorcode = subprocess.call(command, shell=True, cwd=self.directory) if errorcode: raise CalculationFailed('{} in {} returned an error: {}' .format(self.name, self.directory, errorcode)) self.read_results()
def calculate(self, atoms=None, properties=['energy'], system_changes=all_changes): Calculator.calculate(self, atoms, properties, system_changes) self.write_input(self.atoms, properties, system_changes) if self.command is None: raise RuntimeError('Please set $%s environment variable ' % ('ASE_' + self.name.upper() + '_COMMAND') + 'or supply the command keyword') command = self.command.replace('PREFIX', self.prefix) olddir = os.getcwd() try: os.chdir(self.directory) errorcode = subprocess.call(command, shell=True) finally: os.chdir(olddir) if errorcode: raise RuntimeError('%s returned an error: %d' % (self.name, errorcode)) self.read_results()
def calculate_numerical_forces(self, atoms, d=0.001, parallel=True): """Calculate numerical forces using finite difference. All atoms will be displaced by +d and -d in all directions.""" if (str(self.__class__) == "<class 'amp.Amp'>"): if (str(self.model.__class__) == "<class 'amp.model.tflow.NeuralNetwork'>"): disp_atoms = self.generate_disp(atoms, d=d) atomic_energies = self.calculateE_bunch(disp_atoms, parallel=parallel) atomic_energies = np.array( np.split( np.array(np.split(atomic_energies, len(atoms) * 3)), len(atoms))) forces = np.zeros((len(atoms), 3)) for i in range(len(atoms)): for j in range(3): forces [i][j] = \ -1 * ( atomic_energies[i][j][1] - atomic_energies[i][j][0] ) / (2 * d) return forces else: Calculator.calculate(self, atoms) disp_atoms = self.generate_disp(atoms, d=d) log = self._log log('Calculation requested.') from amp.utilities import hash_images images, seq_keys = hash_images(disp_atoms, list_seq=True) keys = list(images.keys())[0] ############### load neighborlist file list most recently used if hasattr(self, "neighborlist_keys"): n_keys = self.neighborlist_keys else: n_keys = None if hasattr(self, "fingerprints_keys"): f_keys = self.fingerprints_keys else: f_keys = None ############# update neighborlists & fingerprints file lists self.neighborlist_keys, self.fingerprints_keys = \ self.descriptor.calculate_fingerprints( images=images, log=log, calculate_derivatives=False, neighborlist_keys = n_keys, fingerprints_keys = f_keys, parallel = self._parallel if parallel else None, ) log('Calculating potential energy...', tic='pot-energy') seq_keys = np.array( np.split( np.array(np.split(np.array(seq_keys), len(atoms) * 3)), len(atoms))) forces = np.zeros((len(atoms), 3)) for a in range(len(atoms)): for i in range(3): eplus = self.model.calculate_energy( self.descriptor.fingerprints[seq_keys[a][i][1]]) self.descriptor.fingerprints.close() eminus = self.model.calculate_energy( self.descriptor.fingerprints[seq_keys[a][i][0]]) self.descriptor.fingerprints.close() forces[a][i] = (eminus - eplus) / (2 * d) log('Potential energy calc ended...', toc='pot-energy') return forces else: return np.array( [[self.numeric_force(atoms, a, i, d) for i in range(3)] for a in range(len(atoms))])