def createPlotTimelines(timelines,
                        custom,
                        implementation=None,
                        gnuplotTerminal=None,
                        showWindow=True,
                        registry=None):
    """Creates a plotting object
    :param timelines: The timelines object
    :type timelines: TimeLineCollection
    :param custom: specifies how the block should look like
    :param implementation: the implementation that should be used
    """
    if implementation==None:
        implementation=configuration().get("Plotting","preferredImplementation")

    if implementation not in lookupTable:
        error("Requested plotting implementation",implementation,
              "not in list of available implementations",list(lookupTable.keys()))

    options={
        "showWindow" : showWindow,
        "registry"   : registry
    }

    if implementation=="gnuplot":
        if not gnuplotTerminal:
            gnuplotTerminal=configuration().get("Plotting","gnuplot_terminal")
            if gnuplotTerminal not in validTerminals():
                error("Terminal",gnuplotTerminal,"not in list of valid terminals",
                      ", ".join(validTerminals()))
        options["terminal"]=gnuplotTerminal

    return lookupTable[implementation](timelines,
                                       custom,
                                       **options)
示例#2
0
 def checkAndCreateHookInstances(self, toDict, prefix):
     for h in self.getHooksWithPrefix(prefix):
         self.hookmessage("Checking", h)
         if configuration().getboolean(h, "enabled", default=True):
             subdict = {}
             mod = configuration().get(h, "module", default="")
             if mod == "":
                 self.warning("No module specified for", h)
                 continue
             subdict["minRunTime"] = configuration().getfloat(h,
                                                              "minRunTime",
                                                              default=-1)
             subdict["stopOnError"] = configuration().getboolean(
                 h, "stopOnError", default=False)
             self.hookmessage("Trying to import", mod)
             try:
                 try:
                     module = __import__(mod, globals(), locals(),
                                         ["dummy"])
                 except ImportError:
                     e = sys.exc_info(
                     )[1]  # Needed because python 2.5 does not support 'as e'
                     self.hookmessage("ImportError:", e)
                     mod = "PyFoam.Infrastructure.RunHooks." + mod
                     self.hookmessage("Trying to import", mod)
                     try:
                         module = __import__(mod, globals(), locals(),
                                             ["dummy"])
                     except ImportError:
                         self.hookmessage("ImportError:", e)
                         self.warning("Could not import module",
                                      mod.split(".")[-1], "for", h,
                                      "(Tried", mod, "too)")
                         continue
             except SyntaxError:
                 e = sys.exc_info()[
                     1]  # Needed because python 2.5 does not support 'as e'
                 self.hookmessage("SyntaxError:", e)
                 self.warning("Syntax error when trying to import", mod)
                 continue
             try:
                 theClass = getattr(module, mod.split(".")[-1])
             except AttributeError:
                 e = sys.exc_info()[
                     1]  # Needed because python 2.5 does not support 'as e'
                 self.hookmessage("AttributeError:", e)
                 self.hookmessage("Attributes:", dir(module))
                 self.warning("Class",
                              mod.split(".")[-1], "missing form", mod)
                 continue
             try:
                 subdict["instance"] = theClass(self, h)
             except Exception:
                 self.warning("Problem while creating instance of",
                              theClass, ":", traceback.format_exc())
                 self.stopExecutionOnHookError(subdict["stopOnError"])
                 continue
             toDict[h] = subdict
         else:
             self.hookmessage(h, "is disabled")
示例#3
0
 def addLocalConfig(self, directory=None):
     """
     Adds a local directory (assuming it is found)
     """
     if directory != None:
         configuration().addFile(path.join(directory, "LocalConfigPyFoam"),
                                 silent=True)
示例#4
0
def createPlotTimelines(timelines,
                        custom,
                        implementation=None,
                        gnuplotTerminal=None,
                        showWindow=True,
                        quiet=False,
                        registry=None):
    """Creates a plotting object
    :param timelines: The timelines object
    :type timelines: TimeLineCollection
    :param custom: specifies how the block should look like
    :param implementation: the implementation that should be used
    """
    if implementation == None:
        implementation = configuration().get("Plotting",
                                             "preferredImplementation")

    if implementation not in lookupTable:
        error("Requested plotting implementation", implementation,
              "not in list of available implementations",
              list(lookupTable.keys()))

    options = {"showWindow": showWindow, "quiet": quiet, "registry": registry}

    if implementation == "gnuplot":
        if not gnuplotTerminal:
            gnuplotTerminal = configuration().get("Plotting",
                                                  "gnuplot_terminal")
            if gnuplotTerminal not in validTerminals():
                error("Terminal", gnuplotTerminal,
                      "not in list of valid terminals",
                      ", ".join(validTerminals()))
        options["terminal"] = gnuplotTerminal

    return lookupTable[implementation](timelines, custom, **options)
    def checkAndCreateHookInstances(self,toDict,prefix):
        for h in self.getHooksWithPrefix(prefix):
            self.hookmessage("Checking",h)
            if configuration().getboolean(h,"enabled",default=True):
                subdict={}
                modName=configuration().get(h,"module",default="")
                if modName=="":
                    self.warning("No module specified for",h)
                    continue
                subdict["minRunTime"]=configuration().getfloat(h,
                                                               "minRunTime",
                                                               default=-1)
                subdict["stopOnError"]=configuration().getboolean(h,
                                                                  "stopOnError",
                                                                  default=False)
                self.hookmessage("Trying to import",modName)

                module=None
                modNames=[modName,
                          "PyFoam.Site."+modName,
                          "PyFoam.Infrastructure.RunHooks."+modName]
                for mod in modNames:
                    try:
                        self.hookmessage("Trying module:",mod)
                        module=__import__(mod,globals(),locals(),["dummy"])
                        self.hookmessage("Got module:",mod)
                        break
                    except ImportError:
                        e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e'
                        self.hookmessage("ImportError:",e,"for",modName)
                    except SyntaxError:
                        e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e'
                        self.hookmessage("SyntaxError:",e)
                        self.warning("Syntax error when trying to import",mod)
                        break
                if module is None:
                    self.warning("Could not import module",modName,
                                 "for",h,"(Tried",", ".join(modNames),")")
                    continue

                try:
                    theClass=getattr(module,mod.split(".")[-1])
                except AttributeError:
                    e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e'
                    self.hookmessage("AttributeError:",e)
                    self.hookmessage("Attributes:",dir(module))
                    self.warning("Class",mod.split(".")[-1],"missing form",
                             mod)
                    continue
                try:
                    subdict["instance"]=theClass(self,h)
                except Exception:
                    self.warning("Problem while creating instance of",
                                 theClass,":",traceback.format_exc())
                    self.stopExecutionOnHookError(subdict["stopOnError"])
                    continue
                toDict[h]=subdict
            else:
                self.hookmessage(h,"is disabled")
 def dumpHooks(self, lst):
     for h in lst:
         print_(h)
         try:
             print_("  enabled:",
                    configuration().getboolean(h, "enabled", default=True))
             print_("  module:", configuration().get(h, "module"))
             print_("  minRunTime:",
                    configuration().getfloat(h, "minRunTime", default=0))
             print_("  description:",
                    configuration().get(h, "description", default="None"))
         except configparser.NoOptionError:
             e = sys.exc_info()[
                 1]  # Needed because python 2.5 does not support 'as e'
             self.error("Hook", h, "incompletely defined (", e, ")")
 def dumpHooks(self,lst):
     for h in lst:
         print_(h)
         try:
             print_("  enabled:",configuration().getboolean(h,
                                                            "enabled",
                                                            default=True))
             print_("  module:",configuration().get(h,
                                                    "module"))
             print_("  minRunTime:",configuration().getfloat(h,
                                                             "minRunTime",
                                                             default=0))
             print_("  description:",configuration().get(h,
                                                         "description",
                                                         default="None"))
         except configparser.NoOptionError:
             e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e'
             self.error("Hook",h,"incompletely defined (",e,")")
def createPlotTimelines(timelines,
                        custom,
                        implementation=None,
                        showWindow=True,
                        registry=None):
    """Creates a plotting object
    @param timelines: The timelines object
    @type timelines: TimeLineCollection
    @param custom: specifies how the block should look like
    @param implementation: the implementation that should be used
    """
    if implementation==None:
        implementation=configuration().get("Plotting","preferredImplementation")

    if implementation not in lookupTable:
        error("Requested plotting implementation",implementation,
              "not in list of available implementations",list(lookupTable.keys()))

    return lookupTable[implementation](timelines,
                                       custom,
                                       showWindow=showWindow,
                                       registry=registry)
示例#9
0
def createPlotTimelines(timelines,
                        custom,
                        implementation=None,
                        showWindow=True,
                        registry=None):
    """Creates a plotting object
    @param timelines: The timelines object
    @type timelines: TimeLineCollection
    @param custom: specifies how the block should look like
    @param implementation: the implementation that should be used
    """
    if implementation == None:
        implementation = configuration().get("Plotting",
                                             "preferredImplementation")

    if implementation not in lookupTable:
        error("Requested plotting implementation", implementation,
              "not in list of available implementations",
              list(lookupTable.keys()))

    return lookupTable[implementation](timelines,
                                       custom,
                                       showWindow=showWindow,
                                       registry=registry)
示例#10
0
 def addLocalConfig(self,directory=None):
     """
     Adds a local directory (assuming it is found)
     """
     if directory!=None:
         configuration().addFile(path.join(directory,"LocalConfigPyFoam"),silent=True)
