PARAMS["OptMaxCycles"] = 100 + (len(molecule.atoms) / 100) * 300 PARAMS["OptThresh"] = 0.001 Opt = GeomOptimizer(energy_force_function) molecule = Opt.Opt(molecule, callback=emit_callback) return molecule if __name__ == "__main__": import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from networks import tensormol01, tensormol02, ani1 molecule = Mol() molecule.FromXYZString("""12 benzene C 0.00000 1.40272 0 H 0.00000 2.49029 0 C -1.21479 0.70136 0 H -2.15666 1.24515 0 C -1.21479 -0.70136 0 H -2.15666 -1.24515 0 C 0.00000 -1.40272 0 H 0.00000 -2.49029 0 C 1.21479 -0.70136 0 H 2.15666 -1.24515 0 C 1.21479 0.70136 0 H 2.15666 1.24515 0""") print(main(tensormol01.main(), molecule)) print(main(ani1.main(), molecule)) print(main(tensormol02.main(), molecule))
if __name__ == "__main__": import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from networks import tensormol01, tensormol02, ani1 from TensorMol import Mol molecule = Mol() molecule.FromXYZString("""12 benzene C 0.00000 1.40272 0 H 0.00000 2.49029 0 C -1.21479 0.70136 0 H -2.15666 1.24515 0 C -1.21479 -0.70136 0 H -2.15666 -1.24515 0 C 0.00000 -1.40272 0 H 0.00000 -2.49029 0 C 1.21479 -0.70136 0 H 2.15666 -1.24515 0 C 1.21479 0.70136 0 H 2.15666 1.24515 0""") energy, force = main(tensormol01.main(), molecule) print(energy) print(force) energy, force = main(ani1.main(), molecule) print(energy) print(force) energy, force = main(tensormol02.main(), molecule) print(energy) print(force)
scanner = RelaxedScan(energy_force_function, molecule, at1=atom_one - 1, at2=atom_two - 1, nstep_=steps) return scanner.Scan(molecule, maxr=final_distance, callback=on_step) if __name__ == "__main__": import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from networks import tensormol01, tensormol02 molecule = Mol() molecule.FromXYZString("""12 C 0.00000 1.40272 0 H 0.00000 2.49029 0 C -1.21479 0.70136 0 H -2.15666 1.24515 0 C -1.21479 -0.70136 0 H -2.15666 -1.24515 0 C 0.00000 -1.40272 0 H 0.00000 -2.49029 0 C 1.21479 -0.70136 0 H 2.15666 -1.24515 0 C 1.21479 0.70136 0 H 2.15666 1.24515 0""") print(main(tensormol01.main(), molecule, steps=5)) print(main(tensormol02.main(), molecule, steps=5))
return Opt.Search(molecule, callback=on_conformer_found) if __name__ == "__main__": import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from networks import tensormol01, tensormol02 molecule = Mol() molecule.FromXYZString("""17 C -7.079716 0.739119 0.004923 C -8.238053 1.731919 0.077636 C -7.724415 3.170105 0.092342 H -8.833846 1.547485 0.980576 H -8.903682 1.588910 -0.783054 C -8.864043 4.177735 0.119896 H -7.097400 3.358841 -0.788140 H -7.075050 3.330803 0.962085 O -8.312426 5.486788 0.162899 H -9.492618 4.035746 1.004615 H -9.484897 4.090243 -0.777434 H -9.058605 6.110699 0.167557 N -7.585679 -0.628274 -0.011876 H -6.410729 0.868898 0.862747 H -6.493932 0.913096 -0.904042 H -8.045861 -0.833867 0.874128 H -6.800998 -1.275620 -0.079674""") print(main(tensormol01.main(), molecule, n_conf=5)) print(main(tensormol02.main(), molecule, n_conf=5))
def main(): """Entry point.""" while True: try: tensormol01_network = tensormol01.main() ani1_network = ani1.main() tensormol02_network = tensormol02.main() # FYI: SocketIO is threaded. def on_run_calculation(options): """Run when a calculation is submitted.""" print('Tensormol received ' + options.get('calculation').get('_id')) global calculation_running if calculation_running: return calculation_running = True calculation = options.get('calculation') calculation_id = calculation.get('_id') calculation_type = calculation.get('type') geometries = calculation.get('geometries') # charge = calculation.get('charge') # multiplicity = calculation.get('multiplicity') network_name = calculation.get('network') print('Calculation Received: ', calculation_id) # print(calculation) def ping_calculation_running(calculation): global ping_timer def socket_callback(error='', result=''): if error: print(error) raise Exception('Socket Error!') # print(result) socket_io.emit('pingCalculationRunning', {'calculationId': calculation_id}, socket_callback) print('Running Calculation: ', calculation_id) ping_timer = threading.Timer(3, ping_calculation_running, [calculation]) ping_timer.start() def stop_pinging(): global ping_timer if ping_timer: ping_timer.cancel() try: socket_result = {} def socket_callback(error='', result=''): nonlocal socket_result if error: print(error) raise Exception('Socket Error!') socket_result = result socket_io.emit('setCalculationRunning', {'calculationId': calculation_id}, socket_callback) socket_io.wait_for_callbacks() if not socket_result.get('updated'): # calculation not started, exit calculation_running = False return print('Calculation Started: ', calculation_id) ping_calculation_running(calculation) molecule = Mol() molecule.FromXYZString( str(len(geometries[0].split('\n'))) + '\n\n' + geometries[0]) secondary_molecule = Mol() if len(geometries) > 1: secondary_molecule.FromXYZString( str(len(geometries[1].split('\n'))) + '\n\n' + geometries[1]) # add switch based on network name if network_name == 'tensormol01': network = tensormol01_network elif network_name == 'ani1': network = ani1_network else: network = tensormol02_network # TODO: Attempt to run saveCalculationResult a few times on # error in callback if calculation_type == 'groundState': energy, force = energy_and_force.main( network, molecule) socket_io.emit( 'saveCalculationResult', { 'calculationId': calculation_id, 'properties': { 'energy': energy, 'force': force.tolist() } }) elif calculation_type == 'geometryOptimization': firstm = None def on_optimization_step_completed(mol_hist): nonlocal firstm print("Emitting Callback") firstm = mol_hist[0] energies = [firstm.properties['energy']] geometries = [ '\n'.join(str(firstm).split('\n')[2:]) ] if len(mol_hist) > 1: for m in mol_hist[-9:]: energies.append(m.properties['energy']) geometries.append('\n'.join( str(m).split('\n')[2:])) socket_io.emit( 'saveIntermediateCalculationResult', { 'calculationId': calculation_id, 'properties': { 'geometries': geometries, 'energies': energies } }) finalm = geometry_optimization.main( network, molecule, on_optimization_step_completed) first_xyz = '\n'.join(str(firstm).split('\n')[2:]) final_xyz = '\n'.join(str(finalm).split('\n')[2:]) socket_io.emit( 'saveCalculationResult', { 'calculationId': calculation_id, 'properties': { 'geometries': [first_xyz, final_xyz], 'energies': [ firstm.properties['energy'], finalm.properties['energy'] ] } }) elif calculation_type == 'harmonicSpectra': finalm, w, v, td = harmonic_spectra.main( network, molecule) xyz = '\n'.join(str(finalm).split('\n')[2:]) socket_io.emit( 'saveCalculationResult', { 'calculationId': calculation_id, 'properties': { 'optimizedGeometry': xyz, 'frequencies': w.tolist(), 'intensities': v.tolist(), 'freeEnergy': { '300K1ATM': td['Grot'] + td['Gtrans'] + td['Gvib'] } } }) elif calculation_type == 'nudgedElasticBand': # run neb # values = nudged_elastic_band.main( # network, [mol1, mol2]) pass elif calculation_type == 'conformerSearch': # TODO: add parameters. (window etc.) nConf = calculation.get('numberOfConformers') if nConf is None: print("Bad Conformer number: ", nConf) nConf = 20 def on_conformer_found(mol_hist): socket_io.emit( 'saveIntermediateCalculationResult', { 'calculationId': calculation_id, 'properties': { 'geometries': [ '\n'.join(str(m).split('\n')[2:]) for m in mol_hist ], 'energies': [ m.properties['energy'] for m in mol_hist ] } }) mol_hist = conformer_search.main( network, molecule, on_conformer_found, n_conf=nConf) socket_io.emit( 'saveCalculationResult', { 'calculationId': calculation_id, 'properties': { 'geometries': [ '\n'.join(str(m).split('\n')[2:]) for m in mol_hist ], 'energies': [m.properties['energy'] for m in mol_hist] } }) elif calculation_type == 'relaxedScan': # TODO: add parameters. (window etc.) atoms = calculation.get('atoms') final_distance = calculation.get('finalDistance') steps = calculation.get('steps') if steps is None: print("Bad steps number: ", steps) steps = 20 def on_step(mol_hist): socket_io.emit( 'saveIntermediateCalculationResult', { 'calculationId': calculation_id, 'properties': { 'geometries': [ '\n'.join(str(m).split('\n')[2:]) for m in mol_hist ], 'energies': [ m.properties['energy'] for m in mol_hist ], 'distances': [ m.properties['rs_r'] for m in mol_hist ] } }) mol_hist = relaxed_scan.main( network, molecule, on_step, steps=steps, final_distance=final_distance, atom_one=atoms[0], atom_two=atoms[1]) socket_io.emit( 'saveCalculationResult', { 'calculationId': calculation_id, 'properties': { 'geometries': [ '\n'.join(str(m).split('\n')[2:]) for m in mol_hist ], 'energies': [m.properties['energy'] for m in mol_hist], 'distances': [m.properties['rs_r'] for m in mol_hist] } }) else: print('Unknown CalculationType!', calculation_type) except Exception as e: print("Unexpected error:", e) socket_io.emit( 'saveCalculationResult', { 'calculationId': calculation_id, 'properties': {}, 'error': { 'message': str(e), 'createdAt': int(round(time.time() * 1000)), }, }) stop_pinging() calculation_running = False raise stop_pinging() calculation_running = False print('Calculation Finished: ', calculation_id) # Start SocketIO Client address = os.environ.get('DATABASE_ADDRESS') or 'localhost' port = os.environ.get('DATABASE_PORT') or '8080' socket_io = SocketIO( address, port, params={ 'program': 'tensormol', 'serverId': 'free' }) socket_io.on('runCalculation', on_run_calculation) socket_io.wait() except Exception as e: print('Error:', e) pass