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

Add center methods

parent 3719cc16
......@@ -5,6 +5,7 @@ from __future__ import print_function
from py_unsio import *
from uns_simu import *
from cfalcon import *
from ccod import *
import numpy as np
#
......@@ -16,6 +17,7 @@ class CSnapshot:
__verbose=False
__float32=True
__loaded=False
__analysis=None
simname=None
select=None
......@@ -231,6 +233,26 @@ class CSnapshot:
status=0
return status,radius
#
# shift
#
def shift(self,data,center):
"""
shift data array to center
Return :
Center coordinates
Arguments:
data : one dimension numpy array ( nbody x 3 ), pos or vel
center : coordinates where to shift ( 1D array with 3 values)
"""
data=np.reshape(data,(-1,3)) # data reshaped in a 2D array [nbody,3]
data[:,0] -= center[0]
data[:,1] -= center[1]
data[:,2] -= center[2]
#
# center
#
......@@ -250,10 +272,10 @@ class CSnapshot:
cxv=np.zeros(6,dtype='f')
cxv[:]=np.NaN
if pos is not None:
cxv[0:3]=self.__centerOnWeight(pos,data_weight,center)
cxv[0:3]=self.__centerOnWeight(pos,data_weight,center)
if vel is not None:
cxv[3:6]=self.__centerOnWeight(vel,data_weight,center)
cxv[3:6]=self.__centerOnWeight(vel,data_weight,center)
return cxv
#
# __centerOnWeight
......@@ -276,6 +298,80 @@ class CSnapshot:
data[:,2] -= zcom
return xcom,ycom,zcom
#
#
#
def centerOnFile(self,pos,vel,mass,com,component,center_file):
"""
center positions and velocities according to paramaters
Return :
True if center has been found
Arguments:
pos : one dimension numpy array ( nbody x 3 )
vel : one dimension numpy array ( nbody x 3 )
mass : one dimension numpy array ( nbody )
center_file : can be an absolute file or a string like '@mdf001' means find COD of this simulation
com : boolean, if True and !center_file then center according to COM
"""
if center_file is None and com: # no center file but COM reqested
print("Centering via COM", file=sys.stderr)
self.center(pos,vel,data_weight=mass,center=True)
else :
if center_file is not None:
ok,tcxv=self._getCenterFromFile(center_file,component)
if ok:
print("Centering according to [%s]\n"%(center_file),tcxv[1:4],file=sys.stderr)
self.shift(pos,tcxv[1:4])
else:
print("No center found according to [%s]\n"%(center_file),file=sys.stderr)
else:
pass
#
#
#
def _getCenterFromFile(self,center_cod,comp=None):
"""
Return :
boolean,tcxv (boolean True if file exist)
Arguments:
According to argument 'center_cod'
- if it's cod file, will return cod
- if it's '@sim' it will return sim's cod at component comp
"""
#embed()
# check if simu
if self.__vdebug:
print(" __getCenterFromFile [%s]"%(center_cod),file=sys.stderr)
if os.path.isfile(center_cod) : # it's a file
ok,time=self.getData("time")
return CCod(None).getCodFromFile(center_cod,time)
else:
tmp=center_cod.split('@')
if (len(tmp)>1): # it's simulation name
simname=tmp[1]
if self.__vdebug:
print("Simulation name from COD [%s] comp <%s>\n"%(simname,comp))
print("SELF.__ANALYSIS = <%s>"%(self.__analysis))
cod=CCod(simname,verbose_debug=self.__vdebug)
ok,time=self.getData("time")
cod_base=None
if self.__analysis is not None:
if hasattr(self.__analysis,"cod_dir"):
cod_base=self.__analysis.cod_dir
if self.__vdebug:
print(" def __getCenterCod -> cod_base=[%s]"%(cod_base))
return cod.getCodFromComp(comp,time,cod_file_base=cod_base)
print("Unknown centering file[%s]\nNo centering..."%(center_cod),file=sys.stderr)
return False,[]
#
# computeDensity
#
......
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