Commit a2de79dc authored by LAMBERT Jean-charles's avatar LAMBERT Jean-charles
Browse files

in progess...

parent 37a43154
#!/usr/bin/env python
import numpy as np
import os,time
import sys
import argparse,textwrap
sys.path=['/home/jcl/works/GIT/uns_projects/py/modules/','/home/jcl/works/GIT/uns_projects/py/modules/simulations']+sys.path
from simulations.crectify import *
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# commandLine, parse the command line
def commandLine():
# help
parser = argparse.ArgumentParser(description="Convert eigens vectors to rect file",
formatter_class=argparse.RawTextHelpFormatter)
#formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# options
hh=textwrap.dedent('''\
Simulation name
if not empty, all EV files from ANALYSIS/rectify directory
will be converted to rect files''')
parser.add_argument('--simname', help=hh,default=None)
parser.add_argument('--ev', help='Eigen vector input file',default=None)
parser.add_argument('--rect',help='Rectify output file', default=None)
parser.add_argument('--verbose',help='verbose mode', default=False)
# parse
args = parser.parse_args()
# start main funciton
process(args)
# -----------------------------------------------------
# process, is the core function
def process(args):
try:
c=CRectify()
c.buildRectFile(simname=args.simname,ev_in=args.ev,rect_out=args.rect)
except Exception as x :
print (x.message)
# -----------------------------------------------------
# main program
if __name__ == '__main__':
commandLine()
#!/usr/bin/python
from __future__ import print_function
from uns_simu import *
from csnapshot import *
from cfalcon import *
from ctree import *
from multiprocessing import Process, Lock,Pool
import multiprocessing
import Queue # necessary to raise Queue.Empty signal
import time
import os
import signal
#
# ----
#
class CCom:
"""
compute Center Of Mass on UNS snapshots
"""
#
# ----
#
# constructor
__analysis=None
def __init__(self,analysis,verbose=False,verbose_debug=False):
"""
Constructor of CCom class
- analysis : is a class object instantiate from CUnsAnalysis class
"""
self.__vdebug=verbose_debug
self.__verbose=verbose
if analysis is not None:
self.__analysis=analysis
self.__smartAnalysisInit()
#
# __smartAnalysis
def __smartAnalysis(self):
"""
This is a special method called by constructor during smart pipeline analysis
"""
if hasattr(self.__analysis,'com'):
print ("COM exist")
else:
# we go here the first time
print ("COM does not exist")
self.__analysis.com=True
# then we run some initialisations
self.__smartAnalysisInit()
#
# __smartAnalysisInit
#
def __smartAnalysisInit(self):
"""
start some initialisations
"""
### build COM Dir
if hasattr(self.__analysis,'com_dir'):
pass
#
else: # default simdir simulation
self.__analysis.com_dir=self.__analysis.sim_info['dir']+"/ANALYSIS/com"
print("COM DIR = ", self.__analysis.com_dir, self.__analysis.sim_info['name'])
self.__com_file_base=self.__analysis.com_dir
self.__analysis.com_file=self.__analysis.com_dir+"/com_all.txt"
# lock process
self.__analysis.lock[self.__analysis.lock_id].acquire()
# build directory
if (not os.path.isdir(self.__analysis.com_dir)) :
try:
print("Core ID ",self.__analysis.core_id," create directory [%s]\n"%(self.__analysis.com_dir))
os.makedirs(self.__analysis.com_dir)
except OSError:
print("Unable to create directory [%s]\n"%(self.__analysis.com_dir))
self.__analysis.lock[self.__analysis.lock_id].release()
sys.exit()
else:
#
if not os.path.isfile(self.__analysis.com_file):
f=open(self.__analysis.com_file,"w")
try:
f.write("%s\n"%(self.__analysis.sim_info['name']))
f.close()
except:
print("Unable to create file [%s], aborting"%(self.__analysis.com_file))
sys.exit()
# release process
self.__analysis.lock[self.__analysis.lock_id].release()
#
# smartAnalysis
#
def smartAnalysis(self,analysis=None):
"""
Main core function to compute COD on current snapshot store in data_analysis
"""
if analysis is None:
data=self.__analysis
else:
data=analysis
if not hasattr(data,'com_select'):
print("\n\nAlgo error,data_analysis structure must contain 'com_select' field, aborting...\n")
sys.exit()
data.com_select=data.com_select.replace(" ", "")
self.simname = data.sim_info['name']
#
uns_snap=data.uns_snap
ok,time=uns_snap.getData("time")
if self.__vdebug:
print(self.simname+" time [%e] "%(time))
# read COM file
if not hasattr(data,'com_first_new_time'):
data.lock[data.lock_id].acquire()
exist_time,ticxv=self.getComFromFile(data.com_file,1,time)
data.lock[data.lock_id].release()
if not exist_time:
data.com_first_new_time=True
else: # speed up searching
if self.__vdebug:
print("speedup COM searching....\n")
exist_time=False
icxv=np.zeros((6,7))
cpt=0
if not exist_time:
if self.__vdebug:
print("Time [%f] does not exist!!\n"%(time))
for select in data.com_select.split(":"):
if self.__vdebug:
print("COM select[%s]\n"%(select))
icxv[cpt][0] = cpt
ok,mass=uns_snap.getData(select,"mass")
if mass.size==0 : # no mass
if self.__vdebug:
print("COM no mass...\n")
icxv[cpt][1:]=99999.
else:
ok1,pos=uns_snap.getData(select,"pos")
ok2,vel=uns_snap.getData(select,"vel")
if pos.size>1 or vel.size>1 :
icxv[cpt][1:]=uns_snap.center(pos,vel,mass)
cpt=cpt+1
# save COM on disk
data.lock[data.lock_id].acquire()
self.saveComToFile(data.com_file,time,icxv)
data.lock[data.lock_id].release()
#
# ----
#
def saveComToFile(self,com_file,time,icxv):
"""
save COM record to file
com_file : COM file name
time : current time to save
icxv : 2D numpy array, usually (6,7)
6 lines for components
7 columns ( comp's index, cx,cy,cz,cvx,cvy,cvz )
"""
try:
print("SAVING to com_file[%s]\n"%(com_file))
f=open(com_file,"a")
for n in range(icxv.shape[0]):
f.write("%e\n"%(time))
f.write("%d %e %e %e %e %e %e\n"%(icxv[n][0]+1,\
icxv[n][1],icxv[n][2],icxv[n][3],\
icxv[n][4],icxv[n][5],icxv[n][6]))
f.close()
except:
print("\n\nUnable to save COM to file ... aborting...\n")
sys.exit()
#
# ----
#
def getComFromFile(self, com_file,index, time,n=-1):
"""
check if current time exist in com file
"""
ok=False
tcxv=[]
if os.path.isfile(com_file):
try:
f=open(com_file,"r")
simname=f.readline()
while True:
tcxv=[]
xx=f.readline()
if (len(xx)>0):
tcxv.append( np.float64(xx))
tcxv.append(map(np.float64,(f.readline().split())))
if (len(tcxv)>0):
atime=tcxv[0]
read_index=tcxv[1][0]
if (atime-0.001)<time and (atime+0.001)>time and (index-0.001)<read_index and (index+0.001)>read_index:
f.close()
return True,tcxv
else:
f.close()
return False,[]
else:
return False,[]
except EOFError:
pass
return ok,tcxv
#!/usr/bin/env python
from __future__ import print_function
from uns_simu import *
from csnapshot import *
import py_unstools # rectify swig
from multiprocessing import Lock
import time
import os
#
# ----
#
class CRectify:
"""
compute Center Of Mass on UNS snapshots
"""
#
# ----
#
# constructor
__analysis=None
def __init__(self,analysis=None,verbose=False,verbose_debug=False):
"""
Constructor of CRectify class
- analysis : is a class object instantiate from CUnsAnalysis class
"""
self.__vdebug=verbose_debug
self.__verbose=verbose
if analysis is not None:
self.__analysis=analysis
self.__smartAnalysisInit()
#
# __smartAnalysisInit
#
def __smartAnalysisInit(self):
"""
start some initialisations
"""
data=self.__analysis
if not hasattr(data,'rectify_select'):
print("\n\nYou must set a fied [rectify_select] in your <data> object, aborting...\n\n")
sys.exit()
data.rectify_select=data.rectify_select.replace(" ","") # remove blank
### build COM Dir
if hasattr(data,'rectify_dir'):
self.__rectify_dir=data.rectify_dir
#
else: # default simdir simulation
self.__rectify_dir=data.sim_info['dir']+"/ANALYSIS/rectify"
print("RECTIFY DIR = ", self.__rectify_dir, data.sim_info['name'])
self.simname=data.sim_info['name']
self.__com_file_base=self.__rectify_dir
# lock process
data.lock[data.lock_id].acquire()
# build directory
if (not os.path.isdir(self.__rectify_dir)) :
try:
print("Core ID ",data.core_id," create directory [%s]\n"%(self.__rectify_dir))
os.makedirs(self.__rectify_dir)
except OSError:
print("Unable to create directory [%s]\n"%(self.__rectify_dir))
data.lock[data.lock_id].release()
sys.exit()
# release process
data.lock[data.lock_id].release()
# check components vs cod
### COD Dir name
if hasattr(data,'cod_dir'):
pass
#
else: # default simdir simulation
data.cod_dir=data.sim_info['dir']+"/ANALYSIS"+self.__COD_DIR_NAME
self.__cod_base_dir=data.cod_dir
self.comp_cod=[] # list to store combo : component/cod to apply rectify
### re build select component variable according to components existing at mid
### simulation time (pre-computed by cuns_analysis.py and set to data.list_components
self.__new_select=""
for colon_s in data.rectify_select.split(":"): # colon separate analysis
comp_L,cod_R=colon_s.split("#") # hashtag separate comp_Left from cod_Right
cod_R=cod_R.split("@")
if len(cod_R)>1:
rcut=float(cod_R[1])
codf=cod_R[0]
else:
rcut=100.
codf=cod_R[0]
# check components exist?
ok=True
for comma_s in comp_L.split(","):
xx=data.list_components.find(comma_s) # find if selection exist
if xx==-1:
ok=False
print("Rectify#Warning : In combo [%s], component <%s> from select <%s> does not exist...\n"\
%(colon_s,comma_s,comp_L))
break
# components are ok, check cod file
if ok:
cod_file=self.__cod_base_dir+"/"+self.simname+"."+codf+".cod"
if (os.path.exists(cod_file)):
ok=True
else:
ok=False
print("Rectify#Warning : In combo [%s], COD <%s> does not exist...\n"\
%(colon_s,cod_file))
# both comp_L and cod_r exist, build combo
if ok:
self.comp_cod.append([comp_L,codf,cod_file,rcut])
if self.__vdebug:
for comp_cod in self.comp_cod:
print("Rectify combo: <%s> with [%s]\n"%(comp_cod[0],comp_cod[2]))
#
# smartAnalysis
#
def smartAnalysis(self,analysis=None):
"""
Main core function to compute RECTIFY on current snapshot store in data_analysis
"""
if analysis is None:
data=self.__analysis
else:
data=analysis
uns_snap=data.uns_snap # link to UNS object
ok,time=uns_snap.getData("time")
c=py_unstools.CRectify()
# loop on combo comp/cod
for comp_cod in self.comp_cod:
select = comp_cod[0]
cod_sel = comp_cod[1]
cod_file = comp_cod[2]
rcut = comp_cod[3]
# pre computed rectify file
pre_rect_file=self.__rectify_dir+"/"+self.simname+"."+select+"-"+cod_sel+".ev"
# lock file
#data.lock[data.lock_id].acquire()
exist_time,e_vectors=self.__checkTimeInPreRect(time,pre_rect_file)
#data.lock[data.lock_id].release()
if not exist_time :
ok1,pos = uns_snap.getData(select,"pos" )
ok2,vel = uns_snap.getData(select,"vel" )
ok3,mass = uns_snap.getData(select,"mass")
if pos.size>1 and vel.size>1 and mass.size>1 :
if self.__vdebug:
print("Rectifing time (%f) select <%s> cod[%s]\n"%(time.item(),select,cod_file))
ok,eigen_v=c.computeEigenVectors(pos,vel,mass,mass,16,time.item(),False,False,str(cod_file),rcut)
print("Time [%f] eigen:\n"%(time),eigen_v)
data.lock[data.lock_id].acquire()
self.__saveEigenVectors(pre_rect_file,eigen_v)
data.lock[data.lock_id].release()
#
# checkTimeInPreRect
#
def __checkTimeInPreRect(self,time,eigen_file):
"""
check in pre-computed Eigen Vectors stored in file if time has been already computed
*IN*
time : current time
egein_file : file with eigen vectors values
*OUT*
boolean : True if time exist, False otherwise
numpy : numppy array[16] => time[1] codx[3] codv[3] eigen_vectors[9]
array
"""
if os.path.isfile(eigen_file):
try:
f=open(eigen_file,"r")
while True:
tce=map(np.float64,(f.readline().split()))
if (len(tce)>0):
atime=tce[0]
if (atime-0.001)<time and (atime+0.001)>time:
f.close()
return True,tce
else:
f.close()
return False,np.array([])
except EOFError:
pass
return False,np.array([])
#
# saveEigenVectors
#
def __saveEigenVectors(self,pre_rect_file,eigen_v):
"""
"""
try:
print("SAVING to pre_rect_file[%s]\n"%(pre_rect_file))
f=open(pre_rect_file,"a")
eigen_v.tofile(f,sep=" ",format="%e")
f.write("\n")
f.close()
except:
print("\n\nUnable to save pre_rect_file[%s] ... aborting...\n"%(pre_rect_file))
sys.exit()
#
# buildRectFile
#
def buildRectFile(self,simname=None,ev_in=None,rect_out=None):
"""
Build rect file from eigen vectors file
"""
if simname is not None:
self.simname = simname
self.__sql3 = UnsSimu(simname,dbname=self.__dbname,verbose=self.__verbose)
self.__r = self.__sql3.getInfo() # return None if does not exist
if self.__vdebug:
self.__sql3.printInfo(simname)
self.__slist = self.__sql3.getSnapshotList()
if (self.__slist is None):
message="In CLASS <"+self.__class__.__name__+"> : Unknown simulation ["+simname+\
"] in UNS database..."
raise Exception(message)
else :
self.__convertEv2Rect(ev_in,rect_out)
#
# __convertEv2Rect
#
def __convertEv2Rect(self,ev_in=None,rect_out=None):
if ev_in is not None and os.path.isfile(ev_in):
f=None
try:
if rect_out is not None:
f=open(rect_out,"w")
except EOFError:
print("Unable to open file [%s] for writing, aborting....\n"%(rect_out))
sys.exit()
a=np.loadtxt(ev_in) # load file
ii=a[:,0].argsort()
ref0=np.array([1.,0.,0.])
ref1=np.array([0.,1.,0.])
ref2=np.array([0.,0.,1.])
for id in ii:
time=a[id,0]
cx=a[id,1:4]
cv=a[id,4:7]
ev0=a[id,7:10]
ev1=a[id,10:13]
ev2=a[id,13:16]
tmp=np.dot(ref0,ev0)
if tmp<0.0:
ev0 = ev0*-1.
tmp=np.dot(ref2,ev2)
if tmp<0.0:
ev2 = ev2*-1.
ev1=np.cross(ev2,ev0)
ref0=ev0
ref1=ev1
ref2=ev2
if f is not None:
f.write("%e %e %e %e %e %e %e %e %e %e %e %e %e %e %e %e\n"\
%(a[id,0],cx[0],cx[1],cx[2],cv[0],cv[1],cv[2],ev0[0],\
ev0[1],ev0[2],ev1[0],ev1[1],ev1[2],ev2[0],ev2[1],ev2[2]))
else:
print("%e %e %e %e %e %e %e %e %e %e %e %e %e %e %e %e"\
%(a[id,0],cx[0],cx[1],cx[2],cv[0],cv[1],cv[2],ev0[0],\
ev0[1],ev0[2],ev1[0],ev1[1],ev1[2],ev2[0],ev2[1],ev2[2]))
Supports Markdown
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