Example #1
0
def runtests(testname='interface 1', plotsoln=False):
#-------------------------------------
    '''
    ================================
    Main code to run a set of tests:
    ================================
    '''

    if plotsoln:
        from plotclaw import plotclaw
    
    # set data values:
    data = ClawData()
    parms= ClawData()
    data.tfinal = 8     # final time
    data.ic =  9           #C^5 Newton polynomial initial condition
    data.a  =  1.0         #Pulse half-width
    data.x0 = -4.0         #Pulse half-width
    data.xlower = -10.0
    data.xupper =  10.0
    data.mthbc_xlower = 2
    data.mthbc_xupper = 2
    data.mthbc=[2,2]
    parms.time_integrator = 4
    parms.cfl_max         = 2.9
    parms.cfl_desired     = 2.85

    if plotsoln:
        data.nout = 10          # output at several times for plotting
        data.write('setplot1.data')
    else:
        data.nout = 1          # only output at final time

    if testname=='homogeneous':
        # Homogenous medium
        data.ninter=1
        data.interface=0.0
        data.c = 1.0,1.0
        data.rho = 1.0,1.0
        data.write('setprob.data')
        data.write('sharpclaw.data')
        #Set parameters that are uniform for all runs
        parms.char_decomp     = 0
        parms.lim_type        = 0
        parms.write('sharpclaw.data')
        parms.write('claw.data')

        #Set run-specific parameters
        runnames=['TVD2','WENO5','UC7','ClawPack']
        runparms={}
        for name in runnames: 
            runparms[name]=ClawData()
            runparms=setparms(runparms,name)
        mxvals = array([200,400,800,1600,3200])        # mx values 

    elif testname=='interface 1':
        # Weak interface
        data.ninter=1
        data.interface=0.0
        data.c = 1.0,0.5
        data.rho = 1.0,4.0
        data.write('setprob.data')
        data.write('sharpclaw.data')
        #Set parameters that are uniform for all runs
        parms.char_decomp     = 0
        parms.lim_type        = 0
        parms.write('sharpclaw.data')
        parms.write('claw.data')

        #Set run-specific parameters
        #runnames=['WENO5 Wave','UC3 Wave','UC7 Wave']
        runnames=['TVD2','WENO5','UC7','ClawPack']
        runparms={}
        for name in runnames: 
            runparms[name]=ClawData()
            runparms=setparms(runparms,name)
        mxvals = array([200,400,800,1600,3200])        # mx values 

    elif testname=='interface 2':
        # Strong interface
        data.ninter=1
        data.interface=0.0
        data.c = 1.0,0.5
        data.rho = 1.0,4000.0
        data.write('setprob.data')
        data.write('sharpclaw.data')
        data.write('claw.data')
        #Set parameters that are uniform or default
        parms.char_decomp     = 0
        parms.lim_type        = 0

        #Set run-specific parameters
        runnames=['TVD2','WENO5','WENO5 Char','ClawPack']
        runparms={}
        for name in runnames: 
            runparms[name]=ClawData()
            runparms=setparms(runparms,name)
        mxvals = array([200,400,800,1600,3200])        # mx values 
 
    elif testname=='periodic':
        data.a = 1.0         #Pulse half-width
        data.x0 = -1.0         #Pulse center
        data.xlower = -10.0
        data.xupper =  10.0
        data.tfinal = 8.      # final time
        #data.interface=range(data.xlower+1,data.xupper)
        delta=2.0
        data.interface=list(arange(data.xlower+delta,data.xupper,delta))
        data.ninter=len(data.interface)
        print 'ninter: ',data.ninter
        data.c   = [1.,1.]*((data.ninter+1)/2)
        data.rho = [1.,3.]*((data.ninter+1)/2)
        print len(data.c)
        data.write('setprob.data')
        data.write('sharpclaw.data')
        data.write('claw.data')

        #Set run-specific parameters
        runnames=['TVD2','WENO5','WENO5 Char','ClawPack']
        runparms={}
        for name in runnames: 
            runparms[name]=ClawData()
            runparms=setparms(runparms,name)
        mxvals = array([200,400,800,1600,3200])        # mx values 
 
    else:
        print "Unrecognized input:  itest = ", itest
        sys.exit(1)

    print "------------------------------------------------------------------"

    exact = {}
    
    table = {}  # dictionary of data and results for each test
    
    for run in runnames:
        table[run] = {} # dictionary to hold data
                                          # and results for this test
        this_table = table[run]  # short name
        this_table['mxvals'] = mxvals       # grid resolutions to test
        this_table['ave_cell_area'] = (data.xupper-data.xlower) / mxvals
        this_table['tfinal'] = data.tfinal
        this_table['errors'] = empty(len(mxvals))
        this_table['runtimes'] = empty(len(mxvals))
        
        for igrid in range(len(mxvals)):
            mx = mxvals[igrid]
            parms.mx = mx
            #data.write('setprob.data')
            #os.system('dos2unix claw2ez.data')
            #os.system('dos2unix setprob.data')

            print "-------------------------------------------------------"
            print 'Running code for run %s with mx = %g ...' \
                                  % (run,mx)
        
            odir = '.'
            # or use next line to put each set of output in separate subdir:
            # odir = 'output_lim%ggrid%gmx%g' % (mthlim,igrid,mx)

            # run fortran code:
            t1 = time.time()
            if run=='ClawPack':
                parms.write('claw.data')
                runparms[run].write('claw.data')
                runclaw(outdir=odir, overwrite=True, xclawcmd='xclaw')
            else:
                parms.write('sharpclaw.data')
                runparms[run].write('sharpclaw.data')
                runclaw(outdir=odir, overwrite=True, xclawcmd='xsclaw')
            CPUtime = time.time() - t1
            this_table['runtimes'][igrid] = CPUtime
            print "CPU time = ",CPUtime
        
            # compute exact solution if not already computed
            if not exact.has_key(mx):
                exact[mx]=compute_exact_solution(odir,frame=data.nout)
            # compute errors:
            try:
                errors = compute_errors(exact[mx],odir,frame=data.nout)
            except:
                errors = array([inf])
    
            # max-norm of error:
            errormax = abs(errors).max()   

            # approx 1-norm of error:
            errorsum = abs(errors).sum() 
            error1 = errorsum * this_table['ave_cell_area'][igrid]
    
            this_table['errors'][igrid] = error1
        
            if plotsoln:
                # put each set of plots in a different directory:
                pdir = 'plots_%g_%gmx%g' % (prm1,prm2,data.mx)
                plotclaw(outdir=odir, plotdir=pdir, overwrite=True, \
                     indexentry='%s = %2i, %s = %2i, mx = %4i, \
                     ' % (param1,prm1,param2,prm2,mx))
    
    # save results:
    db = shelve.open('table.db')
    db['tfinal'] = data.tfinal
    db['table'] = table
    db['runnames'] = runnames
    db.close
    
    # output tables:
    #make_text_table(table, runnames, mxvals, fname='errortables.txt')
    make_latex_table(table, runnames, testname, fname='errortables.tex')
