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

to test UNSIO library

parent 4857469a
......@@ -96,9 +96,9 @@ def main(argv):
if (smooth):
z=np.polyfit(time,cpu,4)
f4=np.poly1d(z)
plt.plot(time,f4(time),label=f_label) #,'r.-')
plt.plot(time,f4(time),label=f_label)
else:
plt.plot(time,cpu,label=f_label) #,'r.-')
plt.plot(time,cpu,label=f_label)
plt.xlabel('Simulation time')
......
#!/usr/bin/env python
from __future__ import print_function
import sys
sys.path=['/home/jcl/works/GIT/uns_projects/py/modules/','/home/jcl/works/GIT/uns_projects/py/modules/simulations']+sys.path
#from py_unstools import * # import py_unstools package
from py_unsio import *
from uns_simu import *
import argparse
import os
from simulations.ctestunsio import *
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# commandLine, parse the command line
def commandLine():
# help
parser = argparse.ArgumentParser(description="Test UNSIO library",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# options
parser.add_argument('--nbody', help='#bodies to test', type=int, default=100000)
parser.add_argument('--verbose',help='verbose mode',dest="verbose", action="store_true", default=False)
# parse
args = parser.parse_args()
# start main funciton
process(args)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# commandLine, parse the command line
def process(args):
uns=CTestunsio(nbody=args.nbody)
#uns.saveModel("")
uns.testIO()
# -----------------------------------------------------
# main program
if __name__ == '__main__':
commandLine()
#!/usr/bin/env python
from __future__ import print_function
from uns_simu import *
import os,sys
import numpy as np
import tempfile
from py_unsio import *
from csnapshot import *
#
# class CTestunsio
#
class CTestunsio:
"""
This class aims to test UNSIO library
"""
__comp=["halo","gas","stars","disk","bndry","bulge"]
#__comp=["gas","halo"]#,"disk","bndry"]#,"bulge"]
__array3d=["pos","vel","acc"]
__array1d=["mass","rho","hsml","pot","metal","age"]
__arrayInt=["id"]
# -----------------------------------------------------
#
def __init__(self,nbody=None,seed=666,single=True,verbose=None):
self.__nbody = nbody
self.__verbose = verbose
self.__seed = seed
self.__single = single
self.__initSeed(self.__seed)
# -----------------------------------------------------
#
def __initSeed(self,seed=None):
if seed is None:
seed=self.__seed
np.random.seed(seed)
# -----------------------------------------------------
#
def __dataF(self,n):
if (self.__single):
x=np.float32(np.random.sample(n))
else:
x=np.float64(np.random.sample(n))
return x
# -----------------------------------------------------
#
def __saveArray(self,comp,attr,dim):
data=self.__dataF(self.__nbody*dim)
#print ("Saving comp[%s] attribute [%s] size [%d] "%(comp,attr,data.size),type(data),data,file=sys.stderr)
ok=self.__unso.setArrayF(comp,attr,data) # save real arrays
if ok:
print(" <%s>"%(attr),data[0],file=sys.stderr,end="")
# -----------------------------------------------------
#
def saveModel(self,filename=None,unstype="gadget3",single=True):
"""
save model in requested format
"""
self.__initSeed() # reset random generator
# create temporary file
if filename is None:
f = tempfile.NamedTemporaryFile()
self.__model_file = f.name
f.close()
else:
self.__model_file = filename
## SAVE FILE
# instantiate output object
if (single):
print("SINGLE")
self.__unso=CunsOut(self.__model_file,unstype); # output file
else:
print("DOUBLE")
self.__unso=CunsOutD(self.__model_file,unstype); # output file
print("\nSaving in ",unstype," format......")
self.__unso.setValueF("time",0) # save time
for comp in self.__comp:
print ("[%s] : "%(comp),file=sys.stderr,end="")
for array3d in self.__array3d:
self.__saveArray(comp,array3d,3)
for array1d in self.__array1d:
self.__saveArray(comp,array1d,1)
for arrayInt in self.__arrayInt:
self.__unso.setArrayI(comp,arrayInt,np.arange(self.__nbody,dtype=np.int32)) # save real arrays
print("\n",file=sys.stderr)
self.__unso.save() # trigger save ops
self.__unso.close()
print("Outfile = [%s]"%(self.__model_file))
# -----------------------------------------------------
#
def __compareArray(self,comp,attr,dim,real=True):
if real:# float
data_ref=self.__dataF(self.__nbody*dim)
else: #integer
data_ref=np.arange(self.__nbody,dtype=np.int32)
ok,data=self.__unsi.getData(comp,attr)
print("%f "%(data_ref[0]),end="",file=sys.stderr)
if ok:
#print ("Checking comp[%s] attribute [%s] size [%d] "%(comp,attr,data.size),type(data),data,file=sys.stderr)
print (" <%s>"%(attr),file=sys.stderr,end="")
ok=(data_ref==data).all()
if not ok:
print("\nInconsitency: <%s> [%s]"%(comp,attr),data_ref.size,data.size,file=sys.stderr)
print(data_ref,data)
sys.exit()
# -----------------------------------------------------
#
def __compareModel(self,single=True):
"""
load model from disk and compare with generated values
"""
self.__initSeed() # reset random generator
if not os.path.isfile(self.__model_file):
print("File [%s] does not exist, aborting..\n"%(self.__model_file),file=sys.stderr)
sys.exit()
# instantiate CSnapshot object
self.__unsi=CSnapshot(self.__model_file,float32=single)
self.__unsi.nextFrame() # load snaphot
for comp in self.__comp:
print ("[%s] : "%(comp),file=sys.stderr,end="")
for array3d in self.__array3d:
self.__compareArray(comp,array3d,3)
for array1d in self.__array1d:
self.__compareArray(comp,array1d,1)
for arrayInt in self.__arrayInt:
self.__compareArray(comp,arrayInt,1,real=False)
print("\n",file=sys.stderr)
self.__unsi.close()
# -----------------------------------------------------
#
def testIO(self):
"""
test models snasphot that unsio knows to write
"""
model=["gadget3"]#,"gadget3"]
for mm in model:
print ("Testing model [%s]"%(mm),file=sys.stderr)
self.saveModel(unstype=mm,single=self.__single)
print ("\n\nComparing model [%s]"%(mm),file=sys.stderr)
self.__compareModel(single=self.__single)
# remove temporary file
#os.remove("dsdalsdjlsj")
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