示例#11
0
    def addOptions(self):
        what = OptionGroup(self.parser, "What", "Define what should be shown")
        self.parser.add_option_group(what)

        what.add_option("--dump",
                        action="store_true",
                        dest="dump",
                        default=False,
                        help="Dump the information as Python-dictionaries")

        what.add_option(
            "--disk-usage",
            action="store_true",
            dest="diskusage",
            default=False,
            help=
            "Show the disk-usage of the case (in MB) - may take a long time")

        what.add_option(
            "--parallel-info",
            action="store_true",
            dest="parallel",
            default=False,
            help=
            "Print information about parallel runs (if present): number of processors and processor first and last time. The mtime will be that of the processor-directories"
        )

        what.add_option("--no-state",
                        action="store_false",
                        dest="state",
                        default=True,
                        help="Don't read state-files")

        what.add_option("--no-hostname",
                        action="store_false",
                        dest="hostname",
                        default=True,
                        help="Don't look up the hostname in the pickled data")

        what.add_option(
            "--advanced-state",
            action="store_true",
            dest="advancedState",
            default=False,
            help="Additional state information (run started, last output seen)"
        )

        what.add_option(
            "--estimate-end-time",
            action="store_true",
            dest="estimateEndTime",
            default=False,
            help=
            "Print an estimated end time (calculated from the start time of the run, the current time and the current simulation time)"
        )

        what.add_option("--start-end-time",
                        action="store_true",
                        dest="startEndTime",
                        default=False,
                        help="Start and end time from the controlDict")

        what.add_option(
            "--custom-data",
            action="append",
            dest="customData",
            default=[],
            help=
            "Specification of additional data that is read from the pickled data-sets. The format is 'name=spec1::spec2::...' where 'name' is the name under which the data ist shown and 'specN' are the dictionary keys under which the data is accessed. If only 'spec1::spec2::..' is given then a name of the form 'CustomN' will be used. Can be specified more than once"
        )

        what.add_option(
            "--solver-name-for-custom-data",
            action="store",
            dest="solverNameForCustom",
            default=None,
            help=
            "This is used if '--custom-data' is specified as the data will be searched in 'PyFoamRunner.<solver name>.analyzed'. If unset then the utility will try to automatically determine the name of the solver which might be wrong"
        )

        what.add_option(
            "--hg-info",
            action="store_true",
            dest="hgInfo",
            default=False,
            help=
            "Looks for .hg in the directories and reports mercurial version info (for those who keep track of their cases with mercurial)"
        )

        how = OptionGroup(self.parser, "How", "How the things should be shown")
        self.parser.add_option_group(how)

        how.add_option("--sort-by",
                       type="choice",
                       action="store",
                       dest="sort",
                       default=configuration().get("CommandOptionDefaults",
                                                   "sortListCases",
                                                   default="name"),
                       choices=self.sortChoices,
                       help="Sort the cases by a specific key (Keys: " +
                       ", ".join(self.sortChoices) + ") Default: %default")
        how.add_option("--reverse-sort",
                       action="store_true",
                       dest="reverse",
                       default=False,
                       help="Sort in reverse order")
        how.add_option("--relative-times",
                       action="store_true",
                       dest="relativeTime",
                       default=False,
                       help="Show the timestamps relative to the current time")

        behave = OptionGroup(self.parser, "Behaviour", "Additional output etc")
        self.parser.add_option_group(behave)

        behave.add_option(
            "--progress",
            action="store_true",
            dest="progress",
            default=False,
            help="Print the directories while they are being processed")

        behave.add_option(
            "--no-progress-bar",
            action="store_false",
            dest="progressBar",
            default=True,
            help="Do not show a progress bar as directories are being processed"
        )
        behave.add_option(
            "--dead-threshold",
            action="store",
            type="float",
            dest="deadThreshold",
            default=configuration().get("CommandOptionDefaults",
                                        "deadThresholdListCases"),
            help=
            "Number of seconds without updates after which the case is assumed to be dead. Default: %default"
        )

        select = OptionGroup(
            self.parser, "Selection",
            "Select which cases should be shown. First the select-patterns are applied then the ignore patterns. If no select-patterns are specified then all cases are processed"
        )
        self.parser.add_option_group(select)

        select.add_option("--recursive",
                          action="store_true",
                          dest="recursive",
                          default=False,
                          help="Recursively search for case directories")
        select.add_option(
            "--substring-select",
            action="append",
            dest="substringSelect",
            default=[],
            help=
            "Substrings that should be in the case-name. Can be specified more than once"
        )
        select.add_option(
            "--ignore-substring",
            action="append",
            dest="substringIgnore",
            default=[],
            help=
            "Substrings that should not be in the case-name. Can be specified more than once"
        )
        select.add_option(
            "--glob-select",
            action="append",
            dest="globSelect",
            default=[],
            help=
            "Glob-pattern that the case-name should match. Can be specified more than once"
        )
        select.add_option(
            "--no-glob-select",
            action="append",
            dest="globIgnore",
            default=[],
            help=
            "Glob-pattern that the case-name should not match. Can be specified more than once"
        )
示例#12
0
    def addOptions(self):
        what=OptionGroup(self.parser,
                         "What",
                         "Define what should be shown")
        self.parser.add_option_group(what)

        what.add_option("--dump",
                        action="store_true",
                        dest="dump",
                        default=False,
                        help="Dump the information as Python-dictionaries")

        what.add_option("--disk-usage",
                        action="store_true",
                        dest="diskusage",
                        default=False,
                        help="Show the disk-usage of the case (in MB) - may take a long time")

        what.add_option("--parallel-info",
                        action="store_true",
                        dest="parallel",
                        default=False,
                        help="Print information about parallel runs (if present): number of processors and processor first and last time. The mtime will be that of the processor-directories")

        what.add_option("--no-state",
                        action="store_false",
                        dest="state",
                        default=True,
                        help="Don't read state-files")

        what.add_option("--advanced-state",
                        action="store_true",
                        dest="advancedState",
                        default=False,
                        help="Additional state information (run started, last output seen)")

        what.add_option("--estimate-end-time",
                        action="store_true",
                        dest="estimateEndTime",
                        default=False,
                        help="Print an estimated end time (calculated from the start time of the run, the current time and the current simulation time)")

        what.add_option("--start-end-time",
                        action="store_true",
                        dest="startEndTime",
                        default=False,
                        help="Start and end time from the controlDict")

        what.add_option("--custom-data",
                        action="append",
                        dest="customData",
                        default=[],
                        help="Specification of additional data that is read from the pickled data-sets. The format is 'name=spec1::spec2::...' where 'name' is the name under which the data ist shown and 'specN' are the dictionary keys under which the data is accessed. If only 'spec1::spec2::..' is given then a name of the form 'CustomN' will be used. Can be specified more than once")

        what.add_option("--solver-name-for-custom-data",
                        action="store",
                        dest="solverNameForCustom",
                        default=None,
                        help="This is used if '--custom-data' is specified as the data will be searched in 'PyFoamRunner.<solver name>.analyzed'. If unset then the utility will try to automatically determine the name of the solver which might be wrong")

        how=OptionGroup(self.parser,
                         "How",
                         "How the things should be shown")
        self.parser.add_option_group(how)

        how.add_option("--sort-by",
                        type="choice",
                        action="store",
                        dest="sort",
                        default=configuration().get("CommandOptionDefaults","sortListCases",default="name"),
                        choices=self.sortChoices,
                        help="Sort the cases by a specific key (Keys: "+", ".join(self.sortChoices)+") Default: %default")
        how.add_option("--reverse-sort",
                       action="store_true",
                       dest="reverse",
                       default=False,
                       help="Sort in reverse order")
        how.add_option("--relative-times",
                       action="store_true",
                       dest="relativeTime",
                       default=False,
                       help="Show the timestamps relative to the current time")

        behave=OptionGroup(self.parser,
                         "Behaviour",
                         "Additional output etc")
        self.parser.add_option_group(behave)

        behave.add_option("--progress",
                          action="store_true",
                          dest="progress",
                          default=False,
                          help="Print the directories while they are being processed")
示例#13
0
    def addOptions(self):
        what=OptionGroup(self.parser,
                         "What",
                         "Define what should be shown")
        self.parser.add_option_group(what)

        what.add_option("--dump",
                        action="store_true",
                        dest="dump",
                        default=False,
                        help="Dump the information as Python-dictionaries")

        what.add_option("--disk-usage",
                        action="store_true",
                        dest="diskusage",
                        default=False,
                        help="Show the disk-usage of the case (in MB) - may take a long time")

        what.add_option("--parallel-info",
                        action="store_true",
                        dest="parallel",
                        default=False,
                        help="Print information about parallel runs (if present): number of processors and processor first and last time. The mtime will be that of the processor-directories")

        what.add_option("--no-state",
                        action="store_false",
                        dest="state",
                        default=True,
                        help="Don't read state-files")

        what.add_option("--advanced-state",
                        action="store_true",
                        dest="advancedState",
                        default=False,
                        help="Additional state information (run started, last output seen)")

        what.add_option("--estimate-end-time",
                        action="store_true",
                        dest="estimateEndTime",
                        default=False,
                        help="Print an estimated end time (calculated from the start time of the run, the current time and the current simulation time)")

        what.add_option("--start-end-time",
                        action="store_true",
                        dest="startEndTime",
                        default=False,
                        help="Start and end time from the controlDict")

        how=OptionGroup(self.parser,
                         "How",
                         "How the things should be shown")
        self.parser.add_option_group(how)

        how.add_option("--sort-by",
                        type="choice",
                        action="store",
                        dest="sort",
                        default=configuration().get("CommandOptionDefaults","sortListCases",default="name"),
                        choices=self.sortChoices,
                        help="Sort the cases by a specific key (Keys: "+string.join(self.sortChoices,", ")+") Default: %default")
        how.add_option("--reverse-sort",
                       action="store_true",
                       dest="reverse",
                       default=False,
                       help="Sort in reverse order")
        how.add_option("--relative-times",
                       action="store_true",
                       dest="relativeTime",
                       default=False,
                       help="Show the timestamps relative to the current time")

        behave=OptionGroup(self.parser,
                         "Behaviour",
                         "Additional output etc")
        self.parser.add_option_group(behave)

        behave.add_option("--progress",
                          action="store_true",
                          dest="progress",
                          default=False,
                          help="Print the directories while they are being processed")
 def getHooksWithPrefix(self,prefix):
     lst=[]
     for h in configuration().sections():
         if h.find(prefix+"_")==0:
             lst.append(h)
     return lst
