"git@developer.sourcefind.cn:tsoc/superbenchmark.git" did not exist on "ce1860b9b640fd03069a007d00396d86a7e60269"
Commit 2409a22f authored by fanding2000's avatar fanding2000
Browse files

Format fix. More options in readme

parent ce29afea
This diff is collapsed.
This diff is collapsed.
from basic_function import format_parser from basic_function import format_parser
from basic_function import CSP_generator_normal from basic_function import CSP_generator_normal
import os import os
import concurrent.futures import concurrent.futures
import sys import sys
def process_crystal(seed, sg, molecules,output_path,add_name): def process_crystal(seed, sg, molecules,output_path,add_name):
aaa = CSP_generator_normal.CrystalGenerator(molecules, space_group=sg) aaa = CSP_generator_normal.CrystalGenerator(molecules, space_group=sg)
molecules_number = sum(len(molecule.atoms) for molecule in molecules) molecules_number = sum(len(molecule.atoms) for molecule in molecules)
new_crystal = aaa.generate(seed=seed) new_crystal = aaa.generate(seed=seed)
sys.stdout.flush() sys.stdout.flush()
if new_crystal is not None: if new_crystal is not None:
cif_out = format_parser.write_cif_file(new_crystal) cif_out = format_parser.write_cif_file(new_crystal)
with open(f"{output_path}/structures/{add_name}_{sg}_{seed}_z{len(molecules)}_{molecules_number}.cif", 'w') as target: with open(f"{output_path}/structures/{add_name}_{sg}_{seed}_z{len(molecules)}_{molecules_number}.cif", 'w') as target:
target.writelines(cif_out) target.writelines(cif_out)
return True return True
return False return False
def CSP_generater_parallel(molecules,output_path,need_structure = 100, space_group_list=[1],max_workers=8,add_name='',start_seed=1): def CSP_generater_parallel(molecules,output_path,need_structure = 100, space_group_list=[1],max_workers=8,add_name='',start_seed=1):
space_groups = space_group_list space_groups = space_group_list
accept_count = need_structure accept_count = need_structure
try: try:
os.makedirs("{}/structures".format(output_path)) os.makedirs("{}/structures".format(output_path))
except: except:
print("Warning, these is already an structures folder in this path, skip mkdir") print("Warning, these is already an structures folder in this path, skip mkdir")
for sg in space_groups: for sg in space_groups:
accept = 0 accept = 0
seed = start_seed seed = start_seed
with concurrent.futures.ProcessPoolExecutor(max_workers=max_workers) as executor: with concurrent.futures.ProcessPoolExecutor(max_workers=max_workers) as executor:
futures = {} futures = {}
while accept < accept_count: while accept < accept_count:
# submit new task # submit new task
while len(futures) < max_workers and accept + len(futures) < accept_count: while len(futures) < max_workers and accept + len(futures) < accept_count:
future = executor.submit(process_crystal, seed, sg, molecules, output_path,add_name) future = executor.submit(process_crystal, seed, sg, molecules, output_path,add_name)
futures[future] = seed futures[future] = seed
seed += 1 seed += 1
# check the finished task # check the finished task
done, _ = concurrent.futures.wait(futures, return_when=concurrent.futures.FIRST_COMPLETED) done, _ = concurrent.futures.wait(futures, return_when=concurrent.futures.FIRST_COMPLETED)
for future in done: for future in done:
if future.result(): if future.result():
accept += 1 accept += 1
# remove it from list, no matter what result it is # remove it from list, no matter what result it is
del futures[future] del futures[future]
# cancel all task if the number need is arrived. # cancel all task if the number need is arrived.
if accept >= accept_count: if accept >= accept_count:
for future in futures: for future in futures:
future.cancel() future.cancel()
break break
def CSP_generater_serial(molecules,output_path,need_structure = 100, densely_pack_method=False, space_group_list=[1]): def CSP_generater_serial(molecules,output_path,need_structure = 100, densely_pack_method=False, space_group_list=[1]):
""" """
:param molecules: a list [molecule1, molecule2, ...] :param molecules: a list [molecule1, molecule2, ...]
:param output_path: a str indicate the path of output folder :param output_path: a str indicate the path of output folder
:param need_structure: int :param need_structure: int
:param space_group_list:a list indicate the space group need to search :param space_group_list:a list indicate the space group need to search
""" """
try: try:
os.makedirs("{}\\structures".format(output_path)) os.makedirs("{}\\structures".format(output_path))
except: except:
print("Warning, these is already an structures folder in this path, skip mkdir") print("Warning, these is already an structures folder in this path, skip mkdir")
for sg in space_group_list: for sg in space_group_list:
aaa = CSP_generator_normal.CrystalGenerator(molecules, space_group=sg) aaa = CSP_generator_normal.CrystalGenerator(molecules, space_group=sg)
accept=0 accept=0
i=1 i=1
while accept<need_structure: while accept<need_structure:
new_crystal = aaa.generate(seed=i,densely_pack_method=densely_pack_method) new_crystal = aaa.generate(seed=i,densely_pack_method=densely_pack_method)
if new_crystal==None: if new_crystal==None:
i += 1 i += 1
continue continue
cif_out = format_parser.write_cif_file(new_crystal) cif_out = format_parser.write_cif_file(new_crystal)
target = open("{}\\structures\\{}_{}.cif".format(output_path,sg,i), 'w') target = open("{}\\structures\\{}_{}.cif".format(output_path,sg,i), 'w')
target.writelines(cif_out) target.writelines(cif_out)
target.close() target.close()
accept+=1 accept+=1
i += 1 i += 1
def CSP_generater_one_test(molecules,output_path,space_group=1,seed=1): def CSP_generater_one_test(molecules,output_path,space_group=1,seed=1):
""" """
used to test the generator for a given space group and seed used to test the generator for a given space group and seed
:param molecules: a list [molecule1, molecule2, ...] :param molecules: a list [molecule1, molecule2, ...]
:param output_path: a str indicate the path of output folder :param output_path: a str indicate the path of output folder
:param space_group: a list indicate the space group need to search :param space_group: a list indicate the space group need to search
:param seed: int :param seed: int
:return: write out a cif :return: write out a cif
""" """
aaa = CSP_generator_normal.CrystalGenerator(molecules, space_group=space_group) aaa = CSP_generator_normal.CrystalGenerator(molecules, space_group=space_group)
new_crystal = aaa.generate(seed=seed, test=True) new_crystal = aaa.generate(seed=seed, test=True)
if new_crystal==None: if new_crystal==None:
print("Return failed generate") print("Return failed generate")
else: else:
cif_out = format_parser.write_cif_file(new_crystal,sym=True) cif_out = format_parser.write_cif_file(new_crystal,sym=True)
target = open("{}\\{}_{}.cif".format(output_path,space_group,seed), 'w') target = open("{}\\{}_{}.cif".format(output_path,space_group,seed), 'w')
target.writelines(cif_out) target.writelines(cif_out)
target.close() target.close()
This diff is collapsed.
This diff is collapsed.
from importlib.metadata import version from importlib.metadata import version
from packaging.version import Version from packaging.version import Version
__version__ = version('sevenn') __version__ = version('sevenn')
from e3nn import __version__ as e3nn_ver from e3nn import __version__ as e3nn_ver
if Version(e3nn_ver) < Version('0.5.0'): if Version(e3nn_ver) < Version('0.5.0'):
raise ValueError( raise ValueError(
'The e3nn version MUST be 0.5.0 or later due to changes in CG coefficient ' 'The e3nn version MUST be 0.5.0 or later due to changes in CG coefficient '
'convention.' 'convention.'
) )
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment