add_atom_in_slab(bulk,batch_path_head+'bulk.str')
except:
    batch_path_head='D:\\Github\\batchfile\\'
    add_atom_in_slab(bulk,batch_path_head+'bulk.str')
add_atom_in_slab(ref_domain1,batch_path_head+'half_layer2.str')
add_atom_in_slab(ref_domain2,batch_path_head+'full_layer2.str')
#symmetry library, note the Fe output file is the same, but O and Pb output file is different due to different sorbate configuration
for i in range(DOMAIN_NUMBER): 
    vars()['sym_file_domain'+str(int(i+1))]={'Fe':batch_path_head+'Fe output file for Genx reading.txt',\
              'O':batch_path_head+'O output file for Genx reading'+str(O_NUMBER[i])+'_'+str(int(DOMAIN[i]))+'.txt',\
              'Pb':batch_path_head+'Pb output file for Genx reading'+str(Pb_NUMBER[i])+'_'+str(int(DOMAIN[i]))+'.txt'}
###################create domain classes and initiate the chemical equivalent domains####################
#when change or create a new domain, make sure the terminated_layer (start from 0)set right
######################################setup domains############################################
for i in range(DOMAIN_NUMBER):
    vars()['domain_class_'+str(int(i+1))]=domain_creator3.domain_creator(ref_domain=vars()['ref_domain'+str(int(DOMAIN[i]))],id_list=vars()['ref_id_list_'+str(int(DOMAIN[i]))],terminated_layer=0,domain_tag='_D'+str(int(i+1)),new_var_module=vars()['rgh_domain'+str(int(i+1))])
    vars()['domain'+str(int(i+1))+'A']=vars()['domain_class_'+str(int(i+1))].domain_A
    vars()['domain'+str(int(i+1))+'B']=vars()['domain_class_'+str(int(i+1))].domain_B
    vars(vars()['domain_class_'+str(int(i+1))])['domainA']=vars()['domain'+str(int(i+1))+'A']
    vars(vars()['domain_class_'+str(int(i+1))])['domainB']=vars()['domain'+str(int(i+1))+'B']
    #Adding sorbates to domainA and domainB
    #add_atom(domain=vars()['domain'+str(int(i+1))+'A'],ref_coor=Pb_COORS[i]+O_COORS[i],ids=vars()['sorbate_ids_domain'+str(int(i+1))+'a'],els=vars()['sorbate_els_domain'+str(int(i+1))])
    #add_atom(domain=vars()['domain'+str(int(i+1))+'B'],ref_coor=np.array(Pb_COORS[i]+O_COORS[i])-[0.,0.06955,0.5],ids=vars()['sorbate_ids_domain'+str(int(i+1))+'b'],els=vars()['sorbate_els_domain'+str(int(i+1))])
    #Pb_ATTACH_ATOM=[(['O1_1_0']),(['O1_2_0'])]
    HO_index=0
    pb_coors_a=[]
    O_coors_a=[]
    for j in range(Pb_NUMBER[i]):
        if len(Pb_ATTACH_ATOM[i][j])==1:
            HO_index=HO_index+2
            ids=[Pb_ATTACH_ATOM[i][j][0]+'_D'+str(int(i+1))+'A']
bulk = model.Slab(T_factor='B')
domain0 =  model.Slab(c = 1.0,T_factor='B')
domain0_1 =  model.Slab(c = 1.0,T_factor='B')
rgh=UserVars()
rgh.new_var('beta', 0.0)
################################################build up ref domains############################################
#add atoms for bulk and two ref domains (domain0<half layer> and domain0_1<full layer>)
#In those two reference domains, the atoms are ordered according to first hight (z values), then y values
#it is a super surface structure by stacking the surface slab on bulk slab, the repeat vector was counted 
add_atom_in_slab(bulk,batch_path_head+'bulk.str')
add_atom_in_slab(domain0,batch_path_head+'half_layer.str')
add_atom_in_slab(domain0_1,batch_path_head+'full_layer.str')
###################create domain classes and initiate the chemical equivalent domains####################
#when change or create a new domain, make sure the terminated_layer (start from 0)set right
######################################setup domain1############################################
domain_class_1=domain_creator3.domain_creator(ref_domain=domain0,id_list=ref_id_list,terminated_layer=0,new_var_module=rgh_domain1)
domain1A=domain_class_1.domain_A
domain1B=domain_class_1.domain_B
domain_class_1.domain1A=domain1A
domain_class_1.domain1B=domain1B
#Adding sorbates to domain1A and domain1B
add_atom(domain=domain1A,ref_coor=[0.5,0.56955,2.0],ids=sorbate_ids_domain1a,els=sorbate_els_domain1)
add_atom(domain=domain1B,ref_coor=[0.5,0.5,1.5],ids=sorbate_ids_domain1b,els=sorbate_els_domain1)
#set variables
domain_class_1.set_new_vars(head_list=['u_o_n','u_Fe_n','oc_n'],N_list=[4,3,7])
domain_class_1.set_discrete_new_vars_batch(batch_path_head+discrete_vars_file_domain1)
######################################do grouping###############################################
#note the grouping here is on a layer basis, ie atoms of same layer are groupped together
#you may group in symmetry, then atoms of same layer are not independent.
atm_gp_list_domain1=domain_class_1.grouping_sequence_layer(domain=[domain1A,domain1B], first_atom_id=['O1_1_0_D1A','O1_7_0_D1B'],\
                            sym_file=sym_file, id_match_in_sym=id_match_in_sym,layers_N=7,use_sym=False)