示例#15
0
    def addOptions(self):
        what = OptionGroup(self.parser, "What", "Define what should be shown")
        self.parser.add_option_group(what)

        what.add_option("--dump",
                        action="store_true",
                        dest="dump",
                        default=False,
                        help="Dump the information as Python-dictionaries")

        what.add_option(
            "--disk-usage",
            action="store_true",
            dest="diskusage",
            default=False,
            help=
            "Show the disk-usage of the case (in MB) - may take a long time")

        what.add_option(
            "--parallel-info",
            action="store_true",
            dest="parallel",
            default=False,
            help=
            "Print information about parallel runs (if present): number of processors and processor first and last time. The mtime will be that of the processor-directories"
        )

        what.add_option("--no-state",
                        action="store_false",
                        dest="state",
                        default=True,
                        help="Don't read state-files")

        what.add_option(
            "--advanced-state",
            action="store_true",
            dest="advancedState",
            default=False,
            help="Additional state information (run started, last output seen)"
        )

        what.add_option(
            "--estimate-end-time",
            action="store_true",
            dest="estimateEndTime",
            default=False,
            help=
            "Print an estimated end time (calculated from the start time of the run, the current time and the current simulation time)"
        )

        what.add_option("--start-end-time",
                        action="store_true",
                        dest="startEndTime",
                        default=False,
                        help="Start and end time from the controlDict")

        what.add_option(
            "--custom-data",
            action="append",
            dest="customData",
            default=[],
            help=
            "Specification of additional data that is read from the pickled data-sets. The format is 'name=spec1::spec2::...' where 'name' is the name under which the data ist shown and 'specN' are the dictionary keys under which the data is accessed. If only 'spec1::spec2::..' is given then a name of the form 'CustomN' will be used. Can be specified more than once"
        )

        what.add_option(
            "--solver-name-for-custom-data",
            action="store",
            dest="solverNameForCustom",
            default=None,
            help=
            "This is used if '--custom-data' is specified as the data will be searched in 'PyFoamRunner.<solver name>.analyzed'. If unset then the utility will try to automatically determine the name of the solver which might be wrong"
        )

        how = OptionGroup(self.parser, "How", "How the things should be shown")
        self.parser.add_option_group(how)

        how.add_option("--sort-by",
                       type="choice",
                       action="store",
                       dest="sort",
                       default=configuration().get("CommandOptionDefaults",
                                                   "sortListCases",
                                                   default="name"),
                       choices=self.sortChoices,
                       help="Sort the cases by a specific key (Keys: " +
                       ", ".join(self.sortChoices) + ") Default: %default")
        how.add_option("--reverse-sort",
                       action="store_true",
                       dest="reverse",
                       default=False,
                       help="Sort in reverse order")
        how.add_option("--relative-times",
                       action="store_true",
                       dest="relativeTime",
                       default=False,
                       help="Show the timestamps relative to the current time")

        behave = OptionGroup(self.parser, "Behaviour", "Additional output etc")
        self.parser.add_option_group(behave)

        behave.add_option(
            "--progress",
            action="store_true",
            dest="progress",
            default=False,
            help="Print the directories while they are being processed")
    def prepare(self,sol,
                cName=None,
                overrideParameters=None,
                numberOfProcessors=None):
        """Do the actual preparing
        @param numberOfProcessors: If set this overrides the value set in the
        command line"""

        if cName==None:
            cName=sol.name

        if self.opts.onlyVariables:
            self.opts.verbose=True

        vals={}
        vals,self.metaData=self.getDefaultValues(cName)
        vals.update(self.addDictValues("System",
                                       "Automatically defined values",
                                       {
                                           "casePath" : '"'+path.abspath(cName)+'"',
                                           "caseName" : '"'+path.basename(path.abspath(cName))+'"',
                                           "foamVersion" : foamVersion(),
                                           "foamFork" : foamFork(),
                                           "numberOfProcessors" : numberOfProcessors if numberOfProcessors!=None else self.opts.numberOfProcessors
                                       }))

        if len(self.opts.extensionAddition)>0:
            vals.update(self.addDictValues("ExtensionAdditions",
                                           "Additional extensions to be processed",
                                           dict((e,True) for e in self.opts.extensionAddition)))

        valsWithDefaults=set(vals.keys())

        self.info("Looking for template values",cName)
        for f in self.opts.valuesDicts:
            self.info("Reading values from",f)
            vals.update(ParsedParameterFile(f,
                                            noHeader=True,
                                            doMacroExpansion=True).getValueDict())

        setValues={}
        for v in self.opts.values:
            self.info("Updating values",v)
            vals.update(eval(v))
            setValues.update(eval(v))

        if overrideParameters:
            vals.update(overrideParameters)

        unknownValues=set(vals.keys())-valsWithDefaults
        if len(unknownValues)>0:
            self.warning("Values for which no default was specified: "+
                         ", ".join(unknownValues))

        if self.opts.verbose and len(vals)>0:
            print_("\nUsed values\n")
            nameLen=max(len("Name"),
                        max(*[len(k) for k in vals.keys()]))
            format="%%%ds - %%s" % nameLen
            print_(format % ("Name","Value"))
            print_("-"*40)
            for k,v in sorted(iteritems(vals)):
                print_(format % (k,v))
            print_("")
        else:
            self.info("\nNo values specified\n")

        self.checkCorrectOptions(vals)

        derivedScript=path.join(cName,self.opts.derivedParametersScript)
        if path.exists(derivedScript):
            self.info("Deriving variables in script",derivedScript)
            scriptText=open(derivedScript).read()
            glob={}
            oldVals=vals.copy()
            exec_(scriptText,glob,vals)
            added=[]
            changed=[]
            for k,v in iteritems(vals):
                if k not in oldVals:
                    added.append(k)
                elif vals[k]!=oldVals[k]:
                    changed.append(k)
            if len(changed)>0 and (not self.opts.allowDerivedChanges and not configuration().getboolean("PrepareCase","AllowDerivedChanges")):
                self.error(self.opts.derivedParametersScript,
                           "changed values of"," ".join(changed),
                           "\nTo allow this set --allow-derived-changes or the configuration item 'AllowDerivedChanges'")
            if len(added)>0:
                self.info("Added values:"," ".join(added))
            if len(changed)>0:
                self.info("Changed values:"," ".join(changed))
            if len(added)==0 and len(changed)==0:
                self.info("Nothing added or changed")
        else:
            self.info("No script",derivedScript,"for derived values")

        if self.opts.onlyVariables:
            return

        if self.opts.doClear:
            self.info("Clearing",cName)
            sol.clear(processor=True,
                      pyfoam=True,
                      vtk=True,
                      removeAnalyzed=True,
                      keepParallel=False,
                      clearHistory=False,
                      clearParameters=True,
                      additional=["postProcessing"])

        if self.opts.writeParameters:
            fName=path.join(cName,self.parameterOutFile)
            self.info("Writing parameters to",fName)
            with WriteParameterFile(fName,noHeader=True) as w:
                w.content.update(vals,toString=True)
                w["foamVersion"]=vals["foamVersion"]
                w.writeFile()

        if self.opts.writeReport:
            fName=path.join(cName,self.parameterOutFile+".rst")
            self.info("Writing report to",fName)
            with open(fName,"w") as w:
                helper=RestructuredTextHelper(defaultHeading=1)
                w.write(".. title:: "+self.__strip(vals["caseName"])+"\n")
                w.write(".. sectnum::\n")
                w.write(".. header:: "+self.__strip(vals["caseName"])+"\n")
                w.write(".. header:: "+time.asctime()+"\n")
                w.write(".. footer:: ###Page### / ###Total###\n\n")

                w.write("Parameters set in case directory "+
                        helper.literal(self.__strip(vals["casePath"]))+" at "+
                        helper.emphasis(time.asctime())+"\n\n")
                w.write(".. contents::\n\n")
                if len(self.opts.valuesDicts):
                    w.write(helper.heading("Parameter files"))
                    w.write("Parameters read from files\n\n")
                    w.write(helper.enumerateList([helper.literal(f) for f in self.opts.valuesDicts]))
                    w.write("\n")
                if len(setValues)>0:
                    w.write(helper.heading("Overwritten parameters"))
                    w.write("These parameters were set from the command line\n\n")
                    w.write(helper.definitionList(setValues))
                    w.write("\n")
                w.write(helper.heading("Parameters with defaults"))
                w.write(self.makeReport(vals))
                if len(unknownValues)>0:
                    w.write(helper.heading("Unspecified parameters"))
                    w.write("If these parameters are actually used then specify them in "+
                            helper.literal(self.defaultParameterFile)+"\n\n")
                    tab=helper.table(True)
                    for u in unknownValues:
                        tab.addRow(u)
                        tab.addItem("Value",vals[u])
                    w.write(str(tab))

        self.addToCaseLog(cName)

        for over in self.opts.overloadDirs:
            self.info("Overloading files from",over)
            self.overloadDir(sol.name,over)

        zeroOrig=path.join(sol.name,"0.org")

        hasOrig=path.exists(zeroOrig)
        cleanZero=True

        if not hasOrig:
            self.info("Not going to clean '0'")
            self.opts.cleanDirectories.remove("0")
            cleanZero=False

        if self.opts.doCopy:
            if hasOrig:
                self.info("Found 0.org. Clearing 0")
                zeroDir=path.join(sol.name,"0")
                if path.exists(zeroDir):
                    rmtree(zeroDir)
                else:
                    self.info("No 0-directory")

            self.info("")
        else:
            cleanZero=False

        if self.opts.doTemplates:
            self.searchAndReplaceTemplates(sol.name,
                                           vals,
                                           self.opts.templateExt)

            self.info("")

        backupZeroDir=None

        if self.opts.doMeshCreate:
            if self.opts.meshCreateScript:
                scriptName=path.join(sol.name,self.opts.meshCreateScript)
                if not path.exists(scriptName):
                    self.error("Script",scriptName,"does not exist")
            elif path.exists(path.join(sol.name,self.defaultMeshCreate)):
                scriptName=path.join(sol.name,self.defaultMeshCreate)
            else:
                scriptName=None

            if scriptName:
                self.info("Executing",scriptName,"for mesh creation")
                if self.opts.verbose:
                    echo="Mesh: "
                else:
                    echo=None
                result="".join(execute([scriptName],workdir=sol.name,echo=echo))
                open(scriptName+".log","w").write(result)
            else:
                self.info("No script for mesh creation found. Looking for 'blockMeshDict'")
                if sol.blockMesh()!="":
                    self.info(sol.blockMesh(),"found. Executing 'blockMesh'")
                    bm=BasicRunner(argv=["blockMesh","-case",sol.name])
                    bm.start()
                    if not bm.runOK():
                        self.error("Problem with blockMesh")
                for r in sol.regions():
                    self.info("Checking region",r)
                    s=SolutionDirectory(sol.name,region=r,
                                        archive=None,paraviewLink=False)
                    if s.blockMesh()!="":
                        self.info(s.blockMesh(),"found. Executing 'blockMesh'")
                        bm=BasicRunner(argv=["blockMesh","-case",sol.name,
                                             "-region",r])
                        bm.start()
                        if not bm.runOK():
                            self.error("Problem with blockMesh")

            self.info("")

            if cleanZero and path.exists(zeroDir):
                self.warning("Mesh creation recreated 0-directory")
                if self.opts.keepZeroDirectoryFromMesh:
                    backupZeroDir=zeroDir+".bakByPyFoam"
                    self.info("Backing up",zeroDir,"to",backupZeroDir)
                    move(zeroDir,backupZeroDir)
                else:
                    self.info("Data in",zeroDir,"will be removed")

        if self.opts.doCopy:
            self.copyOriginals(sol.name)

            self.info("")

            if backupZeroDir:
                self.info("Copying backups from",backupZeroDir,"to",zeroDir)
                self.overloadDir(zeroDir,backupZeroDir)
                self.info("Removing backup",backupZeroDir)
                rmtree(backupZeroDir)

        if self.opts.doPostTemplates:
            self.searchAndReplaceTemplates(sol.name,
                                           vals,
                                           self.opts.postTemplateExt)

            self.info("")

        if self.opts.doCaseSetup:
            if self.opts.caseSetupScript:
                scriptName=path.join(sol.name,self.opts.caseSetupScript)
                if not path.exists(scriptName):
                    self.error("Script",scriptName,"does not exist")
            elif path.exists(path.join(sol.name,self.defaultCaseSetup)):
                scriptName=path.join(sol.name,self.defaultCaseSetup)
            else:
                scriptName=None

            if scriptName:
                self.info("Executing",scriptName,"for case setup")
                if self.opts.verbose:
                    echo="Case:"
                else:
                    echo=None
                result="".join(execute([scriptName],workdir=sol.name,echo=echo))
                open(scriptName+".log","w").write(result)
            else:
                self.info("No script for case-setup found. Nothing done")
            self.info("")

        if self.opts.doFinalTemplates:
            self.searchAndReplaceTemplates(sol.name,
                                           vals,
                                           self.opts.finalTemplateExt)

        if self.opts.doTemplateClean:
            self.info("Clearing templates")
            for d in self.opts.cleanDirectories:
                for e in [self.opts.templateExt,
                          self.opts.postTemplateExt,
                          self.opts.finalTemplateExt]:
                    self.cleanExtension(path.join(sol.name,d),e)
            self.info("")

        self.info("Case setup finished")
 def getHooksWithPrefix(self, prefix):
     lst = []
     for h in configuration().sections():
         if h.find(prefix + "_") == 0:
             lst.append(h)
     return lst
