Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
LAMBERT Jean-charles
uns_projects
Commits
5a6adb65
Commit
5a6adb65
authored
Oct 02, 2017
by
LAMBERT Jean-charles
Browse files
more work on remove halo...
parent
7b521f8b
Changes
4
Hide whitespace changes
Inline
Side-by-side
py/mains/remove_sim_halo.py
View file @
5a6adb65
...
...
@@ -8,29 +8,25 @@ sys.path=['/home/jcl/works/GIT/uns_projects/py/modules/','/home/jcl/works/GIT/un
from
py_unsio
import
*
from
uns_simu
import
*
import
argparse
import
os
,
subprocess
from
simulations.creducesim
import
*
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# commandLine, parse the command line
def
commandLine
():
dbname
=
None
modulo
=
10
test
=
False
# help
parser
=
argparse
.
ArgumentParser
(
description
=
"Remove halo from simulation"
,
parser
=
argparse
.
ArgumentParser
(
description
=
"Remove halo from
a
simulation"
,
formatter_class
=
argparse
.
ArgumentDefaultsHelpFormatter
)
# options
parser
.
add_argument
(
'simname'
,
help
=
'UNS Simulation name'
)
parser
.
add_argument
(
'--dir'
,
help
=
'directory to store new files'
,
default
=
None
)
parser
.
add_argument
(
'--keep'
,
help
=
'keep halo every frequency'
,
default
=
modulo
,
type
=
int
)
parser
.
add_argument
(
'--overwrite'
,
help
=
'overwrite new frame if present'
,
default
=
False
)
parser
.
add_argument
(
'--test'
,
help
=
'test without doing anything'
,
dest
=
"test"
,
action
=
"store_true"
,
default
=
test
)
parser
.
add_argument
(
'--keep'
,
help
=
'keep halo every frequency'
,
default
=
10
,
type
=
int
)
parser
.
add_argument
(
'--overwrite'
,
help
=
'overwrite new frame if present'
,
dest
=
"overwrite"
,
action
=
"store_true"
,
default
=
False
)
parser
.
add_argument
(
'--test'
,
help
=
'test without doing anything'
,
dest
=
"test"
,
action
=
"store_true"
,
default
=
False
)
parser
.
add_argument
(
'--dbname'
,
help
=
'UNS database file name'
,
default
=
dbname
)
parser
.
add_argument
(
'--verbose'
,
help
=
'verbose mode'
,
dest
=
"verbose"
,
action
=
"store_true"
,
default
=
False
)
# parse
...
...
@@ -49,61 +45,6 @@ def process(args):
else
:
simu
.
resizeSim
()
# -----------------------------------------------------
# process, is the core function
def
process2
(
args
):
try
:
simu
=
UnsSimu
(
args
.
simname
,
verbose
=
args
.
verbose
)
except
Exception
as
x
:
print
(
x
.
message
)
else
:
info
=
simu
.
getInfo
()
# get simulation info
simname
=
info
[
"name"
]
simtype
=
(
info
[
"dir"
]).
split
(
"/"
)[
2
]
list
=
simu
.
getSnapshotList
()
# get real path even if there is a link
# remove /net/direct in case mounted
realpath
=
(
os
.
path
.
realpath
(
list
[
0
])).
replace
(
"/net/direct"
,
""
)
print
(
"Real Path [%s]"
%
(
realpath
),
file
=
sys
.
stderr
)
if
args
.
dir
is
None
:
# get file system root
inrootdir
=
realpath
.
split
(
"/"
)[
1
]
print
(
"inrootdir = [%s] <%s>"
%
(
inrootdir
,
simtype
),
file
=
sys
.
stderr
)
args
.
dir
=
"/"
+
inrootdir
+
"/"
+
simtype
+
"2/"
+
simname
+
"/SNAPS"
print
(
"target dir <%s>"
%
(
args
.
dir
),
file
=
sys
.
stderr
)
# check dir
if
not
os
.
path
.
isdir
(
args
.
dir
):
print
(
"Create directory [%s]"
%
(
args
.
dir
),
file
=
sys
.
stderr
)
try
:
if
not
args
.
test
:
os
.
makedirs
(
args
.
dir
)
except
:
print
(
"Unable to create directory [%s]"
%
(
args
.
dir
))
sys
.
exit
()
cpt
=
1
for
snap
in
list
:
print
(
snap
,
file
=
sys
.
stderr
)
if
cpt
%
args
.
keep
==
1
:
select
=
"all"
else
:
select
=
"gas,stars,disk,bndry,bulge"
pass
basename
=
os
.
path
.
basename
(
snap
)
# input
newsnap
=
args
.
dir
+
"/"
+
basename
# output
# check output file
if
not
os
.
path
.
isfile
(
newsnap
)
or
args
.
overwrite
:
cmd
=
"uns2uns %s %s select=%s type=gadget3"
%
(
snap
,
newsnap
,
select
)
print
(
"<%s>"
%
(
cmd
),
file
=
sys
.
stderr
)
if
not
args
.
test
:
subprocess
.
call
([
"uns2uns"
,
snap
,
newsnap
,
"select="
+
select
,
"type=gadget3"
])
cpt
+=
1
# -----------------------------------------------------
# main program
if
__name__
==
'__main__'
:
...
...
py/mains/test_unsio.py
View file @
5a6adb65
...
...
@@ -24,6 +24,8 @@ def commandLine():
# 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
)
parser
.
add_argument
(
'--double'
,
help
=
'test with double real'
,
dest
=
"double"
,
action
=
"store_true"
,
default
=
False
)
parser
.
add_argument
(
'--uns2uns'
,
help
=
'save intermediate file with uns2uns'
,
dest
=
"uns2uns"
,
action
=
"store_true"
,
default
=
False
)
# parse
args
=
parser
.
parse_args
()
...
...
@@ -33,7 +35,7 @@ def commandLine():
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# commandLine, parse the command line
def
process
(
args
):
uns
=
CTestunsio
(
nbody
=
args
.
nbody
)
uns
=
CTestunsio
(
nbody
=
args
.
nbody
,
single
=
not
args
.
double
,
uns2uns
=
args
.
uns2uns
)
#uns.saveModel("")
uns
.
testIO
()
...
...
py/modules/simulations/creducesim.py
View file @
5a6adb65
...
...
@@ -2,7 +2,7 @@
from
__future__
import
print_function
from
uns_simu
import
*
import
os
,
sys
import
os
,
sys
,
subprocess
#
# class CReducesim
...
...
py/modules/simulations/ctestunsio.py
View file @
5a6adb65
...
...
@@ -5,9 +5,11 @@ from uns_simu import *
import
os
,
sys
import
numpy
as
np
import
tempfile
from
py_unsio
import
*
import
py_unsio
as
unsio
from
csnapshot
import
*
import
subprocess
#
# class CTestunsio
#
...
...
@@ -16,19 +18,22 @@ 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"
]
__
3D
=
[
3
,
"pos"
,
"vel"
,
"acc
"
]
__
1D
=
[
1
,
"mass"
,
"pot
"
]
__
1Dgas
=
[
1
,
"rho"
,
"hsml"
,
"metal
"
]
__
1Dstars
=
[
1
,
"metal"
,
"age"
]
__
1Dint
=
[
1
,
"id"
]
__comp
=
{
'halo'
:[
__1D
,
__3D
],
'gas'
:[
__1D
,
__3D
,
__1Dgas
],
'stars'
:[
__1D
,
__3D
,
__1Dstars
],
'disk'
:[
__1D
,
__3D
],
'bndry'
:[
__1D
,
__3D
],
'bulge'
:[
__1D
,
__3D
]}
__compNemo
=
{
'all'
:[
__1D
,
__3D
,
__1Dgas
]
}
# -----------------------------------------------------
#
def
__init__
(
self
,
nbody
=
None
,
seed
=
666
,
single
=
True
,
verbose
=
None
):
def
__init__
(
self
,
nbody
=
None
,
seed
=
666
,
single
=
True
,
verbose
=
None
,
uns2uns
=
False
):
self
.
__nbody
=
nbody
self
.
__verbose
=
verbose
self
.
__seed
=
seed
self
.
__single
=
single
self
.
__uns2uns
=
uns2uns
self
.
__initSeed
(
self
.
__seed
)
...
...
@@ -50,13 +55,13 @@ class CTestunsio:
return
x
# -----------------------------------------------------
#
def
__saveArray
(
self
,
comp
,
attr
,
dim
):
def
__saveArray
(
self
,
comp
,
attr
,
dim
,
real
=
True
):
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
=
""
)
if
real
:
data
=
self
.
__dataF
(
self
.
__nbody
*
dim
)
ok
=
self
.
__unso
.
setArrayF
(
comp
,
attr
,
data
)
# save real arrays
else
:
ok
=
self
.
__unso
.
setArrayI
(
comp
,
attr
,
np
.
arange
(
self
.
__nbody
*
dim
,
dtype
=
np
.
int32
))
# save real arrays
# -----------------------------------------------------
#
...
...
@@ -74,33 +79,60 @@ class CTestunsio:
else
:
self
.
__model_file
=
filename
fff
=
self
.
__model_file
## SAVE FILE
# instantiate output object
if
(
single
):
print
(
"SINGLE"
)
self
.
__unso
=
CunsOut
(
self
.
__model_file
,
unstype
);
# output file
print
(
"SINGLE
precision floating values
"
)
self
.
__unso
=
unsio
.
CunsOut
(
self
.
__model_file
,
unstype
);
# output file
else
:
print
(
"DOUBLE"
)
self
.
__unso
=
CunsOutD
(
self
.
__model_file
,
unstype
);
# output file
print
(
"DOUBLE
precision floating values
"
)
self
.
__unso
=
unsio
.
CunsOutD
(
self
.
__model_file
,
unstype
);
# output file
print
(
"
\n
Saving 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
)
select_comp
=
self
.
__comp
# comp for gadget2 gadget3
if
unstype
==
"nemo"
:
select_comp
=
self
.
__compNemo
# comp for nemo
for
comp
,
all_array
in
select_comp
.
iteritems
():
print
(
"[%-6s] : "
%
(
comp
),
file
=
sys
.
stderr
,
end
=
""
)
# save reals array
for
block_array
in
all_array
:
dim
=
block_array
[
0
]
for
array
in
block_array
[
1
:]:
print
(
" %s"
%
(
array
),
end
=
""
)
self
.
__saveArray
(
comp
,
array
,
dim
,
real
=
True
)
# save real
# save integer arrays
dim
=
self
.
__1Dint
[
0
]
for
array
in
self
.
__1Dint
[
1
:]:
print
(
" %s"
%
(
array
),
end
=
""
)
self
.
__saveArray
(
comp
,
array
,
dim
,
real
=
False
)
# save integer
print
(
"
\n
"
)
self
.
__unso
.
save
()
# trigger save ops
self
.
__unso
.
close
()
if
self
.
__uns2uns
:
# test uns2uns
ff
=
tempfile
.
NamedTemporaryFile
()
myfile
=
ff
.
name
ff
.
close
()
if
single
:
outfloat
=
"float=t"
else
:
outfloat
=
"float=f"
cmd
=
"uns2uns in=%s out=%s select=%s type=%s %s"
%
(
self
.
__model_file
,
myfile
,
"all"
,
unstype
,
outfloat
)
print
(
"<%s>"
%
(
cmd
),
file
=
sys
.
stderr
)
#subprocess.call([cmd],shell=True)
subprocess
.
call
([
"uns2uns"
,
"in="
+
self
.
__model_file
,
"out="
+
myfile
,
"select=all"
,
"type="
+
unstype
,
outfloat
])
#sys.exit()
os
.
remove
(
self
.
__model_file
)
self
.
__model_file
=
myfile
print
(
"Outfile = [%s]"
%
(
self
.
__model_file
))
...
...
@@ -115,10 +147,10 @@ class CTestunsio:
ok
,
data
=
self
.
__unsi
.
getData
(
comp
,
attr
)
print
(
"%f "
%
(
data_ref
[
0
]),
end
=
""
,
file
=
sys
.
stderr
)
#
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
=
""
)
#
print (" <%s>"%(attr),file=sys.stderr,end="")
ok
=
(
data_ref
==
data
).
all
()
if
not
ok
:
print
(
"
\n
Inconsitency: <%s> [%s]"
%
(
comp
,
attr
),
data_ref
.
size
,
data
.
size
,
file
=
sys
.
stderr
)
...
...
@@ -127,7 +159,7 @@ class CTestunsio:
# -----------------------------------------------------
#
def
__compareModel
(
self
,
single
=
True
):
def
__compareModel
(
self
,
unstype
=
"gadget3"
,
single
=
True
):
"""
load model from disk and compare with generated values
"""
...
...
@@ -140,32 +172,43 @@ class CTestunsio:
# 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
)
select_comp
=
self
.
__comp
# comp for gadget2 gadget3
if
unstype
==
"nemo"
:
select_comp
=
self
.
__compNemo
# comp for nemo
for
comp
,
all_array
in
select_comp
.
iteritems
():
print
(
"checking [%-6s] : "
%
(
comp
),
file
=
sys
.
stderr
,
end
=
""
)
# compare real array
for
block_array
in
all_array
:
dim
=
block_array
[
0
]
for
array
in
block_array
[
1
:]:
print
(
" %s"
%
(
array
),
end
=
""
)
self
.
__compareArray
(
comp
,
array
,
dim
,
real
=
True
)
# compare integer arrays
dim
=
self
.
__1Dint
[
0
]
for
array
in
self
.
__1Dint
[
1
:]:
print
(
" %s"
%
(
array
),
end
=
""
)
self
.
__compareArray
(
comp
,
array
,
dim
,
real
=
False
)
print
(
"
\n
"
)
self
.
__unsi
.
close
()
# -----------------------------------------------------
#
def
testIO
(
self
):
"""
test models snasphot that unsio knows
to writ
e
test models snasphot that unsio knows
howto dump on a fil
e
"""
model
=
[
"gadget3"
]
#
,"gadget
3
"]
model
=
[
"gadget3"
,
"gadget
2"
,
"nemo
"
]
for
mm
in
model
:
print
(
"Testing model [%s]"
%
(
mm
),
file
=
sys
.
stderr
)
self
.
saveModel
(
unstype
=
mm
,
single
=
self
.
__single
)
print
(
"
\n\n
Comparing model [%s]"
%
(
mm
),
file
=
sys
.
stderr
)
self
.
__compareModel
(
single
=
self
.
__single
)
self
.
__compareModel
(
unstype
=
mm
,
single
=
self
.
__single
)
# remove temporary file
#
os.remove(
"dsdalsdjlsj"
)
os
.
remove
(
self
.
__model_file
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment