1- import sys
2- import os
3- import random
4-
5- from multioptpy .Utils import calc_tools
6- import numpy as np
7-
8- import multioptpy
9-
10- def orientation_search (parser ):
11- parser .add_argument ("-nsample" , "--number_of_samples" , type = int , default = 5 , help = 'the number of sampling orientations' )
12- parser .add_argument ("-part" , "--part" , nargs = "*" , type = str , help = 'the part number (ex.) 1,2,3 or 1-3' , default = None )
13- parser .add_argument ("-dist" , "--distance" , type = float , default = 5.0 , help = 'the distance of parts [ang.]' )
14- return parser
15-
16-
17- def num_parse (numbers ):
18- sub_list = []
19-
20- sub_tmp_list = numbers .split ("," )
21- for sub in sub_tmp_list :
22- if "-" in sub :
23- for j in range (int (sub .split ("-" )[0 ]),int (sub .split ("-" )[1 ])+ 1 ):
24- sub_list .append (j )
25- else :
26- sub_list .append (int (sub ))
27- return sub_list
28-
29-
30- def save_xyz_file (coord_list , element_list , file_name , n_sample ):
31- no_ext_file_name = os .path .splitext (file_name )[0 ]
32- sample_file_name = no_ext_file_name + "_Sample_" + str (n_sample )+ ".xyz"
33- with open (sample_file_name , 'w' ) as f :
34- f .write (str (len (coord_list ))+ "\n " )
35- f .write ("Sample_" + str (n_sample )+ "\n " )
36- for i in range (len (coord_list )):
37- f .write (element_list [i ]+ " " + str (coord_list [i ][0 ])+ " " + str (coord_list [i ][1 ])+ " " + str (coord_list [i ][2 ])+ "\n " )
38-
39- return sample_file_name
40-
41-
42- def make_random_orientation_xyz_file (coord_list , part_list , part_dist , input_file_name , n_sample ):#ang.
43- part_coord_list = []
44- part_element_list = []
45- for part in part_list :
46- tmp_part_coord_list = []
47- tmp_part_element_list = []
48- for p in part :
49- tmp_part_coord_list .append (coord_list [p - 1 ])
50- tmp_part_element_list .append (element_list [p - 1 ])
51- part_coord_list .append (np .array (tmp_part_coord_list ))
52- part_element_list .append (tmp_part_element_list )
53-
54- # Random rotation
55- rand_rotated_part_coord_list = []
56- for i in range (len (part_coord_list )):
57- part_center = calc_tools .Calculationtools ().calc_center (part_coord_list [i ], part_element_list [i ])
58- centered_part_coord_list = part_coord_list [i ] - part_center
59- random_x_angle = random .uniform (0 , 2 * np .pi )
60- random_y_angle = random .uniform (0 , 2 * np .pi )
61- random_z_angle = random .uniform (0 , 2 * np .pi )
62- print ("Random angles (Radian): " , random_x_angle , random_y_angle , random_z_angle )
63- rotated_part_coord = calc_tools .rotate_molecule (centered_part_coord_list , "x" , random_x_angle )
64- rotated_part_coord = calc_tools .rotate_molecule (rotated_part_coord , "y" , random_y_angle )
65- rotated_part_coord = calc_tools .rotate_molecule (rotated_part_coord , "z" , random_z_angle )
66- rand_rotated_part_coord_list .append (rotated_part_coord )
67-
68-
69- # Random translation
70- rand_tr_rot_part_coord_list = rand_rotated_part_coord_list
71-
72- if len (rand_tr_rot_part_coord_list ) == 2 :
73- rand_dist = random .uniform (part_dist , 1.1 * part_dist )
74- rand_tr_rot_part_coord_list [0 ] = rand_tr_rot_part_coord_list [0 ] - np .array ([part_dist / 2 , 0 , 0 ])
75- rand_tr_rot_part_coord_list [1 ] = rand_tr_rot_part_coord_list [1 ] + np .array ([part_dist / 2 , 0 , 0 ])
76- else :
77- fragm_num = len (rand_tr_rot_part_coord_list )
78- rand_dist = random .uniform (part_dist , 1.1 * part_dist )
79- rand_sample_list = random .sample (range (fragm_num ), fragm_num )
80- distance_polygen_point_from_center = 0.5 * rand_dist * np .tan ((fragm_num - 2 ) * np .pi / fragm_num )
81- tmp_angle = 0.0
82- for i in range (len (rand_sample_list )):
83- rand_tr_rot_part_coord_list [rand_sample_list [i ]] = rand_tr_rot_part_coord_list [rand_sample_list [i ]] + np .array ([distance_polygen_point_from_center * np .cos (tmp_angle ), distance_polygen_point_from_center * np .sin (tmp_angle ), 0 ])
84- tmp_angle += 2 * np .pi / fragm_num
85-
86- combined_coord_list = np .zeros ((len (coord_list ), 3 ))
87-
88- # Combine all parts
89- for i in range (len (part_list )):
90- part = part_list [i ]
91-
92- for j in range (len (part )):
93-
94- combined_coord_list [part [j ]- 1 ] = rand_tr_rot_part_coord_list [i ][j ]
95-
96- input_file_name = save_xyz_file (combined_coord_list , element_list , input_file_name , n_sample )
97-
98- return input_file_name
99-
100-
101-
102- if __name__ == '__main__' :
103- parser = multioptpy .interface .init_parser ()
104- parser = orientation_search (parser )
105- args = multioptpy .interface .optimizeparser (parser )
106-
107- part_dist = args .distance
108-
109- with open (args .INPUT , 'r' ) as f :
110- words = f .read ().splitlines ()
111-
112- element_list = []
113- coord_list = []
114-
115- for word in words :
116- splitted_word = word .split ()
117-
118- if len (splitted_word ) > 3 :
119- element_list .append (str (splitted_word [0 ]))
120- coord_list .append ([float (splitted_word [1 ]), float (splitted_word [2 ]), float (splitted_word [3 ])])
121-
122- coord_list = np .array (coord_list , dtype = "float64" )
123- atom_num = len (coord_list )
124-
125- if args .part is None :
126- print ("No part is specified. exit...." )
127- sys .exit (0 )
128-
129- part_list = []
130- for i in range (len (args .part )):
131- part_list .append (num_parse (args .part [i ]))
132-
133- flattened_part_list = [item for sublist in part_list for item in sublist ]
134- missing_parts = [num for num in range (1 , atom_num + 1 ) if num not in flattened_part_list ]
135- if len (missing_parts ) > 0 :
136- part_list .append (missing_parts )
137-
138- print ("Number of Parts: " , len (part_list ))
139- n_sample = args .number_of_samples
140-
141- original_input_file = args .INPUT
142-
143- for i in range (n_sample ):
144- print ("Sampling orientation: " , i )
145- args .INPUT = make_random_orientation_xyz_file (coord_list , part_list , part_dist , original_input_file , i )
146- bpa = multioptpy .optimization .Optimize (args )
147- bpa .run ()
148-
149-
150- print ("Sampling orientation: " , i , "done" )
151-
152- print ("Orientation search done" )
1+ """Backward-compatible standalone script. Delegates to the installed entry point."""
2+ from multioptpy .entrypoints import run_orientsearch
3+
4+ if __name__ == "__main__" :
5+ run_orientsearch ()
0 commit comments