示例#18
0
    def __init__(self,
                 args=None,
                 description=None,
                 epilog=None,
                 examples=None,
                 usage=None,
                 interspersed=False,
                 nr=None,
                 changeVersion=True,
                 exactNr=True,
                 subcommands=None,
                 inputApp=None,
                 localConfigurationFile=None,
                 findLocalConfigurationFile=None,
                 **kwArgs):
        """
        :param description: description of the command
        :param epilog: text to be printed after the options-help
        :param examples: usage examples to be printed after the epilog
        :param usage: Usage
        :param interspersed: Is the command line allowed to be interspersed (options after the arguments)
        :param args: Command line arguments when using the Application as a 'class' from a script
        :param nr: Number of required arguments
        :param changeVersion: May this application change the version of OF used?
        :param exactNr: Must not have more than the required number of arguments
        :param subcommands: parse and use subcommands from the command line. Either True or a list with subcommands
        :param inputApp: Application with input data. Used to allow a 'pipe-like' behaviour if the class is used from a Script
        :param localConfigurationFile: Use this file (or list of files) as a local configuration
        :param findLocalConfigurationFile: Method to find a configuration file BEFORE the actual parameters are parsed
        """

        global _LocalConfigurationFile

        if _LocalConfigurationFile is not None:
            configuration().addFile(_LocalConfigurationFile)

        if isinstance(localConfigurationFile, string_types):
            configuration().addFile(localConfigurationFile)
        elif localConfigurationFile is not None:
            for c in localConfigurationFile:
                configuration().addFile(c)

        if subcommands:
            self.subs = True
            if interspersed:
                self.error(
                    "Subcommand parser does not work with 'interspersed'")
            if subcommands == True:
                subcommands = []
            self.parser = SubcommandFoamOptionParser(args=args,
                                                     description=description,
                                                     epilog=epilog,
                                                     examples=examples,
                                                     usage=usage,
                                                     subcommands=subcommands)
            nr = None
            exactNr = False
        else:
            self.subs = False
            self.parser = FoamOptionParser(args=args,
                                           description=description,
                                           epilog=epilog,
                                           examples=examples,
                                           usage=usage,
                                           interspersed=interspersed)

        self.calledName = sys.argv[0]
        self.calledAsClass = (args != None)
        if self.calledAsClass:
            self.calledName = self.__class__.__name__ + " used by " + sys.argv[
                0]
            self.parser.prog = self.calledName
        elif not _LocalConfigurationFile and findLocalConfigurationFile:
            if args:
                usedArgs = args
            else:
                usedArgs = sys.argv[1:]
            _LocalConfigurationFile = findLocalConfigurationFile(usedArgs)
            if _LocalConfigurationFile and not path.exists(
                    _LocalConfigurationFile):
                # Fix functions that do not check for the existence
                _LocalConfigurationFile = None
            if _LocalConfigurationFile:
                configuration().addFile(_LocalConfigurationFile)

        self.generalOpts = None

        self.__appData = self.iDict()
        if inputApp:
            self.__appData["inputData"] = inputApp.getData()

        grp = OptionGroup(self.parser, "Default",
                          "Options common to all PyFoam-applications")

        if changeVersion:
            # the options are evaluated in Basics.FoamOptionParser
            grp.add_option(
                "--foamVersion",
                dest="foamVersion",
                default=None,
                help=
                "Change the OpenFOAM-version that is to be used. To get a list of know Foam-versions use the pyFoamVersion.py-utility"
            )
            if "WM_PROJECT_VERSION" in environ:
                grp.add_option("--currentFoamVersion",
                               dest="foamVersion",
                               const=environ["WM_PROJECT_VERSION"],
                               default=None,
                               action="store_const",
                               help="Use the current OpenFOAM-version " +
                               environ["WM_PROJECT_VERSION"])

            grp.add_option(
                "--force-32bit",
                dest="force32",
                default=False,
                action="store_true",
                help=
                "Forces the usage of a 32-bit-version if that version exists as 32 and 64 bit. Only used when --foamVersion is used"
            )
            grp.add_option(
                "--force-64bit",
                dest="force64",
                default=False,
                action="store_true",
                help=
                "Forces the usage of a 64-bit-version if that version exists as 32 and 64 bit. Only used when --foamVersion is used"
            )
            grp.add_option(
                "--force-debug",
                dest="compileOption",
                const="Debug",
                default=None,
                action="store_const",
                help=
                "Forces the value Debug for the WM_COMPILE_OPTION. Only used when --foamVersion is used"
            )
            grp.add_option(
                "--force-opt",
                dest="compileOption",
                const="Opt",
                default=None,
                action="store_const",
                help=
                "Forces the value Opt for the WM_COMPILE_OPTION. Only used when --foamVersion is used"
            )
            grp.add_option(
                "--force-system-compiler",
                dest="foamCompiler",
                const="system",
                default=None,
                action="store_const",
                help=
                "Force using a 'system' compiler (compiler installed in the system)"
            )
            grp.add_option(
                "--force-openfoam-compiler",
                dest="foamCompiler",
                const="OpenFOAM",
                default=None,
                action="store_const",
                help=
                "Force using a 'OpenFOAM' compiler (compiler installed in ThirdParty)"
            )
            grp.add_option(
                "--force-compiler",
                dest="wmCompiler",
                default=None,
                action="store",
                help="Overwrite value for WM_COMPILER (for instance Gcc47 ...)"
            )

        grp.add_option(
            "--psyco-accelerated",
            dest="psyco",
            default=False,
            action="store_true",
            help=
            "Accelerate the script using the psyco-library (EXPERIMENTAL and requires a separatly installed psyco)"
        )
        grp.add_option(
            "--profile-python",
            dest="profilePython",
            default=False,
            action="store_true",
            help=
            "Profile the python-script (not the OpenFOAM-program) - mostly of use for developers"
        )
        grp.add_option(
            "--profile-cpython",
            dest="profileCPython",
            default=False,
            action="store_true",
            help=
            "Profile the python-script (not the OpenFOAM-program) using the better cProfile library - mostly of use for developers"
        )
        grp.add_option(
            "--profile-hotshot",
            dest="profileHotshot",
            default=False,
            action="store_true",
            help=
            "Profile the python-script using the hotshot-library (not the OpenFOAM-program) - mostly of use for developers - DEPRECATED as this library will by removed from standard python and is no longer supported"
        )
        grp.add_option(
            "--profile-line-profiler",
            dest="profileLineProfiler",
            default=False,
            action="store_true",
            help=
            "Profile the python-script using the line_profiler-library (not the OpenFOAM-program) - mostly of use for developers - EXPERIMENTAL"
        )

        dbg = OptionGroup(
            self.parser, "Debugging",
            "Options mainly used for debugging PyFoam-Utilities")

        dbg.add_option(
            "--location-of-local-config",
            dest="locationOfLocalConfig",
            default=False,
            action="store_true",
            help=
            "Prints the location of the found LocalConfigPyFoam-file that is used (if any)"
        )
        dbg.add_option(
            "--traceback-on-error",
            dest="traceback",
            default=False,
            action="store_true",
            help=
            "Prints a traceback when an error is encountered (for debugging)")
        dbg.add_option(
            "--interactive-debugger",
            dest="interactiveDebug",
            default=False,
            action="store_true",
            help=
            "In case of an exception start the interactive debugger PDB. Also implies --traceback-on-error"
        )
        dbg.add_option(
            "--catch-USR1-signal",
            dest="catchUSR1Signal",
            default=False,
            action="store_true",
            help=
            "If the USR1-signal is sent to the application with 'kill -USR1 <pid>' the application ens and prints a traceback. If interactive debugging is enabled then the debugger is entered. Use to investigate hangups"
        )
        dbg.add_option("--also-catch-TERM-signal",
                       dest="alsoCatchTERMsignal",
                       default=False,
                       action="store_true",
                       help="In addition to USR1 catch the regular TERM-kill")
        dbg.add_option(
            "--keyboard-interrupt-trace",
            dest="keyboardInterrupTrace",
            default=False,
            action="store_true",
            help=
            "Make the application behave like with --catch-USR1-signal if <Ctrl>-C is pressed"
        )
        dbg.add_option(
            "--syntax-error-debugger",
            dest="syntaxErrorDebugger",
            default=False,
            action="store_true",
            help=
            "Only makes sense with --interactive-debugger: Do interactive debugging even when a syntax error was encountered"
        )
        dbg.add_option(
            "--i-am-a-developer",
            dest="developerMode",
            default=False,
            action="store_true",
            help=
            "Switch on all of the above options. Usually this makes only sense if you're developing PyFoam'"
        )
        dbg.add_option(
            "--interactive-after-execution",
            dest="interacticeAfterExecution",
            default=False,
            action="store_true",
            help=
            "Instead of ending execution drop to an interactive shell (which is IPython if possible)"
        )

        grp.add_option(
            "--dump-application-data",
            dest="dumpAppData",
            default=False,
            action="store_true",
            help=
            "Print the dictionary with the generated application data after running"
        )
        grp.add_option("--pickle-application-data",
                       dest="pickleApplicationData",
                       default=None,
                       action="store",
                       type="string",
                       help="""\
Write a pickled version of the application data to a file. If the
filename given is 'stdout' then the pickled data is written to
stdout. The usual standard output is then captured and added to the
application data as an entry 'stdout' (same for 'stderr'). Be careful
with these option for commands that generate a lot of output""")

        self.parser.add_option_group(grp)
        self.parser.add_option_group(dbg)

        self.addOptions()
        self.parser.parse(nr=nr, exactNr=exactNr)
        ensureDynamicLibraries()
        if len(kwArgs) > 0:
            self.parser.processKeywordArguments(kwArgs)
        self.opts = self.parser.getOptions()
        if self.subs:
            self.cmdname = self.parser.cmdname

        if self.opts.locationOfLocalConfig:
            if _LocalConfigurationFile:
                print_("Local configuration found at", _LocalConfigurationFile)
            else:
                print_("No LocalConfigPyFoam-file found")

        if "WM_PROJECT_VERSION" not in environ:
            warning(
                "$WM_PROJECT_VERSION unset. PyFoam will not be able to determine the OpenFOAM-version and behave strangely"
            )
        if self.opts.developerMode:
            self.opts.syntaxErrorDebugger = True
            self.opts.keyboardInterrupTrace = True
            self.opts.alsoCatchTERMsignal = True
            self.opts.catchUSR1Signal = True
            self.opts.interactiveDebug = True
            self.opts.traceback = True

        if self.opts.interactiveDebug:
            sys.excepthook = lambda a1, a2, a3: pyFoamExceptionHook(
                a1, a2, a3, debugOnSyntaxError=self.opts.syntaxErrorDebugger)
            self.opts.traceback = True
        if self.opts.catchUSR1Signal:
            import signal
            signal.signal(signal.SIGUSR1, pyFoamSIG1HandlerPrintStack)
            if self.opts.alsoCatchTERMsignal:
                signal.signal(signal.SIGTERM, pyFoamSIG1HandlerPrintStack)
            self.opts.traceback = True

        if self.opts.keyboardInterrupTrace:
            import signal
            signal.signal(signal.SIGINT, pyFoamSIG1HandlerPrintStack)
            self.opts.traceback = True

        if self.opts.psyco:
            try:
                import psyco
                psyco.full()
            except ImportError:
                warning("No psyco installed. Continuing without acceleration")
        profOptions = sum([
            self.opts.profilePython, self.opts.profileCPython,
            self.opts.profileHotshot, self.opts.profileLineProfiler
        ])
        if profOptions > 0:
            if profOptions > 1:
                self.error(
                    "Only one profiling option can be specified at a time")
            print_("Running profiled")
            fnAdd = ""
            if self.opts.profilePython:
                import profile
            elif self.opts.profileCPython:
                import cProfile as profile
            elif self.opts.profileLineProfiler:
                import line_profiler
                profile = line_profiler.LineProfiler(self.run)
                import PyFoam.RunDictionary.SolutionDirectory
                profile.add_module(PyFoam.RunDictionary.SolutionDirectory)
                fnAdd = ".lineProfiler"
            else:
                import hotshot
            profileData = path.basename(sys.argv[0]) + fnAdd + ".profile"
            if self.opts.profilePython or self.opts.profileCPython:
                profile.runctx('self.run()', None, {'self': self}, profileData)
                print_("Reading python profile")
                import pstats
                stats = pstats.Stats(profileData)
            elif self.opts.profileLineProfiler:
                import inspect
                nr = profile.add_module(inspect.getmodule(self))
                self.warning("Adding", nr, "functions for line-profiling")
                profile.runctx('self.run()', None, {'self': self})
                profile.dump_stats(profileData)
                profile.print_stats(open(profileData + ".printed", "w"))
                stats = None
            else:
                profileData += ".hotshot"
                prof = hotshot.Profile(profileData)
                prof.runctx('self.run()', {}, {'self': self})
                print_("Writing and reading hotshot profile")
                prof.close()
                import hotshot.stats
                stats = hotshot.stats.load(profileData)
            if stats:
                stats.strip_dirs()
                stats.sort_stats('time', 'calls')
                stats.print_stats(20)

            self.parser.restoreEnvironment()
        else:
            try:
                if self.opts.pickleApplicationData == "stdout":
                    # Redirect output to memory
                    from PyFoam.ThirdParty.six.moves import StringIO

                    oldStdout = sys.stdout
                    oldStderr = sys.stderr
                    sys.stdout = StringIO()
                    sys.stderr = StringIO()

                result = self.run()

                # do this at the earliest possible moment
                self.parser.restoreEnvironment()

                if self.opts.pickleApplicationData == "stdout":
                    # restore stdout
                    self.__appData["stdout"] = sys.stdout.getvalue()
                    self.__appData["stderr"] = sys.stderr.getvalue()
                    sys.stdout = oldStdout
                    sys.stderr = oldStderr

                if self.opts.pickleApplicationData:
                    from PyFoam.ThirdParty.six.moves import cPickle as pickle
                    if self.opts.pickleApplicationData == "stdout":
                        pick = pickle.Pickler(sys.stdout)
                    else:
                        pick = pickle.Pickler(
                            open(self.opts.pickleApplicationData, 'wb'))
                    pick.dump(self.__appData)
                    del pick
                if self.opts.dumpAppData:
                    import pprint
                    print_("Application data:")
                    printer = pprint.PrettyPrinter()
                    printer.pprint(self.__appData)

                if self.opts.interacticeAfterExecution:
                    print_("\nDropping to interactive shell ... ", end="")
                    ns = {}
                    ns.update(locals())
                    ns.update(globals())
                    try:
                        import IPython
                        print_("found IPython ...", end="")
                        if "embed" in dir(IPython):
                            print_("up-to-date IPython\n")
                            IPython.embed(user_ns=ns)
                        else:
                            print_("old-school IPython\n")
                            IPython.Shell.IPythonShellEmbed(argv="",
                                                            user_ns=ns)()

                    except ImportError:
                        print_("no IPython -> regular shell\n")
                        from code import InteractiveConsole
                        c = InteractiveConsole(ns)
                        c.interact()
                    print_("\nEnding interactive shell\n")
                return result
            except PyFoamException:
                e = sys.exc_info()[1]
                if self.opts.traceback or self.calledAsClass:
                    raise
                else:
                    self.errorPrint(str(e))
