diff --git a/docs/cdens.md b/docs/cdens.md index 5210e27..8d9832b 100644 --- a/docs/cdens.md +++ b/docs/cdens.md @@ -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 diff --git a/numjuggler/main.py b/numjuggler/main.py index bf3c62f..2224020 100644 --- a/numjuggler/main.py +++ b/numjuggler/main.py @@ -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='') diff --git a/numjuggler/mapparsers.py b/numjuggler/mapparsers.py index b142105..5efb844 100644 --- a/numjuggler/mapparsers.py +++ b/numjuggler/mapparsers.py @@ -6,6 +6,7 @@ # Parsers of map files for different modes + def lines(fname): """ Iterator over meaningful lines in the map file. @@ -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): @@ -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 diff --git a/travis_run_all.sh b/travis_run_all.sh index 96c931d..e916d53 100755 --- a/travis_run_all.sh +++ b/travis_run_all.sh @@ -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 @@ -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 diff --git a/travis_tests/cdens/inp.map1.ref b/travis_tests/cdens/inp.map1.ref index 67463c4..6ef6d68 100644 --- a/travis_tests/cdens/inp.map1.ref +++ b/travis_tests/cdens/inp.map1.ref @@ -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 diff --git a/travis_tests/cdens/inp.map2.ref b/travis_tests/cdens/inp.map2.ref index 3fda1a6..a8b0ca3 100644 --- a/travis_tests/cdens/inp.map2.ref +++ b/travis_tests/cdens/inp.map2.ref @@ -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 diff --git a/travis_tests/cdens/inp.map3.ref b/travis_tests/cdens/inp.map3.ref index eea8c9c..ae0cfea 100644 --- a/travis_tests/cdens/inp.map3.ref +++ b/travis_tests/cdens/inp.map3.ref @@ -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 diff --git a/travis_tests/cdens/inp.map4.ref b/travis_tests/cdens/inp.map4.ref index 23cbcd7..2beaae3 100644 --- a/travis_tests/cdens/inp.map4.ref +++ b/travis_tests/cdens/inp.map4.ref @@ -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 diff --git a/travis_tests/cdens/inp.map5.ref b/travis_tests/cdens/inp.map5.ref index 23cbcd7..2beaae3 100644 --- a/travis_tests/cdens/inp.map5.ref +++ b/travis_tests/cdens/inp.map5.ref @@ -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 diff --git a/travis_tests/cdens/inp.map6.ref b/travis_tests/cdens/inp.map6.ref index bdfc687..fe03e63 100644 --- a/travis_tests/cdens/inp.map6.ref +++ b/travis_tests/cdens/inp.map6.ref @@ -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 diff --git a/travis_tests/cdens/map1 b/travis_tests/cdens/map1 index 049ddf2..02f91b8 100644 --- a/travis_tests/cdens/map1 +++ b/travis_tests/cdens/map1 @@ -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