Skip to content

Commit

Permalink
Merge pull request inr-kit#7 from travleev/issue5
Browse files Browse the repository at this point in the history
Issue5
  • Loading branch information
travleev authored May 26, 2020
2 parents 9a37538 + dcee1ea commit d922abb
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 36 deletions.
6 changes: 6 additions & 0 deletions docs/cdens.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ The content of `densities.txt`:
m 5: 1e-3 # For all cells filled with material 5 multiply density by 1e-3


UPD 2020.05.22: format specifier can be added as the last entry on the line in the map file. The format specifier is passed as a string and its `.format()` method is used
to convert density to string representation. For example (compare with above):

c 1 -- 10: 0.01 {:7.3f} # Specify format for new densities
c 12: 0.1 # Use default formatting

m 5: 1e-3 {:10.3e} # Specify format
14 changes: 8 additions & 6 deletions numjuggler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,21 +262,23 @@ def main(args=sys.argv[1:]):
t, r = tr
if t == 'c' and c.name in r:
dorig = c.get_d()
dnew = dorig * m[tr]
c.set_d(str(dnew))
coef, fmt = m[tr]
dnew = dorig * coef
c.set_d(fmt.format(dnew))
modified = True
if t == 'm' and c.get_m() in r:
dorig = c.get_d()
dnew = dorig * m[tr]
c.set_d(str(dnew))
coef, fmt = m[tr]
dnew = dorig * coef
c.set_d(fmt.format(dnew))
modified = True
if not modified and mdef:
# If no rules to modify density found, apply the
# default:
dnew = c.get_d()
for t, val in mdef.items():
for t, (val, fmt) in mdef.items():
dnew *= val
c.set_d(str(dnew))
c.set_d(fmt.format(dnew))
print(c.card(), end='')


Expand Down
18 changes: 13 additions & 5 deletions numjuggler/mapparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# Parsers of map files for different modes


def lines(fname):
"""
Iterator over meaningful lines in the map file.
Expand All @@ -17,10 +18,17 @@ def lines(fname):
ll = l.split('#')[0].strip()
if ll and ':' in ll:
t = ll[0] # 1-st entry, c, u, m etc
ranges, val = ll[1:].split(':')
ranges, val = ll[1:].split(':', 1)
ranges = ranges.strip()
rr = list(_get_map_ranges(ranges))
yield t, rr, val

# TODO: extract format from val, if val has more than 1 entry
if '{' in val:
val, fmt = val.split('{')
fmt = '{' + fmt
else:
fmt = '{:10.3e}' # default formatting for density
yield t, rr, val, fmt


def cdens(fname):
Expand All @@ -29,10 +37,10 @@ def cdens(fname):
"""
res = OrderedDict()
resdef = {}
for t, rr, val in lines(fname):
for t, rr, val, fmt in lines(fname):
for r in rr:
res[(t, r)] = float(val)
res[(t, r)] = float(val), fmt
# rr can be an empty list that means the default rule
if len(rr) == 0:
resdef[t] = float(val)
resdef[t] = float(val), fmt
return res, resdef
4 changes: 4 additions & 0 deletions travis_run_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# Assuming that numjuggler is installed properly
# and tests are in travis_tests(see .travis.yml)

# Check numjuggler version
numjuggler --version

odir=$(pwd)

cd $odir/travis_tests/renum
Expand All @@ -19,5 +22,6 @@ cd $odir/travis_tests/cdens
i=inp.i
o="--mode cdens"
for m in $(ls map?); do
echo $m
numjuggler $o --map $m inp.i > inp.$m && diff -w inp.$m.ref inp.$m > $m.diff || exit 1
done
8 changes: 4 additions & 4 deletions travis_tests/cdens/inp.map1.ref
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Test density changes for cells and materials
1 0 -1
2 1 0.1 -1
3 1 -0.011 -1
4 2 0.2 -1
5 2 -220.0 -1
2 1 0.100 -1
3 1 -1.100e-02 -1
4 2 2.000e-01 -1
5 2 -2.200e+02 -1

c surfaces
1 so 10
Expand Down
6 changes: 3 additions & 3 deletions travis_tests/cdens/inp.map2.ref
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Test density changes for cells and materials
1 0 -1
2 1 0.01 -1
3 1 -0.0011 -1
4 2 -0.002 -1
2 1 1.000e-02 -1
3 1 -1.100e-03 -1
4 2 -2.000e-03 -1
5 2 -22.0 -1

c surfaces
Expand Down
8 changes: 4 additions & 4 deletions travis_tests/cdens/inp.map3.ref
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Test density changes for cells and materials
1 0 -1
2 1 1.0 -1
3 1 -1.1 -1
4 2 20.0 -1
5 2 -22.0 -1
2 1 1.000e+00 -1
3 1 -1.100e+00 -1
4 2 2.000e+01 -1
5 2 -2.200e+01 -1

c surfaces
1 so 10
Expand Down
8 changes: 4 additions & 4 deletions travis_tests/cdens/inp.map4.ref
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Test density changes for cells and materials
1 0 -1
2 1 0.1 -1
3 1 -0.11 -1
4 2 2.0 -1
5 2 -2.2 -1
2 1 1.000e-01 -1
3 1 -1.100e-01 -1
4 2 2.000e+00 -1
5 2 -2.200e+00 -1

c surfaces
1 so 10
Expand Down
8 changes: 4 additions & 4 deletions travis_tests/cdens/inp.map5.ref
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Test density changes for cells and materials
1 0 -1
2 1 0.1 -1
3 1 -0.11 -1
4 2 2.0 -1
5 2 -2.2 -1
2 1 1.000e-01 -1
3 1 -1.100e-01 -1
4 2 2.000e+00 -1
5 2 -2.200e+00 -1

c surfaces
1 so 10
Expand Down
8 changes: 4 additions & 4 deletions travis_tests/cdens/inp.map6.ref
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Test density changes for cells and materials
1 0 -1
2 1 0.1 -1
3 1 -0.11 -1
4 2 200.0 -1
5 2 -220.0 -1
2 1 1.000e-01 -1
3 1 -1.100e-01 -1
4 2 2.000e+02 -1
5 2 -2.200e+02 -1

c surfaces
1 so 10
Expand Down
4 changes: 2 additions & 2 deletions travis_tests/cdens/map1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Modify all cells
c 1 -- 2 : 0.1
c 3 -- 4 : 0.01
c 1 -- 2 : 0.1 {:7.3f}
c 3 -- 4 : 0.01 {:10.3e}
c: 10.0

0 comments on commit d922abb

Please sign in to comment.