示例#19
0
    def prepare(self,
                sol,
                cName=None,
                overrideParameters=None,
                numberOfProcessors=None):
        """Do the actual preparing
        :param numberOfProcessors: If set this overrides the value set in the
        command line"""

        if cName == None:
            cName = sol.name

        if self.opts.onlyVariables:
            self.opts.verbose = True

        vals = {}
        vals, self.metaData = self.getDefaultValues(cName)
        vals.update(
            self.addDictValues(
                "System", "Automatically defined values", {
                    "casePath":
                    '"' + path.abspath(cName) + '"',
                    "caseName":
                    '"' + path.basename(path.abspath(cName)) + '"',
                    "foamVersion":
                    foamVersion(),
                    "foamFork":
                    foamFork(),
                    "numberOfProcessors":
                    numberOfProcessors if numberOfProcessors != None else
                    self.opts.numberOfProcessors
                }))

        if len(self.opts.extensionAddition) > 0:
            vals.update(
                self.addDictValues(
                    "ExtensionAdditions",
                    "Additional extensions to be processed",
                    dict((e, True) for e in self.opts.extensionAddition)))

        valsWithDefaults = set(vals.keys())

        self.info("Looking for template values", cName)
        for f in self.opts.valuesDicts:
            self.info("Reading values from", f)
            vals.update(
                ParsedParameterFile(f, noHeader=True,
                                    doMacroExpansion=True).getValueDict())

        setValues = {}
        for v in self.opts.values:
            self.info("Updating values", v)
            vals.update(eval(v))
            setValues.update(eval(v))

        if overrideParameters:
            vals.update(overrideParameters)

        unknownValues = set(vals.keys()) - valsWithDefaults
        if len(unknownValues) > 0:
            self.warning("Values for which no default was specified: " +
                         ", ".join(unknownValues))

        if self.opts.verbose and len(vals) > 0:
            print_("\nUsed values\n")
            nameLen = max(len("Name"), max(*[len(k) for k in vals.keys()]))
            format = "%%%ds - %%s" % nameLen
            print_(format % ("Name", "Value"))
            print_("-" * 40)
            for k, v in sorted(iteritems(vals)):
                print_(format % (k, v))
            print_("")
        else:
            self.info("\nNo values specified\n")

        self.checkCorrectOptions(vals)

        derivedScript = path.join(cName, self.opts.derivedParametersScript)
        derivedAdded = None
        derivedChanged = None
        if path.exists(derivedScript):
            self.info("Deriving variables in script", derivedScript)
            scriptText = open(derivedScript).read()
            glob = {}
            oldVals = vals.copy()
            exec_(scriptText, glob, vals)
            derivedAdded = []
            derivedChanged = []
            for k, v in iteritems(vals):
                if k not in oldVals:
                    derivedAdded.append(k)
                elif vals[k] != oldVals[k]:
                    derivedChanged.append(k)
            if len(derivedChanged) > 0 and (
                    not self.opts.allowDerivedChanges
                    and not configuration().getboolean("PrepareCase",
                                                       "AllowDerivedChanges")):
                self.error(
                    self.opts.derivedParametersScript, "changed values of",
                    " ".join(derivedChanged),
                    "\nTo allow this set --allow-derived-changes or the configuration item 'AllowDerivedChanges'"
                )
            if len(derivedAdded) > 0:
                self.info("Added values:", " ".join(derivedAdded))
            if len(derivedChanged) > 0:
                self.info("Changed values:", " ".join(derivedChanged))
            if len(derivedAdded) == 0 and len(derivedChanged) == 0:
                self.info("Nothing added or changed")
        else:
            self.info("No script", derivedScript, "for derived values")

        if self.opts.onlyVariables:
            return

        self.__writeToStateFile(sol, "Starting")

        if self.opts.doClear:
            self.info("Clearing", cName)
            self.__writeToStateFile(sol, "Clearing")
            sol.clear(processor=True,
                      pyfoam=True,
                      vtk=True,
                      removeAnalyzed=True,
                      keepParallel=False,
                      clearHistory=False,
                      clearParameters=True,
                      additional=["postProcessing"])
            self.__writeToStateFile(sol, "Done clearing")

        if self.opts.writeParameters:
            fName = path.join(cName, self.parameterOutFile)
            self.info("Writing parameters to", fName)
            with WriteParameterFile(fName, noHeader=True) as w:
                w.content.update(vals, toString=True)
                w["foamVersion"] = vals["foamVersion"]
                w.writeFile()

        if self.opts.writeReport:
            fName = path.join(cName, self.parameterOutFile + ".rst")
            self.info("Writing report to", fName)
            with open(fName, "w") as w:
                helper = RestructuredTextHelper(defaultHeading=1)
                w.write(".. title:: " + self.__strip(vals["caseName"]) + "\n")
                w.write(".. sectnum::\n")
                w.write(".. header:: " + self.__strip(vals["caseName"]) + "\n")
                w.write(".. header:: " + time.asctime() + "\n")
                w.write(".. footer:: ###Page### / ###Total###\n\n")

                w.write("Parameters set in case directory " +
                        helper.literal(self.__strip(vals["casePath"])) +
                        " at " + helper.emphasis(time.asctime()) + "\n\n")
                w.write(".. contents::\n\n")
                if len(self.opts.valuesDicts):
                    w.write(helper.heading("Parameter files"))
                    w.write("Parameters read from files\n\n")
                    w.write(
                        helper.enumerateList([
                            helper.literal(f) for f in self.opts.valuesDicts
                        ]))
                    w.write("\n")
                if len(setValues) > 0:
                    w.write(helper.heading("Overwritten parameters"))
                    w.write(
                        "These parameters were set from the command line\n\n")
                    w.write(helper.definitionList(setValues))
                    w.write("\n")
                w.write(helper.heading("Parameters with defaults"))
                w.write(self.makeReport(vals))
                if len(unknownValues) > 0:
                    w.write(helper.heading("Unspecified parameters"))
                    w.write(
                        "If these parameters are actually used then specify them in "
                        + helper.literal(self.defaultParameterFile) + "\n\n")
                    tab = helper.table(True)
                    for u in unknownValues:
                        tab.addRow(u)
                        tab.addItem("Value", vals[u])
                    w.write(str(tab))
                if not derivedAdded is None:
                    w.write(helper.heading("Derived Variables"))
                    w.write("Script with derived Parameters" +
                            helper.literal(derivedScript) + "\n\n")
                    if len(derivedAdded) > 0:
                        w.write("These values were added:\n")
                        tab = helper.table(True)
                        for a in derivedAdded:
                            tab.addRow(a)
                            tab.addItem("Value", str(vals[a]))
                        w.write(str(tab))
                    if len(derivedChanged) > 0:
                        w.write("These values were changed:\n")
                        tab = helper.table(True)
                        for a in derivedChanged:
                            tab.addRow(a)
                            tab.addItem("Value", str(vals[a]))
                            tab.addItem("Old", str(oldVals[a]))
                        w.write(str(tab))
                    w.write("The code of the script:\n")
                    w.write(helper.code(scriptText))

        self.addToCaseLog(cName)

        for over in self.opts.overloadDirs:
            self.info("Overloading files from", over)
            self.__writeToStateFile(sol, "Overloading")
            self.overloadDir(sol.name, over)

        self.__writeToStateFile(sol, "Initial")

        zeroOrig = path.join(sol.name, "0.org")

        hasOrig = path.exists(zeroOrig)
        cleanZero = True

        if not hasOrig:
            self.info("Not going to clean '0'")
            self.opts.cleanDirectories.remove("0")
            cleanZero = False

        if self.opts.doCopy:
            if hasOrig:
                self.info("Found 0.org. Clearing 0")
                zeroDir = path.join(sol.name, "0")
                if path.exists(zeroDir):
                    rmtree(zeroDir)
                else:
                    self.info("No 0-directory")

            self.info("")
        else:
            cleanZero = False

        if self.opts.doTemplates:
            self.__writeToStateFile(sol, "Templates")
            self.searchAndReplaceTemplates(
                sol.name,
                vals,
                self.opts.templateExt,
                ignoreDirectories=self.opts.ignoreDirectories)

            self.info("")

        backupZeroDir = None

        if self.opts.doMeshCreate:
            self.__writeToStateFile(sol, "Meshing")
            if self.opts.meshCreateScript:
                scriptName = path.join(sol.name, self.opts.meshCreateScript)
                if not path.exists(scriptName):
                    self.error("Script", scriptName, "does not exist")
            elif path.exists(path.join(sol.name, self.defaultMeshCreate)):
                scriptName = path.join(sol.name, self.defaultMeshCreate)
            else:
                scriptName = None

            if scriptName:
                self.info("Executing", scriptName, "for mesh creation")
                if self.opts.verbose:
                    echo = "Mesh: "
                else:
                    echo = None
                self.executeScript(scriptName, workdir=sol.name, echo=echo)
            else:
                self.info(
                    "No script for mesh creation found. Looking for 'blockMeshDict'"
                )
                if sol.blockMesh() != "":
                    self.info(sol.blockMesh(), "found. Executing 'blockMesh'")
                    bm = BasicRunner(argv=["blockMesh", "-case", sol.name])
                    bm.start()
                    if not bm.runOK():
                        self.error("Problem with blockMesh")
                for r in sol.regions():
                    self.info("Checking region", r)
                    s = SolutionDirectory(sol.name,
                                          region=r,
                                          archive=None,
                                          paraviewLink=False)
                    if s.blockMesh() != "":
                        self.info(s.blockMesh(),
                                  "found. Executing 'blockMesh'")
                        bm = BasicRunner(argv=[
                            "blockMesh", "-case", sol.name, "-region", r
                        ])
                        bm.start()
                        if not bm.runOK():
                            self.error("Problem with blockMesh")

            self.info("")

            if cleanZero and path.exists(zeroDir):
                self.warning("Mesh creation recreated 0-directory")
                if self.opts.keepZeroDirectoryFromMesh:
                    backupZeroDir = zeroDir + ".bakByPyFoam"
                    self.info("Backing up", zeroDir, "to", backupZeroDir)
                    move(zeroDir, backupZeroDir)
                else:
                    self.info("Data in", zeroDir, "will be removed")
            self.__writeToStateFile(sol, "Done Meshing")

        if self.opts.doCopy:
            self.__writeToStateFile(sol, "Copying")
            self.copyOriginals(sol.name)

            self.info("")

            if backupZeroDir:
                self.info("Copying backups from", backupZeroDir, "to", zeroDir)
                self.overloadDir(zeroDir, backupZeroDir)
                self.info("Removing backup", backupZeroDir)
                rmtree(backupZeroDir)

        if self.opts.doPostTemplates:
            self.__writeToStateFile(sol, "Post-templates")
            self.searchAndReplaceTemplates(
                sol.name,
                vals,
                self.opts.postTemplateExt,
                ignoreDirectories=self.opts.ignoreDirectories)

            self.info("")

        if self.opts.doCaseSetup:
            self.__writeToStateFile(sol, "Case setup")
            if self.opts.caseSetupScript:
                scriptName = path.join(sol.name, self.opts.caseSetupScript)
                if not path.exists(scriptName):
                    self.error("Script", scriptName, "does not exist")
            elif path.exists(path.join(sol.name, self.defaultCaseSetup)):
                scriptName = path.join(sol.name, self.defaultCaseSetup)
            else:
                scriptName = None

            if scriptName:
                self.info("Executing", scriptName, "for case setup")
                if self.opts.verbose:
                    echo = "Case:"
                else:
                    echo = None
                self.executeScript(scriptName, workdir=sol.name, echo=echo)
            elif path.exists(path.join(sol.name, "system", "setFieldsDict")):
                self.info(
                    "So setup script found. But 'setFieldsDict'. Executing setFields"
                )
                sf = BasicRunner(argv=["setFields", "-case", sol.name])
                sf.start()
                if not sf.runOK():
                    self.error("Problem with setFields")
            else:
                self.info("No script for case-setup found. Nothing done")
            self.info("")
            self.__writeToStateFile(sol, "Done case setup")

        if self.opts.doFinalTemplates:
            self.__writeToStateFile(sol, "Final templates")
            self.searchAndReplaceTemplates(
                sol.name,
                vals,
                self.opts.finalTemplateExt,
                ignoreDirectories=self.opts.ignoreDirectories)

        if self.opts.doTemplateClean:
            self.info("Clearing templates")
            for d in self.opts.cleanDirectories:
                for e in [
                        self.opts.templateExt, self.opts.postTemplateExt,
                        self.opts.finalTemplateExt
                ]:
                    self.cleanExtension(path.join(sol.name, d), e)
            self.info("")

        self.info("Case setup finished")
        self.__writeToStateFile(sol, "Finished OK")
