Changeset 5169 for branches/uq


Ignore:
Timestamp:
Mar 26, 2015, 6:48:40 AM (9 years ago)
Author:
mmh
Message:

fix multiple simulations

Location:
branches/uq
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/uq/gui/scripts/analyzer.tcl

    r5168 r5169  
    107107itcl::body Rappture::Analyzer::constructor {tool args} {
    108108    set _tool $tool
    109     puts "Analyzer init"
     109
    110110    # use this to store all simulation results
    111111    set _resultset [Rappture::ResultSet ::\#auto]
     
    420420            return
    421421        }
    422     }
    423 
    424     lappend args -uq_type [$uq type]
    425     lappend args -uq_args [$uq args]
    426     puts "args=$args"
     422        lappend args -uq_type [$uq type]
     423        lappend args -uq_args [$uq args]
     424    }
     425
     426    puts "simulate args=$args"
    427427
    428428    # simulation is needed -- go to simulation page
     
    501501# ----------------------------------------------------------------------
    502502itcl::body Rappture::Analyzer::reset {{when -eventually}} {
     503    puts "RESET $when"
    503504    if {$when == "-eventually"} {
    504505        after cancel [list catch [itcl::code $this reset -now]]
     
    506507        return
    507508    }
     509
     510    puts "RESET contains [$_resultset contains [$_tool xml object]]"
     511    puts [$_tool xml object]
    508512
    509513    # check to see if simulation is really needed
     
    11231127    }
    11241128
    1125 
    11261129    #
    11271130    # Break up the remaining lines according to =RAPPTURE-ERROR=> messages.
  • branches/uq/gui/scripts/resultset.tcl

    r3827 r5169  
    1 # -*- mode: tcl; indent-tabs-mode: nil -*- 
     1# -*- mode: tcl; indent-tabs-mode: nil -*-
    22# ----------------------------------------------------------------------
    33#  COMPONENT: ResultSet - set of XML objects for simulated results
     
    4040    private variable _notify         ;# info used for notify command
    4141}
    42                                                                                
     42
    4343# ----------------------------------------------------------------------
    4444# CONSTRUCTOR
     
    189189# ----------------------------------------------------------------------
    190190itcl::body Rappture::ResultSet::diff {option args} {
     191    puts "ResultSet::diff $option $args"
     192    flush stdout
    191193    switch -- $option {
    192194        names {
     
    198200            }
    199201            set col [lindex $args 0]
    200 
     202            puts "col=$col"
    201203            set which "all"
    202204            if {[llength $args] > 1} {
     
    306308# ----------------------------------------------------------------------
    307309itcl::body Rappture::ResultSet::contains {xmlobj} {
     310    puts "CONTAINS [$_results size]"
    308311    # no results? then this must be new
    309312    if {[$_results size] == 0} {
     
    316319    # is a column to represent the quantity with the difference.
    317320    #
     321    puts "CONTAINS A"
    318322    set xmlobj0 [$_results get -format xmlobj end]
     323    puts "CONTAINS B $xmlobj0 , $xmlobj"
     324    puts "DIFF IS [$xmlobj0 diff $xmlobj]"
     325    flush stdout
    319326    foreach {op vpath oldval newval} [$xmlobj0 diff $xmlobj] {
     327        puts "$op $vpath $oldval $newval"
     328        flush stdout
    320329        if {[$xmlobj get $vpath.about.diffs] == "ignore"} {
    321330            continue
     
    328337        if {[$_results column names $vpath] == ""} {
    329338            # no column for this quantity yet
     339            puts "NO COLUMN"
    330340            return 0
    331341        }
    332342    }
    333 
     343    puts "CONTAINS F"
    334344    #
    335345    # If we got this far, then look through existing results for
     
    357367        }
    358368    }
    359 
     369    puts "CONTAINS Z"
    360370    # must be some differences
    361371    return 0
  • branches/uq/lang/tcl/scripts/library.tcl

    r5102 r5169  
    7070        }
    7171        set fname [file join $Rappture::installdir lib library.xml]
    72 
    7372        set fid [::open $fname r]
    7473        set info [read $fid]
     
    215214    public method remove {{path ""}}
    216215    public method xml {{path ""}}
    217     public method uq_get_vars {{template 0}}
     216    public method uq_get_vars {{tfile ""}}
    218217
    219218    public method diff {libobj}
     
    691690# ----------------------------------------------------------------------
    692691itcl::body Rappture::LibraryObj::diff {libobj} {
     692
     693    puts "Library DIFF $libobj"
    693694    set rlist ""
    694695
     
    697698    set otherv [Rappture::entities $libobj input]
    698699
     700    puts "thisv=$thisv\n"
     701    puts "otherv=$otherv\n"
     702
    699703    # scan through values for this object, and compare against other one
    700704    foreach path $thisv {
     705      puts "path=$path"
    701706        set i [lsearch -exact $otherv $path]
    702707        if {$i < 0} {
     
    704709            lappend rlist - $path $raw ""
    705710        } else {
     711          puts "tvalue=[value $this $path]"
     712          puts "ovalue=[value $libobj $path]"
    706713            foreach {traw tnorm} [value $this $path] break
    707714            foreach {oraw onorm} [value $libobj $path] break
     715            puts "tnorm=$tnorm onorm=$onorm traw=$traw oraw=$oraw"
    708716            if {![string equal $tnorm $onorm]} {
    709717                lappend rlist c $path $traw $oraw
     
    718726        lappend rlist + $path "" $oraw
    719727    }
     728    puts "LDIFF returning $rlist"
    720729    return $rlist
    721730}
     
    740749# ----------------------------------------------------------------------
    741750itcl::body Rappture::LibraryObj::value {libobj path} {
     751  puts "VALUE $path [$libobj element -as type $path]"
    742752    switch -- [$libobj element -as type $path] {
    743753        structure {
     
    771781                # then normalize to default units
    772782                set units [$libobj get $path.units]
     783                puts "val=$val units=$units"
    773784                if {"" != $units} {
    774                     set val [Rappture::Units::convert $val \
     785                    set val [Rappture::Units::mconvert $val \
    775786                        -context $units -to $units -units off]
    776787                }
     
    10401051}
    10411052
    1042 itcl::body Rappture::LibraryObj::uq_get_vars {{template 0}} {
     1053itcl::body Rappture::LibraryObj::uq_get_vars {{tfile ""}} {
    10431054    set varlist ""
    1044     set n [$_node selectNodes /run/input//number]
     1055
     1056    if {$tfile == ""} {
     1057        set node $_node
     1058    } else {
     1059        set fid [::open $tfile r]
     1060        set doc [dom parse [read $fid]]
     1061        set node [$doc documentElement]
     1062        close $fid
     1063    }
     1064
     1065    set n [$node selectNodes /run/input//number]
    10451066    foreach _n $n {
    10461067        set x [$_n selectNodes current/text()]
     
    10541075                -context $units -to $units -units off]
    10551076            }
    1056             if {$template != 0} {
     1077            if {$tfile != ""} {
    10571078              $x nodeValue @@[$_n getAttribute id]
    10581079            }
     
    10601081        }
    10611082    }
     1083
     1084    if {$tfile != ""} {
     1085        set fid [open $tfile w]
     1086        puts $fid "<?xml version=\"1.0\"?>"
     1087        puts $fid [$node asXML]
     1088        close $fid
     1089        $doc delete
     1090    }
    10621091    return $varlist
    10631092}
  • branches/uq/lang/tcl/scripts/task.tcl

    r5168 r5169  
    173173    # if there are any args, use them to override parameters
    174174    set _outputcb ""
     175    set uq_type ""
    175176    foreach {path val} $args {
    176177        if {$path == "-output"} {
     
    211212    } result]
    212213
    213     # This will turn the driver xml into a template
    214     # and return a list of the UQ variables and their PDFs.
    215     set uq_varlist [$_xmlobj uq_get_vars 1]
    216 
    217     if {$uq_varlist != ""} {
    218         # write out the template file for submit
     214    if {$uq_type != ""} {
     215        # Copy xml into a new file
    219216        set tfile "template[pid].xml"
    220         set status [catch {
    221             set fid [open $tfile w]
    222             puts $fid "<?xml version=\"1.0\"?>"
    223             puts $fid [$_xmlobj xml]
    224             close $fid
    225         } result]
     217        set fid [open $tfile w]
     218        puts $fid "<?xml version=\"1.0\"?>"
     219        puts $fid [$_xmlobj xml]
     220        close $fid
     221
     222        # Return a list of the UQ variables and their PDFs.
     223        # Also turns $tfile into a template file.
     224        set uq_varlist [$_xmlobj uq_get_vars $tfile]
    226225    }
    227226
     
    239238        }
    240239
    241         if {$uq_varlist == ""} {
     240        if {$uq_type == ""} {
    242241            regsub -all @driver $cmd $file cmd
    243242
     
    404403    if {$status == 0} {
    405404        set result [string trim $job(output)]
    406         puts "result=$result"
    407         if {$uq_varlist != ""} {
     405
     406        if {$uq_type != ""} {
    408407            # UQ. Collect data from all jobs and put it in one xml run file.
    409             file delete -force -- run_uq.xml
    410             exec puq_analyze.py puq_[pid].hdf5
    411             append result "\n" "=RAPPTURE-RUN=>run_uq.xml"
     408            #file delete -force -- run_uq.xml
     409            set res [exec puq_analyze.py puq_[pid].hdf5]
     410            append result "\n" $res
    412411        }
    413412        if {[regexp {=RAPPTURE-RUN=>([^\n]+)} $result match file]} {
  • branches/uq/puq/get_params.py

    r5168 r5169  
    5555# This just saves PUQ state into HDF5 file, so later we can have PUQ analyze
    5656# the results and it knows about the input parameters, UQ method, etc.
    57 sw = Sweep(uq, RapptureHost('puq', dfile), None)
     57host = RapptureHost('puq', dfile)
     58sw = Sweep(uq, host, None)
     59host.psweep = sw.psweep
    5860sw.run(hname, overwrite=True)
    5961
  • branches/uq/puq/puq_analyze.py

    r5168 r5169  
    1111from puq.jpickle import unpickle
    1212import xml.etree.ElementTree as xml
     13from itertools import product, combinations
    1314
    1415# Redirect stdout and stderr to files for debugging.
     
    1617sys.stdout = open("uq_debug.out", 'a')
    1718sys.stderr = open("uq_debug.err", 'a')
     19
    1820
    1921# Restore the state of a PUQ session from a HDF5 file.
     
    2931        sw.psweep.reinit()
    3032    return sw
     33
     34
     35# do some name substitution for better labels
     36def subs_names(varl):
     37    varlist = []
     38    for v in varl:
     39        varlist.append(str(v[0]))
     40    return varlist
     41
     42
     43def plot_resp1(io, resp, name):
     44    print 'plot_resp1', name
     45    varlist = subs_names(resp.vars)
     46    curve = 'output.curve(response-%s)' % name
     47    io.put(curve + '.about.label', '%s Response for %s' % (name, varlist[0]))
     48    io.put(curve + '.xaxis.label', varlist[0])
     49    io.put(curve + '.yaxis.label', name)
     50    x = np.linspace(*resp.vars[0][1], num=50)
     51    y = np.array(resp.eval(x))
     52    for a, b in zip(x, y):
     53        io.put(curve + '.component.xy', "%s %s\n" % (a, b), append=1)
     54
     55
     56def plot_resp2(io, resp, name, varl):
     57    numpoints = 50
     58    m2d = 'output.mesh('+varl[0][0]+varl[1][0]+')'
     59    io.put(m2d+'.about.label', '2D Mesh')
     60    io.put(m2d+'.dim', '2')
     61    io.put(m2d+'.hide', 'yes')
     62    io.put(m2d+'.grid.xaxis.numpoints', '%s' % numpoints)
     63    io.put(m2d+'.grid.yaxis.numpoints', '%s' % numpoints)
     64    io.put(m2d+'.grid.xaxis.min', '%g' % (varl[0][1][0]))
     65    io.put(m2d+'.grid.xaxis.max', '%g' % (varl[0][1][1]))
     66    io.put(m2d+'.grid.yaxis.min', '%g' % (varl[1][1][0]))
     67    io.put(m2d+'.grid.yaxis.max', '%g' % (varl[1][1][1]))
     68
     69    varlist = subs_names(varl)
     70    fname = 'output.field(response-'+name+varlist[0]+varlist[1]+')'
     71    io.put(fname + '.about.xaxis.label', varlist[0])
     72    io.put(fname + '.about.yaxis.label', varlist[1])
     73    title = name + ' Response (%s)' % ' vs '.join(varlist)
     74    io.put(fname + '.about.label', title)
     75    io.put(fname + '.component.mesh', m2d)
     76    io.put(fname + '.about.view', 'heightmap')
     77    x = np.linspace(*varl[0][1], num=numpoints)
     78    y = np.linspace(*varl[1][1], num=numpoints)
     79    pts = np.array([(b,a) for a,b in product(y,x)])
     80    allpts = np.empty((numpoints**2, len(resp.vars)))
     81    for i, v in enumerate(resp.vars):
     82        if v[0] == varl[0][0]:
     83            allpts[:,i] = pts[:,0]
     84        elif v[0] == varl[1][0]:
     85            allpts[:,i] = pts[:,1]
     86        else:
     87            allpts[:,i] = np.mean(v[1])
     88    pts = np.array(resp.evala(allpts))
     89    for z in pts:
     90        io.put(fname + '.component.values', '%g\n' % z, append=1)
     91
     92def plot_resp3(io, resp, name, varl):
     93    numpoints = 40
     94    m3d = 'output.mesh(m3d)'
     95    io.put(m3d+'.about.label', '3D Mesh')
     96    io.put(m3d+'.dim', '3')
     97    io.put(m3d+'.hide', 'yes')
     98    io.put(m3d+'.grid.xaxis.numpoints', '%s' % numpoints)
     99    io.put(m3d+'.grid.yaxis.numpoints', '%s' % numpoints)
     100    io.put(m3d+'.grid.zaxis.numpoints', '%s' % numpoints)
     101
     102    # for now, scale to [0,1]
     103    io.put(m3d+'.grid.xaxis.min', '0')
     104    io.put(m3d+'.grid.xaxis.max', '1')
     105    io.put(m3d+'.grid.yaxis.min', '0')
     106    io.put(m3d+'.grid.yaxis.max', '1')
     107    io.put(m3d+'.grid.zaxis.min', '0')
     108    io.put(m3d+'.grid.zaxis.max', '1')
     109
     110    varlist = subs_names(varl)
     111    fname = 'output.field(response-'+name+varlist[0]+varlist[1]+varlist[2]+')'
     112    io.put(fname + '.about.xaxis.label',
     113        '{%s [%.3g - %.3g]}' %
     114        (varlist[0], varl[0][1][0], varl[0][1][1]))
     115    io.put(fname + '.about.yaxis.label',
     116        '{%s [%.3g - %.3g]}' %
     117        (varlist[1], varl[1][1][0], varl[1][1][1]))
     118    io.put(fname + '.about.zaxis.label',
     119        '{%s [%.3g - %.3g]}' %
     120        (varlist[2], varl[2][1][0], varl[2][1][1]))
     121
     122    title = name + ' Response (%s)' % ' vs '.join(varlist)
     123    io.put(fname + '.about.label', title)
     124    io.put(fname + '.component.mesh', m3d)
     125    #io.put(fname + '.about.description', '3D Field Description')
     126    io.put(fname + '.about.view', 'vtkvolume')
     127    x = np.linspace(*varl[0][1], num=numpoints)
     128    y = np.linspace(*varl[1][1], num=numpoints)
     129    z = np.linspace(*varl[2][1], num=numpoints)
     130
     131    # generate points in the right order, with x changing fastest
     132    pts = np.array([(c,b,a) for a,b,c in product(z,y,x)])
     133    allpts = np.empty((numpoints**3, len(resp.vars)))
     134    for i, v in enumerate(resp.vars):
     135        if v[0] == varl[0][0]:
     136            allpts[:,i] = pts[:,0]
     137        elif v[0] == varl[1][0]:
     138            allpts[:,i] = pts[:,1]
     139        elif v[0] == varl[2][0]:
     140            allpts[:,i] = pts[:,2]
     141        else:
     142            allpts[:,i] = np.mean(v[1])
     143    pts = np.array(resp.evala(allpts))
     144    for z in pts:
     145        io.put(fname + '.component.values', '%g\n' % z, append=1)
     146
     147
     148def plot_resp(io, resp, name):
     149    if resp is not None:
     150        if len(resp.vars) == 1:
     151            plot_resp1(io, resp, name)
     152            return
     153        if len(resp.vars) >= 3:
     154            for v1, v2, v3 in combinations(resp.vars, 3):
     155                plot_resp3(io, resp, name, [v1,v2,v3])
     156
     157        # plot all combinations of 2 variables
     158        for v1, v2 in combinations(resp.vars, 2):
     159            plot_resp2(io, resp, name, [v1,v2])
    31160
    32161
     
    68197
    69198
     199
    70200def dist(x1,y1, x2,y2, x3,y3): # x3,y3 is the point
    71201    diffx = x2-x1
     
    87217
    88218    return np.sqrt(dx*dx + dy*dy)
     219
    89220
    90221# Plots advanced probability curves
     
    220351#    plot_pdf_acurve(h5, acurves, vname, 95, desc)
    221352
     353
     354# If more than one variable, display sensitivity.
     355# Curves have indexed variables, so skip them.
     356if ['[' in x for x in h5[uqtype]].count(False):
     357    for v in h5[uqtype]:
     358        if '[' in v:
     359            continue
     360        lab = h5['/output/data/%s' % v].attrs['description']
     361        sens = unpickle(h5['/%s/%s/sensitivity' % (uqtype, v)].value)
     362        elem = xml.SubElement(dout, 'histogram', {'id': 'sens-%s' % v})
     363        about = xml.SubElement(elem, 'about')
     364        xml.SubElement(about, 'label').text = '%s Sensitivity' % lab
     365        xml.SubElement(elem, 'type').text = 'scatter'
     366        xaxis = xml.SubElement(elem, 'xaxis')
     367        xml.SubElement(xaxis, 'label').text = 'Parameters'
     368        yaxis = xml.SubElement(elem, 'yaxis')
     369        xml.SubElement(yaxis, 'label').text = 'Sensitivity'
     370        comp = xml.SubElement(elem, 'component')
     371        xy = xml.SubElement(comp, 'xy')
     372        pts = ''
     373        for name in sens:
     374            n = name[0]
     375            try:
     376                n = h5['/input/params/%s' % n].attrs['label']
     377            except:
     378                pass
     379            pts += "%s %s\n" % (n, name[1]['ustar'])
     380        xy.text = pts
     381
    222382with open('run_uq.xml', 'w') as f:
    223383    f.write("<?xml version=\"1.0\"?>\n")
    224384    dtree.write(f)
    225385
    226 with open('run_uq2.xml', 'w') as f:
    227     f.write("<?xml version=\"1.0\"?>\n")
    228     dtree.write(f)
     386
     387import Rappture
     388io = Rappture.library('run_uq.xml')
     389
     390for v in h5[uqtype]:
     391    if '[' in v:
     392        continue
     393    rs = unpickle(h5['/%s/%s/response' % (uqtype, v)].value)
     394    print 'Plotting response', v
     395    plot_resp(io, rs, v)
     396
     397Rappture.result(io)
     398
    229399h5.close()
Note: See TracChangeset for help on using the changeset viewer.