Commit 280748a2 authored by jclamber's avatar jclamber
Browse files

updates

git-svn-id: http://svn.oamp.fr/repos/uns_projects/trunk@110 f264a43e-d52d-4b82-913b-c2bd5215a18a
parent acad8431
#!/usr/bin/python
#
from __future__ import print_function
#
# save stars particles to density field for displaying with glnemo2
......@@ -11,24 +11,70 @@ import numpy as np # arrays are treated as numpy arrays
import math
import argparse
import sys
#from IPython import embed
class snap:
time = None
nbody = None
mass = None
pos = None
vel = None
ids = None
ages = None
hsml = None
rho = None
metal = None
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# commandLine, parse the command line
def commandLine():
parser = argparse.ArgumentParser(description="Save stars particles born before/after a specific time")
dt=3
parser = argparse.ArgumentParser(description="Save stars particles born before/after a specific time",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('input', help='UNS input file with stars particle')
parser.add_argument('output', help="NEMO output file ")
parser.add_argument('--dt', help="save particles:[age > time-time*dt%%],(time=current time)",default=dt,type=float)
#parser.add_argument('time', help="time of reference to keep stars particles")
args = parser.parse_args()
compute(args.input,args.output)#,args.time)
process(args.input,args.output,args.dt)#,args.time)
# -----------------------------------------------------
# selectAges
def selectAges(snap,dt): #,times):
select=(snap.ages>=(snap.time-snap.time*dt/100.)) # select particles in the ramge of ages
snap.pos=np.reshape(snap.pos,(-1,3)) # pos reshaped in a 2D array [nbody,3]
snap.vel=np.reshape(snap.vel,(-1,3)) # pos reshaped in a 2D array [nbody,3]
# rescale pos
snap.pos = snap.pos[select]
snap.pos = np.reshape(snap.pos,snap.pos.size) # flatten the array (mandatory for unsio)
# rescale vel
snap.vel = snap.vel[select]
snap.vel = np.reshape(snap.vel,snap.vel.size) # flatten the array (mandatory for unsio)
#rescale mass
snap.mass = snap.mass[select]
# rescale ages
snap.ages = snap.ages[select]
# hsml
snap.hsml=np.zeros(snap.ages.size,dtype='float32')
snap.hsml += 0.1
# -----------------------------------------------------
# compute, is the core function
def compute(simname,out): #,times):
def process(simname,out,dt): #,times):
components="stars"
verbose=False
......@@ -41,46 +87,45 @@ def compute(simname,out): #,times):
# get file name
# sim_name=uns.getFileName()
print "simname=",simname, " out=",out #," times=",times
print ("simname=",simname, " out=",out,file=sys.stderr)
# load frame
ok=uns.nextFrame("")
print ok
#print ok
if (ok) :
ok,ages = uns.getArrayF("stars","age")
ok,snap.ages = uns.getArrayF("stars","age")
if ( ok ) :
print "ok ",ok, ages
print "min=", ages.min()," max=",ages.max()
ok,timec= uns.getValueF("time")
ok,pos = uns.getArrayF("stars","pos")
ok,vel = uns.getArrayF("stars","vel")
ok,mass = uns.getArrayF("stars","mass")
hsml=np.zeros(ages.size,dtype='float32')
hsml += 1.0
print "HSML :", hsml.size , hsml, hsml.dtype
#print "ok ",ok, snap.ages
print ("min=", snap.ages.min()," max=",snap.ages.max(),file=sys.stderr)
#embed()
ok,snap.time = uns.getValueF("time")
ok,snap.pos = uns.getArrayF("stars","pos")
ok,snap.vel = uns.getArrayF("stars","vel")
ok,snap.mass = uns.getArrayF("stars","mass")
# select ages according to dt
selectAges(snap,dt)
# instantiate output object
unso=CunsOut(out,"nemo"); # output file
# save data
unso.setValueF("time",timec) # save time
unso.setArrayF("all","pos",pos) # save pos
unso.setArrayF("all","vel",vel) # save vel
unso.setArrayF("all","mass",mass) # save mass
unso.setArrayF("all","rho",ages) # save ages to rho
unso.setArrayF("all","hsml",hsml) # save hsml
unso.setValueF("time",snap.time) # save time
unso.setArrayF("all","pos",snap.pos) # save pos
unso.setArrayF("all","vel",snap.vel) # save vel
unso.setArrayF("all","mass",snap.mass) # save mass
unso.setArrayF("all","rho",snap.ages) # save ages to rho
unso.setArrayF("all","hsml",snap.hsml) # save hsml
unso.save()
else:
print "there are no age for stars in this snapshot !!!"
print ("there are no age for stars in this snapshot !!!",file=sys.stderr)
else :
print "Didn't load anything...."
print ("Didn't load anything....",file=sys.stderr)
# -----------------------------------------------------
......
#!/usr/bin/python
import sys
sys.path.append('/home/jcl/works/SVN/uns_projects/trunk/py/modules/')
from uns_simu import *
import argparse
......
......@@ -5,6 +5,7 @@ import sys
from py_unsio import * # import py_unsio package (UNSIO)
import numpy as np
import argparse
#from IPython import embed
class snap:
time = None
......@@ -19,17 +20,17 @@ class snap:
def commandLine():
time="all"
verbose=False
component="halo"
offset=0
# help
# help
parser = argparse.ArgumentParser(description="Select components by their id",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# options
parser.add_argument('simname', help='Simulation name or snapshot')
parser.add_argument('outname', help='Output name snapshot')
parser.add_argument('npoints', help='#points to keep',type=int, default=component)
parser.add_argument('--component', help='Simulation component', default=component)
parser.add_argument('--offset',help='index offset of the isolated component', type=int,default=offset)
parser.add_argument('component', help='Simulation component')
parser.add_argument('massratio', help='galaxies mass ratio separated by comma (ex: "m1/m2,m1")')
parser.add_argument('idgal', help='index of galaxy to extract',type=int)
#parser.add_argument('--time',help='simulation time', default=time)
parser.add_argument('--verbose',help='verbosity', default=verbose)
......@@ -37,7 +38,7 @@ def commandLine():
args = parser.parse_args()
# start main funciton
process(args.simname,args.outname,args.npoints,args.component,args.offset,time,args.verbose)
process(args.simname,args.outname,args.component,args.massratio,args.idgal,time,args.verbose)
# -----------------------------------------------------
#
......@@ -72,26 +73,49 @@ def saveSnapshot(snap,idnew,outname):
# -----------------------------------------------------
#
def getComponentByIds(snap,offset,npoints):
def getComponentByIds(snap,massratio,idgal):
print("ID min %d / max %d"%(snap.ids.min(),snap.ids.max()),file=sys.stderr)
istart = snap.ids.min()+offset
iend = istart+npoints-1
#embed()
# parse massratio variable
m1om2,m1=massratio.split(",")
m1om2 = float(m1om2) # m1 / m2 massration of the 2 galaxies
m1 = float(m1) # m1 mass of gal1
m2 = m1 / m1om2 # m2 mass of gal2
mtot = m1 + m2 # mtot total mass
#compute #bodies per galaxy
nbody = np.zeros(2) # array to store #bodies ofr each particles
nbody[0] = m1 * snap.nbody / mtot # nbody gal1
nbody[1] = m2 * snap.nbody / mtot # nbody gal2
nbody=nbody.astype(int) # convert to int
offset = snap.ids.min() # first ID value for the component
if (idgal==1):
istart=offset # first ID of gal1
iend=istart+nbody.max() # end of ID
else:
istart=offset+nbody.max() # according to Sergey, massive galaxy always first so we put nbody.max()
iend=istart+nbody.min()
if (iend-istart > snap.nbody) :
print("Error, iend %d computed > %d bodies"%(iend-istart,snap.nbody),file=sys.stderr)
sys.exit()
#embed()
idnew = (snap.ids>=istart) & (snap.ids<=iend) # selection criteria
indexes = np.array((idnew.nonzero())).reshape(-1,) # retreive indexes array
#embed()
print(idnew,indexes)
return True,indexes
# -----------------------------------------------------
# process, is the core function
def process(simname,outname,npoints,component,offset,time,verbose):
def process(simname,outname,component,massratio,idgal,time,verbose):
#print (simname, " " , time,file=sys.stderr)
# Create a UNSIO object
uns = CunsIn(simname,component,time,verbose)
......@@ -112,7 +136,7 @@ def process(simname,outname,npoints,component,offset,time,verbose):
if not find:
print ("none")
else:
ok,idnew=getComponentByIds(snap,offset,npoints)
ok,idnew=getComponentByIds(snap,massratio,idgal)
if ok:
saveSnapshot(snap,idnew,outname)
# -----------------------------------------------------
......
#/usr/bin/python
import sqlite3
import os,sys
import os,sys,glob
class UnsSimu:
'sqlite3 management for UNS sqlite3 database'
......@@ -12,9 +12,8 @@ class UnsSimu:
__dbunsio=".unsio"
# constructor
def __init__(self,name,dbname="") :
self.name = name
if (dbname != "" ) :
def __init__(self,dbname=None) :
if (dbname != None ) :
self.dbname = dbname
if (not os.path.isfile(dbname)) :
print 'sqlite3 db file ['+dbname+'] does not exist...\n'
......@@ -23,12 +22,12 @@ class UnsSimu:
self.__conn = sqlite3.connect(dbname)
self.__status = 1
else : # we must parse .unsio file
status,self.dbname=self.parseDotUnsio("")
self.__conn = sqlite3.connect(dbname)
self.__status = 1
status,self.dbname=self.parseDotUnsio("")
self.__conn = sqlite3.connect(self.dbname)
#print "CONN = ",self.__conn
if (self.__conn != 0):
self.__conn.row_factory = sqlite3.Row
# parse $HOME/.unsio file to detect db file name
def parseDotUnsio(self,file):
......@@ -51,12 +50,41 @@ class UnsSimu:
#print data
if (gparam['dbname']!="") :
return True,gparam['dbname']
if (not os.path.isfile(gparam['dbname'])) :
return False, self.dbname
else :
return True,gparam['dbname']
else:
False,self.dbname
def printInfo(self,name):
r = self.getInfo(name)
if (r):
for keys in r.keys():
print keys," : ",r[keys] #for row in cursor:
def getInfo(self,name):
if ( self.__status) :
c = self.__conn.cursor()
sql="select * from info where name=='"+name+"'"
cursor=c.execute(sql)
#all_rows = c.fetchall()
#print('1):', all_rows)
r = c.fetchone()
return r
def getSnapshotList(self,name):
r = self.getInfo(name)
if (r and r['type']=="Gadget"):
if ( os.path.exists(r['dir'] )):
base = r['dir']+"/"+r['base']
print "base : ", base
snap_list = sorted(glob.glob(base+'_?'))
snap_list = snap_list+sorted(glob.glob(base+'_??'))
snap_list = snap_list+sorted(glob.glob(base+'_???'))
snap_list = snap_list+sorted(glob.glob(base+'_????'))
snap_list = snap_list+sorted(glob.glob(base+'_?????'))
return snap_list
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