示例#20
0
    def __init__(self,
                 args=None,
                 exactNr=True,
                 interspersed=True,
                 usage="%prog <caseDirectory>",
                 examples=None,
                 nr=1,
                 description=None,
                 **kwargs):
        self.defaultMeshCreate = configuration().get("PrepareCase",
                                                     "MeshCreateScript")
        self.defaultCaseSetup = configuration().get("PrepareCase",
                                                    "CaseSetupScript")
        self.defaultParameterFile = configuration().get(
            "PrepareCase", "DefaultParameterFile")
        self.defaultIgnoreDirecotries = configuration().getList(
            "PrepareCase", "IgnoreDirectories")

        description2 = """\
Prepares a case for running. This is intended to be the replacement for
boiler-plate scripts. The steps done are

  1. Clear old data from the case (including processor directories)

  2. if a folder 0.org is present remove the 0 folder too

  3. go through all folders and for every found file with the extension .template
do template expansion using the pyratemp-engine (automatically create variables
casePath and caseName)

  4. create a mesh (either by using a script or if a blockMeshDict is present by
running blockMesh. If none of these is present assume that there is a valid mesh
present)

  5. copy every foo.org that is found to to foo (recursively if directory)

  6. do template-expansion for every file with the extension .postTemplate

  7. execute another preparation script (caseSetup.sh). If no such script is found
but a setFieldsDict in system then setFields is executed

  8. do final template-expansion for every file with the extension .finalTemplate

The used parameters are written to a file 'PyFoamPrepareCaseParameters' and are used by other utilities
"""
        examples2 = """\
%prog . --paramter-file=parameters.base

  Prepare the current case with the parameters list in parameters.base

%prog . --paramter-file=parameters.base --values-string="{'visco':1e-3}"

  Changes the value of the parameter visco

%prog . --no-mesh-create

  Skip the mesh creation phase
"""
        PyFoamApplication.__init__(
            self,
            args=args,
            description=description if description else description2,
            usage=usage,
            examples=examples if examples else examples2,
            interspersed=interspersed,
            nr=nr,
            exactNr=exactNr,
            findLocalConfigurationFile=self.localConfigInArgs,
            **kwargs)
    def __init__(self,
                 args=None,
                 description=None,
                 epilog=None,
                 examples=None,
                 usage=None,
                 interspersed=False,
                 nr=None,
                 changeVersion=True,
                 exactNr=True,
                 subcommands=None,
                 inputApp=None,
                 localConfigurationFile=None,
                 findLocalConfigurationFile=None,
                 **kwArgs):
        """
        @param description: description of the command
        @param epilog: text to be printed after the options-help
        @param examples: usage examples to be printed after the epilog
        @param usage: Usage
        @param interspersed: Is the command line allowed to be interspersed (options after the arguments)
        @param args: Command line arguments when using the Application as a 'class' from a script
        @param nr: Number of required arguments
        @param changeVersion: May this application change the version of OF used?
        @param exactNr: Must not have more than the required number of arguments
        @param subcommands: parse and use subcommands from the command line. Either True or a list with subcommands
        @param inputApp: Application with input data. Used to allow a 'pipe-like' behaviour if the class is used from a Script
        @param localConfigurationFile: Use this file (or list of files) as a local configuration
        @param findLocalConfigurationFile: Method to find a configuration file BEFORE the actual parameters are parsed
        """

        global _LocalConfigurationFile

        if _LocalConfigurationFile is not None:
             configuration().addFile(_LocalConfigurationFile)

        if isinstance(localConfigurationFile,string_types):
             configuration().addFile(localConfigurationFile)
        elif localConfigurationFile is not None:
             for c in localConfigurationFile:
                  configuration().addFile(c)

        if subcommands:
             self.subs=True
             if interspersed:
                  self.error("Subcommand parser does not work with 'interspersed'")
             if subcommands==True:
                  subcommands=[]
             self.parser=SubcommandFoamOptionParser(args=args,
                                                    description=description,
                                                    epilog=epilog,
                                                    examples=examples,
                                                    usage=usage,
                                                    subcommands=subcommands)
             nr=None
             exactNr=False
        else:
             self.subs=False
             self.parser=FoamOptionParser(args=args,
                                          description=description,
                                          epilog=epilog,
                                          examples=examples,
                                          usage=usage,
                                          interspersed=interspersed)

        self.calledName=sys.argv[0]
        self.calledAsClass=(args!=None)
        if self.calledAsClass:
            self.calledName=self.__class__.__name__+" used by "+sys.argv[0]
            self.parser.prog=self.calledName
        elif not _LocalConfigurationFile and findLocalConfigurationFile:
            if args:
                usedArgs=args
            else:
                usedArgs=sys.argv[1:]
            _LocalConfigurationFile=findLocalConfigurationFile(usedArgs)
            if _LocalConfigurationFile and not path.exists(_LocalConfigurationFile):
                # Fix functions that do not check for the existence
                _LocalConfigurationFile=None
            if _LocalConfigurationFile:
                 configuration().addFile(_LocalConfigurationFile)

        self.generalOpts=None

        self.__appData=self.iDict()
        if inputApp:
            self.__appData["inputData"]=inputApp.getData()

        grp=OptionGroup(self.parser,
                        "Default",
                        "Options common to all PyFoam-applications")

        if changeVersion:
            # the options are evaluated in Basics.FoamOptionParser
            grp.add_option("--foamVersion",
                           dest="foamVersion",
                           default=None,
                           help="Change the OpenFOAM-version that is to be used. To get a list of know Foam-versions use the pyFoamVersion.py-utility")
            if "WM_PROJECT_VERSION" in environ:
                grp.add_option("--currentFoamVersion",
                               dest="foamVersion",
                               const=environ["WM_PROJECT_VERSION"],
                               default=None,
                               action="store_const",
                               help="Use the current OpenFOAM-version "+environ["WM_PROJECT_VERSION"])

            grp.add_option("--force-32bit",
                           dest="force32",
                           default=False,
                           action="store_true",
                           help="Forces the usage of a 32-bit-version if that version exists as 32 and 64 bit. Only used when --foamVersion is used")
            grp.add_option("--force-64bit",
                           dest="force64",
                           default=False,
                           action="store_true",
                           help="Forces the usage of a 64-bit-version if that version exists as 32 and 64 bit. Only used when --foamVersion is used")
            grp.add_option("--force-debug",
                           dest="compileOption",
                           const="Debug",
                           default=None,
                           action="store_const",
                           help="Forces the value Debug for the WM_COMPILE_OPTION. Only used when --foamVersion is used")
            grp.add_option("--force-opt",
                           dest="compileOption",
                           const="Opt",
                           default=None,
                           action="store_const",
                           help="Forces the value Opt for the WM_COMPILE_OPTION. Only used when --foamVersion is used")
            grp.add_option("--force-system-compiler",
                           dest="foamCompiler",
                           const="system",
                           default=None,
                           action="store_const",
                           help="Force using a 'system' compiler (compiler installed in the system)")
            grp.add_option("--force-openfoam-compiler",
                           dest="foamCompiler",
                           const="OpenFOAM",
                           default=None,
                           action="store_const",
                           help="Force using a 'OpenFOAM' compiler (compiler installed in ThirdParty)")
            grp.add_option("--force-compiler",
                           dest="wmCompiler",
                           default=None,
                           action="store",
                           help="Overwrite value for WM_COMPILER (for instance Gcc47 ...)")

        grp.add_option("--psyco-accelerated",
                       dest="psyco",
                       default=False,
                       action="store_true",
                       help="Accelerate the script using the psyco-library (EXPERIMENTAL and requires a separatly installed psyco)")
        grp.add_option("--profile-python",
                       dest="profilePython",
                       default=False,
                       action="store_true",
                       help="Profile the python-script (not the OpenFOAM-program) - mostly of use for developers")
        grp.add_option("--profile-cpython",
                       dest="profileCPython",
                       default=False,
                       action="store_true",
                       help="Profile the python-script (not the OpenFOAM-program) using the better cProfile library - mostly of use for developers")
        grp.add_option("--profile-hotshot",
                       dest="profileHotshot",
                       default=False,
                       action="store_true",
                       help="Profile the python-script using the hotshot-library (not the OpenFOAM-program) - mostly of use for developers - DEPRECATED as this library will by removed from standard python and is no longer supported")
        grp.add_option("--profile-line-profiler",
                       dest="profileLineProfiler",
                       default=False,
                       action="store_true",
                       help="Profile the python-script using the line_profiler-library (not the OpenFOAM-program) - mostly of use for developers - EXPERIMENTAL")

        dbg=OptionGroup(self.parser,
                        "Debugging",
                        "Options mainly used for debugging PyFoam-Utilities")

        dbg.add_option("--location-of-local-config",
                       dest="locationOfLocalConfig",
                       default=False,
                       action="store_true",
                       help="Prints the location of the found LocalConfigPyFoam-file that is used (if any)")
        dbg.add_option("--traceback-on-error",
                       dest="traceback",
                       default=False,
                       action="store_true",
                       help="Prints a traceback when an error is encountered (for debugging)")
        dbg.add_option("--interactive-debugger",
                       dest="interactiveDebug",
                       default=False,
                       action="store_true",
                       help="In case of an exception start the interactive debugger PDB. Also implies --traceback-on-error")
        dbg.add_option("--catch-USR1-signal",
                       dest="catchUSR1Signal",
                       default=False,
                       action="store_true",
                       help="If the USR1-signal is sent to the application with 'kill -USR1 <pid>' the application ens and prints a traceback. If interactive debugging is enabled then the debugger is entered. Use to investigate hangups")
        dbg.add_option("--also-catch-TERM-signal",
                       dest="alsoCatchTERMsignal",
                       default=False,
                       action="store_true",
                       help="In addition to USR1 catch the regular TERM-kill")
        dbg.add_option("--keyboard-interrupt-trace",
                       dest="keyboardInterrupTrace",
                       default=False,
                       action="store_true",
                       help="Make the application behave like with --catch-USR1-signal if <Ctrl>-C is pressed")
        dbg.add_option("--syntax-error-debugger",
                       dest="syntaxErrorDebugger",
                       default=False,
                       action="store_true",
                       help="Only makes sense with --interactive-debugger: Do interactive debugging even when a syntax error was encountered")
        dbg.add_option("--i-am-a-developer",
                       dest="developerMode",
                       default=False,
                       action="store_true",
                       help="Switch on all of the above options. Usually this makes only sense if you're developing PyFoam'")
        dbg.add_option("--interactive-after-execution",
                       dest="interacticeAfterExecution",
                       default=False,
                       action="store_true",
                       help="Instead of ending execution drop to an interactive shell (which is IPython if possible)")

        grp.add_option("--dump-application-data",
                       dest="dumpAppData",
                       default=False,
                       action="store_true",
                       help="Print the dictionary with the generated application data after running")
        grp.add_option("--pickle-application-data",
                       dest="pickleApplicationData",
                       default=None,
                       action="store",
                       type="string",
                       help="""\
Write a pickled version of the application data to a file. If the
filename given is 'stdout' then the pickled data is written to
stdout. The usual standard output is then captured and added to the
application data as an entry 'stdout' (same for 'stderr'). Be careful
with these option for commands that generate a lot of output""")

        self.parser.add_option_group(grp)
        self.parser.add_option_group(dbg)

        self.addOptions()
        self.parser.parse(nr=nr,exactNr=exactNr)
        if len(kwArgs)>0:
            self.parser.processKeywordArguments(kwArgs)
        self.opts=self.parser.getOptions()
        if self.subs:
            self.cmdname=self.parser.cmdname

        if self.opts.locationOfLocalConfig:
            if _LocalConfigurationFile:
                print_("Local configuration found at",
                      _LocalConfigurationFile)
            else:
                print_("No LocalConfigPyFoam-file found")

        if "WM_PROJECT_VERSION" not in environ:
             warning("$WM_PROJECT_VERSION unset. PyFoam will not be able to determine the OpenFOAM-version and behave strangely")
        if self.opts.developerMode:
             self.opts.syntaxErrorDebugger=True
             self.opts.keyboardInterrupTrace=True
             self.opts.alsoCatchTERMsignal=True
             self.opts.catchUSR1Signal=True
             self.opts.interactiveDebug=True
             self.opts.traceback=True

        if self.opts.interactiveDebug:
            sys.excepthook=lambda a1,a2,a3:pyFoamExceptionHook(a1,
                                                               a2,
                                                               a3,
                                                               debugOnSyntaxError=self.opts.syntaxErrorDebugger)
            self.opts.traceback=True
        if self.opts.catchUSR1Signal:
             import signal
             signal.signal(signal.SIGUSR1,pyFoamSIG1HandlerPrintStack)
             if self.opts.alsoCatchTERMsignal:
                  signal.signal(signal.SIGTERM,pyFoamSIG1HandlerPrintStack)
             self.opts.traceback=True

        if self.opts.keyboardInterrupTrace:
             import signal
             signal.signal(signal.SIGINT,pyFoamSIG1HandlerPrintStack)
             self.opts.traceback=True

        if self.opts.psyco:
            try:
                import psyco
                psyco.full()
            except ImportError:
                warning("No psyco installed. Continuing without acceleration")
        profOptions=sum([self.opts.profilePython,
                         self.opts.profileCPython,
                         self.opts.profileHotshot,
                         self.opts.profileLineProfiler])
        if profOptions>0:
            if profOptions>1:
                self.error("Only one profiling option can be specified at a time")
            print_("Running profiled")
            fnAdd=""
            if self.opts.profilePython:
                import profile
            elif self.opts.profileCPython:
                import cProfile as profile
            elif self.opts.profileLineProfiler:
                import line_profiler
                profile=line_profiler.LineProfiler(self.run)
                import PyFoam.RunDictionary.SolutionDirectory
                profile.add_module(PyFoam.RunDictionary.SolutionDirectory)
                fnAdd=".lineProfiler"
            else:
                import hotshot
            profileData=path.basename(sys.argv[0])+fnAdd+".profile"
            if self.opts.profilePython or self.opts.profileCPython:
                profile.runctx('self.run()',None,{'self':self},profileData)
                print_("Reading python profile")
                import pstats
                stats=pstats.Stats(profileData)
            elif self.opts.profileLineProfiler:
                import inspect
                nr=profile.add_module(inspect.getmodule(self))
                self.warning("Adding",nr,"functions for line-profiling")
                profile.runctx('self.run()',None,{'self':self})
                profile.dump_stats(profileData)
                profile.print_stats(open(profileData+".printed","w"))
                stats=None
            else:
                profileData+=".hotshot"
                prof=hotshot.Profile(profileData)
                prof.runctx('self.run()',{},{'self':self})
                print_("Writing and reading hotshot profile")
                prof.close()
                import hotshot.stats
                stats=hotshot.stats.load(profileData)
            if stats:
                stats.strip_dirs()
                stats.sort_stats('time','calls')
                stats.print_stats(20)

            self.parser.restoreEnvironment()
        else:
            try:
                if self.opts.pickleApplicationData=="stdout":
                    # Redirect output to memory
                    from PyFoam.ThirdParty.six.moves import StringIO

                    oldStdout=sys.stdout
                    oldStderr=sys.stderr
                    sys.stdout=StringIO()
                    sys.stderr=StringIO()

                result=self.run()

                # do this at the earliest possible moment
                self.parser.restoreEnvironment()

                if self.opts.pickleApplicationData=="stdout":
                    # restore stdout
                    self.__appData["stdout"]=sys.stdout.getvalue()
                    self.__appData["stderr"]=sys.stderr.getvalue()
                    sys.stdout=oldStdout
                    sys.stderr=oldStderr

                if self.opts.pickleApplicationData:
                    from PyFoam.ThirdParty.six.moves import cPickle as pickle
                    if self.opts.pickleApplicationData=="stdout":
                        pick=pickle.Pickler(sys.stdout)
                    else:
                        pick=pickle.Pickler(open(self.opts.pickleApplicationData,'wb'))
                    pick.dump(self.__appData)
                    del pick
                if self.opts.dumpAppData:
                    import pprint
                    print_("Application data:")
                    printer=pprint.PrettyPrinter()
                    printer.pprint(self.__appData)

                if self.opts.interacticeAfterExecution:
                     print_("\nDropping to interactive shell ... ",end="")
                     ns={}
                     ns.update(locals())
                     ns.update(globals())
                     try:
                          import IPython
                          print_("found IPython ...",end="")
                          if "embed" in dir(IPython):
                               print_("up-to-date IPython\n")
                               IPython.embed(user_ns=ns)
                          else:
                               print_("old-school IPython\n")
                               IPython.Shell.IPythonShellEmbed(argv="",user_ns=ns)()

                     except ImportError:
                          print_("no IPython -> regular shell\n")
                          from code import InteractiveConsole
                          c=InteractiveConsole(ns)
                          c.interact()
                     print_("\nEnding interactive shell\n")
                return result
            except PyFoamException:
                e=sys.exc_info()[1]
                if self.opts.traceback or self.calledAsClass:
                    raise
                else:
                    self.errorPrint(str(e))
