def test__configure(): _qoidb_filename_in = "pypospack.qoi.yaml" from pypospack.qoi import QoiDatabase from pypospack.qoi import QoiManager qoimanager = QoiManager(qoi_database=_qoidb_filename_in, fullauto=False) qoimanager.configure() assert isinstance(qoimanager.obj_Qoi, OrderedDict) assert all( [isinstance(v, qoi.Qoi) for k, v in qoimanager.obj_Qoi.items()])
def dev__defect_calculation(): print(80*'-') print('{:^80}'.format('defect_calculation')) print(80*'-') simulation_dir = 'rank_test' testing_set = testing_set_Si_sw.get_testing_set_Si() configuration = PyposmatConfigurationFile() configuration.qois = testing_set['qoi_db'].qois configuration.structures = testing_set['structure_db'] qoi_manager = QoiManager( qoi_database=configuration.qois, fullauto=False ) qoi_manager.configure() qoi_manager.determine_tasks() print('qoi_manager.tasks') print(len('qoi_manager.tasks')*'-') for k,v in qoi_manager.tasks.items(): print(k,v) task_manager = TaskManager(base_directory=simulation_dir) task_manager.configure( tasks = qoi_manager.tasks, structures = testing_set['structure_db'] ) task_managger.evaluate_tasks() qoi_manager.calculate_qois( task_results=task_manager.results ) qoi_manager.qois
def test__init__with_None(): _qoidb = None from pypospack.qoi import QoiDatabase from pypospack.qoi import QoiManager qoimanager = QoiManager(qoi_database=_qoidb, fullauto=False) assert isinstance(qoimanager.qoidb, QoiDatabase)
def test__init__with_filename(): _qoidb_filename_in = "pypospack.qoi.yaml" from pypospack.qoi import QoiDatabase from pypospack.qoi import QoiManager qoimanager = QoiManager(qoi_database=_qoidb_filename_in, fullauto=False) assert isinstance(qoimanager.qoidb, QoiDatabase) assert isinstance(qoimanager.qoidb.qoi_names, list) assert isinstance(qoimanager.qoi_names, list)
def test__get_required_tasks(qoi): _qoidb_filename_in = "pypospack.qoi.yaml" from pypospack.qoi import QoiDatabase from pypospack.qoi import QoiManager _qoi_type = qoi _qoi_structure = 'MgO_NaCl' _qoi_name = "{}.{}".format(_qoi_structure, _qoi_type) _qoi_structures = OrderedDict() _qoi_structures['ideal'] = 'MgO_NaCl' _qoidb_QoiDatabase = QoiDatabase() _qoidb_QoiDatabase.add_qoi(qoi_name=_qoi_name, qoi_type=_qoi_type, structures=_qoi_structures, target=4.5) qoimanager = QoiManager(qoi_database=_qoidb_QoiDatabase, fullauto=False) qoimanager.configure() qoimanager.determine_tasks() _task_type = 'lmps_min_all' _qoi_task_name = "{}.{}".format(_qoi_structures['ideal'], _task_type) assert isinstance(qoimanager.tasks, OrderedDict) assert _qoi_task_name in qoimanager.tasks assert qoimanager.tasks[_qoi_task_name]['task_type'] == _task_type assert qoimanager.tasks[_qoi_task_name]['task_structure'] \ == _qoi_structures['ideal']
def test__calculate_stacking_fault(potential,qoi_db,structure_db): simulation_directory = 'rank_test' configuration = PyposmatConfigurationFile() configuration.qois = qoi_db.qois configuration.structures = structure_db qoi_manager = QoiManager(qoi_database=configuration.qois,fullauto=False) qoi_manager.configure() qoi_manager.determine_tasks() task_manager = TaskManager(base_directory='rank_test') task_manager.configure(tasks=qoi_manager.tasks,structures=structure_db) task_manager.evaluate_tasks(parameters=potential,potential=potential) qoi_manager.calculate_qois( task_results=task_manager.results) qoi_manager.qois
def test__get_required_tasks(): _qoidb_filename_in = "pypospack.qoi.yaml" from pypospack.qoi import QoiDatabase from pypospack.qoi import QoiManager qoimanager = QoiManager(qoi_database=_qoidb_filename_in, fullauto=False) qoimanager.configure() qoimanager.determine_required_tasks() assert isinstance(qoimanager.required_tasks, OrderedDict)
def test____init____fullauto_False__qoi_database_OrderedDict(): qoi_database=get_qoi_database_from_PyposmatConfigurationFile(config_fn=config_fn) o = QoiManager(qoi_database=qoi_database,fullauto=False)
print(80*'-') print(qoi_db.qois) print(80*'-') print('{:^80}'.format('STRUCTURE DATABASE')) print(80*'-') print('Structure directory:{}'.format(structure_db['structure_directory'])) print(20*'-' + ' ' + 59*'-') print('{:^20} {:^59}'.format('structure_name','structure_filename')) print(20*'-' + ' ' + 59*'-') for k,v in structure_db['structures'].items(): print('{:<20} {:<59}'.format(k,v)) qoi_manager = QoiManager( qoi_database=configuration.qois, fullauto=False) qoi_manager.configure() qoi_manager.determine_tasks() print(80*'-') print('{:^80}'.format('TASKS DATABASE')) print(80*'-') for k,v in qoi_manager.tasks.items(): tsk_name = k tsk_type = v['task_type'] tsk_structure = v['task_structure'] print(tsk_name,tsk_type,tsk_structure,tsk_structure) task_manager = TaskManager(base_directory='rank_test') task_manager.configure(
def test__init__with_implicit_None(): from pypospack.qoi import QoiDatabase from pypospack.qoi import QoiManager qoimanager = QoiManager(fullauto=False) assert isinstance(qoimanager.qoidb, QoiDatabase)
class PyposmatEngine2(object): EAM_EOS_EMBEDDING_FUNCTIONS = ['eam_embed_eos_rose'] """class for evaluating a simulation This class combines the two classes QoiManager, which manages classes used to manage the classes used in modelling and calculating material properties, and TaskManager, which manages classes used to manage the classes used in specific simulations. Args: filename_in(str): path in filename_out(str): path out base_directory(str): This is the base directory from which the PyposmatEngine will create and run simulations. By default this is set to None, which means it will use the current working directory as the base directory. fullauto(bool): Attributes: pyposmat_filename_in(str): path in pyposmat_filename_out(str) path out base_directory(str): the base directory rank_directory(str): This reflect the MPI rank of the processsor that the PyposmatEngine is running on. If there is no MPI available, this is automatically set to rank0000. configuration(pypospack.pyposmat.PyposmatConfigurationFile) qoi_manager(pypospack.qoi.QoiManager) task_mamanger(pypospack.task.TaskManager) """ def __init__(self, configuration = 'pypospack.config.in', data = 'pypospack.results.out', base_directory = None, fullauto = False): self.base_directory = None self.configuration = None self.qoi_manager = None self.task_manager = None self._structure_directory = None if fullauto: self._initialize_configuration(configuration=configuration) self._initialize_base_directory(base_directory=base_directory) self._initialize_task_manager() self._initialize_qoi_manager() def _initialize_configuration(self,configuration): if instance(configuration,str): self.configuration = PyposmatConfigurationFile() self.configuration.read(filename=configuration) elif isinstance(configuration,PyposmatConfigurationFile): self.configuration = configuration else: msg = ("configuration must either be a string or an instanced " "PyposmatConfigurationFile") raise TypeError(msg) def _initialize_base_directory(self,base_directory): if base_directory is None: self.base_directory = os.getcwd() elif isinstance(base_directory,str): self.base_directory = base_directory else: msg = "base_directory has to be a string" raise TypeError(msg) if not os.path.exists(self.base_directory): os.mkdir(self.base_directory) def _initialize_qoi_manager(self,qois=None): if qois is None: qois = self.configuration.qois self.qoi_manager = QoiManager(qoi_database=qois,fullauto=True) def _initialize_task_manager(self): self.task_manager = TaskManager( base_directory=self.base_directory) self.task_manager.configure( tasks=self.qoi_manager.tasks, structures = self.structures) @property def structure_directory(self): if type(self._structure_directory) is type(str): return self._structure_directory if type(self.configuration) is not type(None): return self.configuration.structures['structure_directory'] else: return None @property def structures(self): """(collections.OrderedDict)""" return self.configuration.structures @property def potential(self): """(collections.OrderedDict)""" return self.configuration.potential def configure(self): """ When writing a new PypospackEngine this method will likely have to be modified """ self.create_base_directories() self.read_configuration_file() self.configure_qoi_manager() self.configure_task_manager() def create_base_directories(self,base_directory=None): assert isinstance(base_directory,str) or base_directory is None # <-------- determine the base directory. if base_directory is None: if self.base_directory is None: self.base_directory = os.getcwd() elif isinstance(base_directory,str): self.base_directory = base_directory else: msg_err = "the base directory must be a string" raise ValueError(msg_err) # <-------- create the base directory if the base directory does # not exist if not os.path.exists(self.base_directory): os.mkdir(self.base_directory) # <-------- the rank directory is determined by the MPI rank # this is not implemented yet if self.rank_directory is None: _rank_directory = "rank0" self.rank_directory = os.path.join( self.base_directory, _rank_directory) def write_eam_setfl_file(self,parameters,potential,setfl_fn): from pypospack.potential import EamPotential is_debug = False assert isinstance(parameters,OrderedDict) assert isinstance(potential,OrderedDict) assert isinstance(setfl_fn,str) if is_debug: print(parameters) print(potential) print(setfl_fn) p = EamPotential( symbols=potential['symbols'], func_pair=potential['pair_type'], func_density=potential['density_type'], func_embedding=potential['embedding_type'] ) p.write_setfl_file( filename=setfl_fn, symbols=potential['symbols'], Nr=potential['N_r'], rmax=potential['r_max'], rcut=potential['r_cut'], Nrho=potential['N_rho'], rhomax=potential['rho_max'], parameters=parameters ) def _initialize_potential(potential): if isinstance(potential,dict): potential_definition = copy.deepcopy(self.configuration.potential) elif isinstance(potential,Potential) def evaluate_parameter_set(self,parameters): """ Arguments: parameters(OrderedDict): hashtable of parameter values with the key value defined by the configuration file or the PyposmatConfigurationFile object. Only the free parameter values need to be defined. Constrained parameter values will be automatically generated Returns: OrderedDict: hashtable of results with the key value being defined by the the either configuration file or the PyposmatConfigurationFile object Exceptions: LammpsSimulationError """ self._initialize_task_manager() parameters_ = copy.deepcopy(parameters) potential_ = copy.deepcopy(self.configuration.potential) # if the filename is EAM, it is faster to create a single EAM file, and # then use that file everywhere if _potential['potential_type'] == 'eam': setfl_fn = os.path.join( os.getcwd(), '{}.eam.alloy'.format("".join(_potential['symbols'])) ) if _potential['embedding_type'] in self.EAM_EOS_EMBEDDING_FUNCTIONS: self.write_eam_setfl_file( parameters=_parameters, potential=_potential, setfl_fn=setfl_fn ) assert os.path.isfile(setfl_fn) _potential['setfl_filename'] = setfl_fn try: self.task_manager.evaluate_tasks( parameters=_parameters, potential=_potential) except LammpsSimulationError as e: str_neighlist_overflow = 'Neighbor list overflow' raise except: print("--- FATAL ERROR ---") print("self.configuration.potential:") for k,v in self.configuration.potential.items(): print("\t",k,'=',v) print("current_parameter_set:") for k,v in _parameters.items(): print("\t",k,'=',v) print("--- END ERROR INFO ---") print(type(self.configuration.potential)) raise else: # send the results from the task calculations to calculate QOIs _task_results = self.task_manager.results self.qoi_manager.calculate_qois( task_results=_task_results) # populate qoi values _qoi_results = OrderedDict() for k_qoi,v_qoi in self.qoi_manager.qois.items(): _qoi_val = v_qoi['qoi_val'] _qoi_results[k_qoi] = _qoi_val # populate errors _qoi_errors = OrderedDict() for k_qoi,v_qoi in self.qoi_manager.qois.items(): _qoi_error_name = '{}.{}'.format(k_qoi,'err') _qoi_error = v_qoi['qoi_err'] _qoi_errors[_qoi_error_name] = _qoi_error _results = OrderedDict() _results['parameters'] = copy.deepcopy(_parameters) _results['qois'] = copy.deepcopy(_qoi_results) _results['errors'] = copy.deepcopy(_qoi_errors) return _results
def _initialize_qoi_manager(self,qois=None): if qois is None: qois = self.configuration.qois self.qoi_manager = QoiManager(qoi_database=qois,fullauto=True)
def configure_qoi_manager(self,qoi_database): self.qoi_manager = QoiManager(qoi_database=qoi_database,fullauto=False) self.qoi_manager.configure() self.qoi_manager.determine_tasks()
class TestQoi(object): def __init__(self,qoi_db,structure_db,potential): self.configuration = None self.qoi_manager = None self.task_directory = 'rank_test' self.task_manager = None self.setup_configuration_file(qoi_db=qoi_db,structure_db=structure_db) self.check_structure_db(structure_db) self.configure_qoi_manager(qoi_database=self.configuration.qois) self.check_task_database() self.configure_task_manager(task_directory=self.task_directory, tasks=self.qoi_manager.tasks, structures=self.configuration.structures) self.evaluate_tasks(parameters=potential, potential=potential) self.check_task_results(task_results=self.task_manager.results) self.calculate_qois() self.check_qoi_results() def setup_configuration_file(self,qoi_db,structure_db): print('setup_configuration_file') self.configuration = PyposmatConfigurationFile() self.configuration.qois = qoi_db.qois self.configuration.structures = structure_db def check_structure_db(self,structure_db): print('check_structures') structure_directory = self.configuration.structures['structure_directory'] assert os.path.isdir(structure_directory) for structure_name,structure_fn in self.configuration.structures['structures'].items(): print(structure_name,structure_fn) assert os.path.isfile(os.path.join(structure_directory,structure_fn)) def configure_qoi_manager(self,qoi_database): self.qoi_manager = QoiManager(qoi_database=qoi_database,fullauto=False) self.qoi_manager.configure() self.qoi_manager.determine_tasks() def check_task_database(self): print('{} {} {}'.format('task_name','task_type','task_structure')) for k,v in self.qoi_manager.tasks.items(): task_name = k task_type = v['task_type'] task_structure = v['task_structure'] print('{} {} {}'.format(task_name,task_type,task_structure)) assert k in task_list assert task_type == task_list[k]['task_type'] assert task_structure == task_list[k]['task_structure'] assert len(self.qoi_manager.tasks) == len(task_list) def configure_task_manager(self,task_directory,tasks,structures): self.task_manager = TaskManager(base_directory=task_directory) self.task_manager.configure(tasks=tasks, structures=structures) def evaluate_tasks(self,parameters,potential): self.task_manager.evaluate_tasks(parameters=parameters,potential=potential) def check_task_results(self,task_results): table_col_names = ['name','value'] table_rows = [] for k,v in task_results.items(): table_rows.append([str(k),str(v)]) print('\n'.join([','.join(v) for v in table_rows])) def calculate_qois(self): self.qoi_manager.calculate_qois(task_results=self.task_manager.results) def check_qoi_results(self): print(self.qoi_manager.qois)