Example #2
0
def runtests(testname='interface 1', plotsoln=False):
    #-------------------------------------
    '''
    ================================
    Main code to run a set of tests:
    ================================
    '''

    if plotsoln:
        from plotclaw import plotclaw

    # set data values:
    data = ClawData()
    parms = ClawData()
    data.tfinal = 8  # final time
    data.ic = 9  #C^5 Newton polynomial initial condition
    data.a = 1.0  #Pulse half-width
    data.x0 = -4.0  #Pulse half-width
    data.xlower = -10.0
    data.xupper = 10.0
    data.mthbc_xlower = 2
    data.mthbc_xupper = 2
    data.mthbc = [2, 2]
    parms.time_integrator = 4
    parms.cfl_max = 2.9
    parms.cfl_desired = 2.85

    if plotsoln:
        data.nout = 10  # output at several times for plotting
        data.write('setplot1.data')
    else:
        data.nout = 1  # only output at final time

    if testname == 'homogeneous':
        # Homogenous medium
        data.ninter = 1
        data.interface = 0.0
        data.c = 1.0, 1.0
        data.rho = 1.0, 1.0
        data.write('setprob.data')
        data.write('sharpclaw.data')
        #Set parameters that are uniform for all runs
        parms.char_decomp = 0
        parms.lim_type = 0
        parms.write('sharpclaw.data')
        parms.write('claw.data')

        #Set run-specific parameters
        runnames = ['TVD2', 'WENO5', 'UC7', 'ClawPack']
        runparms = {}
        for name in runnames:
            runparms[name] = ClawData()
            runparms = setparms(runparms, name)
        mxvals = array([200, 400, 800, 1600, 3200])  # mx values

    elif testname == 'interface 1':
        # Weak interface
        data.ninter = 1
        data.interface = 0.0
        data.c = 1.0, 0.5
        data.rho = 1.0, 4.0
        data.write('setprob.data')
        data.write('sharpclaw.data')
        #Set parameters that are uniform for all runs
        parms.char_decomp = 0
        parms.lim_type = 0
        parms.write('sharpclaw.data')
        parms.write('claw.data')

        #Set run-specific parameters
        #runnames=['WENO5 Wave','UC3 Wave','UC7 Wave']
        runnames = ['TVD2', 'WENO5', 'UC7', 'ClawPack']
        runparms = {}
        for name in runnames:
            runparms[name] = ClawData()
            runparms = setparms(runparms, name)
        mxvals = array([200, 400, 800, 1600, 3200])  # mx values

    elif testname == 'interface 2':
        # Strong interface
        data.ninter = 1
        data.interface = 0.0
        data.c = 1.0, 0.5
        data.rho = 1.0, 4000.0
        data.write('setprob.data')
        data.write('sharpclaw.data')
        data.write('claw.data')
        #Set parameters that are uniform or default
        parms.char_decomp = 0
        parms.lim_type = 0

        #Set run-specific parameters
        runnames = ['TVD2', 'WENO5', 'WENO5 Char', 'ClawPack']
        runparms = {}
        for name in runnames:
            runparms[name] = ClawData()
            runparms = setparms(runparms, name)
        mxvals = array([200, 400, 800, 1600, 3200])  # mx values

    elif testname == 'periodic':
        data.a = 1.0  #Pulse half-width
        data.x0 = -1.0  #Pulse center
        data.xlower = -10.0
        data.xupper = 10.0
        data.tfinal = 8.  # final time
        #data.interface=range(data.xlower+1,data.xupper)
        delta = 2.0
        data.interface = list(arange(data.xlower + delta, data.xupper, delta))
        data.ninter = len(data.interface)
        print 'ninter: ', data.ninter
        data.c = [1., 1.] * ((data.ninter + 1) / 2)
        data.rho = [1., 3.] * ((data.ninter + 1) / 2)
        print len(data.c)
        data.write('setprob.data')
        data.write('sharpclaw.data')
        data.write('claw.data')

        #Set run-specific parameters
        runnames = ['TVD2', 'WENO5', 'WENO5 Char', 'ClawPack']
        runparms = {}
        for name in runnames:
            runparms[name] = ClawData()
            runparms = setparms(runparms, name)
        mxvals = array([200, 400, 800, 1600, 3200])  # mx values

    else:
        print "Unrecognized input:  itest = ", itest
        sys.exit(1)

    print "------------------------------------------------------------------"

    exact = {}

    table = {}  # dictionary of data and results for each test

    for run in runnames:
        table[run] = {}  # dictionary to hold data
        # and results for this test
        this_table = table[run]  # short name
        this_table['mxvals'] = mxvals  # grid resolutions to test
        this_table['ave_cell_area'] = (data.xupper - data.xlower) / mxvals
        this_table['tfinal'] = data.tfinal
        this_table['errors'] = empty(len(mxvals))
        this_table['runtimes'] = empty(len(mxvals))

        for igrid in range(len(mxvals)):
            mx = mxvals[igrid]
            parms.mx = mx
            #data.write('setprob.data')
            #os.system('dos2unix claw2ez.data')
            #os.system('dos2unix setprob.data')

            print "-------------------------------------------------------"
            print 'Running code for run %s with mx = %g ...' \
                                  % (run,mx)

            odir = '.'
            # or use next line to put each set of output in separate subdir:
            # odir = 'output_lim%ggrid%gmx%g' % (mthlim,igrid,mx)

            # run fortran code:
            t1 = time.time()
            if run == 'ClawPack':
                parms.write('claw.data')
                runparms[run].write('claw.data')
                runclaw(outdir=odir, overwrite=True, xclawcmd='xclaw')
            else:
                parms.write('sharpclaw.data')
                runparms[run].write('sharpclaw.data')
                runclaw(outdir=odir, overwrite=True, xclawcmd='xsclaw')
            CPUtime = time.time() - t1
            this_table['runtimes'][igrid] = CPUtime
            print "CPU time = ", CPUtime

            # compute exact solution if not already computed
            if not exact.has_key(mx):
                exact[mx] = compute_exact_solution(odir, frame=data.nout)
            # compute errors:
            try:
                errors = compute_errors(exact[mx], odir, frame=data.nout)
            except:
                errors = array([inf])

            # max-norm of error:
            errormax = abs(errors).max()

            # approx 1-norm of error:
            errorsum = abs(errors).sum()
            error1 = errorsum * this_table['ave_cell_area'][igrid]

            this_table['errors'][igrid] = error1

            if plotsoln:
                # put each set of plots in a different directory:
                pdir = 'plots_%g_%gmx%g' % (prm1, prm2, data.mx)
                plotclaw(outdir=odir, plotdir=pdir, overwrite=True, \
                     indexentry='%s = %2i, %s = %2i, mx = %4i, \
                     '                       % (param1,prm1,param2,prm2,mx))

    # save results:
    db = shelve.open('table.db')
    db['tfinal'] = data.tfinal
    db['table'] = table
    db['runnames'] = runnames
    db.close

    # output tables:
    #make_text_table(table, runnames, mxvals, fname='errortables.txt')
    make_latex_table(table, runnames, testname, fname='errortables.tex')