示例#22
0
文件: RunHook.py 项目: minhbau/PyFoam
 def conf(self):
     """Quick access to the configuration"""
     return configuration().sectionProxy(self.name)
示例#23
0
文件: RunHook.py 项目: LeeRuns/PyFoam
 def conf(self):
     """Quick access to the configuration"""
     return configuration().sectionProxy(self.name)
    def addOptions(self):
        what=OptionGroup(self.parser,
                         "What",
                         "Define what should be shown")
        self.parser.add_option_group(what)

        what.add_option("--dump",
                        action="store_true",
                        dest="dump",
                        default=False,
                        help="Dump the information as Python-dictionaries")

        what.add_option("--disk-usage",
                        action="store_true",
                        dest="diskusage",
                        default=False,
                        help="Show the disk-usage of the case (in MB) - may take a long time")

        what.add_option("--parallel-info",
                        action="store_true",
                        dest="parallel",
                        default=False,
                        help="Print information about parallel runs (if present): number of processors and processor first and last time. The mtime will be that of the processor-directories")

        what.add_option("--no-state",
                        action="store_false",
                        dest="state",
                        default=True,
                        help="Don't read state-files")

        what.add_option("--no-hostname",
                        action="store_false",
                        dest="hostname",
                        default=True,
                        help="Don't look up the hostname in the pickled data")

        what.add_option("--advanced-state",
                        action="store_true",
                        dest="advancedState",
                        default=False,
                        help="Additional state information (run started, last output seen)")

        what.add_option("--estimate-end-time",
                        action="store_true",
                        dest="estimateEndTime",
                        default=False,
                        help="Print an estimated end time (calculated from the start time of the run, the current time and the current simulation time)")

        what.add_option("--start-end-time",
                        action="store_true",
                        dest="startEndTime",
                        default=False,
                        help="Start and end time from the controlDict")

        what.add_option("--custom-data",
                        action="append",
                        dest="customData",
                        default=[],
                        help="Specification of additional data that is read from the pickled data-sets. The format is 'name=spec1::spec2::...' where 'name' is the name under which the data ist shown and 'specN' are the dictionary keys under which the data is accessed. If only 'spec1::spec2::..' is given then a name of the form 'CustomN' will be used. Can be specified more than once")

        what.add_option("--solver-name-for-custom-data",
                        action="store",
                        dest="solverNameForCustom",
                        default=None,
                        help="This is used if '--custom-data' is specified as the data will be searched in 'PyFoamRunner.<solver name>.analyzed'. If unset then the utility will try to automatically determine the name of the solver which might be wrong")

        what.add_option("--hg-info",
                        action="store_true",
                        dest="hgInfo",
                        default=False,
                        help="Looks for .hg in the directories and reports mercurial version info (for those who keep track of their cases with mercurial)")

        how=OptionGroup(self.parser,
                         "How",
                         "How the things should be shown")
        self.parser.add_option_group(how)

        how.add_option("--sort-by",
                        type="choice",
                        action="store",
                        dest="sort",
                        default=configuration().get("CommandOptionDefaults","sortListCases",default="name"),
                        choices=self.sortChoices,
                        help="Sort the cases by a specific key (Keys: "+", ".join(self.sortChoices)+") Default: %default")
        how.add_option("--reverse-sort",
                       action="store_true",
                       dest="reverse",
                       default=False,
                       help="Sort in reverse order")
        how.add_option("--relative-times",
                       action="store_true",
                       dest="relativeTime",
                       default=False,
                       help="Show the timestamps relative to the current time")

        behave=OptionGroup(self.parser,
                         "Behaviour",
                         "Additional output etc")
        self.parser.add_option_group(behave)

        behave.add_option("--progress",
                          action="store_true",
                          dest="progress",
                          default=False,
                          help="Print the directories while they are being processed")

        behave.add_option("--no-progress-bar",
                          action="store_false",
                          dest="progressBar",
                          default=True,
                          help="Do not show a progress bar as directories are being processed")
        behave.add_option("--dead-threshold",
                          action="store",
                          type="float",
                          dest="deadThreshold",
                          default=configuration().get("CommandOptionDefaults","deadThresholdListCases"),
                          help="Number of seconds without updates after which the case is assumed to be dead. Default: %default")

        select=OptionGroup(self.parser,
                           "Selection",
                           "Select which cases should be shown. First the select-patterns are applied then the ignore patterns. If no select-patterns are specified then all cases are processed")
        self.parser.add_option_group(select)

        select.add_option("--recursive",
                          action="store_true",
                          dest="recursive",
                          default=False,
                          help="Recursively search for case directories")
        select.add_option("--substring-select",
                          action="append",
                          dest="substringSelect",
                          default=[],
                          help="Substrings that should be in the case-name. Can be specified more than once")
        select.add_option("--ignore-substring",
                          action="append",
                          dest="substringIgnore",
                          default=[],
                          help="Substrings that should not be in the case-name. Can be specified more than once")
        select.add_option("--glob-select",
                          action="append",
                          dest="globSelect",
                          default=[],
                          help="Glob-pattern that the case-name should match. Can be specified more than once")
        select.add_option("--no-glob-select",
                          action="append",
                          dest="globIgnore",
                          default=[],
                          help="Glob-pattern that the case-name should not match. Can be specified more than once")
    def __init__(self,
                 args=None,
                 exactNr=True,
                 interspersed=True,
                 usage="%prog <caseDirectory>",
                 examples=None,
                 nr=1,
                 description=None,
                 **kwargs):
        self.defaultMeshCreate=configuration().get("PrepareCase","MeshCreateScript")
        self.defaultCaseSetup=configuration().get("PrepareCase","CaseSetupScript")
        self.defaultParameterFile=configuration().get("PrepareCase","DefaultParameterFile")

        description2="""\
Prepares a case for running. This is intended to be the replacement for
boiler-plate scripts. The steps done are

  1. Clear old data from the case (including processor directories)

  2. if a folder 0.org is present remove the 0 folder too

  3. go through all folders and for every found file with the extension .template
do template expansion using the pyratemp-engine (automatically create variables
casePath and caseName)

  4. create a mesh (either by using a script or if a blockMeshDict is present by
running blockMesh. If none of these is present assume that there is a valid mesh
present)

  5. copy every foo.org that is found to to foo (recursively if directory)

  6. do template-expansion for every file with the extension .postTemplate

  7. execute another preparation script (caseSetup.sh)

  8. do final template-expansion for every file with the extension .finalTemplate

The used parameters are written to a file 'PyFoamPrepareCaseParameters' and are used by other utilities
"""
        examples2="""\
%prog . --paramter-file=parameters.base

  Prepare the current case with the parameters list in parameters.base

%prog . --paramter-file=parameters.base --values-string="{'visco':1e-3}"

  Changes the value of the parameter visco

%prog . --no-mesh-create

  Skip the mesh creation phase
"""
        PyFoamApplication.__init__(self,
                                   args=args,
                                   description=description if description else description2,
                                   usage=usage,
                                   examples=examples if examples else examples2,
                                   interspersed=interspersed,
                                   nr=nr,
                                   exactNr=exactNr,
                                   findLocalConfigurationFile=self.localConfigInArgs,
                                   **kwargs)