def main () : parser = argparse.ArgumentParser( description="*** Compute the free energy along a string. ***") parser.add_argument('-s', '--string-folder', help='The folder of the string to be computed.') parser.add_argument('-o', '--output', default = "energy.out", help='The output energy in string folder.') args = parser.parse_args() string_file = args.string_folder + "/string.out" string_force = args.string_folder + "/force.out" if not os.path.exists (string_file) : raise RuntimeError ("cannot find string file " + string_file) if not os.path.exists (string_force) : raise RuntimeError ("cannot find string force file " + string_force + ". maybe the simulation is not finished") string = np.loadtxt (string_file) force = np.loadtxt (string_force) numb_node = string.shape[0] dim = string.shape[1] # compute arc (alpha) alpha_seg = StringUtils.arc_seg (string) alpha = StringUtils.arc_norm (string) # integrate the energy energy = np.zeros (alpha.shape) tagent = compute_string_tegent (alpha, string) for ii in range (1, numb_node) : v0 = np.dot (tagent[ii-1], force[ii-1]) v1 = np.dot (tagent[ii] , force[ii] ) vn = force[ii] - v1 * tagent[ii] # print (str(v1) + # " t: " + str(v1 * tagent[ii]) + # " |t|: " + str(np.sqrt(np.dot(v1 * tagent[ii], v1 * tagent[ii]))) + # " n: " + str(vn) + # " |n|: " + str(np.sqrt(np.dot(vn,vn))) + # " t.n: " + str(np.dot(v1 * tagent[ii], vn)) # ) de = 0.5 * (v0 + v1) * alpha_seg[ii] energy[ii] = energy[ii-1] - de new_shape = np.array ([alpha.shape[0], 2]) result = np.zeros (new_shape) result[:,0] = alpha result[:,1] = energy string_energy = args.string_folder + "/energy.out" np.savetxt (string_energy, result)
def init_source_string (string_dir, numb_node_tgt) : """ init a string from existing nodes""" if not os.path.isdir (string_dir) : raise RuntimeError ("Dir " + string_dir + " not found") file_name = string_dir + "/string.out" if not os.path.exists (file_name) : raise RuntimeError ("cannot find file " + file_name) string = np.loadtxt (file_name) alpha = StringUtils.arc_norm (string) smooth_str = interp1d (alpha, string, axis=0, kind="linear") alpha_eq = np.linspace (0, 1, numb_node_tgt) string = smooth_str (alpha_eq) return string
def init_source_string(string_dir, numb_node_tgt): """ init a string from existing nodes""" if not os.path.isdir(string_dir): raise RuntimeError("Dir " + string_dir + " not found") file_name = string_dir + "/string.out" if not os.path.exists(file_name): raise RuntimeError("cannot find file " + file_name) string = np.loadtxt(file_name) alpha = StringUtils.arc_norm(string) smooth_str = interp1d(alpha, string, axis=0, kind="linear") alpha_eq = np.linspace(0, 1, numb_node_tgt) string = smooth_str(alpha_eq) return string
def generate_from_source (source_string_dir_, string) : # check the dir and string file if not os.path.isdir (source_string_dir_) : raise RuntimeError ("cannot find dir " + source_string_dir_) cwd = os.getcwd() os.chdir (source_string_dir_) source_string_dir = os.getcwd () os.chdir (cwd) file_name = source_string_dir + "/string.out" if not os.path.exists (file_name) : raise RuntimeError ("cannot find file " + file_name) # load source string source_string = np.loadtxt (file_name) if source_string.shape[1] != string.shape[1] : raise RuntimeError ("size of the string nodes does not match") # mk dir of the string str_force = StringForce () string_name = str_force.mk_string_name (0) if not os.path.isdir(string_name) : ret = Popen(["cp", '-a', str_force.string_template, string_name], stdout=PIPE, stderr=PIPE) stdout, stderr = ret.communicate() if ret.returncode != 0 : raise RuntimeError ("cannot copy template dir to " + string_name) # generate nodes cwd = os.getcwd() cmd_gen_dir = "tools/gen.dir.absdep.sh" os.chdir (string_name) my_alpha = StringUtils.arc_norm (string) source_alpha = StringUtils.arc_norm (source_string) for ii in range (string.shape[0]) : # min_val = 1e10 # min_posi = 0 # for jj in range(source_string.shape[0]) : # norm = np.linalg.norm (string[ii] - source_string[jj]) # # print ("id " + str(jj) + # # " diff " + str(string[ii]) + " " + str(source_string[jj]) + " norm " + str(norm)) # if norm < min_val : # min_val = norm # min_posi = jj for jj in range(source_string.shape[0]) : if (my_alpha[ii] >= source_alpha[jj]) : min_posi = jj this_node = str_force.mk_node_name (ii) str_force.mk_node_param (string[ii]) source_node = str_force.mk_node_name (min_posi) ret = Popen ([cmd_gen_dir, this_node, source_string_dir+"/"+source_node], stdout=PIPE, stderr=PIPE) stdout, stderr = ret.communicate() if ret.returncode == 1 : raise RuntimeError ("cannot generate node. LOCATION: string: " + string_name + " node index: " + str(this_node) + ". error info: " + str(stderr, encoding='ascii') ) if ret.returncode == 10 : print ("# detected node: " + string_name + "/" + this_node) else : print ("# generated node: " + string_name + "/" + this_node) np.savetxt ("string.out", string) os.chdir (cwd)
def generate_from_source(source_string_dir_, string): # check the dir and string file if not os.path.isdir(source_string_dir_): raise RuntimeError("cannot find dir " + source_string_dir_) cwd = os.getcwd() os.chdir(source_string_dir_) source_string_dir = os.getcwd() os.chdir(cwd) file_name = source_string_dir + "/string.out" if not os.path.exists(file_name): raise RuntimeError("cannot find file " + file_name) # load source string source_string = np.loadtxt(file_name) if source_string.shape[1] != string.shape[1]: raise RuntimeError("size of the string nodes does not match") # mk dir of the string str_force = StringForce() string_name = str_force.mk_string_name(0) if not os.path.isdir(string_name): ret = Popen(["cp", '-a', str_force.string_template, string_name], stdout=PIPE, stderr=PIPE) stdout, stderr = ret.communicate() if ret.returncode != 0: raise RuntimeError("cannot copy template dir to " + string_name) # generate nodes cwd = os.getcwd() cmd_gen_dir = "tools/gen.dir.absdep.sh" os.chdir(string_name) my_alpha = StringUtils.arc_norm(string) source_alpha = StringUtils.arc_norm(source_string) for ii in range(string.shape[0]): # min_val = 1e10 # min_posi = 0 # for jj in range(source_string.shape[0]) : # norm = np.linalg.norm (string[ii] - source_string[jj]) # # print ("id " + str(jj) + # # " diff " + str(string[ii]) + " " + str(source_string[jj]) + " norm " + str(norm)) # if norm < min_val : # min_val = norm # min_posi = jj for jj in range(source_string.shape[0]): if (my_alpha[ii] >= source_alpha[jj]): min_posi = jj this_node = str_force.mk_node_name(ii) str_force.mk_node_param(string[ii]) source_node = str_force.mk_node_name(min_posi) ret = Popen( [cmd_gen_dir, this_node, source_string_dir + "/" + source_node], stdout=PIPE, stderr=PIPE) stdout, stderr = ret.communicate() if ret.returncode == 1: raise RuntimeError("cannot generate node. LOCATION: string: " + string_name + " node index: " + str(this_node) + ". error info: " + str(stderr, encoding='ascii')) if ret.returncode == 10: print("# detected node: " + string_name + "/" + this_node) else: print("# generated node: " + string_name + "/" + this_node) np.savetxt("string.out", string) os.chdir(cwd)