diff --git a/src/Makefile.common b/src/Makefile.common index 52c8a892..b55618f8 100644 --- a/src/Makefile.common +++ b/src/Makefile.common @@ -24,10 +24,10 @@ CLAW_PYTHON ?= $(PYTHON) # Variables below should be set in Makefile that "includes" this one. # Default values if not set: -CLAW_EXE ?= xclaw +EXE ?= xclaw CLAW_PKG ?= classic -OUT_DIR ?= _output -PLOT_DIR ?= _plots +OUTDIR ?= _output +PLOTDIR ?= _plots LIB_PATHS ?= $(CURDIR)/ OVERWRITE ?= True RESTART ?= False @@ -115,10 +115,10 @@ endif .objs: $(MODULE_FILES) $(OBJECTS); # The order here is to again build the module files correctly -$(CLAW_EXE): $(MODULE_FILES) $(MODULE_OBJECTS) $(OBJECTS) $(MAKEFILE_LIST) ; - $(LINK) $(MODULE_OBJECTS) $(OBJECTS) $(ALL_INCLUDE) $(ALL_LFLAGS) -o $(CLAW_EXE) +$(EXE): $(MODULE_FILES) $(MODULE_OBJECTS) $(OBJECTS) $(MAKEFILE_LIST) ; + $(LINK) $(MODULE_OBJECTS) $(OBJECTS) $(ALL_INCLUDE) $(ALL_LFLAGS) -o $(EXE) -.exe: $(CLAW_EXE) +.exe: $(EXE) debug: @echo 'debugging -- MODULES:' @@ -131,7 +131,7 @@ debug: #---------------------------------------------------------------------------- # Command to create *.html files from *.f etc: -CC2HTML = $(CLAW_PYTHON) $(CLAW)/doc/clawcode2html.py --force +CC2HTML = $(CLAW_PYTHON) $(CLAWUTIL)/src/python/clawutil/clawcode2html.py --force # make list of html files to be created by 'make .htmls': HTML = \ @@ -164,27 +164,27 @@ Makefile.html : Makefile ; $(CC2HTML) $< # Make data files needed by Fortran code: .data: $(SETRUN_FILE) $(MAKEFILE_LIST) ; - $(CLAW_PYTHON) $(SETRUN_FILE) $(CLAW_PKG) - touch .data + $(MAKE) data data: $(MAKEFILE_LIST); -rm -f .data - $(MAKE) .data + $(CLAW_PYTHON) $(SETRUN_FILE) $(CLAW_PKG) + touch .data #---------------------------------------------------------------------------- # Run the code and put fort.* files into subdirectory named output: # runclaw will execute setrun.py to create data files and determine # what executable to run, e.g. xclaw or xamr. -.output: $(CLAW_EXE) .data $(MAKEFILE_LIST); - $(CLAW_PYTHON) $(CLAW)/clawutil/src/python/clawutil/runclaw.py $(CLAW_EXE) $(OUT_DIR) \ - $(OVERWRITE) $(RESTART) - @echo $(OUT_DIR) > .output +.output: $(EXE) .data $(MAKEFILE_LIST); + $(MAKE) output #---------------------------------------------------------------------------- # Run the code without checking dependencies: output: $(MAKEFILE_LIST); -rm -f .output - $(MAKE) .output + $(CLAW_PYTHON) $(CLAWUTIL)/src/python/clawutil/runclaw.py $(EXE) $(OUTDIR) \ + $(OVERWRITE) $(RESTART) + @echo $(OUTDIR) > .output #---------------------------------------------------------------------------- @@ -194,17 +194,18 @@ output: $(MAKEFILE_LIST); # Plotting command PLOTCMD ?= $(CLAW_PYTHON) $(VISCLAW)/src/python/visclaw/plotclaw.py -# Rule to make the plots into subdirectory specified by PLOT_DIR, -# using data in subdirectory specified by OUT_DIR and the plotting -# commands specified in CLAW_SETPLOT. +# Rule to make the plots into subdirectory specified by PLOTDIR, +# using data in subdirectory specified by OUTDIR and the plotting +# commands specified in SETPLOT_FILE. .plots: .output $(SETPLOT_FILE) $(MAKEFILE_LIST) ; - $(PLOTCMD) $(OUT_DIR) $(PLOT_DIR) $(SETPLOT_FILE) - @echo $(PLOT_DIR) > .plots + $(MAKE) plots # Make the plots without checking dependencies: plots: $(SETPLOT_FILE) $(MAKEFILE_LIST); -rm -f .plots - $(MAKE) .plots + $(PLOTCMD) $(OUTDIR) $(PLOTDIR) $(SETPLOT_FILE) + @echo $(PLOTDIR) > .plots + #---------------------------------------------------------------------------- @@ -226,13 +227,13 @@ new: -rm -f $(OBJECTS) -rm -f $(MODULE_OBJECTS) -rm -f $(MODULE_FILES) - -rm -f $(CLAW_EXE) - $(MAKE) $(CLAW_EXE) MAKELEVEL=0 + -rm -f $(EXE) + $(MAKE) $(EXE) MAKELEVEL=0 # Clean up options: clean: - -rm -f $(CLAW_EXE) $(HTML) + -rm -f $(EXE) $(HTML) -rm -f .data .output .plots .htmls clobber: @@ -241,7 +242,7 @@ clobber: -rm -f $(MODULE_OBJECTS) -rm -f $(MODULE_FILES) -rm -f fort.* *.pyc pyclaw.log - -rm -f -r $(OUT_DIR) $(PLOT_DIR) + -rm -f -r $(OUTDIR) $(PLOTDIR) #---------------------------------------------------------------------------- diff --git a/src/python/clawutil/chardiff.py b/src/python/clawutil/chardiff.py index b11440c1..dd44747c 100755 --- a/src/python/clawutil/chardiff.py +++ b/src/python/clawutil/chardiff.py @@ -34,6 +34,8 @@ def chardiff_file(fname1, fname2, print_all_lines=True, hfile1='', \ if (len(f1) != len(f2)) and verbose: print "*** files have different number of lines" + print " %s: %s lines" % (fname1,len(f1)) + print " %s: %s lines" % (fname2,len(f2)) flen = min(len(f1), len(f2)) table1 = [] @@ -114,9 +116,6 @@ def chardiff_file(fname1, fname2, print_all_lines=True, hfile1='', \ numchanges = sum(changed) html = open(hfile1,"w") - if verbose: - print "Point your browser to: " - print " view all %s lines with diffs: %s" % (flen, hfile1) hf2 = os.path.split(hfile2)[1] html.write(""" @@ -154,8 +153,6 @@ def chardiff_file(fname1, fname2, print_all_lines=True, hfile1='', \ # Only changed lines: html = open(hfile2,"w") - if verbose: - print " view only %s lines with changes: %s" % (numchanges, hfile2) hf1 = os.path.split(hfile1)[1] html.write(""" @@ -211,7 +208,7 @@ def chardiff_dir(dir1, dir2, file_pattern='all', dir3="_char_diff", checkfiles = filecmp.dircmp(dir1,dir2) allsame = (checkfiles.diff_files==[]) and (checkfiles.left_list == checkfiles.right_list) - if allsame and verbose: + if allsame: print "*All* files in the two directories are equal" elif verbose: if len(checkfiles.diff_files)>0: @@ -343,7 +340,16 @@ def __init__(self, msg): # Run diff if os.path.isfile(args[0]) and os.path.isfile(args[1]): - sys.exit(chardiff_file(args[0],args[1],verbose=verbose)) + try: + hfile1 = "diff_all_lines.html" + hfile2 = "diff_changed_lines.html" + (flen,numchanges) = chardiff_file(args[0],args[1],\ + hfile1=hfile1,hfile2=hfile2,verbose=verbose) + print "View all %s lines with diffs: %s" % (flen, hfile1) + print "View only %s lines with changes: %s" % (numchanges, hfile2) + sys.exit(0) + except: + sys.exit(1) elif os.path.isdir(args[0]) and os.path.isdir(args[1]): if len(args) > 2: if args[2][0] == '[': diff --git a/src/python/clawutil/clawcode2html.py b/src/python/clawutil/clawcode2html.py new file mode 100644 index 00000000..69763797 --- /dev/null +++ b/src/python/clawutil/clawcode2html.py @@ -0,0 +1,586 @@ +#!/usr/bin/env python + +# convert CLAWPACK source code, data files, READMEs, etc. into html. +# for example, +# clawcode2html filename.f +# generates +# filename.f.html +# If filename.f.html already exists, you will be prompted before +# overwriting unless the -f or --force flag is used, e.g. +# clawcode2html --force filename.f +# +# Based on mathcode2html.py from +# http://www.amath.washington.edu/~rjl/mathcode2html +# with some modifications for CLAWPACK. +# +# The environment variable CLAW must be properly set to the root +# of the claw directory before using this script. +# +# Most of code is put into
 environment.
+# Any comments enclosed by begin_html and end_html are taken out 
+# of the 
 environment and indented properly (using a table) 
+# to match the surrounding code.
+
+# By default, the html comments are offset in blue font, except if the 
+# input file has extension .txt or no extension, in which case it's 
+# left as black.  The default_color can be changed below.
+# Also, the begin_html line may contain [color: red] for example,
+# to determine the color of this comment.
+
+# You may modify the recognized extensions and comment characters below.
+
+# Allows many latex commands in comments if jsMath is used on webpage.
+# In this case make sure the variable jsMathScript is properly set below.
+# If you don't want to include a call to the jsMath script on the html
+# page, invoke mathcode2html with the --nojsmath option.
+
+
+#---------------------------------------------------------------------------
+# Copyright (C) 2008   Randall J. LeVeque 
+#
+# Distributed as part of Clawpack, under the BSD license
+# See www.clawpack.org
+#---------------------------------------------------------------------------
+
+
+import sys,os,glob
+import string,re
+import time
+try:
+    from optparse import OptionParser
+except:
+    print 'You must use a more recent version of Python'
+    sys.exit(1)
+
+# parse command line arguments:
+parser = OptionParser()
+parser.add_option("-f", "--force",
+                  action="store_true", dest="forced", default=False,
+                  help="force action even if it overwrites html file")
+parser.add_option("--nojsmath",
+                  action="store_false", dest="nojsMath", default=False,
+                  help="don't include call to jsMath script in html")
+parser.add_option("-q", "--quiet",
+                  action="store_false", dest="verbose", default=True,
+                  help="don't print status messages to stdout")
+parser.add_option("--noheader",
+                  action="store_false", dest="header", default=True,
+                  help="suppress printing header at top of html page")
+parser.add_option("--dropext",
+                  action="store_true", dest="dropext", default=False,
+                  help="drop the file extension before adding .html for output")
+parser.add_option("--eagle",
+                  action="store_true", dest="eagle", default=False,
+                  help="load the eagleclaw.css style")
+
+(options, infiles) = parser.parse_args()
+
+forced = options.forced
+nojsMath = options.nojsMath
+verbose = options.verbose
+header = options.header
+dropext = options.dropext 
+eagle = options.eagle 
+
+
+
+# CLAWPACK environment variables:
+
+try:
+    clawdir = os.getenv('CLAW')  # assumes environment variable set properly
+except:
+    print "CLAW environment variable not set"
+    print "You need to run setenv.py before setup.py"
+    sys.exit(1)
+
+if clawdir == None:
+    print "CLAW environment variable not set"
+    print "You need to run setenv.py before setup.py"
+    sys.exit(1)
+
+
+# Create addresses for links on html pages.
+# change the lines below if you want to point to a webpage home instead
+# of the local file system  (e.g.  clawaddr = "http://www.mywebpage/claw")
+
+clawaddr = 'http://localhost:50005'
+#clawaddr = 'http://kingkong.amath.washington.edu/claw4'
+
+
+# Set comment characters for different programming languages:
+# Augment or modify as desired.
+commentchar = { '.f'  : ['!', '#'], \
+                '.f95'  : ['!','#'], \
+                '.f90'  : ['!','#'], \
+                '.m'  : '%', \
+                '.py' : '#', \
+                '.sh' : '#', \
+                '.data' : ['#','=:'], \
+                '.txt': None, \
+                'Makefile': '#', \
+                ''    : None}
+
+firstfort = ['c','*','C','!']   # valid fortran .f comment char's in col. 1
+firstfort95 = ['!']   # valid fortran .f95 comment char's in col. 1
+
+leadingindent = ''    # additional indentation for webpage, if desired
+
+default_color = 'blue'
+
+
+try:
+    infiles.remove('clawcode2html.py')  # can't apply this code to itself!
+except:
+    pass
+
+
+for infilename in infiles:
+
+    # check if this is a code file of a recognized language.
+    ext = os.path.splitext(infilename)[1]
+    if infilename == 'Makefile':
+        ext = 'Makefile'  # special case
+    
+    if not commentchar.has_key(ext):
+        print "  "
+        print "  Warning: Unrecognized extension, will proceed"
+        print "           with no replacement of comment characters"
+        commentchar[ext] = None 
+    
+    
+    # open input and output files:
+    #-----------------------------
+    
+    try:
+        ifile = open(infilename,'r')
+    except:
+        print "File not found:", infilename
+        sys.exit(1)
+
+    # Search for [use: ...] statements in file:
+    all_lines = ifile.read()
+    regexp = re.compile(r"\[use:[^\]]*jsMath")
+    result = regexp.search(all_lines)
+
+    # use jsMath if [use:jsMath] found and option --nojsmath was
+    # not used in call:
+    usejsMath = (result is not None) & (not nojsMath)
+
+    regexp = re.compile(r"\[use:(?P[^\]]*).css\]")
+    result = regexp.search(all_lines)
+    if result is not None:
+        cssfile = result.group('cssfile').strip() + '.css'
+    else:
+        cssfile = None
+
+    
+    ifile.seek(0)  # return to start of file
+    lines = ifile.readlines()
+    
+    if dropext:
+        infileroot = os.path.splitext(infilename)[0]
+        outfilename = infileroot + '.html'
+    else:
+        outfilename = infilename + '.html'
+
+    if (glob.glob(outfilename) != []) & (not forced):
+        sys.stdout.write('  OK to overwrite %s?  '  %  outfilename)
+        answer = raw_input()
+        if answer not in ['y','Y','yes']:
+            print '  Aborting!'
+            sys.exit(1)
+    
+    ofile = open(outfilename,'w')
+    
+    
+    # start creating html file:
+    #--------------------------
+    
+    if verbose:
+        print '  Converting ', infilename, ' to ', outfilename
+
+    
+    ofile.write("""""" % infilename)
+
+    ofile.write('\n\n\n %s \n\n'  % outfilename)
+    if eagle:
+        ofile.write("""
+          
+          
+          
+           
+          
+        """ % (clawaddr,clawaddr))
+    elif cssfile:
+        ofile.write("""
+          
+          
+          
+           
+          
+        """ % (clawaddr, cssfile, clawaddr))
+    else:
+        ofile.write(
+           """
+          
+           
+           
+           
+           
+           """ % clawaddr)
+
+    
+    # determine time and reformat:
+    time1 = time.asctime()
+    year = time1[-5:]
+    day = time1[:-14]
+    hour = time1[-13:-5]
+    creationtime = day + year + ' at ' + hour
+
+    # put full file name with path into a comment for future reference:
+    fullinfilename = os.path.join(os.getcwd(),infilename)
+    ofile.write('\n\n'  % fullinfilename)
+    ofile.write('\n\n'  % creationtime)
+    
+    
+    if header:
+        #ofile.write('
\n') + ofile.write('\n' % outfilename) + + ofile.write(""" +
\n') + ofile.write('  %s CLAWPACK""" % clawaddr) + + ofile.write('  
\n') + + ofile.write(' Source file:   %s \n' \ + % (infilename,infilename)) + ofile.write('
\n') + ofile.write(' Directory:   %s \n' % os.getcwd()) + ofile.write('
\n') + ofile.write(' Converted:   %s \n' % creationtime) + ofile.write('  using clawcode2html\n'\ + % clawaddr) + ofile.write('
\n') + ofile.write('  This documentation file will \n') + ofile.write('not reflect any later changes in the source file. \n') + ofile.write('
\n') + ofile.write('

\n') + + + if usejsMath: + + # Set jsMathScript to the file or URL of the java script load.js. + + # proper location for using webserver started by executing + # "python startserver.py" in the $CLAW directory: + jsMathScript = "%s/doc/load.js" % clawaddr + + # script location for posting on kingkong server: + # jsMathScript = "http://kingkong.amath.washington.edu/claw/doc/load.js" + + # script location for posting on rjl's webpage: + # jsMathScript = "http://www.amath.washington.edu/~rjl/jsMath/easy/load.js" + + ofile.write(" \n") + ofile.write(" \n" % jsMathScript) + + + ofile.write(""" + + $\phantom{******** If you see this on the webpage then the + browser could not locate *********}$
+ $\phantom{******** the jsMath file load.js *********}$

+ \n""") + + # define any latex macros that you want to use in jsMath: + + ofile.write(""" + + $\\newcommand{\\vector}[1]{\\left[\\begin{array}{c} #1 \\end{array}\\right]}$ + $\\newenvironment{matrix}{\\left[\\begin{array}{cccccccccc}} {\\end{array}\\right]}$ + $\\newcommand{\\A}{{\\cal A}}$ + $\\newcommand{\\W}{{\\cal W}}$ + \n""") + # + # end of jsMath stuff + + + # start writing input file to output file... + + # code is in pre-formatted environment: + ofile.write('

 \n')
+    
+    insidehtml = 0;   # set to 1 when we're processing html comments
+    lineno = 0;       # line number counter for error message
+    
+    for line in lines:
+    
+        lineno += 1
+    
+        if string.count(line,"begin_html"):
+            regexp = re.compile(r"\[color:(?P[^\]]*)\]")
+            result = regexp.search(line)
+	    if result:
+	        font_color = result.group('color')
+	    else:
+	        font_color = default_color
+    
+            if insidehtml:
+                print '  Error at line ', lineno, '\n'
+                print '  Unexpected begin_html  when already in html mode\n'
+                print '  Missing end_html? \n'
+                sys.exit(1)
+    
+            # switch out of pre-formatted mode and create table to indent
+            ofile.write('
\n\n') + + # the next column of the table has the comment itself: + ofile.write('
\n') + + # The first column of the table is spaces for indentation + # to match surrounding source code. + # Count how many spaces there are before the begin_html + # or to the first comment character preceeding that string: + regexp = re.compile(r"(?P[ ]*)(#|%|begin)") + result = regexp.search(line) + if result: + numindent = len(leadingindent) + len(result.group('spaces')) + numindent = numindent + ofile.write('
')
+                for i in range(numindent):
+                    ofile.write(' ')
+                ofile.write('
') + else: + print ' Strange error in clawcode2html - should not be here' + print ' at line number ', lineno + + + ofile.write('\n
\n') + if ext not in ('.txt', ''): + # use colored font for comments except for text files. + ofile.write('\n' % font_color) + insidehtml = 1; + + elif string.count(line,"end_html"): + # switch back to pre-formatted environment + ofile.write('
\n') + ofile.write('
 \n')
+            insidehtml = 0;
+    
+        else:
+            if insidehtml:
+    
+                # replace blank line in html comment by new paragraph 

: + blankline = (string.split(line) == []) + if not blankline: + firstchar = string.split(line)[0][0] + if ((ext in ['.f','.f95']) & (firstchar not in firstfort)): + if firstchar not in commentchar[ext]: + print ' Error... in line ', lineno,'\n' \ + ' In html but not in a comment.'\ + ' Forgotten "end_html" ?' + sys.exit(1) + + # fortran comments may have comment symbol in column 1 + # strip lines inside an html comment of leading symbol: + if ext == '.f': + if line[0] in firstfort: + line = ' ' + line[1:] + if ext == '.f95': + if line[0] in firstfort95: + line = ' ' + line[1:] + + # replace any comment character by ' ' + if commentchar[ext]: + for char in commentchar[ext]: + line = string.replace(line,char,' ') + + + blankline = (string.split(line) == []) + if blankline: + line = ('

\n') + + + # Allow wiki formatting of links: + # ------------------------------- + + # Replace [name: placemark] by + # (to jump to a different spot in the same html file) + regexp = re.compile(r"\[name:[ ]*(?P[^ ^\]]*)\]") + result = regexp.search(line) + while result: + placemark = result.group('placemark') + oldpat = result.group() + newpat = '' % placemark + line = line.replace(oldpat,newpat) + result = regexp.search(line) + + # Replace links of the form [code: target] + # by html links to both target and target.html. + # Also allows [code: target#placemark] with links to target + # and target.html#placemark. + + regexp = re.compile(r"\[code:[ ]*(?P[^ ^\]^#]*)([#]?)" + \ + r"(?P[^\]]*)\]") + result = regexp.search(line) + while result: + targetname = result.group('target') + placemark = result.group('placemark') + oldpat = result.group() + if placemark: + newpat = '%s' \ + % (targetname,targetname) + \ + ' [.html]' \ + % (targetname,placemark) + else: + oldpat = result.group() + newpat = '%s' \ + % (targetname,targetname) + \ + ' [.html]' \ + % targetname + line = line.replace(oldpat,newpat) + result = regexp.search(line) + + + # replace links of the form [link: target text] + # by an html link from text to the target page. + regexp = re.compile(r"\[link:[ ]?(?P[^ ^\]]*)" + \ + r"([ ]*)(?P[^\]]*)\]") + result = regexp.search(line) + while result: + targetname = result.group('target') + text = result.group('text') + oldpat = result.group() + if text=='': + text = targetname + newpat = '' + \ + text + ' ' + line = line.replace(oldpat,newpat) + result = regexp.search(line) + + + # replace links of the form [http:etc text] + # by an html link from text to the http page. + regexp = re.compile(r"\[http:(?P[^ ^\]]*)" + \ + r"([ ]*)(?P[^\]]*)\]") + result = regexp.search(line) + while result: + targetname = result.group('target') + text = result.group('text') + oldpat = result.group() + if text=='': + text = targetname[2:] + newpat = '' + \ + text + ' ' + line = line.replace(oldpat,newpat) + result = regexp.search(line) + + + # replace links of the form [www.etc text] + # by an html link from text to the http://www.etc page. + regexp = re.compile(r"\[www.(?P[^ ^\]]*)" + \ + r"([ ]*)(?P[^\]]*)\]") + result = regexp.search(line) + while result: + targetname = result.group('target') + text = result.group('text') + oldpat = result.group() + if text=='': + text = 'www.' + targetname + newpat = '' + \ + text + ' ' + line = line.replace(oldpat,newpat) + result = regexp.search(line) + + + # special things for CLAWPACK: + + # replace links of the form [clawcode:clawpack/1d/lib/step1.f] + # for example by links relative to clawaddr, + # along with a link to the .html version. + regexp = re.compile(r"\[clawcode:[ ]*(?P[^ ^\]^#]*)([#]?)" + \ + r"(?P[^\]]*)\]") + result = regexp.search(line) + while result: + targetname = result.group('target').lstrip() + placemark = result.group('placemark') + oldpat = result.group() + if placemark: + newpat = 'claw/%s' \ + % (clawaddr,targetname,targetname) + \ + ' [.html]' \ + % (clawaddr,targetname,placemark) + else: + newpat = '%s' \ + % (clawaddr,targetname,targetname) + \ + ' [.html]' \ + % (clawaddr,targetname) + line = line.replace(oldpat,newpat) + result = regexp.search(line) + + + # replace links of the form [claw:clawpack/1d/lib] + # for example by links relative to clawaddr, + # with no .html version. + regexp = re.compile(r"\[claw:[ ]?(?P[^ ^\]]*)" + \ + r"([ ]*)(?P[^\]]*)\]") + result = regexp.search(line) + while result: + targetname = result.group('target') + text = result.group('text') + oldpat = result.group() + if text=='': + text = '$CLAW/' + targetname + newpat = '' + text + ' ' + line = line.replace(oldpat,newpat) + result = regexp.search(line) + + # place text surrounded by triple braces with + # pre environment with background color: + newpat = '

'
+                line = line.replace('{{{',newpat)
+                line = line.replace('}}}','
') + + + else: + # not insidehtml - make regular comments default_color. + # Determine if this line contains a comment and if so, + # what column the comment starts in: + + startcomment = 1000 + if (ext == '.f') & (line[0] in firstfort): + startcomment = 0 + elif (ext == '.f95') & (line[0] in firstfort95): + startcomment = 0 + else: + if commentchar[ext]: + for c in commentchar[ext]: + commentcol = string.find(line,c) + if (commentcol>-1)&(commentcol' % default_color + \ + line[startcomment:-1] + '\n' + + + # output the (possibly modified) line to the output file: + ofile.write('%s' % leadingindent+line) + + # Done with all lines. Add closing stuff at bottom of html file: + ofile.write('
\n') + + ifile.close() + ofile.close() + diff --git a/src/python/clawutil/clawdata.py b/src/python/clawutil/clawdata.py old mode 100755 new mode 100644 index 3ac08dcd..f7801115 --- a/src/python/clawutil/clawdata.py +++ b/src/python/clawutil/clawdata.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python -# encoding: utf-8 r""" Data Module @@ -11,20 +9,12 @@ data classes are now subclasses of ClawData, which checks if attributes already exist before setting. - -:Authors: - Kyle T. Mandli and Randall J. LeVeque - """ -# ============================================================================ -# Distributed under the terms of the Berkeley Software Distribution (BSD) -# license -# http://www.opensource.org/licenses/ -# ============================================================================ import os import logging - + +import numpy as np # ======================== @@ -41,6 +31,8 @@ def __init__(self, attributes=None): for attr in attributes: self.__setattr__(attr,None) + def add_attribute(self, name, value=None, add_to_list=True): + setattr(self,name,value) # ======================== class ClawData(object): @@ -195,26 +187,25 @@ class ClawInputData(ClawData): r""" Object that will be written out to claw.data. """ - def __init__(self, ndim): + def __init__(self, num_dim): super(ClawInputData,self).__init__() - self.add_attribute('ndim',ndim) # Set default values: - self.add_attribute('mx',100) - self.add_attribute('meqn',1) - self.add_attribute('mwaves',1) - self.add_attribute('maux',0) + self.add_attribute('num_dim',num_dim) + self.add_attribute('num_eqn',1) + self.add_attribute('num_waves',1) + self.add_attribute('num_aux',0) self.add_attribute('output_style',1) - self.add_attribute('output_ntimes',1) - self.add_attribute('output_times',[1.]) - self.add_attribute('output_step_interval',10) - self.add_attribute('total_steps',5) - self.add_attribute('output_time_interval',1.) + self.add_attribute('output_times',[]) + self.add_attribute('num_output_times',None) + self.add_attribute('output_t0',True) + self.add_attribute('output_step_interval',None) + self.add_attribute('total_steps',None) + self.add_attribute('tfinal',None) self.add_attribute('output_format',1) self.add_attribute('output_q_components','all') self.add_attribute('output_aux_components',[]) self.add_attribute('output_aux_onlyonce',True) - self.add_attribute('tfinal',1.0) self.add_attribute('dt_initial',1.e-5) self.add_attribute('dt_max',1.e99) @@ -223,44 +214,41 @@ def __init__(self, ndim): self.add_attribute('cfl_max',1.0) self.add_attribute('steps_max',50000) self.add_attribute('order',2) - self.add_attribute('order_trans',0) + self.add_attribute('transverse_waves',2) self.add_attribute('dimensional_split',0) self.add_attribute('verbosity',0) self.add_attribute('verbosity_regrid',0) - self.add_attribute('src_split',0) - self.add_attribute('mcapa',0) + self.add_attribute('source_split',0) + self.add_attribute('capa_index',0) self.add_attribute('limiter',[4]) self.add_attribute('t0',0.) - self.add_attribute('xlower',0.) - self.add_attribute('xupper',1.) self.add_attribute('num_ghost',2) - self.add_attribute('bc_xlower',1) - self.add_attribute('bc_xupper',1) - self.add_attribute('restart',0) - self.add_attribute('restart_frame',0) self.add_attribute('fwave',False) self.add_attribute('restart',False) self.add_attribute('restart_file','') self.add_attribute('regions',[]) self.add_attribute('gauges',[]) - - if ndim >= 2: - self.add_attribute('my',100) - self.add_attribute('ylower',0.) - self.add_attribute('yupper',1.) - self.add_attribute('bc_ylower',1) - self.add_attribute('bc_yupper',1) - - if ndim == 3: - self.add_attribute('mz',100) - self.add_attribute('zlower',0.) - self.add_attribute('zupper',1.) - self.add_attribute('bc_zlower',1) - self.add_attribute('bc_zupper',1) - - if ndim not in [1,2,3]: - raise ValueError("Only ndim=1, 2, or 3 supported ") + if num_dim == 1: + self.add_attribute('lower',[0.]) + self.add_attribute('upper',[1.]) + self.add_attribute('num_cells',[100]) + self.add_attribute('bc_lower',[0]) + self.add_attribute('bc_upper',[0]) + elif num_dim == 2: + self.add_attribute('lower',[0.,0.]) + self.add_attribute('upper',[1.,1.]) + self.add_attribute('num_cells',[100,100]) + self.add_attribute('bc_lower',[0,0]) + self.add_attribute('bc_upper',[0,0]) + elif num_dim == 3: + self.add_attribute('lower',[0.,0.,0.]) + self.add_attribute('upper',[1.,1.,1.]) + self.add_attribute('num_cells',[100,100,100]) + self.add_attribute('bc_lower',[0,0,0]) + self.add_attribute('bc_upper',[0,0,0]) + else: + raise ValueError("Only num_dim=1, 2, or 3 supported ") def write(self): print 'Creating data file claw.data for use with xclaw' @@ -271,28 +259,34 @@ class AmrclawInputData(ClawInputData): r""" Object that will be written out to amrclaw.data. """ - def __init__(self, ndim): + def __init__(self, num_dim): # Set default values: # Some defaults are inherited from ClawInputData: - super(AmrclawInputData,self).__init__(ndim) + super(AmrclawInputData,self).__init__(num_dim) - self.add_attribute('amrlevels_max',1) - self.add_attribute('refinement_ratio_x',[1]) - self.add_attribute('refinement_ratio_t',[1]) - self.add_attribute('auxtype',[]) + self.add_attribute('amr_levels_max',1) + self.add_attribute('refinement_ratios_x',[1]) + self.add_attribute('refinement_ratios_y',[1]) + if num_dim == 3: + self.add_attribute('refinement_ratios_z',[1]) + if num_dim == 1: + raise Exception("*** 1d AMR not yet supported") + self.add_attribute('variable_dt_refinement_ratios',False) + + self.add_attribute('refinement_ratios_t',[1]) + self.add_attribute('aux_type',[]) self.add_attribute('checkpt_style',1) self.add_attribute('checkpt_interval',1000) self.add_attribute('checkpt_time_interval',1000.) self.add_attribute('checkpt_times',[1000.]) - self.add_attribute('checkpt_ntimes',1) self.add_attribute('flag_richardson',False) self.add_attribute('flag_richardson_tol',1.0) - self.add_attribute('flag_gradient',True) - self.add_attribute('flag_gradient_tol',0.05) + self.add_attribute('flag2refine',True) + self.add_attribute('flag2refine_tol',0.05) self.add_attribute('regrid_interval',2) self.add_attribute('regrid_buffer_width',3) self.add_attribute('clustering_cutoff',0.7) @@ -311,8 +305,7 @@ def __init__(self, ndim): self.add_attribute('uprint',False) - - if ndim == 1: + if num_dim == 1: # attributes needed only because 1d AMR is done using 2d amrclaw: self.add_attribute('my',1) @@ -320,21 +313,15 @@ def __init__(self, ndim): self.add_attribute('yupper',1.) self.add_attribute('bc_ylower',1) self.add_attribute('bc_yupper',1) - self.add_attribute('refinement_ratio_y',[1,1,1,1,1,1]) - - elif ndim >= 2: + self.add_attribute('refinement_ratios_y',[1,1,1,1,1,1]) - self.add_attribute('refinement_ratio_y',[1]) - - - if ndim not in [1,2]: - print '*** Error: only ndim=1 or 2 supported so far ***' - raise AttributeError("Only ndim=1 or 2 supported so far") def write(self): print 'Creating data file amrclaw.data for use with xamr' make_amrclawdatafile(self) - #make_setgauges_datafile(self) + make_setgauges_datafile(self) + if len(self.regions) > 0: + print "*** Warning: regions not yet implemented!" @@ -365,7 +352,7 @@ def open_datafile(name, datasource='setrun.py'): return file -def data_write(file, dataobj, name=None, descr=''): +def data_write_old(file, dataobj, name=None, descr=''): r""" Write out value to data file, in the form :: @@ -405,6 +392,43 @@ def data_write(file, dataobj, name=None, descr=''): padded_name = string.ljust(name, 12) file.write('%s =: %s %s\n' % (padded_value, padded_name, descr)) +def data_write(file, value, name=None, description=''): + r""" + Write out value to data file, in the form :: + + value # name [description] + + Remove brackets and commas from lists, and replace booleans by T/F. + + :Input: + - *name* - (string) normally a string defining the variable, + ``if name==None``, write a blank line. + - *description* - (string) optional description + """ + + import string + from numpy import ndarray + if name is None: + file.write('\n') + else: + # Convert value to an appropriate string repr + if isinstance(value,ndarray): + value = list(value) + if isinstance(value,tuple) | isinstance(value,list): + # Remove [], (), and ',' + string_value = repr(value)[1:-1] + string_value = string_value.replace(',','') + elif isinstance(value,bool): + if value: + string_value = 'T' + else: + string_value = 'F' + else: + string_value = repr(value) + padded_value = string.ljust(string_value, 40) + #padded_name = string.ljust(name, 12) + file.write('%s # %s %s\n' % (padded_value, name, description)) + def make_clawdatafile(clawdata): r""" @@ -416,125 +440,185 @@ def make_clawdatafile(clawdata): # open file and write a warning header: file = open_datafile('claw.data') - ndim = clawdata.ndim - data_write(file, clawdata, 'ndim', '(number of dimensions)') - data_write(file, clawdata, 'mx', '(cells in x direction)') - if ndim > 1: - data_write(file, clawdata, 'my', '(cells in y direction)') - if ndim == 3: - data_write(file, clawdata, 'mz', '(cells in z direction)') + write_clawdata_noamr(clawdata, file) + file.close() + + +def write_clawdata_noamr(clawdata, file): + r""" + Write out the Clawpack parameters that are used both for Classic and AMR. + """ + + #import pdb; pdb.set_trace() + num_dim = clawdata.num_dim + data_write(file, clawdata.num_dim, 'num_dim') + data_write(file, clawdata.lower, 'lower') + data_write(file, clawdata.upper, 'upper') + data_write(file, clawdata.num_cells, 'num_cells') data_write(file, clawdata, None) # writes blank line - data_write(file, clawdata, 'meqn', '(number of equations)') - data_write(file, clawdata, 'mwaves', '(number of waves)') - data_write(file, clawdata, 'maux', '(number of aux variables)') + data_write(file, clawdata.num_eqn, 'num_eqn') + data_write(file, clawdata.num_waves, 'num_waves') + data_write(file, clawdata.num_aux, 'num_aux') data_write(file, clawdata, None) # writes blank line - data_write(file, clawdata, 'output_style', '(style of specifying output times)') - if clawdata.output_style == 1: - data_write(file, clawdata, 'output_ntimes', '(number of output times)') - data_write(file, clawdata, 'tfinal', '(final time)') - elif clawdata.output_style == 2: - clawdata.output_ntimes = len(clawdata.output_times) - data_write(file, clawdata, 'output_ntimes', '(number of output times)') - data_write(file, clawdata, 'output_times', '(output times)') - elif clawdata.output_style == 3: - data_write(file, clawdata, 'output_step_interval', '(timesteps between output)') - data_write(file, clawdata, 'total_steps', '(number of output times)') - elif clawdata.output_style == 4: - data_write(file, clawdata, 'output_time_interval', '(output interval)') - data_write(file, clawdata, 'tfinal', '(final time)') + data_write(file, clawdata.t0, 't0') + data_write(file, clawdata, None) + data_write(file, clawdata.output_style, 'output_style') + + if clawdata.output_style==1: + data_write(file, clawdata.num_output_times, 'num_output_times') + data_write(file, clawdata.tfinal, 'tfinal') + data_write(file, clawdata.output_t0, 'output_t0') + elif clawdata.output_style==2: + if len(clawdata.output_times) == 0: + raise AttributeError("*** output_style==2 requires nonempty list" \ + + " of output times") + clawdata.num_output_times = len(clawdata.output_times) + data_write(file, clawdata.num_output_times, 'num_output_times') + data_write(file, clawdata.output_times, 'output_times') + elif clawdata.output_style==3: + data_write(file, clawdata.output_step_interval, 'output_step_interval') + data_write(file, clawdata.total_steps, 'total_steps') + data_write(file, clawdata.output_t0, 'output_t0') else: - print '*** Error: unrecognized output_style' - raise - return + raise AttributeError("*** Unrecognized output_style: %s"\ + % clawdata.output_style) + data_write(file, clawdata, None) if clawdata.output_format in [1,'ascii']: clawdata.output_format = 1 - elif clawdata.output_format in [2,'binary']: + elif clawdata.output_format in [2,'netcdf']: clawdata.output_format = 2 else: raise ValueError("*** Error in data parameter: " + \ "output_format unrecognized: ",clawdata.output_format) - data_write(file, clawdata, 'output_format', '(output format)') - clawdata._iout_q = clawdata.meqn * [1] - if clawdata.output_q_components != 'all': - for i in range(clawdata.meqn): - if i+1 not in clawdata.output_q_components: - clawdata._iout_q[i] = 0 - data_write(file, clawdata, '_iout_q', '(which components of q)') - if clawdata.maux > 0: - clawdata._iout_aux = clawdata.maux * [1] - if clawdata.output_aux_components != 'all': - for i in range(clawdata.maux): - if i+1 not in clawdata.output_aux_components: - clawdata._iout_aux[i] = 0 - data_write(file, clawdata, '_iout_aux', '(which components of aux)') - data_write(file, clawdata, 'output_aux_onlyonce', '(only once?)') + data_write(file, clawdata.output_format, 'output_format') + + if clawdata.output_q_components == 'all': + iout_q = clawdata.num_eqn * [1] + elif clawdata.output_q_components == 'none': + iout_q = clawdata.num_eqn * [0] + else: + iout_q = np.where(clawdata.output_q_components, 1, 0) + + data_write(file, iout_q, 'iout_q') + + if clawdata.num_aux > 0: + if clawdata.output_aux_components == 'all': + iout_aux = clawdata.num_aux * [1] + elif clawdata.output_aux_components == 'none': + iout_aux = clawdata.num_aux * [0] + else: + iout_aux = np.where(clawdata.output_aux_components, 1, 0) + data_write(file, iout_aux, 'iout_aux') + data_write(file, clawdata.output_aux_onlyonce, 'output_aux_onlyonce') + data_write(file, clawdata, None) - data_write(file, clawdata, 'dt_initial', '(initial time step dt)') - data_write(file, clawdata, 'dt_max', '(max allowable dt)') - data_write(file, clawdata, 'cfl_max', '(max allowable Courant number)') - data_write(file, clawdata, 'cfl_desired', '(desired Courant number)') - data_write(file, clawdata, 'steps_max', '(max time steps per call to claw)') + data_write(file, clawdata.dt_initial, 'dt_initial') + data_write(file, clawdata.dt_max, 'dt_max') + data_write(file, clawdata.cfl_max, 'cfl_max') + data_write(file, clawdata.cfl_desired, 'cfl_desired') + data_write(file, clawdata.steps_max, 'steps_max') data_write(file, clawdata, None) - data_write(file, clawdata, 'dt_variable', '(1 for variable dt, 0 for fixed)') - data_write(file, clawdata, 'order', '(1 or 2)') - if ndim == 1: - #data_write(file, clawdata, 'order_trans', '(not used in 1d)') + data_write(file, clawdata.dt_variable, 'dt_variable') + data_write(file, clawdata.order, 'order') + if num_dim == 1: pass else: - data_write(file, clawdata, 'order_trans', '(transverse order)') - data_write(file, clawdata, 'dimensional_split', '(use dimensional splitting?)') + if clawdata.transverse_waves in [0,'none']: + clawdata.transverse_waves = 0 + elif clawdata.transverse_waves in [1,'increment']: + clawdata.transverse_waves = 1 + elif clawdata.transverse_waves in [2,'all']: + clawdata.transverse_waves = 2 + else: + raise AttributeError("Unrecognized transverse_waves: %s" \ + % clawdata.transverse_waves) + data_write(file, clawdata.transverse_waves, 'transverse_waves') + + if clawdata.dimensional_split in [0,'unsplit']: + clawdata.dimensional_split = 0 + elif clawdata.dimensional_split in [1,'godunov']: + clawdata.dimensional_split = 1 + elif clawdata.dimensional_split in [2,'strang']: + clawdata.dimensional_split = 2 + else: + raise AttributeError("Unrecognized dimensional_split: %s" \ + % clawdata.dimensional_split) + data_write(file, clawdata.dimensional_split, 'dimensional_split') - data_write(file, clawdata, 'verbosity', '(verbosity of output)') - data_write(file, clawdata, 'src_split', '(source term splitting)') - data_write(file, clawdata, 'mcapa', '(aux index for capacity fcn)') + data_write(file, clawdata.verbosity, 'verbosity') + + if clawdata.source_split in [0,'none']: + clawdata.source_split = 0 + elif clawdata.source_split in [1,'godunov']: + clawdata.source_split = 1 + elif clawdata.source_split in [2,'strang']: + clawdata.source_split = 2 + else: + raise AttributeError("Unrecognized source_split: %s" \ + % clawdata.source_split) + data_write(file, clawdata.source_split, 'source_split') + + data_write(file, clawdata.capa_index, 'capa_index') + if clawdata.num_aux > 0: + data_write(file, clawdata.aux_type, 'aux_type') + data_write(file, clawdata.fwave, 'fwave') data_write(file, clawdata, None) - data_write(file, clawdata, 'limiter', '(limiter choice for each wave)') - data_write(file, clawdata, None) + for i in range(len(clawdata.limiter)): + if clawdata.limiter[i] in [0,'none']: clawdata.limiter[i] = 0 + elif clawdata.limiter[i] in [1,'minmod']: clawdata.limiter[i] = 1 + elif clawdata.limiter[i] in [2,'superbee']: clawdata.limiter[i] = 2 + elif clawdata.limiter[i] in [3,'mc']: clawdata.limiter[i] = 3 + elif clawdata.limiter[i] in [4,'vanleer']: clawdata.limiter[i] = 4 + else: + raise AttributeError("Unrecognized limiter: %s" \ + % clawdata.limiter[i]) + data_write(file, clawdata.limiter, 'limiter') - data_write(file, clawdata, 't0', '(initial time)') - data_write(file, clawdata, 'xlower', '(xlower)') - data_write(file, clawdata, 'xupper', '(xupper)') - if ndim > 1: - data_write(file, clawdata, 'ylower', '(ylower)') - data_write(file, clawdata, 'yupper', '(yupper)') - if ndim == 3: - data_write(file, clawdata, 'zlower', '(zlower)') - data_write(file, clawdata, 'zupper', '(zupper)') data_write(file, clawdata, None) - data_write(file, clawdata, 'num_ghost', '(number of ghost cells)') - - for bdry in ['xlower','xupper','ylower','yupper','zlower','zupper']: - bc = getattr(clawdata, 'bc_'+bdry, None) - if bc == 'user': - setattr(clawdata, 'bc_'+bdry, 0) - if bc == 'extrap': - setattr(clawdata, 'bc_'+bdry, 1) - if bc == 'periodic': - setattr(clawdata, 'bc_'+bdry, 2) - if bc == 'wall': - setattr(clawdata, 'bc_'+bdry, 3) - - data_write(file, clawdata, 'bc_xlower', '(type of BC at xlower)') - data_write(file, clawdata, 'bc_xupper', '(type of BC at xupper)') - if ndim > 1: - data_write(file, clawdata, 'bc_ylower', '(type of BC at ylower)') - data_write(file, clawdata, 'bc_yupper', '(type of BC at yupper)') - if ndim == 3: - data_write(file, clawdata, 'bc_zlower', '(type of BC at zlower)') - data_write(file, clawdata, 'bc_zupper', '(type of BC at zupper)') + + data_write(file, clawdata.num_ghost, 'num_ghost') + for i in range(num_dim): + if clawdata.bc_lower[i] in [0,'user']: clawdata.bc_lower[i] = 0 + elif clawdata.bc_lower[i] in [1,'extrap']: clawdata.bc_lower[i] = 1 + elif clawdata.bc_lower[i] in [2,'periodic']: clawdata.bc_lower[i] = 2 + elif clawdata.bc_lower[i] in [3,'wall']: clawdata.bc_lower[i] = 3 + else: + raise AttributeError("Unrecognized bc_lower: %s" \ + % clawdata.bc_lower[i]) + data_write(file, clawdata.bc_lower, 'bc_lower') + + for i in range(num_dim): + if clawdata.bc_upper[i] in [0,'user']: clawdata.bc_upper[i] = 0 + elif clawdata.bc_upper[i] in [1,'extrap']: clawdata.bc_upper[i] = 1 + elif clawdata.bc_upper[i] in [2,'periodic']: clawdata.bc_upper[i] = 2 + elif clawdata.bc_upper[i] in [3,'wall']: clawdata.bc_upper[i] = 3 + else: + raise AttributeError("Unrecognized bc_upper: %s" \ + % clawdata.bc_upper[i]) + data_write(file, clawdata.bc_upper, 'bc_upper') data_write(file, clawdata, None) - data_write(file, clawdata, 'restart', '(T to restart from a past run)') - data_write(file, clawdata, 'restart_frame', '(which frame to restart from)') + data_write(file, clawdata.restart, 'restart') + data_write(file, clawdata.restart_file, 'restart_file') + data_write(file, clawdata.checkpt_style, 'checkpt_style') + if clawdata.checkpt_style==2: + num_checkpt_times = len(clawdata.checkpt_times) + data_write(file, num_checkpt_times, 'num_checkpt_times') + data_write(file, clawdata.checkpt_times, 'checkpt_times') + elif clawdata.checkpt_style==3: + data_write(file, clawdata.checkpt_interval, 'checkpt_interval') + elif clawdata.checkpt_style not in [0,1]: + raise AttributeError("*** Unrecognized checkpt_style: %s"\ + % clawdata.checkpt_style) + data_write(file, clawdata, None) - file.close() def make_amrclawdatafile(clawdata): r""" @@ -546,205 +630,76 @@ def make_amrclawdatafile(clawdata): # open file and write a warning header: file = open_datafile('amrclaw.data') - ndim = clawdata.ndim - data_write(file, clawdata, 'ndim', '(number of dimensions)') - data_write(file, clawdata, 'mx', '(cells in x direction)') - data_write(file, clawdata, 'my', '(cells in y direction)') - if ndim == 3: - data_write(file, clawdata, 'mz', '(cells in z direction)') + write_clawdata_noamr(clawdata, file) + write_clawdata_amr(clawdata, file) + file.close() - data_write(file, clawdata, 'amrlevels_max', '(max number of grid levels)') - if len(clawdata.refinement_ratio_x) < max(abs(clawdata.amrlevels_max)-1, 1): + +def write_clawdata_amr(clawdata, file): + r""" + Write the input parameters only used by AMRClaw. + """ + + num_dim = clawdata.num_dim + data_write(file, clawdata.amr_levels_max, 'amr_levels_max') + + num_ratios = max(abs(clawdata.amr_levels_max)-1, 1) + if len(clawdata.refinement_ratios_x) < num_ratios: raise ValueError("*** Error in data parameter: " + \ - "require len(refinement_ratio_x) >= %s " % max(abs(clawdata.amrlevels_max) - 1, 1)) - if len(clawdata.refinement_ratio_y) < max(abs(clawdata.amrlevels_max)-1, 1): + "require len(refinement_ratios_x) >= %s " % num_ratios) + if len(clawdata.refinement_ratios_y) < num_ratios: raise ValueError("*** Error in data parameter: " + \ - "require len(refinement_ratio_y) >= %s " % max(abs(clawdata.amrlevels_max) - 1, 1)) - data_write(file, clawdata, 'refinement_ratio_x', '(refinement ratios)') - data_write(file, clawdata, 'refinement_ratio_y', '(refinement ratios)') - if ndim == 3: - if len(clawdata.refinement_ratio_z) < max(abs(clawdata.amrlevels_max)-1, 1): + "require len(refinement_ratios_y) >= %s " % num_ratios) + data_write(file, clawdata.refinement_ratios_x, 'refinement_ratios_x') + data_write(file, clawdata.refinement_ratios_y, 'refinement_ratios_y') + if num_dim == 3: + if len(clawdata.refinement_ratios_z) < num_ratios: raise ValueError("*** Error in data parameter: " + \ - "require len(refinement_ratio_z) >= %s " % max(abs(clawdata.amrlevels_max) - 1, 1)) - data_write(file, clawdata, 'refinement_ratio_z', '(refinement ratios)') - if len(clawdata.refinement_ratio_t) < max(abs(clawdata.amrlevels_max)-1, 1): + "require len(refinement_ratios_z) >= %s " % num_ratios) + data_write(file, clawdata.refinement_ratios_z, 'refinement_ratios_z') + if len(clawdata.refinement_ratios_t) < num_ratios: raise ValueError("*** Error in data parameter: " + \ - "require len(refinement_ratio_t) >= %s " % max(abs(clawdata.amrlevels_max) - 1, 1)) - data_write(file, clawdata, 'refinement_ratio_t', '(refinement ratios)') + "require len(refinement_ratios_t) >= %s " % num_ratios) + data_write(file, clawdata.refinement_ratios_t, 'refinement_ratios_t') data_write(file, clawdata, None) # writes blank line - data_write(file, clawdata, 't0', '(initial time)') - - data_write(file, clawdata, 'output_style', '(style of specifying output times)') - if clawdata.output_style == 1: - data_write(file, clawdata, 'output_ntimes', '(number of output times)') - data_write(file, clawdata, 'tfinal', '(final time)') - elif clawdata.output_style == 2: - clawdata.output_ntimes = len(clawdata.output_times) - data_write(file, clawdata, 'output_ntimes', '(number of output times)') - data_write(file, clawdata, 'output_times', '(output times)') - elif clawdata.output_style == 3: - data_write(file, clawdata, 'output_step_interval', '(output every output_step_interval steps)') - data_write(file, clawdata, 'total_steps', '(number of steps to take)') - elif clawdata.output_style == 4: - data_write(file, clawdata, 'output_time_interval', '(time between outputs)') - data_write(file, clawdata, 'tfinal', '(final time)') - else: - print '*** Error: unrecognized output_style = ',output_style - raise - return - - data_write(file, clawdata, None) - data_write(file, clawdata, 'dt_initial', '(initial time step dt)') - data_write(file, clawdata, 'dt_max', '(max allowable dt)') - data_write(file, clawdata, 'cfl_max', '(max allowable Courant number)') - data_write(file, clawdata, 'cfl_desired', '(desired Courant number)') - data_write(file, clawdata, 'steps_max', '(max time steps per call to claw)') - data_write(file, clawdata, None) - data_write(file, clawdata, 'dt_variable', '(1 for variable dt, 0 for fixed)') - data_write(file, clawdata, 'order', '(1 or 2)') - if ndim == 1: - data_write(file, clawdata, 'order_trans', '(not used in 1d)') - else: - data_write(file, clawdata, 'order_trans', '(transverse order)') - data_write(file, clawdata, 'dimensional_split', '(use dimensional splitting?)') - - data_write(file, clawdata, 'verbosity', '(verbosity of output)') - data_write(file, clawdata, 'src_split', '(source term splitting)') - data_write(file, clawdata, 'mcapa', '(aux index for capacity fcn)') - data_write(file, clawdata, 'maux', '(number of aux variables)') - if len(clawdata.auxtype) != clawdata.maux: - file.close() - print "*** Error: An auxtype array must be specified of length maux" - raise AttributeError, "require len(clawdata.auxtype) == clawdata.maux" - for i in range(clawdata.maux): - file.write("'%s'\n" % clawdata.auxtype[i]) - data_write(file, clawdata, None) - - data_write(file, clawdata, 'meqn', '(number of equations)') - data_write(file, clawdata, 'mwaves', '(number of waves)') - data_write(file, clawdata, 'limiter', '(limiter choice for each wave)') - if clawdata.fwave: - clawdata.add_attribute('ifwave',1) - else: - clawdata.add_attribute('ifwave',0) - data_write(file, clawdata, 'ifwave', '(use f-wave form?)') - data_write(file, clawdata, None) - data_write(file, clawdata, 'xlower', '(xlower)') - data_write(file, clawdata, 'xupper', '(xupper)') - data_write(file, clawdata, 'ylower', '(ylower)') - data_write(file, clawdata, 'yupper', '(yupper)') - if ndim == 3: - data_write(file, clawdata, 'zlower', '(zlower)') - data_write(file, clawdata, 'zupper', '(zupper)') - data_write(file, clawdata, None) - - for bdry in ['xlower','xupper','ylower','yupper','zlower','zupper']: - bc = getattr(clawdata, 'bc_'+bdry, None) - if bc == 'user': - setattr(clawdata, 'bc_'+bdry, 0) - if bc == 'extrap': - setattr(clawdata, 'bc_'+bdry, 1) - if bc == 'periodic': - setattr(clawdata, 'bc_'+bdry, 2) - if bc == 'wall': - setattr(clawdata, 'bc_'+bdry, 3) - - data_write(file, clawdata, 'num_ghost', '(number of ghost cells)') - data_write(file, clawdata, 'bc_xlower', '(type of BC at xlower)') - data_write(file, clawdata, 'bc_xupper', '(type of BC at xupper)') - data_write(file, clawdata, 'bc_ylower', '(type of BC at ylower)') - data_write(file, clawdata, 'bc_yupper', '(type of BC at yupper)') - if ndim == 3: - data_write(file, clawdata, 'bc_zlower', '(type of BC at zlower)') - data_write(file, clawdata, 'bc_zupper', '(type of BC at zupper)') - data_write(file, clawdata, None) - - data_write(file, clawdata, 'restart', '(1 to restart from a past run)') - data_write(file, clawdata, 'checkpt_style', '(how checkpoints specified)') - - if clawdata.checkpt_style == 2: - clawdata.checkpt_ntimes = len(clawdata.checkpt_times) - data_write(file, clawdata, 'checkpt_ntimes', '(number of checkpoint times)') - data_write(file, clawdata, 'checkpt_times', '(checkpoint times)') - elif clawdata.checkpt_style == 3: - data_write(file, clawdata, 'checkpt_interval', '(step interval for checkpoint)') - elif clawdata.checkpt_style == 4: - print "*** Error: checkpt_style==4 not yet implemented" - raise ValueError("Invalid checkpt_style") - data_write(file, clawdata, 'checkpt_time_interval', '(time interval for checkpoint)') - else: - print "*** Error, unrecognized checkpt_style = ",clawdata.checkpt_style - print "*** Require: 0,2,3, or 4" - raise ValueError("Unrecognized checkpt_style") - return + data_write(file, clawdata.flag_richardson, 'flag_richardson') + data_write(file, clawdata.flag_richardson_tol, 'flag_richardson_tol') + data_write(file, clawdata.flag2refine, 'flag2refine') + data_write(file, clawdata.flag2refine_tol, 'flag2refine_tol') + data_write(file, clawdata.regrid_interval, 'regrid_interval') + data_write(file, clawdata.regrid_buffer_width, 'regrid_buffer_width') + data_write(file, clawdata.clustering_cutoff, 'clustering_cutoff') + data_write(file, clawdata.verbosity_regrid, 'verbosity_regrid') data_write(file, clawdata, None) - data_write(file, clawdata, 'flag_richardson', '(use Richardson extrap?)') - data_write(file, clawdata, 'flag_richardson_tol', '(tolerance for Richardson)') - data_write(file, clawdata, 'flag_gradient', '(use gradient flagging?)') - data_write(file, clawdata, 'flag_gradient_tol', '(tolerance used for gradient)') - data_write(file, clawdata, 'regrid_interval', '(how often to regrid)') - data_write(file, clawdata, 'regrid_buffer_width', '(buffer zone around flagged pts)') - data_write(file, clawdata, 'clustering_cutoff', '(efficiency cutoff for clustering)') - data_write(file, clawdata, 'verbosity_regrid', '(what levels to print grid info)') data_write(file, clawdata, None) - if clawdata.output_format == 'ascii': - clawdata.output_format = 1 - elif clawdata.output_format == 'binary': - clawdata.output_format = 2 - else: - if clawdata.output_format not in [1,2]: - print "*** Unrecognized output_format: ",clawdata.output_format - raise - return - data_write(file, clawdata, 'output_format', '(format for fort.q files)') - - if clawdata.output_q_components=='all': - clawdata.output_q_components = range(1,clawdata.meqn+1) - - nq_components = len(clawdata.output_q_components) - clawdata.add_attribute('output_nq_components',nq_components) - data_write(file, clawdata, 'output_nq_components', '(number of q vals to print)') - if clawdata.output_nq_components > 0: - data_write(file, clawdata, 'output_q_components', '(which components of q)') - - if clawdata.output_aux_components=='all': - clawdata.output_aux_components = range(1,clawdata.maux+1) - - naux_components = len(clawdata.output_aux_components) - clawdata.add_attribute('output_naux_components',naux_components) - data_write(file, clawdata, 'output_naux_components', '(number of aux vals to print)') - if clawdata.output_naux_components > 0: - data_write(file, clawdata, 'output_aux_components', '(which components of aux)') - data_write(file, clawdata, 'output_aux_onlyonce', '(only at t0?)') - - - - data_write(file, clawdata, None) - - data_write(file, clawdata, 'dprint', '(print domain flags)') - data_write(file, clawdata, 'eprint', '(print err est flags)') - data_write(file, clawdata, 'edebug', '(even more err est flags)') - data_write(file, clawdata, 'gprint', '(grid bisection/clustering)') - data_write(file, clawdata, 'nprint', '(proper nesting output)') - data_write(file, clawdata, 'pprint', '(proj. of tagged points)') - data_write(file, clawdata, 'rprint', '(print regridding summary)') - data_write(file, clawdata, 'sprint', '(space/memory output)') - data_write(file, clawdata, 'tprint', '(time step reporting each level)') - data_write(file, clawdata, 'uprint', '(update/upbnd reporting)') + data_write(file, clawdata.dprint, 'dprint') + data_write(file, clawdata.eprint, 'eprint') + data_write(file, clawdata.edebug, 'edebug') + data_write(file, clawdata.gprint, 'gprint') + data_write(file, clawdata.nprint, 'nprint') + data_write(file, clawdata.pprint, 'pprint') + data_write(file, clawdata.rprint, 'rprint') + data_write(file, clawdata.sprint, 'sprint') + data_write(file, clawdata.tprint, 'tprint') + data_write(file, clawdata.uprint, 'uprint') data_write(file, clawdata, None) +def regions_and_gauges(): + r""" + Placeholder... where to put these? + """ clawdata.add_attribute('nregions', len(clawdata.regions)) - data_write(file, clawdata, 'nregions', '(nregions)') + data_write(file, clawdata.nregions, 'nregions') for regions in clawdata.regions: file.write(8*" %g" % tuple(regions) +"\n") clawdata.add_attribute('ngauges', len(clawdata.gauges)) - data_write(file, clawdata, 'ngauges', '(ngauges)') + data_write(file, clawdata.ngauges, 'ngauges') gaugeno_used = [] for gauge in clawdata.gauges: gaugeno = gauge[0] @@ -756,7 +711,6 @@ def make_amrclawdatafile(clawdata): #file.write("%4i %19.10e %17.10e %13.6e %13.6e\n" % tuple(gauge)) file.write(5*" %g" % tuple(gauge) +"\n") - file.close() def make_userdatafile(userdata): r""" @@ -772,8 +726,8 @@ def make_userdatafile(userdata): # write all the parameters: for param in userdata._attributes: - data_write(file, userdata, param, \ - userdata.__descr__[param]) + descr = userdata.__descr__.get(param, '') + data_write(file, getattr(userdata,param), param, descr) file.close() @@ -810,10 +764,10 @@ class ClawRunData(ClawData): r""" Object that will be written out to claw.data. """ - def __init__(self, pkg, ndim): + def __init__(self, pkg, num_dim): super(ClawRunData,self).__init__() self.add_attribute('pkg',pkg) - self.add_attribute('ndim',ndim) + self.add_attribute('num_dim',num_dim) self.add_attribute('datalist',[]) @@ -821,7 +775,7 @@ def __init__(self, pkg, ndim): self.add_attribute('xclawcmd', 'xclaw') # Required data set for basic run parameters: - clawdata = ClawInputData(ndim) + clawdata = ClawInputData(num_dim) self.add_attribute('clawdata', clawdata) self.datalist.append(clawdata) @@ -829,7 +783,7 @@ def __init__(self, pkg, ndim): self.add_attribute('xclawcmd', 'xamr') # Required data set for basic run parameters: - clawdata = AmrclawInputData(ndim) + clawdata = AmrclawInputData(num_dim) self.add_attribute('clawdata', clawdata) self.datalist.append(clawdata) @@ -837,10 +791,10 @@ def __init__(self, pkg, ndim): self.add_attribute('xclawcmd', 'xgeoclaw') # Required data set for basic run parameters: - clawdata = AmrclawInputData(ndim) + clawdata = AmrclawInputData(num_dim) self.add_attribute('clawdata', clawdata) self.datalist.append(clawdata) - geodata = GeoclawInputData(ndim) + geodata = GeoclawInputData(num_dim) self.add_attribute('geodata', geodata) self.datalist.append(geodata) @@ -863,7 +817,7 @@ def add_GaugeData(self): r""" Create a gaugedata attribute for writing to gauges.data. """ - gaugedata = GaugeData(self.ndim) + gaugedata = GaugeData(self.num_dim) self.datalist.append(gaugedata) self.gaugedata = gaugedata return gaugedata @@ -900,11 +854,11 @@ def write(self): make_userdatafile(self) -class GeoclawInputData(ClawData): +class GeoclawInputData(Data): r""" Object that will be written out to the various GeoClaw data files. """ - def __init__(self, ndim): + def __init__(self, num_dim): super(GeoclawInputData,self).__init__() # Set default values: diff --git a/src/python/clawutil/conversion/Makefile_amrclaw_2d b/src/python/clawutil/conversion/Makefile_amrclaw_2d new file mode 100644 index 00000000..66608769 --- /dev/null +++ b/src/python/clawutil/conversion/Makefile_amrclaw_2d @@ -0,0 +1,135 @@ + +# Makefile for Clawpack code in this directory. +# This version only sets the local files and frequently changed +# options, and then includes the standard makefile pointed to by CLAWMAKE. +CLAWMAKE = $(CLAWUTIL)/src/Makefile.common + +# See the above file for details and a list of make options, or type +# make .help +# at the unix prompt. + + +# Adjust these variables if desired: +# ---------------------------------- + +CLAW_PKG = amrclaw # Clawpack package to use +EXE = xamr # Executable to create +SETRUN_FILE = setrun.py # File containing function to make data +OUTDIR = _output # Directory for output +SETPLOT_FILE = setplot.py # File containing function to set plots +PLOTDIR = _plots # Directory for plots + +OVERWRITE ?= True # False ==> make a copy of OUTDIR first +RESTART ?= False # Should = clawdata.restart in setrun + +# Environment variable FC should be set to fortran compiler, e.g. gfortran +FC ?= gfortran # default if not set as environment variable +# Add any desired compiler flags such as -g here: +FFLAGS ?= -O2 +#FFLAGS = -W -Wall -fbounds-check -Wunderflow -O -ffpe-trap=zero,overflow,underflow -g + +# --------------------------------- +# List of sources for this program: +# --------------------------------- +MODULES = \ + $(AMRCLAW)/src/2d/amr_module.f90 \ + +SOURCES = \ + qinit.f \ + setprob.f \ + $(RIEMANN)/src/rpn2_advection.f \ + $(RIEMANN)/src/rpt2_advection.f \ + $(AMRCLAW)/src/2d/amr2.f \ + $(AMRCLAW)/src/2d/setaux.f \ + $(AMRCLAW)/src/2d/bc2amr.f \ + $(AMRCLAW)/src/2d/b4step2.f \ + $(AMRCLAW)/src/2d/qad.f \ + $(AMRCLAW)/src/2d/src2.f \ + $(AMRCLAW)/src/2d/src1d.f \ + $(AMRCLAW)/src/2d/advanc.f \ + $(AMRCLAW)/src/2d/bound.f \ + $(AMRCLAW)/src/2d/stepgrid.f \ + $(AMRCLAW)/src/2d/auxcoarsen.f \ + $(AMRCLAW)/src/2d/fixcapaq.f \ + $(AMRCLAW)/src/2d/estdt.f \ + $(AMRCLAW)/src/2d/init_iflags.f \ + $(AMRCLAW)/src/2d/igetsp.f \ + $(AMRCLAW)/src/2d/reclam.f \ + $(AMRCLAW)/src/2d/birect.f \ + $(AMRCLAW)/src/2d/cleanup.f \ + $(AMRCLAW)/src/2d/colate.f \ + $(AMRCLAW)/src/2d/bufnst.f \ + $(AMRCLAW)/src/2d/spest.f \ + $(AMRCLAW)/src/2d/flag2refine.f \ + $(AMRCLAW)/src/2d/allowflag.f \ + $(AMRCLAW)/src/2d/errest.f \ + $(AMRCLAW)/src/2d/errf1.f \ + $(AMRCLAW)/src/2d/gfixup.f \ + $(AMRCLAW)/src/2d/filval.f \ + $(AMRCLAW)/src/2d/filpatch.f \ + $(AMRCLAW)/src/2d/prefilp.f \ + $(AMRCLAW)/src/2d/flglvl.f \ + $(AMRCLAW)/src/2d/fluxad.f \ + $(AMRCLAW)/src/2d/fluxsv.f \ + $(AMRCLAW)/src/2d/ginit.f \ + $(AMRCLAW)/src/2d/grdfit.f \ + $(AMRCLAW)/src/2d/intfil.f \ + $(AMRCLAW)/src/2d/moment.f \ + $(AMRCLAW)/src/2d/nestck.f \ + $(AMRCLAW)/src/2d/prepf.f \ + $(AMRCLAW)/src/2d/prepc.f \ + $(AMRCLAW)/src/2d/projec.f \ + $(AMRCLAW)/src/2d/signs.f \ + $(AMRCLAW)/src/2d/findcut.f \ + $(AMRCLAW)/src/2d/smartbis.f \ + $(AMRCLAW)/src/2d/putnod.f \ + $(AMRCLAW)/src/2d/putsp.f \ + $(AMRCLAW)/src/2d/regrid.f \ + $(AMRCLAW)/src/2d/setgrd.f \ + $(AMRCLAW)/src/2d/setuse.f \ + $(AMRCLAW)/src/2d/stst1.f \ + $(AMRCLAW)/src/2d/tick.f \ + $(AMRCLAW)/src/2d/trimbd.f \ + $(AMRCLAW)/src/2d/update.f \ + $(AMRCLAW)/src/2d/nodget.f \ + $(AMRCLAW)/src/2d/upbnd.f \ + $(AMRCLAW)/src/2d/basic.f \ + $(AMRCLAW)/src/2d/outval.f \ + $(AMRCLAW)/src/2d/copysol.f \ + $(AMRCLAW)/src/2d/outvar.f \ + $(AMRCLAW)/src/2d/outmsh.f \ + $(AMRCLAW)/src/2d/outtre.f \ + $(AMRCLAW)/src/2d/domain.f \ + $(AMRCLAW)/src/2d/setflags.f \ + $(AMRCLAW)/src/2d/shiftset.f \ + $(AMRCLAW)/src/2d/conck.f \ + $(AMRCLAW)/src/2d/domshrink.f \ + $(AMRCLAW)/src/2d/domprep.f \ + $(AMRCLAW)/src/2d/domup.f \ + $(AMRCLAW)/src/2d/domcopy.f \ + $(AMRCLAW)/src/2d/coarsen.f \ + $(AMRCLAW)/src/2d/intcopy.f \ + $(AMRCLAW)/src/2d/preintcopy.f \ + $(AMRCLAW)/src/2d/icall.f \ + $(AMRCLAW)/src/2d/preicall.f \ + $(AMRCLAW)/src/2d/step2.f90 \ + $(AMRCLAW)/src/2d/flux2.f \ + $(AMRCLAW)/src/2d/inlinelimiter.f \ + $(AMRCLAW)/src/2d/cstore.f \ + $(AMRCLAW)/src/2d/saveqc.f \ + $(AMRCLAW)/src/2d/valout.f \ + $(AMRCLAW)/src/2d/check.f \ + $(AMRCLAW)/src/2d/restrt.f \ + $(AMRCLAW)/src/2d/setgauges.f \ + $(AMRCLAW)/src/2d/dumpgauge.f \ + $(AMRCLAW)/src/2d/quick_sort1.f \ + $(AMRCLAW)/src/2d/opendatafile.f \ + $(AMRCLAW)/src/2d/init_alloc.f90 \ + $(AMRCLAW)/src/2d/restrt_alloc.f90 \ + $(AMRCLAW)/src/2d/resize_alloc.f90 \ +# $(AMRCLAW)/src/2d/resize_alloc_static.f90 \ + +#------------------------------------------------------------------- +# Include Makefile containing standard definitions and make options: +include $(CLAWMAKE) + diff --git a/src/python/clawutil/conversion/convert.py b/src/python/clawutil/conversion/convert.py new file mode 100644 index 00000000..1072ecce --- /dev/null +++ b/src/python/clawutil/conversion/convert.py @@ -0,0 +1,170 @@ +""" +Conversion module for 2d amrclaw setrun.py file from 4.6 to 5.0 format. +""" + + +import os,sys + +clawutil = os.environ['CLAWUTIL'] + '/src/python/clawutil' +template_amrclaw_2d = open(clawutil \ + + '/conversion/setrun_template_amrclaw_2d.py').read() + +sys.path.insert(0,os.getcwd()) +sys.path.append(os.getcwd() + '/pyclaw') + +def convert_setrun(setrun_file='setrun.py', claw_pkg=None): + + setrun_text = open(setrun_file).readlines() + if claw_pkg is None: + for line in setrun_text: + if 'claw_pkg' in line: + if 'classic' in line: + claw_pkg = 'classic' + elif 'amrclaw' in line: + claw_pkg = 'amrclaw' + elif 'geoclaw' in line: + claw_pkg = 'geoclaw' + + if claw_pkg is None: + raise ValueError("*** Could not determine claw_pkg from %s" \ + % setrun_file) + + + setrun_module = os.path.splitext(setrun_file)[0] + exec('from %s import setrun' % setrun_module) + + rundata = setrun() + c = rundata.clawdata + + limiter_map = {0:'none',1:'minmod',2:'superbee',3:'mc',4:'vanleer'} + limiter = [limiter_map.get(i,i) for i in c.mthlim] + + + bc_map = {0:'user', 1:'extrap', 2:'periodic', 3:'wall'} + for b in ['xlower','xupper','ylower','yupper']: + exec("s = bc_map.get(c.mthbc_%s, c.mthbc_%s)" % (b,b)) + if type(s) is str: + exec("""mthbc_%s = "'%s'" """ % (b,s)) + else: + exec("mthbc_%s = '%s'" % (b,s)) + + try: + gauges = c.gauges + except: + gauges = [] + + try: + regions = c.regions + except: + regions = [] + + + # ------------------------------------------------- + # Mapping from old setrun variables to new ones: + + mapping = { + 'xlower': c.xlower, + 'xupper': c.xupper, + 'ylower': c.ylower, + 'yupper': c.yupper, + 'mx': c.mx, + 'my': c.my, + 'num_eqn': c.meqn, + 'num_aux': c.maux, + 'capa_index': c.mcapa, + 't0': c.t0, + 'num_output_times': c.nout, + 'tfinal': c.tfinal, + 'dt_variable': str(c.dt_variable==1), + 'dt_initial': c.dt_initial, + 'dt_max': c.dt_max, + 'cfl_desired': c.cfl_desired, + 'cfl_max': c.cfl_max, + 'steps_max': c.max_steps, + 'order': c.order, + 'transverse_waves': c.order_trans, + 'num_waves': c.mwaves, + 'limiter': str(limiter), + 'source_split': c.src_split, + 'num_ghost': c.mbc, + 'mthbc_xlower': mthbc_xlower, + 'mthbc_xupper': mthbc_xupper, + 'mthbc_ylower': mthbc_ylower, + 'mthbc_yupper': mthbc_yupper, + 'amr_levels_max': abs(c.mxnest), + 'refinement_ratios_x': str(c.inratx), + 'refinement_ratios_y': str(c.inraty), + 'refinement_ratios_t': str(c.inratt), + 'aux_type': str(c.auxtype), + 'flag_richardson': str(c.tol > 0.), + 'flag_richardson_tol': abs(c.tol), + 'flag2refine': str(c.tolsp > 0.), + 'flag2refine_tol': c.tolsp, + 'regrid_interval': c.kcheck, + 'regrid_buffer_width': c.ibuff, + 'clustering_cutoff': c.cutoff, + 'gauges': gauges, + 'regions': regions, + } + + if claw_pkg == 'amrclaw': + newtext = template_amrclaw_2d.format(**mapping) + else: + raise ValueError("*** convert not yet implemented for claw_pkg = %s" \ + % claw_pkg) + + setrun_text = open(setrun_file).readlines() + for line in setrun_text: + if "new_UserData" in line: + if line.strip()[0] != '#': + print "*** Warning: call to new_UserData detected..." + print line + print "*** User data lines from %s have NOT been copied to %s"\ + % (setrun_file, new_setrun_file) + + + os.system("mv %s original_%s" % (setrun_file,setrun_file)) + print 'Moved %s to original_%s ' % (setrun_file,setrun_file) + open(setrun_file,'w').write(newtext) + print 'Created ', setrun_file + return claw_pkg + + +def copy_Makefile(claw_pkg): + if claw_pkg == 'amrclaw': + try: + os.system("mv Makefile original_Makefile") + os.system("cp %s/conversion/Makefile_amrclaw_2d Makefile" \ + % clawutil) + except: + raise Exception("*** Error copying Makefile") + else: + raise ValueError("*** convert not yet implemented for claw_pkg = %s" \ + % claw_pkg) + + print "Moved Makefile to original_Makefile" + print "Created new Makefile template -- must be customized!" + print "*** Edit Makefile based on original_Makefile, e.g. point to" + print "*** any local files, correct Riemann solver, etc." + print "*** You might have to make other modifications to local fortran" + print "*** files -- in particular indices have been reordered!" + + +def convert_setplot(setplot_file='setplot.py'): + + setplot_text = open(setplot_file).read() + setplot_text = setplot_text.replace('pyclaw.plotters','clawpack.visclaw') + setplot_text = setplot_text.replace('2d_grid','2d_patch') + setplot_text = setplot_text.replace('gridedges','patchedges') + setplot_text = setplot_text.replace('gridlines','celledges') + + os.system("mv %s original_%s" % (setplot_file,setplot_file)) + print 'Moved %s to original_%s ' % (setplot_file,setplot_file) + open(setplot_file,'w').write(setplot_text) + print 'Created ', setplot_file + + +if __name__ == "__main__": + claw_pkg = convert_setrun() + copy_Makefile(claw_pkg) + convert_setplot() diff --git a/src/python/clawutil/conversion/pyclaw/__init__.py b/src/python/clawutil/conversion/pyclaw/__init__.py new file mode 100644 index 00000000..8611613a --- /dev/null +++ b/src/python/clawutil/conversion/pyclaw/__init__.py @@ -0,0 +1,3 @@ + +__all__ = ['data'] + diff --git a/src/python/clawutil/conversion/pyclaw/data.py b/src/python/clawutil/conversion/pyclaw/data.py new file mode 100644 index 00000000..acda3564 --- /dev/null +++ b/src/python/clawutil/conversion/pyclaw/data.py @@ -0,0 +1,1429 @@ +#!/usr/bin/env python +# encoding: utf-8 +r""" +Data Module + +Contains the general class definition and the subclasses of the Clawpack data +objects. + +:Authors: + Kyle T. Mandli and Randall J. LeVeque (2008-08-07) Initial version + + Randall J. LeVeque (2008-08-07) Plotting data objects + + Alan McIntyre (2009-01-01) Speed ups and rebuilding of Data + + Kyle T. Mandli (2009-04-01) Stripped down and improved version +""" +# ============================================================================ +# Copyright (C) 2008 Kyle T. Mandli +# Copyright (C) 2008 Randall J. LeVeque +# Copyright (C) 2009 Alan McIntyre +# +# Distributed under the terms of the Berkeley Software Distribution (BSD) +# license +# http://www.opensource.org/licenses/ +# ============================================================================ + +import shutil +import os +import copy +import re +import logging + +# ========== Parse Value Utility Function ==================================== +def _parse_value(value): + r""" + Attempt to make sense of a value string from a config file. If the + value is not obviously an integer, float, or boolean, it is returned as + a string stripped of leading and trailing whitespace. + + :Input: + - *value* - (string) Value string to be parsed + + :Output: + - (id) - Appropriate object based on *value* + """ + value = value.strip() + if not value: + return None + + # assume that values containing spaces are lists of values + if len(value.split()) > 1: + return [_parse_value(vv) for vv in value.split()] + + try: + # see if it's an integer + value = int(value) + except ValueError: + try: + # see if it's a float + value = float(value) + except ValueError: + # see if it's a bool + if value[0] == 'T': + value = True + elif value[0] == 'F': + value = False + + return value + + +# ============================================================================ +# General Data Class +# ============================================================================ +class Data(object): + r""" + Generalized clawpack data object + + Generalized class for Clawpack data. Contains generic methods for reading + and writing data to and from a data file. + + :Initialization: + Input: + - *data_files* - (List of strings) Paths to data files to be read in, + an empty data object can be created by providing no data files. + - *attributes* - (List of strings) List of required attribute names + which will be initialized to None. + + :Version: 1.2 (2009-04-01) + """ + + __name__ = 'Data' + + def __init__(self, data_files=[], attributes=None): + """ + Initialize a Data object + + See :class:`Data` for more info. + """ + + # Internal bookkeeping variables + self.__attributes = [] + self.__owners = {} + + # Setup data logger + self.logger = logging.getLogger('data') + + # Initialize from attribute list provided + if attributes: + for attr in attributes: + self.add_attribute(attr,None,None) + + # Read data files from data_files list + if isinstance(data_files, basestring): + data_files = [data_files] + elif not isinstance(data_files, list): + raise Exception("data_files must be a list of strings") + + if len(data_files) > 0: + self.read(data_files) + + # ========== Return string representation of this Data Object ======== + def __str__(self): + output = "%s%s%s\n" % ("Name".ljust(25),"Value".ljust(12), + "Owner".ljust(12)) + for (k,v) in self.iteritems(): + output += "%s%s%s\n" % (str(k).ljust(25), + str(v).ljust(12), + str(self.__owners[k]).ljust(12)) + return output + + # ========== Access Methods ============================================== + def add_attribute(self, name, value=None, owner=None): + r""" + Adds an attribute called name to the data object + + If an attribute needs to be added to the object, this routine must be + called or the attribute will not be written out. + + :Input: + - *name* - (string) Name of the data attribute + - *value* - (id) Value to set *name* to, defaults to None + - *owner* - (id) Owner of this particular attribute + """ + setattr(self,name,value) + self.__owners[name] = owner + if name not in self.__attributes: + self.__attributes.append(name) + + def remove_attributes(self, arg_list): + r""" + Remove the listed attributes. + """ + + # Convert to list if args is not already a list + if not isinstance(arg_list,list): + arg_list = [arg_list] + + for arg in arg_list: + self.__owners.pop(arg) + self.__attributes.remove(arg) + delattr(self,arg) + + def attributes(): + def fget(self): return self.__attributes + return locals() + attributes = property(**attributes()) + + def has_attribute(self,name): + r""" + Check if this data object has the given attributes + + :Input: + - *name* - (string) Name of attribute + + :Output: + - (bool) - True if data object contains a data attribute name + """ + return name in self.__attributes + + def set_owner(self,name,owner): + r""" + Sets the owner of the given data + + :Input: + - *name* - (string) Name of attribute + - *owner* - (id) Owner of the attribute + """ + if name not in self.__attributes: + raise KeyError("No attribute named %s" % name) + self.__owners[name] = owner + + def get_owner(self,name): + r""" + Returns the owner of the data attribute name + + :Input: + - *name* - (string) Name of attribute + + :Output: + - (id) - Owner of attribute + """ + return self.__owners[name] + + def get_owners(self,supplementary_file=None): + r""" + Returns a list of owners excluding the owner None + + If supplementary_file is provided, None is replace by that owner. + + :Input: + - *supplementary_file* - (string) Supplementary file, defaults to + None. + + :Output: + - (list) - Returns a list of owners + """ + owners = [] + for (key,owner) in self.__owners.iteritems(): + if owner is None: + self.__owners[key] = supplementary_file + # This simultaneously finds one instance of an owner and tests + # to see if the supplementary_file is not None + owner = self.__owners[key] + if owner not in owners and owner is not None: + owners.append(owner) + return owners + + def iteritems(self): + r""" + Returns an iterator of keys and values from this object + + :Output: + - (Iterator) Iterator over keys and values + """ + return [(k,getattr(self,k)) for k in self.__attributes] + + # ========== Read in a collection of data files ========================== + def read(self,data_paths): + r""" + Read data in from a clawpack style data file. + + Any lines of the form:: + + values =: name + + is used to set an attribute of self. + + INPUT + + data_paths : Path to a data file to be read in, can also be a list + of files to be read in. + """ + if isinstance(data_paths, basestring): + data_paths = [data_paths] + + for filename in data_paths: + filename = os.path.abspath(filename) + if not os.path.exists(filename): + raise Exception("No such data file: %s" % filename) + + # self.logger.info("Reading from %s" % filename) + + for lineno, line in enumerate(file(filename)): + if '=:' not in line: + continue + + value, tail = line.split('=:') + varname = tail.split()[0] + + oldval = getattr(self, varname, None) + newval = _parse_value(value) + if oldval is not None: + vals = "(old=%r,new=%r)" % (oldval, newval) + # self.logger.debug("Overwriting %s %s" % (varname, vals)) + + # if newval is None: + # self.logger.warning("Empty value for %s" % varname) + + self.add_attribute(varname, newval, filename) + + # ========== Write out the data from this object ========================= + def write(self,data_files=None,supplementary_file=None): + r""" + Write out the contents of the data object + + This method writes out the current required attributes of the data + object to a file or list of files. The format for the output will be + in the form:: + + values =: name + + The order is either retained from the files in the data list or + written in the order they are in the list of attributes + + The behavior is determined by the arguments passed into the routine: + + No arguments: + + The contents of the object will be written out to the files listed + in data_files only if each attribute is contained in the file. + This implies that if an attribute is not located in any of the + files in data_files then it will not be written out to file. + + If data_files is provided and data_files is a valid owner: + + Write out the attributes appropriate to that file + + If data_files is provided and not(data_files in owner): + + Write all attributes to the data_file given + + If supplementary_file is provided: + + Write out any attributes without an owner to this file, all owned + attributes will be written out to the approriate files + """ + + # Expand supplementary_file if it is not None + if supplementary_file is not None: + supplementary_file = os.path.abspath(supplementary_file) + + # Create list of owners + owners = self.get_owners(supplementary_file=supplementary_file) + + #print 'in write: data_files = ',data_files + # Write to the entire owner list + if data_files is None: + file_list = owners + elif isinstance(data_files,str): + #print '

in write'; import sys; sys.exit(0) + path = os.path.abspath(data_files) + if path not in owners: + # Create temporary data file to store all data in the object + try: + temp_file = open(path,'w') + for key in self.__attributes: + temp_file.write("1 =: %s\n" % key) + temp_file.close() + except IOError, (errno, strerror): + print "I/O error(%s): %s" % (errno, strerror) + raise + except: + raise + # Add the path to the file_list + file_list = [path] + # Check to make sure all the paths are in the owner list + elif isinstance(data_files,list): + for path in data_files: + path = os.path.abspath(path) + if not(path in owners): + print "%s is not a registered owner!" + return + file_list = data_files + else: + raise Exception("Invalid argument list given to write().") + + # Create temporary supplementary file if requested + if supplementary_file is not None: + try: + sup_file = open(supplementary_file,'w') + for attr in self.__attributes: + if self.__owners[attr] is supplementary_file: + sup_file.write("-1 =: %s\n" % attr) + self.__owners[attr] = supplementary_file + sup_file.close() + except: + raise + + + # Regular expression for searching each file + regexp = re.compile(r"(?P.*)=:(?P.*)") + # Loop over each file + for data_path in file_list: + # Open the data file and temporary file + try: + data_file = open(data_path,'r') + except(IOError): + raise + try: + temp_path = os.path.join(os.path.dirname(data_path), \ + 'temp.' + os.path.basename(data_path)) + temp_file = open(temp_path,'w') + except(IOError): + print "IOERROR" + raise + + try: + for line in data_file: + result = regexp.search(line) + if result: + name = re.split(r'\s+', result.group('name').strip())[0] + values = re.split(r'\s+', result.group('values').strip()) + + if len(values) == 0: + line = '' + elif self.__owners[name] == data_path or \ + data_path not in self.__owners: + newvalues = getattr(self,name) + + # Convert newvalues to an appropriate string repr + if isinstance(newvalues,tuple) \ + | isinstance(newvalues,list): + # Remove [], (), and ',' + newstring = repr(newvalues)[1:-1] + newstring = newstring.replace(',','') + elif isinstance(newvalues,bool): + if newvalues: + newstring = 'T' + else: + newstring = 'F' + else: + newstring = repr(newvalues) + + newstart = str.ljust(newstring,25) + line = line.replace(result.group('values') + "=:", \ + newstart + " =:") + else: + print "Error writing out %s" % name + raise AttributeError, name + + # Write the new line + temp_file.write(line) + except: + raise + + # Close files + data_file.close() + temp_file.close() + + # Rename the temporary file to the data_path name + try: + shutil.move(temp_path,data_path) + except: + raise + + +#----------------------------------------------------------------------- + +# New classes and functions for dealing with data in setrun function. + +class ClawInputData(Data): + r""" + Object that will be written out to claw.data. + """ + def __init__(self, ndim): + super(ClawInputData,self).__init__() + self.add_attribute('ndim',ndim) + + # Set default values: + if ndim == 1: + self.add_attribute('mx',100) + self.add_attribute('nout',5) + self.add_attribute('outstyle',1) + self.add_attribute('tfinal',1.0) + self.add_attribute('dt_initial',1.e-5) + self.add_attribute('dt_max',1.e99) + self.add_attribute('cfl_desired',0.9) + self.add_attribute('cfl_max',1.0) + self.add_attribute('max_steps',5000) + self.add_attribute('dt_variable',1) + self.add_attribute('order',2) + self.add_attribute('order_trans',0) + self.add_attribute('verbosity',0) + self.add_attribute('src_split',0) + self.add_attribute('mcapa',0) + self.add_attribute('maux',0) + self.add_attribute('meqn',1) + self.add_attribute('mwaves',1) + self.add_attribute('mthlim',[4]) + self.add_attribute('t0',0.) + self.add_attribute('xlower',0.) + self.add_attribute('xupper',1.) + self.add_attribute('mbc',2) + self.add_attribute('mthbc_xlower',1) + self.add_attribute('mthbc_xupper',1) + self.add_attribute('restart',0) + self.add_attribute('N_restart',0) + + + elif ndim == 2: + self.add_attribute('mx',100) + self.add_attribute('my',100) + self.add_attribute('nout',5) + self.add_attribute('outstyle',1) + self.add_attribute('tfinal',1.0) + self.add_attribute('dt_initial',1.e-5) + self.add_attribute('dt_max',1.e99) + self.add_attribute('cfl_desired',0.9) + self.add_attribute('cfl_max',1.0) + self.add_attribute('max_steps',5000) + self.add_attribute('dt_variable',1) + self.add_attribute('order',2) + self.add_attribute('order_trans',2) + self.add_attribute('verbosity',0) + self.add_attribute('src_split',0) + self.add_attribute('mcapa',0) + self.add_attribute('maux',0) + self.add_attribute('meqn',1) + self.add_attribute('mwaves',1) + self.add_attribute('mthlim',[4]) + self.add_attribute('t0',0.) + self.add_attribute('xlower',0.) + self.add_attribute('xupper',1.) + self.add_attribute('ylower',0.) + self.add_attribute('yupper',1.) + self.add_attribute('mbc',2) + self.add_attribute('mthbc_xlower',1) + self.add_attribute('mthbc_xupper',1) + self.add_attribute('mthbc_ylower',1) + self.add_attribute('mthbc_yupper',1) + self.add_attribute('restart',0) + self.add_attribute('N_restart',0) + + else: + raise AttributeError("Only ndim=1 or 2 supported so far") + + def write(self): + print 'Creating data file claw.data for use with xclaw' + make_clawdatafile(self) + + + +class AmrclawInputData(Data): + r""" + Object that will be written out to amr2ez.data. + """ + def __init__(self, ndim): + super(AmrclawInputData,self).__init__() + self.add_attribute('ndim',ndim) + + # Set default values: + if ndim == 1: + self.add_attribute('mx',100) + self.add_attribute('nout',5) + self.add_attribute('outstyle',1) + self.add_attribute('tfinal',1.0) + self.add_attribute('dt_initial',1.e-5) + self.add_attribute('dt_max',1.e99) + self.add_attribute('cfl_desired',0.9) + self.add_attribute('cfl_max',1.0) + self.add_attribute('max_steps',5000) + self.add_attribute('dt_variable',1) + self.add_attribute('order',2) + self.add_attribute('order_trans',0) + self.add_attribute('verbosity',0) + self.add_attribute('src_split',0) + self.add_attribute('mcapa',0) + self.add_attribute('maux',0) + self.add_attribute('meqn',1) + self.add_attribute('mwaves',1) + self.add_attribute('mthlim',[4]) + self.add_attribute('t0',0.) + self.add_attribute('xlower',0.) + self.add_attribute('xupper',1.) + self.add_attribute('mbc',2) + self.add_attribute('mthbc_xlower',1) + self.add_attribute('mthbc_xupper',1) + self.add_attribute('restart',0) + self.add_attribute('N_restart',0) + + # attributes need only since AMR is done using 2d amrclaw: + self.add_attribute('my',1) + self.add_attribute('ylower',0.) + self.add_attribute('yupper',1.) + self.add_attribute('mthbc_ylower',1) + self.add_attribute('mthbc_yupper',1) + self.add_attribute('inraty',[1,1,1,1,1,1]) + + elif ndim == 2: + self.add_attribute('mx',100) + self.add_attribute('my',100) + self.add_attribute('nout',5) + self.add_attribute('outstyle',1) + self.add_attribute('tfinal',1.0) + self.add_attribute('dt_initial',1.e-5) + self.add_attribute('dt_max',1.e99) + self.add_attribute('cfl_desired',0.9) + self.add_attribute('cfl_max',1.0) + self.add_attribute('max_steps',5000) + self.add_attribute('dt_variable',1) + self.add_attribute('order',2) + self.add_attribute('order_trans',2) + self.add_attribute('verbosity',0) + self.add_attribute('src_split',0) + self.add_attribute('mcapa',0) + self.add_attribute('maux',0) + self.add_attribute('meqn',1) + self.add_attribute('mwaves',1) + self.add_attribute('mthlim',[4]) + self.add_attribute('t0',0.) + self.add_attribute('xlower',0.) + self.add_attribute('xupper',1.) + self.add_attribute('ylower',0.) + self.add_attribute('yupper',1.) + self.add_attribute('mbc',2) + self.add_attribute('mthbc_xlower',1) + self.add_attribute('mthbc_xupper',1) + self.add_attribute('mthbc_ylower',1) + self.add_attribute('mthbc_yupper',1) + self.add_attribute('restart',0) + self.add_attribute('N_restart',0) + self.add_attribute('inraty',[1]) + + if ndim <= 2: + # AMR parameters: + self.add_attribute('mxnest',-1) + self.add_attribute('inratx',[1]) + self.add_attribute('inratt',[1]) + self.add_attribute('auxtype',[]) + self.add_attribute('restart',False) + self.add_attribute('checkpt_iousr',1000) + self.add_attribute('tchk',[]) + self.add_attribute('tol',-1.0) + self.add_attribute('tolsp',0.05) + self.add_attribute('kcheck',2) + self.add_attribute('ibuff',3) + self.add_attribute('cutoff',0.7) + self.add_attribute('PRINT',False) + self.add_attribute('NCAR',False) + self.add_attribute('fortq',True) + self.add_attribute('dprint',False) + self.add_attribute('eprint',False) + self.add_attribute('edebug',False) + self.add_attribute('gprint',False) + self.add_attribute('nprint',False) + self.add_attribute('pprint',False) + self.add_attribute('rprint',False) + self.add_attribute('sprint',False) + self.add_attribute('tprint',False) + self.add_attribute('uprint',False) + else: + print '*** Error: only ndim=1 or 2 supported so far ***' + raise AttributeError("Only ndim=1 or 2 supported so far") + + def write(self): + print 'Creating data file amr2ez.data for use with xamr' + make_amrclawdatafile(self) + make_setgauges_datafile(self) + + +class SharpclawInputData(Data): + r""" + Object that will be written out to claw.data. + """ + def __init__(self, ndim): + super(SharpclawInputData,self).__init__() + self.add_attribute('ndim',ndim) + + # Set default values: + if ndim == 1: + self.add_attribute('mx',100) + self.add_attribute('nout',5) + self.add_attribute('outstyle',1) + self.add_attribute('tfinal',1.0) + self.add_attribute('dt_initial',1.e-5) + self.add_attribute('dt_max',1.e99) + self.add_attribute('cfl_desired',0.9) + self.add_attribute('cfl_max',1.0) + self.add_attribute('max_steps',5000) + self.add_attribute('dt_variable',1) + self.add_attribute('verbosity',0) + self.add_attribute('mcapa',0) + self.add_attribute('maux',0) + self.add_attribute('meqn',1) + self.add_attribute('mwaves',1) + self.add_attribute('mthlim',[5]) + self.add_attribute('t0',0.) + self.add_attribute('xlower',0.) + self.add_attribute('xupper',1.) + self.add_attribute('mbc',3) + self.add_attribute('mthbc_xlower',1) + self.add_attribute('mthbc_xupper',1) + self.add_attribute('restart',0) + self.add_attribute('N_restart',0) + self.add_attribute('time_integrator',2) + self.add_attribute('tfluct_solver',0) + self.add_attribute('char_decomp',0) + self.add_attribute('lim_type',2) + self.add_attribute('src_term',0) + + + elif ndim == 2: + self.add_attribute('mx',100) + self.add_attribute('my',100) + self.add_attribute('nout',5) + self.add_attribute('outstyle',1) + self.add_attribute('tfinal',1.0) + self.add_attribute('dt_initial',1.e-5) + self.add_attribute('dt_max',1.e99) + self.add_attribute('cfl_desired',0.9) + self.add_attribute('cfl_max',1.0) + self.add_attribute('max_steps',5000) + self.add_attribute('dt_variable',1) + self.add_attribute('verbosity',0) + self.add_attribute('mcapa',0) + self.add_attribute('maux',0) + self.add_attribute('meqn',1) + self.add_attribute('mwaves',1) + self.add_attribute('mthlim',[5]) + self.add_attribute('t0',0.) + self.add_attribute('xlower',0.) + self.add_attribute('xupper',1.) + self.add_attribute('ylower',0.) + self.add_attribute('yupper',1.) + self.add_attribute('mbc',3) + self.add_attribute('mthbc_xlower',1) + self.add_attribute('mthbc_xupper',1) + self.add_attribute('mthbc_ylower',1) + self.add_attribute('mthbc_yupper',1) + self.add_attribute('restart',0) + self.add_attribute('N_restart',0) + self.add_attribute('time_integrator',2) + self.add_attribute('tfluct_solver',0) + self.add_attribute('char_decomp',0) + self.add_attribute('lim_type',2) + self.add_attribute('src_term',0) + + else: + raise AttributeError("Only ndim=1 or 2 supported so far") + + def write(self): + print 'Creating data file sharpclaw.data for use with xsclaw' + make_sharpclawdatafile(self) + + + + +def open_datafile(name, datasource='setrun.py'): + """ + Open a data file and write a warning header. + Warning header starts with '#' character. These lines are skipped if + data file is opened using the library routine opendatafile. + + :Input: + - *name* - (string) Name of data file + - *datasource* - (string) Source for the data + + :Output: + - (file) - file object + """ + + import string + + source = string.ljust(datasource,25) + file = open(name, 'w') + file.write('########################################################\n') + file.write('### DO NOT EDIT THIS FILE: GENERATED AUTOMATICALLY ####\n') + file.write('### To modify data, edit %s ####\n' % source) + file.write('### and then "make .data" ####\n') + file.write('########################################################\n\n') + + return file + + +def data_write(file, dataobj, name=None, descr=''): + r""" + Write out value to data file, in the form :: + + value =: name descr + + Remove brackets and commas from lists, and replace booleans by T/F. + Also convert numpy array to a list first. + + :Input: + - *name* - (string) normally a string defining the variable, + ``if name==None``, write a blank line. + - *descr* - (string) A short description to appear on the line + """ + + import string + if name is None: + file.write('\n') + else: + try: + value = getattr(dataobj, name) + except: + print "Variable missing: ",name + print " from dataobj = ", dataobj + raise + # Convert value to an appropriate string repr + import numpy + if isinstance(value,numpy.ndarray): + value = list(value) + if isinstance(value,tuple) | isinstance(value,list): + # Remove [], (), and ',' + string_value = repr(value)[1:-1] + string_value = string_value.replace(',','') + elif isinstance(value,bool): + if value: + string_value = 'T' + else: + string_value = 'F' + else: + string_value = repr(value) + padded_value = string.ljust(string_value, 25) + padded_name = string.ljust(name, 12) + file.write('%s =: %s %s\n' % (padded_value, padded_name, descr)) + + +def make_clawdatafile(clawdata): + r""" + Take the data specified in clawdata and write it to claw.data in the + form required by the Fortran code lib/main.f95. + """ + + + # open file and write a warning header: + file = open_datafile('claw.data') + + ndim = clawdata.ndim + data_write(file, clawdata, 'ndim', '(number of dimensions)') + data_write(file, clawdata, 'mx', '(cells in x direction)') + if ndim > 1: + data_write(file, clawdata, 'my', '(cells in y direction)') + if ndim == 3: + data_write(file, clawdata, 'mz', '(cells in z direction)') + data_write(file, clawdata, None) # writes blank line + + data_write(file, clawdata, 'nout', '(number of output times)') + data_write(file, clawdata, 'outstyle', '(style of specifying output times)') + if clawdata.outstyle == 1: + data_write(file, clawdata, 'tfinal', '(final time)') + elif clawdata.outstyle == 2: + data_write(file, clawdata, 'tout', '(output times)') + elif clawdata.outstyle == 3: + data_write(file, clawdata, 'iout', '(output every iout steps)') + elif clawdata.outstyle == 4: + data_write(file, clawdata, 'output_time_interval', '(between outputs)') + data_write(file, clawdata, 'tfinal', '(final time)') + + else: + print '*** Error: unrecognized outstyle' + raise + return + + data_write(file, clawdata, None) + data_write(file, clawdata, 'dt_initial', '(initial time step dt)') + data_write(file, clawdata, 'dt_max', '(max allowable dt)') + data_write(file, clawdata, 'cfl_max', '(max allowable Courant number)') + data_write(file, clawdata, 'cfl_desired', '(desired Courant number)') + data_write(file, clawdata, 'max_steps', '(max time steps per call to claw)') + data_write(file, clawdata, None) + data_write(file, clawdata, 'dt_variable', '(1 for variable dt, 0 for fixed)') + data_write(file, clawdata, 'order', '(1 or 2)') + if ndim == 1: + data_write(file, clawdata, 'order_trans', '(not used in 1d)') + else: + data_write(file, clawdata, 'order_trans', '(transverse order)') + data_write(file, clawdata, 'verbosity', '(verbosity of output)') + data_write(file, clawdata, 'src_split', '(source term splitting)') + data_write(file, clawdata, 'mcapa', '(aux index for capacity fcn)') + data_write(file, clawdata, 'maux', '(number of aux variables)') + data_write(file, clawdata, None) + + data_write(file, clawdata, 'meqn', '(number of equations)') + data_write(file, clawdata, 'mwaves', '(number of waves)') + data_write(file, clawdata, 'mthlim', '(limiter choice for each wave)') + data_write(file, clawdata, None) + + data_write(file, clawdata, 't0', '(initial time)') + data_write(file, clawdata, 'xlower', '(xlower)') + data_write(file, clawdata, 'xupper', '(xupper)') + if ndim > 1: + data_write(file, clawdata, 'ylower', '(ylower)') + data_write(file, clawdata, 'yupper', '(yupper)') + if ndim == 3: + data_write(file, clawdata, 'zlower', '(zlower)') + data_write(file, clawdata, 'zupper', '(zupper)') + data_write(file, clawdata, None) + + data_write(file, clawdata, 'mbc', '(number of ghost cells)') + data_write(file, clawdata, 'mthbc_xlower', '(type of BC at xlower)') + data_write(file, clawdata, 'mthbc_xupper', '(type of BC at xupper)') + if ndim > 1: + data_write(file, clawdata, 'mthbc_ylower', '(type of BC at ylower)') + data_write(file, clawdata, 'mthbc_yupper', '(type of BC at yupper)') + if ndim == 3: + data_write(file, clawdata, 'mthbc_zlower', '(type of BC at zlower)') + data_write(file, clawdata, 'mthbc_zupper', '(type of BC at zupper)') + + data_write(file, clawdata, 'restart', '(1 to restart from a past run)') + data_write(file, clawdata, 'N_restart', '(which frame to restart from)') + data_write(file, clawdata, None) + + file.close() + +def make_amrclawdatafile(clawdata): + r""" + Take the data specified in clawdata and write it to claw.data in the + form required by the Fortran code lib/main.f95. + """ + + + # open file and write a warning header: + file = open_datafile('amr2ez.data') + + ndim = clawdata.ndim + #data_write(file, clawdata, 'ndim', '(number of dimensions)') + data_write(file, clawdata, 'mx', '(cells in x direction)') + data_write(file, clawdata, 'my', '(cells in y direction)') + if ndim == 3: + data_write(file, clawdata, 'mz', '(cells in z direction)') + + data_write(file, clawdata, 'mxnest', '(max number of grid levels)') + if len(clawdata.inratx) < max(abs(clawdata.mxnest)-1, 1): + raise ValueError("*** Error in data parameter: " + \ + "require len(inratx) >= %s " % max(abs(clawdata.mxnest) - 1, 1)) + data_write(file, clawdata, 'inratx', '(refinement ratios)') + if clawdata.mxnest < 0: + # negative mxnest indicates anisotropic refinement + if len(clawdata.inraty) < max(abs(clawdata.mxnest)-1, 1): + raise ValueError("*** Error in data parameter: " + \ + "require len(inraty) >= %s " % max(abs(clawdata.mxnest) - 1, 1)) + data_write(file, clawdata, 'inraty', '(refinement ratios)') + if ndim == 3: + if len(clawdata.inratz) < max(abs(clawdata.mxnest)-1, 1): + raise ValueError("*** Error in data parameter: " + \ + "require len(inratz) >= %s " % max(abs(clawdata.mxnest) - 1, 1)) + data_write(file, clawdata, 'inratz', '(refinement ratios)') + if len(clawdata.inratt) < max(abs(clawdata.mxnest)-1, 1): + raise ValueError("*** Error in data parameter: " + \ + "require len(inratt) >= %s " % max(abs(clawdata.mxnest) - 1, 1)) + data_write(file, clawdata, 'inratt', '(refinement ratios)') + + data_write(file, clawdata, None) # writes blank line + + data_write(file, clawdata, 'nout', '(number of output times)') + data_write(file, clawdata, 'outstyle', '(style of specifying output times)') + if clawdata.outstyle == 1: + data_write(file, clawdata, 'tfinal', '(final time)') + elif clawdata.outstyle == 2: + data_write(file, clawdata, 'tout', '(output times)') + elif clawdata.outstyle == 3: + data_write(file, clawdata, 'iout', '(output every iout steps)') + elif clawdata.outstyle == 4: + data_write(file, clawdata, 'output_time_interval', '(between outputs)') + data_write(file, clawdata, 'tfinal', '(final time)') + else: + print '*** Error: unrecognized outstyle' + raise + return + + data_write(file, clawdata, None) + data_write(file, clawdata, 'dt_initial', '(initial time step dt)') + data_write(file, clawdata, 'dt_max', '(max allowable dt)') + data_write(file, clawdata, 'cfl_max', '(max allowable Courant number)') + data_write(file, clawdata, 'cfl_desired', '(desired Courant number)') + data_write(file, clawdata, 'max_steps', '(max time steps per call to claw)') + data_write(file, clawdata, None) + data_write(file, clawdata, 'dt_variable', '(1 for variable dt, 0 for fixed)') + data_write(file, clawdata, 'order', '(1 or 2)') + if ndim == 1: + data_write(file, clawdata, 'order_trans', '(not used in 1d)') + else: + data_write(file, clawdata, 'order_trans', '(transverse order)') + data_write(file, clawdata, 'verbosity', '(verbosity of output)') + data_write(file, clawdata, 'src_split', '(source term splitting)') + data_write(file, clawdata, 'mcapa', '(aux index for capacity fcn)') + data_write(file, clawdata, 'maux', '(number of aux variables)') + if len(clawdata.auxtype) != clawdata.maux: + file.close() + print "*** Error: An auxtype array must be specified of length maux" + raise AttributeError, "require len(clawdata.auxtype) == clawdata.maux" + for i in range(clawdata.maux): + file.write("'%s'\n" % clawdata.auxtype[i]) + data_write(file, clawdata, None) + + data_write(file, clawdata, 'meqn', '(number of equations)') + data_write(file, clawdata, 'mwaves', '(number of waves)') + data_write(file, clawdata, 'mthlim', '(limiter choice for each wave)') + data_write(file, clawdata, None) + + data_write(file, clawdata, 't0', '(initial time)') + data_write(file, clawdata, 'xlower', '(xlower)') + data_write(file, clawdata, 'xupper', '(xupper)') + data_write(file, clawdata, 'ylower', '(ylower)') + data_write(file, clawdata, 'yupper', '(yupper)') + if ndim == 3: + data_write(file, clawdata, 'zlower', '(zlower)') + data_write(file, clawdata, 'zupper', '(zupper)') + data_write(file, clawdata, None) + + data_write(file, clawdata, 'mbc', '(number of ghost cells)') + data_write(file, clawdata, 'mthbc_xlower', '(type of BC at xlower)') + data_write(file, clawdata, 'mthbc_xupper', '(type of BC at xupper)') + data_write(file, clawdata, 'mthbc_ylower', '(type of BC at ylower)') + data_write(file, clawdata, 'mthbc_yupper', '(type of BC at yupper)') + if ndim == 3: + data_write(file, clawdata, 'mthbc_zlower', '(type of BC at zlower)') + data_write(file, clawdata, 'mthbc_zupper', '(type of BC at zupper)') + data_write(file, clawdata, None) + + data_write(file, clawdata, 'restart', '(1 to restart from a past run)') + data_write(file, clawdata, 'checkpt_iousr', '(how often to checkpoint)') + if clawdata.checkpt_iousr < 0: + data_write(file, clawdata, 'tchk', '(checkpoint times)') + data_write(file, clawdata, None) + + data_write(file, clawdata, 'tol', '(tolerance for Richardson extrap)') + data_write(file, clawdata, 'tolsp', '(tolerance used in flag2refine)') + data_write(file, clawdata, 'kcheck', '(how often to regrid)') + data_write(file, clawdata, 'ibuff', '(buffer zone around flagged pts)') + data_write(file, clawdata, 'cutoff', '(efficiency cutoff for grid gen.)') + data_write(file, clawdata, None) + + data_write(file, clawdata, 'PRINT', '(print to fort.amr)') + data_write(file, clawdata, 'NCAR', '(obsolete!)') + data_write(file, clawdata, 'fortq', '(Output to fort.q* files)') + data_write(file, clawdata, None) + + data_write(file, clawdata, 'dprint', '(print domain flags)') + data_write(file, clawdata, 'eprint', '(print err est flags)') + data_write(file, clawdata, 'edebug', '(even more err est flags)') + data_write(file, clawdata, 'gprint', '(grid bisection/clustering)') + data_write(file, clawdata, 'nprint', '(proper nesting output)') + data_write(file, clawdata, 'pprint', '(proj. of tagged points)') + data_write(file, clawdata, 'rprint', '(print regridding summary)') + data_write(file, clawdata, 'sprint', '(space/memory output)') + data_write(file, clawdata, 'tprint', '(time step reporting each level)') + data_write(file, clawdata, 'uprint', '(update/upbnd reporting)') + + file.close() + + +def make_sharpclawdatafile(clawdata): + r""" + Take the data specified in clawdata and write it to sharpclaw.data in the + form required by the Fortran code lib/main.f95. + """ + + + # open file and write a warning header: + file = open_datafile('sharpclaw.data') + + ndim = clawdata.ndim + data_write(file, clawdata, 'ndim', '(number of dimensions)') + data_write(file, clawdata, 'mx', '(cells in x direction)') + if ndim > 1: + data_write(file, clawdata, 'my', '(cells in y direction)') + if ndim == 3: + data_write(file, clawdata, 'mz', '(cells in z direction)') + data_write(file, clawdata, None) # writes blank line + + data_write(file, clawdata, 'nout', '(number of output times)') + data_write(file, clawdata, 'outstyle', '(style of specifying output times)') + if clawdata.outstyle == 1: + data_write(file, clawdata, 'tfinal', '(final time)') + elif clawdata.outstyle == 2: + data_write(file, clawdata, 'tout', '(output times)') + elif clawdata.outstyle == 3: + data_write(file, clawdata, 'iout', '(output every iout steps)') + else: + print '*** Error: unrecognized outstyle' + raise + return + + data_write(file, clawdata, None) + data_write(file, clawdata, 'dt_initial', '(initial time step dt)') + data_write(file, clawdata, 'dt_max', '(max allowable dt)') + data_write(file, clawdata, 'cfl_max', '(max allowable Courant number)') + data_write(file, clawdata, 'cfl_desired', '(desired Courant number)') + data_write(file, clawdata, 'max_steps', '(max time steps per call to claw)') + data_write(file, clawdata, None) + data_write(file, clawdata, 'dt_variable', '(1 for variable dt, 0 for fixed)') + data_write(file, clawdata, 'time_integrator', '(time stepping scheme)') + data_write(file, clawdata, 'verbosity', '(verbosity of output)') + data_write(file, clawdata, 'src_term', '(source term present)') + data_write(file, clawdata, 'mcapa', '(aux index for capacity fcn)') + data_write(file, clawdata, 'maux', '(number of aux variables)') + data_write(file, clawdata, 'tfluct_solver', '(total fluctuation solver)') + data_write(file, clawdata, 'char_decomp', '(characteristic decomposition)') + data_write(file, clawdata, None) + + data_write(file, clawdata, 'meqn', '(number of equations)') + data_write(file, clawdata, 'mwaves', '(number of waves)') + data_write(file, clawdata, 'lim_type', '(0=None, 1=TVD, 2=WENO)') + data_write(file, clawdata, 'mthlim', '(limiter choice for each wave)') + data_write(file, clawdata, None) + + data_write(file, clawdata, 't0', '(initial time)') + data_write(file, clawdata, 'xlower', '(xlower)') + data_write(file, clawdata, 'xupper', '(xupper)') + if ndim > 1: + data_write(file, clawdata, 'ylower', '(ylower)') + data_write(file, clawdata, 'yupper', '(yupper)') + if ndim == 3: + data_write(file, clawdata, 'zlower', '(zlower)') + data_write(file, clawdata, 'zupper', '(zupper)') + data_write(file, clawdata, None) + + data_write(file, clawdata, 'mbc', '(number of ghost cells)') + data_write(file, clawdata, 'mthbc_xlower', '(type of BC at xlower)') + data_write(file, clawdata, 'mthbc_xupper', '(type of BC at xupper)') + if ndim > 1: + data_write(file, clawdata, 'mthbc_ylower', '(type of BC at ylower)') + data_write(file, clawdata, 'mthbc_yupper', '(type of BC at yupper)') + if ndim == 3: + data_write(file, clawdata, 'mthbc_zlower', '(type of BC at zlower)') + data_write(file, clawdata, 'mthbc_zupper', '(type of BC at zupper)') + + data_write(file, clawdata, 'restart', '(1 to restart from a past run)') + data_write(file, clawdata, 'N_restart', '(which frame to restart from)') + data_write(file, clawdata, None) + + file.close() + + +def make_userdatafile(userdata): + r""" + Create the data file using the parameters in userdata. + The parameters will be written to this file in the same order they were + specified using userdata.add_attribute. + Presumably the user will read these in using a Fortran routine, such as + setprob.f95, and the order is important. + """ + + # open file and write a warning header: + file = open_datafile(userdata._UserData__fname) + + # write all the parameters: + for param in userdata.attributes: + data_write(file, userdata, param, \ + userdata._UserData__descr[param]) + + file.close() + +def make_setgauges_datafile(clawdata): + """ + Create setgauges.data using gauges attribute of clawdata. + """ + gauges = getattr(clawdata,'gauges',[]) + ngauges = len(gauges) + + print 'Creating data file setgauges.data' + # open file and write a warning header: + file = open_datafile('setgauges.data') + file.write("%4i =: ngauges\n" % ngauges) + gaugeno_used = [] + for gauge in gauges: + gaugeno = gauge[0] + if gaugeno in gaugeno_used: + print "*** Gauge number %s used more than once! " % gaugeno + raise Exception("Repeated gauge number") + else: + gaugeno_used.append(gauge[0]) + file.write("%4i %19.10e %17.10e %13.6e %13.6e\n" % tuple(gauge)) + # or use this variant with =: + #gauge.append(gaugeno) + #file.write("%4i %19.10e %17.10e %13.6e %13.6e =: gauge%s\n" % tuple(gauge)) + file.close() + + +#----------------------------------------------------- +# New version 6/30/09 + +class ClawRunData(Data): + r""" + Object that will be written out to claw.data. + """ + def __init__(self, pkg, ndim): + super(ClawRunData,self).__init__() + self.add_attribute('pkg',pkg) + self.add_attribute('ndim',ndim) + self.add_attribute('datalist',[]) + + + if pkg.lower() in ['classic', 'classicclaw']: + self.add_attribute('xclawcmd', 'xclaw') + + # Required data set for basic run parameters: + clawdata = ClawInputData(ndim) + self.add_attribute('clawdata', clawdata) + self.datalist.append(clawdata) + + elif pkg.lower() in ['amrclaw', 'amr']: + self.add_attribute('xclawcmd', 'xamr') + + # Required data set for basic run parameters: + clawdata = AmrclawInputData(ndim) + self.add_attribute('clawdata', clawdata) + self.datalist.append(clawdata) + + elif pkg.lower() in ['geoclaw']: + self.add_attribute('xclawcmd', 'xgeoclaw') + + # Required data set for basic run parameters: + clawdata = AmrclawInputData(ndim) + self.add_attribute('clawdata', clawdata) + self.datalist.append(clawdata) + geodata = GeoclawInputData(ndim) + self.add_attribute('geodata', geodata) + self.datalist.append(geodata) + + elif pkg.lower() in ['sharpclaw']: + self.add_attribute('xclawcmd', 'xsclaw') + + # Required data set for basic run parameters: + clawdata = SharpclawInputData(ndim) + self.add_attribute('clawdata', clawdata) + self.datalist.append(clawdata) + + else: + raise AttributeError("Unrecognized Clawpack pkg = %s" % pkg) + + def new_UserData(self,name,fname): + r""" + Create a new attribute called name + for application specific data to be written + to the data file fname. + """ + userdata = UserData(fname) + self.datalist.append(userdata) + exec('self.%s = userdata' % name) + return userdata + + def add_GaugeData(self): + r""" + Create a gaugedata attribute for writing to gauges.data. + """ + gaugedata = GaugeData(self.ndim) + self.datalist.append(gaugedata) + self.gaugedata = gaugedata + return gaugedata + + def write(self): + for d in self.datalist: + d.write() + +class UserData(Data): + r""" + Object that will be written out to user file such as setprob.data, as + determined by the fname attribute. + """ + def __init__(self, fname): + super(UserData,self).__init__() + self.__fname = fname # file to be read by Fortran for this data + self.__descr = {} # dictionary to hold descriptions + + def add_param(self,name,value,descr=''): + self.add_attribute(name,value) + self.__descr[name] = descr + + def write(self): + print 'Creating data file %s' % self.__fname + make_userdatafile(self) + +class GaugeData(Data): + r""" + Data to be written out to gauge.data specifying gauges. + DEPRECATED: Use GeoclawInputData instead. + """ + def __init__(self, ndim): + super(GaugeData,self).__init__() + self.add_attribute('ndim',ndim) + self.add_attribute('ngauges',0) + self.__gauge_dict = {} + + def add_gauge(self,gaugeno,location,time_interval): + self.__gauge_dict[gaugeno] = (gaugeno, location, time_interval) + self.ngauges = len(self.__gauge_dict) + + def write(self): + print 'Creating data file gauges.data' + + # open file and write a warning header: + file = open_datafile('gauges.data') + + data_write(file, self, 'ngauges', 'Number of gauges') + data_write(file, self, None) + + ndim = self.ndim + + # write a line for each gauge: + for (gaugeno, gdata) in self.__gauge_dict.iteritems(): + tmin = gdata[2][0] + tmax = gdata[2][1] + if isinstance(gdata[1],(list,tuple)): + xyz = gdata[1] + x = xyz[0] + if ndim>1: + y = xyz[1] + if ndim>2: + z = xyz[2] + else: + x = gdata[1] + + if ndim==1: + file.write('%i %e %e %e' % (gdata[0],x,tmin,tmax)) + elif ndim==2: + file.write('%i %e %e %e %e' % (gdata[0],x,y,tmin,tmax)) + elif ndim==3: + file.write('%i %e %e %e %e %e' % (gdata[0],x,y,z,tmin,tmax)) + + printxyz = {1: 'x ', 2: 'x y ', 3: 'x y z'} + file.write('\n\n# Format of each line: \n# gaugeno %s tmin tmax'\ + % printxyz[ndim]) + file.close() + +class GeoclawInputData(Data): + r""" + Object that will be written out to the various GeoClaw data files. + """ + def __init__(self, ndim): + super(GeoclawInputData,self).__init__() + + # Set default values: + self.add_attribute('igravity',1) + self.add_attribute('iqinit',0) + self.add_attribute('icoriolis',1) + self.add_attribute('Rearth',6367500.0) + self.add_attribute('variable_dt_refinement_ratios',False) + # NEED TO CONTINUE! + + def write(self): + + print 'Creating data file setgeo.data' + # open file and write a warning header: + file = open_datafile('setgeo.data') + data_write(file, self, 'igravity') + data_write(file, self, 'gravity') + data_write(file, self, 'icoordsys') + data_write(file, self, 'icoriolis') + data_write(file, self, 'Rearth') + data_write(file, self, 'variable_dt_refinement_ratios') + file.close() + + print 'Creating data file settsunami.data' + # open file and write a warning header: + file = open_datafile('settsunami.data') + data_write(file, self, 'sealevel') + data_write(file, self, 'drytolerance') + data_write(file, self, 'wavetolerance') + data_write(file, self, 'depthdeep') + data_write(file, self, 'maxleveldeep') + data_write(file, self, 'ifriction') + data_write(file, self, 'coeffmanning') + data_write(file, self, 'frictiondepth') + file.close() + + print 'Creating data file settopo.data' + # open file and write a warning header: + file = open_datafile('settopo.data') + self.ntopofiles = len(self.topofiles) + data_write(file, self, 'ntopofiles') + for tfile in self.topofiles: + try: + fname = os.path.abspath(tfile[-1]) + except: + print "*** Error: file not found: ",tfile[-1] + raise MissingFile("file not found") + file.write("\n'%s' \n " % fname) + file.write("%3i %3i %3i %20.10e %20.10e \n" % tuple(tfile[:-1])) + file.close() + + print 'Creating data file setdtopo.data' + # open file and write a warning header: + file = open_datafile('setdtopo.data') + self.mdtopofiles = len(self.dtopofiles) + data_write(file, self, 'mdtopofiles') + data_write(file, self, None) + for tfile in self.dtopofiles: + try: + fname = "'%s'" % os.path.abspath(tfile[-1]) + except: + print "*** Error: file not found: ",tfile[-1] + raise MissingFile("file not found") + file.write("\n%s \n" % fname) + file.write("%3i %3i %3i\n" % tuple(tfile[:-1])) + file.close() + + print 'Creating data file setqinit.data' + # open file and write a warning header: + file = open_datafile('setqinit.data') + # self.iqinit tells which component of q is perturbed! + data_write(file, self, 'iqinit') + data_write(file, self, None) + for tfile in self.qinitfiles: + try: + fname = "'%s'" % os.path.abspath(tfile[-1]) + except: + print "*** Error: file not found: ",tfile[-1] + raise MissingFile("file not found") + file.write("\n%s \n" % fname) + file.write("%3i %3i \n" % tuple(tfile[:-1])) + file.close() + + make_setgauges_datafile(self) + +# print 'Creating data file setgauges.data' +# # open file and write a warning header: +# file = open_datafile('setgauges.data') +# self.ngauges = len(self.gauges) +# data_write(file, self, 'ngauges') +# data_write(file, self, None) +# gaugeno_used = [] +# for gauge in self.gauges: +# gaugeno = gauge[0] +# if gaugeno in gaugeno_used: +# print "*** Gauge number %s used more than once! " % gaugeno +# raise Exception("Repeated gauge number") +# else: +# gaugeno_used.append(gauge[0]) +# gauge.append(gaugeno) +# file.write("%3i %19.10e %19.10e %15.6e %15.6e =: gauge%s\n" % tuple(gauge)) +# file.close() + + + print 'Creating data file setfixedgrids.data' + # open file and write a warning header: + file = open_datafile('setfixedgrids.data') + self.nfixedgrids = len(self.fixedgrids) + data_write(file, self, 'nfixedgrids') + data_write(file, self, None) + for fixedgrid in self.fixedgrids: + file.write(11*"%g " % tuple(fixedgrid) +"\n") + file.close() + + + print 'Creating data file setregions.data' + # open file and write a warning header: + file = open_datafile('setregions.data') + self.nregions = len(self.regions) + data_write(file, self, 'nregions') + data_write(file, self, None) + for regions in self.regions: + file.write(8*"%g " % tuple(regions) +"\n") + file.close() + diff --git a/src/python/clawutil/conversion/setrun_template_amrclaw_2d.py b/src/python/clawutil/conversion/setrun_template_amrclaw_2d.py new file mode 100644 index 00000000..c7cb4d9e --- /dev/null +++ b/src/python/clawutil/conversion/setrun_template_amrclaw_2d.py @@ -0,0 +1,346 @@ +""" +Module to set up run time parameters for Clawpack. + +The values set in the function setrun are then written out to data files +that will be read in by the Fortran code. + +""" + +import os +import numpy as np + +#------------------------------ +def setrun(claw_pkg='amrclaw'): +#------------------------------ + + """ + Define the parameters used for running Clawpack. + + INPUT: + claw_pkg expected to be "amrclaw" for this setrun. + + OUTPUT: + rundata - object of class ClawRunData + + """ + + from clawpack.clawutil import clawdata + + + assert claw_pkg.lower() == 'amrclaw', "Expected claw_pkg = 'amrclaw'" + + num_dim = 2 + rundata = clawdata.ClawRunData(claw_pkg, num_dim) + + #------------------------------------------------------------------ + # Problem-specific parameters to be written to setprob.data: + #------------------------------------------------------------------ + # Sample setup to write one line to setprob.data ... + #probdata = rundata.new_UserData(name='probdata',fname='setprob.data') + #probdata.add_param('u', 0.5, 'ubar advection velocity') + + #------------------------------------------------------------------ + # Standard Clawpack parameters to be written to claw.data: + # (or to amrclaw.data for AMR) + #------------------------------------------------------------------ + + clawdata = rundata.clawdata # initialized when rundata instantiated + + + # Set single grid parameters first. + # See below for AMR parameters. + + + # --------------- + # Spatial domain: + # --------------- + + # Number of space dimensions: + clawdata.num_dim = num_dim + + # Lower and upper edge of computational domain: + clawdata.lower[0] = {xlower:e} # xlower + clawdata.upper[0] = {xupper:e} # xupper + clawdata.lower[1] = {ylower:e} # ylower + clawdata.upper[1] = {yupper:e} # yupper + + # Number of grid cells: + clawdata.num_cells[0] = {mx:d} # mx + clawdata.num_cells[1] = {my:d} # my + + + # --------------- + # Size of system: + # --------------- + + # Number of equations in the system: + clawdata.num_eqn = {num_eqn:d} + + # Number of auxiliary variables in the aux array (initialized in setaux) + clawdata.num_aux = {num_aux:d} + + # Index of aux array corresponding to capacity function, if there is one: + clawdata.capa_index = {capa_index:d} + + + # ------------- + # Initial time: + # ------------- + + clawdata.t0 = {t0:f} + + + # Restart from checkpoint file of a previous run? + # Note: If restarting, you must also change the Makefile to set: + # RESTART = True + # If restarting, t0 above should be from original run, and the + # restart_file 'fort.chkNNNNN' specified below should be in + # the OUTDIR indicated in Makefile. + + clawdata.restart = False # True to restart from prior results + clawdata.restart_file = 'fort.chk00006' # File to use for restart data + + + # ------------- + # Output times: + #-------------- + + # Specify at what times the results should be written to fort.q files. + # Note that the time integration stops after the final output time. + + clawdata.output_style = 1 + + if clawdata.output_style==1: + # Output ntimes frames at equally spaced times up to tfinal: + # Can specify num_output_times = 0 for no output + clawdata.num_output_times = {num_output_times:d} + clawdata.tfinal = {tfinal:f} + clawdata.output_t0 = True # output at initial (or restart) time? + + elif clawdata.output_style == 2: + # Specify a list or numpy array of output times: + # Include t0 if you want output at the initial time. + clawdata.output_times = [0., 0.1] + + elif clawdata.output_style == 3: + # Output every step_interval timesteps over total_steps timesteps: + clawdata.output_step_interval = 2 + clawdata.total_steps = 4 + clawdata.output_t0 = True # output at initial (or restart) time? + + + clawdata.output_format == 'ascii' # 'ascii' or 'netcdf' + + clawdata.output_q_components = 'all' # could be list such as [True,True] + clawdata.output_aux_components = 'none' # could be list + clawdata.output_aux_onlyonce = True # output aux arrays only at t0 + + + # --------------------------------------------------- + # Verbosity of messages to screen during integration: + # --------------------------------------------------- + + # The current t, dt, and cfl will be printed every time step + # at AMR levels <= verbosity. Set verbosity = 0 for no printing. + # (E.g. verbosity == 2 means print only on levels 1 and 2.) + clawdata.verbosity = 0 + + + + # -------------- + # Time stepping: + # -------------- + + # if dt_variable==True: variable time steps used based on cfl_desired, + # if dt_variable==False: fixed time steps dt = dt_initial always used. + clawdata.dt_variable = {dt_variable:s} + + # Initial time step for variable dt. + # (If dt_variable==0 then dt=dt_initial for all steps) + clawdata.dt_initial = {dt_initial:e} + + # Max time step to be allowed if variable dt used: + clawdata.dt_max = {dt_max:e} + + # Desired Courant number if variable dt used + clawdata.cfl_desired = {cfl_desired:f} + # max Courant number to allow without retaking step with a smaller dt: + clawdata.cfl_max = {cfl_max:f} + + # Maximum number of time steps to allow between output times: + clawdata.steps_max = {steps_max:d} + + + # ------------------ + # Method to be used: + # ------------------ + + # Order of accuracy: 1 => Godunov, 2 => Lax-Wendroff plus limiters + clawdata.order = {order:d} + + # Use dimensional splitting? (not yet available for AMR) + clawdata.dimensional_split = 'unsplit' + + # For unsplit method, transverse_waves can be + # 0 or 'none' ==> donor cell (only normal solver used) + # 1 or 'increment' ==> corner transport of waves + # 2 or 'all' ==> corner transport of 2nd order corrections too + clawdata.transverse_waves = {transverse_waves:d} + + + # Number of waves in the Riemann solution: + clawdata.num_waves = {num_waves:d} + + # List of limiters to use for each wave family: + # Required: len(limiter) == num_waves + # Some options: + # 0 or 'none' ==> no limiter (Lax-Wendroff) + # 1 or 'minmod' ==> minmod + # 2 or 'superbee' ==> superbee + # 3 or 'mc' ==> MC limiter + # 4 or 'vanleer' ==> van Leer + clawdata.limiter = {limiter:s} + + clawdata.fwave = False # True ==> use f-wave version of algorithms + + # Source terms splitting: + # src_split == 0 or 'none' ==> no source term (src routine never called) + # src_split == 1 or 'godunov' ==> Godunov (1st order) splitting used, + # src_split == 2 or 'strang' ==> Strang (2nd order) splitting used, not recommended. + clawdata.source_split = {source_split:d} + + + # -------------------- + # Boundary conditions: + # -------------------- + + # Number of ghost cells (usually 2) + clawdata.num_ghost = {num_ghost:d} + + # Choice of BCs at xlower and xupper: + # 0 or 'user' => user specified (must modify bcNamr.f to use this option) + # 1 or 'extrap' => extrapolation (non-reflecting outflow) + # 2 or 'periodic' => periodic (must specify this at both boundaries) + # 3 or 'wall' => solid wall for systems where q(2) is normal velocity + + clawdata.bc_lower[0] = {mthbc_xlower:s} # at xlower + clawdata.bc_upper[0] = {mthbc_xupper:s} # at xupper + + clawdata.bc_lower[1] = {mthbc_ylower:s} # at ylower + clawdata.bc_upper[1] = {mthbc_yupper:s} # at yupper + + + # --------------- + # Gauges: + # --------------- + clawdata.gauges = {gauges:s} + # for gauges append lines of the form [gaugeno, x, y, t1, t2] + + + + # --------------- + # AMR parameters: + # --------------- + + + # max number of refinement levels: + clawdata.amr_levels_max = {amr_levels_max:d} + + # List of refinement ratios at each level (length at least amr_level_max-1) + clawdata.refinement_ratios_x = {refinement_ratios_x:s} + clawdata.refinement_ratios_y = {refinement_ratios_y:s} + clawdata.refinement_ratios_t = {refinement_ratios_t:s} + + + # Specify type of each aux variable in clawdata.auxtype. + # This must be a list of length num_aux, each element of which is one of: + # 'center', 'capacity', 'xleft', or 'yleft' (see documentation). + clawdata.aux_type = {aux_type:s} + + + # Flag for refinement based on Richardson error estimater: + clawdata.flag_richardson = {flag_richardson:s} # use Richardson? + clawdata.flag_richardson_tol = {flag_richardson_tol:e} # Richardson tolerance + + # Flag for refinement using routine flag2refine: + clawdata.flag2refine = {flag2refine:s} # use this? + clawdata.flag2refine_tol = {flag2refine_tol:e} # tolerance used in this routine + # User can modify flag2refine to change the criterion for flagging. + # Default: check maximum absolute difference of first component of q + # between a cell and each of its neighbors. + + # steps to take on each level L between regriddings of level L+1: + clawdata.regrid_interval = {regrid_interval:d} + + # width of buffer zone around flagged points: + # (typically the same as regrid_interval so waves don't escape): + clawdata.regrid_buffer_width = {regrid_buffer_width:d} + + # clustering alg. cutoff for (# flagged pts) / (total # of cells refined) + # (closer to 1.0 => more small grids may be needed to cover flagged cells) + clawdata.clustering_cutoff = {clustering_cutoff:f} + + # print info about each regridding up to this level: + clawdata.verbosity_regrid = 0 + + + # --------------- + # Regions: + # --------------- + clawdata.regions = {regions:s} + # to specify regions of refinement append lines of the form + # [minlevel,maxlevel,t1,t2,x1,x2,y1,y2] + + + # -------------- + # Checkpointing: + # -------------- + + # Specify when checkpoint files should be created that can be + # used to restart a computation. + + clawdata.checkpt_style = 1 + + if clawdata.checkpt_style == 0: + # Do not checkpoint at all + pass + + elif clawdata.checkpt_style == 1: + # Checkpoint only at tfinal. + pass + + elif clawdata.checkpt_style == 2: + # Specify a list of checkpoint times. + clawdata.checkpt_times = [0.1,0.15] + + elif clawdata.checkpt_style == 3: + # Checkpoint every checkpt_interval timesteps (on Level 1) + # and at the final time. + clawdata.checkpt_interval = 5 + + + # ----- For developers ----- + # Toggle debugging print statements: + clawdata.dprint = False # print domain flags + clawdata.eprint = False # print err est flags + clawdata.edebug = False # even more err est flags + clawdata.gprint = False # grid bisection/clustering + clawdata.nprint = False # proper nesting output + clawdata.pprint = False # proj. of tagged points + clawdata.rprint = False # print regridding summary + clawdata.sprint = False # space/memory output + clawdata.tprint = False # time step reporting each level + clawdata.uprint = False # update/upbnd reporting + + return rundata + + # end of function setrun + # ---------------------- + + +if __name__ == '__main__': + # Set up run-time parameters and write all data files. + import sys + rundata = setrun(*sys.argv[1:]) + rundata.write() + diff --git a/src/python/clawutil/imagediff.py b/src/python/clawutil/imagediff.py index 03f82164..971e33fa 100755 --- a/src/python/clawutil/imagediff.py +++ b/src/python/clawutil/imagediff.py @@ -4,15 +4,20 @@ Can by used from the command line via: python imagediff.py file1 file2 - python imagediff.py dir1 dir2 -Assumes dir1 and dir2 are subdirectories of working directory. + python imagediff.py dir1 dir2 [dir3] +In the latter case dir1 and dir2 are any two directories and all +images will be compared. The optional dir3 argument gives the directory +for the resulting diff's and html files. Default is '_image_diff'. Command line flags include: -v, --verbose = Verbose output + -r, --relocatable = Copy original image files to dir3 also -h, --help = Display this help """ -import os, sys, getopt +help_message = __doc__ + +import os, sys, getopt, shutil def imagediff_file(fname1, fname2, verbose): @@ -59,12 +64,15 @@ def make_imagediff(fname1,fname2,fname3='', verbose=False): def imagediff_dir(dir1, dir2, dir3="_image_diff", ext='.png', \ - overwrite=False, verbose=False): + relocatable=False, overwrite=False, verbose=False): import filecmp,glob if dir1[-1] == '/': dir1 = dir1[:-1] if dir2[-1] == '/': dir2 = dir2[:-1] + dir1 = os.path.abspath(dir1) + dir2 = os.path.abspath(dir2) + files1 = glob.glob("%s/*%s" % (dir1,ext)) files1 = [f.replace(dir1+'/','') for f in files1] @@ -79,7 +87,7 @@ def imagediff_dir(dir1, dir2, dir3="_image_diff", ext='.png', \ print "Comparing files in the directory: ", dir1 print " with the directory: ", dir2 - + if os.path.isdir(dir3): if (len(os.listdir(dir3)) > 0) and (not overwrite): ans = raw_input("Ok to overwrite files in %s ? " % dir3) @@ -90,8 +98,7 @@ def imagediff_dir(dir1, dir2, dir3="_image_diff", ext='.png', \ os.system('mkdir -p %s' % dir3) startdir = os.getcwd() - dir1 = '../' + dir1 - dir2 = '../' + dir2 + os.chdir(dir3) @@ -108,6 +115,24 @@ def imagediff_dir(dir1, dir2, dir3="_image_diff", ext='.png', \ """ % (dir1,dir2,ext)) f_equal, f_diff, f_other = filecmp.cmpfiles(dir1,dir2,files,False) + + if relocatable: + # copy files from dir1 and dir2 into dir3 so the whole thing can + # be moved elsewhere, e.g. to post on web for discussion of a bug. + dir1copy = 'dir1' + dir2copy = 'dir2' + os.system('mkdir %s' % dir1copy) + os.system('mkdir %s' % dir2copy) + for f in files: + shutil.copy(os.path.join(dir1,f),dir1copy) + shutil.copy(os.path.join(dir2,f),dir2copy) + fhtml = os.path.splitext(f)[0] + '.html' ## Specific to Clawpack _plots + if not os.path.isfile(os.path.join(dir1,fhtml)): fhtml = f + shutil.copy(os.path.join(dir1,fhtml),dir1copy) + shutil.copy(os.path.join(dir2,fhtml),dir2copy) + dir1 = 'dir1' + dir2 = 'dir2' + for f in files: fhtml = os.path.splitext(f)[0] + '.html' ## Specific to Clawpack _plots @@ -172,31 +197,41 @@ class Usage(Exception): def __init__(self, msg): self.msg = msg + if __name__ == "__main__": # Parse input arguments argv = sys.argv try: try: - opts, args = getopt.getopt(argv[1:], "hv",["help","verbose"]) + opts, args = getopt.getopt(argv[1:], "hv",["help","verbose","relocatable"]) except getopt.error, msg: raise Usage(msg) # Default script parameter values verbose = False + relocatable = False # option processing for option, value in opts: # Script parameters if option in ("-v","--verbose"): verbose = True + if option in ("-r","--relocatable"): + relocatable = True if option in ("-h","--help"): raise Usage(help_message) + #import pdb; pdb.set_trace() # Run diff if os.path.isfile(args[0]) and os.path.isfile(args[1]): sys.exit(imagediff_file(args[0],args[1],verbose=verbose)) elif os.path.isdir(args[0]) and os.path.isdir(args[1]): - sys.exit(imagediff_dir(args[0],args[1],verbose=verbose)) + if len(args)==3: + sys.exit(imagediff_dir(args[0],args[1],args[2],verbose=verbose,\ + relocatable=relocatable)) + else: + sys.exit(imagediff_dir(args[0],args[1],verbose=verbose,\ + relocatable=relocatable)) else: raise Usage("Both paths must either be files or directories.") diff --git a/src/python/clawutil/runclaw.py b/src/python/clawutil/runclaw.py index 06bc6108..009244c7 100644 --- a/src/python/clawutil/runclaw.py +++ b/src/python/clawutil/runclaw.py @@ -22,7 +22,7 @@ def runclaw(xclawcmd=None, outdir=None, overwrite=True, restart=False, """ import os,glob,shutil,time - verbose = False + verbose = True xclawout = None xclawerr = None @@ -47,11 +47,11 @@ def runclaw(xclawcmd=None, outdir=None, overwrite=True, restart=False, if rundir is None: rundir = os.getcwd() rundir = os.path.abspath(rundir) - print "Will take data from ", rundir + print "==> runclaw: Will take data from ", rundir # directory for fort.* files: outdir = os.path.abspath(outdir) - print '== runclaw: Will write output to ',outdir + print '==> runclaw: Will write output to ',outdir #returncode = clawjob.runxclaw() @@ -83,7 +83,7 @@ def runclaw(xclawcmd=None, outdir=None, overwrite=True, restart=False, hour = str(tm[3]).zfill(2) minute = str(tm[4]).zfill(2) second = str(tm[5]).zfill(2) - outdir_backup = outdir + '_%s%s%s-%s%s%s' \ + outdir_backup = outdir + '_%s-%s-%s-%s%s%s' \ % (year,month,day,hour,minute,second) if verbose: print "==> runclaw: Directory already exists: ",os.path.split(outdir)[1]