Model API

credo.modelrun

System Message: WARNING/2 (/home/docs/sites/readthedocs.org/checkouts/readthedocs.org/user_builds/credo/checkouts/latest/doc/api/model.rst, line 9)

Could not execute ‘dot’. Are you sure you have ‘graphviz’ installed?

A core module for CREDO, since it defines and manages running models of a StGermain-based code such as Underworld.

Primary interface is via the ModelRun, which enables you to specify, configure and run a Model, and save records of this as an XML. This process will produce a credo.modelresult.ModelResult class.

class credo.modelrun.JobParams(**kwargs)

Bases: dict

Small class, to record parameters that specify job control of a ModelRun, such as numbers of processors used.

All attributes are stored as regular dictionary parameters, to facilitate easy updating.

writeInfoXML(parentNode)

Writes information about this class into an existing, open XML doc node, in a child list

class credo.modelrun.ModelRun(name, modelInputFiles, outputPath=None, basePath=None, logPath=None, cpReadPath=None, nproc=1, simParams=None, paramOverrides=None, solverOpts=None, xmlExtras=None, inputFilePath=None)

A class to keep records about a StgDomain/Underworld Model Run, including access to the underlying XML of the actual model.

This is one of the key core classes in CREDO, useful on it’s own for managing analysis, but also underlying the credo.systest module.

The basic usage pattern of this class is that a ModelRun needs to be constructed and configured to specify the basic XML files defining a StGermain Model, but also any customisations and credo.analysis.api.AnalysisOperation classes attached to be performed.

After the model is run (see credo.jobrunner), a ModelResult will be produced as a record of the run and for further analysis.

Examples of using the ModelRun are documented in CREDO, see Doing Model analysis with CREDO.

Key attributes:

name

name of the modelRun.

basePath

The path from which all paths to model input files (on the local machine) are specified relative to, and which the job will run in (if running on local machine).

modelInputFiles

‘Input files’ that comprise the XML model that will be run.

outputPath

Output path that all model results will be saved to (is passed through to StGermain).

cpReadPath

Path that checkpoints are read from (is passed through to StGermain).

logPath

Path that log files of the run will be saved to.

jobParams

A JobParams class, to record options needed to define how the model should be actually run (eg number of procs to use).

simParams

A SimParams object, recording key parameters to control the model. This defaults to None, unless the user specifically instantiates one either in the constructor or subsequently sets this directly later. This idiom assumes that if simParams is None, all the necessary parameters are defined in the StGermain XML directly.

Note

You shouldn’t provide a particular parameter in both a SimParams object, and as an override of the ModelRun’s paramOverrides list. At both construction-time and just before the model is run, a check is performed that this has not occurred.

solverOpts

The name of the file storing options passed through to the PETSc numerical solver framework. Depending on the Model being solver, these can have an important role determining the performance and numerical approach taken. See the ‘System Routines’ section of PETSc 2.0.16 Changes log. This option file is separate to the paramOverrides attribute, although the options passed through to PETSc may be used to further customise matrices specified as part of StGermain components.

A file recording the options used for a modelRun will be saved in the output path, with name specified in credo.modelrun.SOLVER_OPTS_RECORD_FILENAME.

Note

the interface here has been kept as specifying a filename with the options rather than using a Python list, as the solver options can run into the hundreds, and generally several of these files are available and maintained by the developers for different solvers.

paramOverrides

A list of StGermain XML parameter overrides that should be applied, in the form (XML_param_path, override_value). These will then be passed through at the command line.

E.g. if paramOverrides is set to [(“gravity”,0.8)], it means that the “gravity” model parameter will be set to the value 0.8.

Note

Currently this is a conceptual “catch-all” for overriding model parameters, for things not specific to the simParams list. In future this approach may be refactored.

analysisOps

A list of credo.analysis.api.AnalysisOperation that are associated with this ModelRun, and will be applied when the model is actually run (which involves writing and submitting additional StGermain XML).

analysisXML

Initally None, this will be populated with the filename of the additional XML document written containing parameter overrides, and requested analysis operations.

cpFields

A list of fields that the user wishes to checkpoint in the run. Defaults to [], in which case the list (if any) in the model run’s XML will be left as-is.

analysisXMLGen(filename=None)

Generates an XML file, in StGermainData XML format, to over-ride necessary parameters of the model as specified on this ModelRun instance. Returns the name of the just-written XML file.

Overrides can have the following main sources:

  • Over-ridden simulation parameters that have been specified as members of the ModelRun itself, such as cpReadPath, and cpFields;
  • Over-ridden simulation parameters on this ModelRun’s SimParams attribute (if it exists);
  • Requested analysis operations that’ve been added to the ModelRun, as specified in the self.analysisOps member list.

Note

Remember that as well as those overrides written to this XML, the user can over-ride particular parameters in the ModelRun via the command line by setting the self.paramOverrides member dictionary.

checkSolverOptsFile()
checkValidRunConfig()

Check the given modelRun is valid and ready to be run.

defaultModelRunFilename()

Calculates and returns a default filename for the ModelRun’s XML record filename.

genFlattenedXML(cmdLineOverrides=None, flatFilename=None)
getModelRunAppExeCommand()

Return the full path of the executable of the modelling program. (e.g. “/usr/local/bin/StGermain”)

getModelRunCommand(extraCmdLineOpts=None, absXMLPaths=False)

Given a model run, construct the command needed to run that model, and return as a string.

Parameters:
  • extraCmdLineOpts – any extra command line options, to be passed straight through to the model.
  • absXMLPaths – if True, converts any Model XML input files to absolute paths first in cmd line.
getSimParams()

Utility function to get SimParams - since in the current design the self.simParams parameter may be ‘None’, and we need to read from the model XML.

getStdErrFilename()

Get the name of the file this Model’s stderr needs to/has been saved to.

getStdOutFilename()

Get the name of the file this Model’s stdout needs to/has been saved to.

postRunCleanup()

function designed to be run after a modelRun has completed, and will do any post-run cleanup to get ready for analysis - e.g. moving files into the output directory that were created to configure the run and need to be kept.

preRunPreparation()

Do any preparation necessary before the run itself proceeds.

prepareOutputLogDirs()

Prepare the output and log dirs - usually in preparation for running a credo.modelrun.ModelRun.

setPath(inPath)

Do any needed manipulations when setting paths.

writeInfoXML(writePath='', filename='', update=False, prettyPrint=True)

Writes an XML recording the key details of this ModelRun, in CREDO format - useful for benchmarking etc.

writePath and filename can be specified, if not they will use default values (the outputPath of the model, and the value returned by defaultModelRunFilename(), respectively).

class credo.modelrun.SimParams(**kwargs)

A class to keep records about the simulation parameters used for a StgDomain/Underworld Model Run, such as number of timesteps to run for. Has functionality to save this list to an XML record, and also read them from a StGermain XML.

After construction, it will make all these parameters directly available as attributes of the SimParams object.

stgParamInfos

A dictionary of StgParamInfo, specifying which parameters are actually controlled by this class. The keys are the short-hand names which can be used to refer to them, as well as Stgermain names.

checkNoDuplicates(paramOverridesList)

Function to check there are no duplicates between sim param overrides set, and cmd-line parameter overrides.

checkValidParams()

Checks that the parameter set is valid to run a StGermain model (e.g. that either the stop time, or total number of steps, is set).

getParam(paramName)

Get the value of a parameter with given paramName.

nearestDumpStep(finalStep, inputStep)

Utility method to get the nearest step at which a dump result was created.

readFromStgXML(inputFilesList, basePath, cmdLineOverrides)

Reads all the parameters of this class from a given StGermain set of input files

setParam(paramName, val)
writeInfoXML(parentNode)

Writes information about this class into an existing, open XML doc node, in a child list

writeStgDataXML(xmlNode)

Writes the parameters of this class as parameters in a StGermain XML file

class credo.modelrun.StgParamInfo(stgName, pType, defVal)

A simple Class that keeps track of the type of a StgParam, and it’s full name.

stgName

The name of this parameter used in the StGermain dictionary and Model XML files.

pType

Type of this parameter (will be used in casting etc).

defVal

Default value of the parameter.

checkType(value)

Checks that the value is of the correct type of this parameter.

credo.modelrun.checkParamOverridesTypes(paramOverrides)

Checks that, for a given list of paramOverrides, each is of a valid type that can actually be successfully used in a StGermain dictionary.

credo.modelrun.generateResOpts(resTuple)

Given a tuple of desired resolutions for a model, convert this to options to pass to StG on the command line

credo.modelrun.getParamOverridesAsStr(paramOverrides)

Given a list of parameter overrides, return these as a string ready for passing to StGermain on the command line.

credo.modelrun.strRes(resTuple)

Turn a given tuple of resolutions into a string, suitable for using as an output dir

credo.modelrun.writeParamOverridesInfoXML(paramOverrides, parentNode)

Writes a record, under the given parentNode, of all the parameter overrides specified in the list paramOverrides.

credo.modelrun.writeSolverOptsInfoXML(solverOpts, parentNode)

Writes a record, under the given parentNode, of the solver options file used.

credo.modelresult

System Message: WARNING/2 (/home/docs/sites/readthedocs.org/checkouts/readthedocs.org/user_builds/credo/checkouts/latest/doc/api/model.rst, line 19)

Could not execute ‘dot’. Are you sure you have ‘graphviz’ installed?

This module allows recording and post-processing of the results of running a StGermain-based application.

The primary interface is via the ModelRun class.

See also

credo.modelrun.

class credo.modelresult.ModelResult(modelName, outputPath)

A class to keep records about the results of a StgDomain/Underworld model run. These are normally produced as a result of running a ModelRun.

Note

In future, we intend to add the ability to create a ModelResult class by reading in an XML file specifying output directory, etc.

modelName

Name of the Model that was run.

outputPath

Path to the output results the ModelRun produced.

jobMetaInfo

A jobrunner.api.JobMetaInfo, recording information about the run such as time taken, Memory usage etc (generally attached by a credo.jobrunner.api.JobRunner soon after the ModelResult created).

fieldResults

A list of FieldComparisonResult objects.

Note

is a legacy of early design of CREDO to allow construction of XML files from pre-existing sys test scripts, may be removed soon.

freqOutput

Initially None, if readFrequentOutput() is called, this will be populated with a reference to a credo.io.stgfreq.FreqOutput class, to allow post-processing of info in the Frequent Output file saved as part of the model run.

defaultRecordFilename()

Get the default filename to use, based on the model name of a particular model.

readFrequentOutput()

Opens and reads in info from the Frequent Output file produced as part of the run, and saves to the attribute freqOutput.

readFromRecordXML(xmlFilename)
recordFieldResult(fieldName, tol, errors)

Records the info required for a FieldResult in the array of stored FieldResults kept by the ModelResult. Returns a reference to the just-added FieldResult.

writeRecordXML(outputDir='', filename='', prettyPrint=True)

Write an XML record of a ModelResult.

credo.modelresult.getSimInfoFromFreqOutput(outputPath)

utility function to get basic information about the simulation from the FrequentOutput.dat, given a particular output Path.

See also

credo.io.stgfreq.

credo.modelresult.readModelResultFromPath(path)
credo.modelresult.updateModelResultsXMLFieldInfo(filename, newFieldResult, prettyPrint=True)

Update an existing XML record of a ModelResult with info about a particular fieldResult.

credo.modelsuite

System Message: WARNING/2 (/home/docs/sites/readthedocs.org/checkouts/readthedocs.org/user_builds/credo/checkouts/latest/doc/api/model.rst, line 29)

Could not execute ‘dot’. Are you sure you have ‘graphviz’ installed?

This module allows the running of a whole suite of related ModelRun s, and managing and analysing their results as a consistent set.

The key class in this module is the ModelSuite.

For example usage, see the CREDO documentation Examples section, Examples of how to use CREDO, e.g. Using CREDO to run and analyse a Suite of Rayleigh-Taylor problems.

This class also performs a key role in the System tests provided in the credo.systest module.

class credo.modelsuite.JobParamVariant(jobParam, paramRange)

Bases: credo.modelsuite.ModelVariant

A ModelVariant designed to modify job parameters.

applyToModel(modelRun, ii)

Apply the ii-th value in the attr:.paramRange to a particular ModelRun.

cmdLineStr(ii)

Return the command-line string to apply this value.

exception credo.modelsuite.ModelResultNotExistError

Bases: exceptions.Exception

Exception for specifying that a Model Result that CREDO was asked to read in, doesn’t exist.

class credo.modelsuite.ModelSuite(outputPathBase, templateMRun=None)

A class for running a suite of Models (e.g. a group for profiling, or a System Test that requires multiple runs).

The two main ways of using this class are:

outputPathBase

The base path to use for saving model results under.

runs

A list of ModelRun s to be run as part of the suite. See generateRuns() and addRun().

runDescrips

Short (eg 1 line) textual description for each ModelRun stored in the runs.

runCustomOptSets

Custom sets of options (to be used at the command line) associated with each run in runs (strings).

resultsList

Initially None, after the suite has been run (using runAll()), saves a reference to all ModelResult s generated.

subOutputPathGenFunc

This function will can be used to customise the model sub-path based on each modelRun. Override it if you wish to use other than the default.

templateMRun

(Optional) setting this to an ModelRun means this run can be used as a “template” to add variants to, and create a parameter sweep over this run.

iterGen

(Related to auto-generation): A generator function to create an iterator to use when auto-generating a suite based on modelVariants. See Python module itertools module for more.

modelVariants

Set of StgXMLVariant s to apply to the template run in order to auto-generate a suite to vary certain parameters. See templateMRun for more.

addRun(modelRun, runDescrip=None, runCustomOpts=None, forceOutputPathBaseSubdir=True)

Add a model run to the list of those to be run.

Parameters:
  • modelRun – A ModelRun to be added.
  • runDescrip – An (optional) string describing the run.
  • runCustomOpts – (optional) string of any custom options that should be passed through to StGermain, only for this run.
  • forceOutputPathBaseSubdir – if True (default), will update the model run’s output dir to enforce it’s a subdir of outputPathBase
Returns:

the index of the newly added run in the modelRun list.

addVariant(name, modelVariant)

Add a StgXMLVariant to the list to be applied to a template run. See modelVariants.

cleanAllLogFiles()

Remove all stdout and stderr files from each ModelRun’s designated output and log paths.

cleanAllOutputPaths()

Remove all files in each model’s output path. Useful to get rid of results still there from previous jobs. Doesn’t delete sub-directories, in case they are other model runs’ results that should be ignored.

generateRuns(iterGen=<type 'itertools.product'>)

When using a template modelRun, will generate runs for the suite based on it. The generated runs are saved to the runs attribute ready to be run using runAll().

This requires that there are one or more StgXMLVariant recorded on the class already.

Parameters:iterGen – this determines what iterator strategy should be used to generate the runs. Defaults to a product, but a simple “zip” style can be achieved using the itertools.izip iterator generating function. See the Python itertools module for more.
getCustomOpts(runI, extraCmdLineOpts)

Get the custom opts (as a string) to apply for modelRun runI.

getRunByName(runName)

Get a modelRun instance from the suite with a particular name.

getRunIndex(runName)

Get the index within the suite of a run with the given name.

preRunCleanup()

Convenience function to call all sub-methods for tasks to do before running to clean up directories.

readResultsFromPath(basePath, overrideOutputPath=None, checkAllPresent=True)

Read the results generated for a given ModelSuite located off the given basePath where the suite was run, and return the list of results.

This will ignore results in the directory not related to this suite.

Parameters:
  • overrideOutputPath – if specified, this path overrides the default outputPath of the suite itself to search for the results. (I.e. useful if you are reading from a previous suite with different output path.)
  • checkAllPresent – if True this will check that all runs expected for the suite were found in the list of results.
writeAllModelResultXMLs()

Save an XML record of each ModelResult currently in resultsList.

writeAllModelRunXMLs()

Save an XML record of each ModelRun currently in runs.

class credo.modelsuite.ModelVariant(paramRange)

A class that can be added to a ModelSuite to help auto-generate a suite of ModelRuns to perform, where a particular parameter is being varied over a certain range.

This is an abstract base class, you should select an actual ModelVariant.

paramRange

A list, containing the values that the parameter should be varied over. E.g. [0,1,2], or [5.6, 7.8, 9.9]. Needs to be of the correct type for the particular parameter. The Python range() function can be useful in generating such a list.

applyToModel(modelRun, ii)

Function to apply the ii-th value of paramRange to a model.

valLen()

Returns the length of the list of parameter values to vary specified in paramRange.

valStr(ii)

Return a string version of the ii-th parameter value.

class credo.modelsuite.StgXMLVariant(paramPath, paramRange)

Bases: credo.modelsuite.ModelVariant

A ModelVariant designed to modify StGermain XML model input parameters.

paramPath

The value to use when over-riding the parameter in a StGermain dictionary, using the StGermain command line format.

E.g. Setting “gravity” would override the gravity parameter in the dictionary, whereas setting to “components.initialConditionsShape.startX” would override the startX parameter, within the initialConditionsShape component.

applyToModel(modelRun, ii)

Apply the ii-th value in the attr:.paramRange to a particular ModelRun.

cmdLineStr(ii)

Return the command-line string to apply this value.

credo.modelsuite.getModelResultsArray(baseName, baseDir)

Post-processing: given a base model name and base output directory, search this directory for model results, and read into a list of ModelResult .

Note

Needs more checking added, and ability to recover metadata about the ModelRuns.

credo.modelsuite.getOtherParamValsByVarRunIs(varRunIsMap, varDicts, otherParam)

Given a varRunIsMaps generated by varRunIsMap(), a varDict and the name of another variant param in the dict, returns a mapping from the variant values to the values of the other param at the same indices.

credo.modelsuite.getParamValues(modelVariants, iterGen)

Shortcut to create a list of param values using getParamValuesIter()

credo.modelsuite.getParamValuesIter(modelVariants, iterGen)

Given a list of model variants and an iterator generator function (eg itertools.izip or itertools.product) to use, generates a specific iterator that can be used on the modelVariants to obtain the actual param values.

credo.modelsuite.getResultsByVarRunIs(varRunIsMap, results)

Given a varRunIsMap generated by getVarRunIs() and an array of results, gives a mapping directly from variant values to corresponding result.

credo.modelsuite.getSubdirTextParamVals(modelVariants, paramIndices)

Creates a subdirectory text based on the names and values of each variant.

credo.modelsuite.getSubdir_RunIndex(modelRun, modelVariants, paramIndices, runIndex)

Simply prints the index of the run as a subdirectory.

credo.modelsuite.getSubdir_RunIndexAndText(modelRun, modelVariants, paramIndices, runIndex)

Subdir is based on both the run index, and the textual variant names.

credo.modelsuite.getSubdir_TextParamVals(modelRun, modelVariants, paramIndices, runIndex)

Generate an output sub-directory name for a run with a printed version of ModelSuite.modelVariants names, and vales for this run. (Good in the sense of being fairly self-describing, but can be long if you have many model variants).

credo.modelsuite.getTextParamValsSubdirs(modelVariants, indicesIt)

Given a list of ModelVariants and an index iterator, returns a list of all subDirs to use.

credo.modelsuite.getVarRunIs(varName, modelVariants, runDicts)

Given a variant name, modelVariants dict and iterGen function, returns a mapping of values of the named modelVariant to run indices

credo.modelsuite.getVariantCmdLineOverrides(modelVariants, indicesIt)

Generates a list of strings to use at cmd line for each model run, given a list of StgXMLVariant and an iterator into them (e.g. generated by getVariantIndicesIter().

credo.modelsuite.getVariantIndicesIter(modelVariants, iterGen)

Given a list of model variants and iterator generator function to use, generates an iterator of indices into the modelVariants list.

credo.modelsuite.getVariantNameDicts(modelVariants, indicesIt)

Generates a list of dictionaries of parameters to be modified for each model run, given a list of StgXMLVariant and an iterator into them (e.g. generated by getVariantIndicesIter().

credo.modelsuite.getVariantParamPathDicts(modelVariants, indicesIt)

Generates a list of dictionaries of parameters to be modified for each model run, given a list of StgXMLVariant and an iterator into them (e.g. generated by getVariantIndicesIter().

credo.modelsuite.writeInputsOutputsToCSV(mSuite, observablesDict, fname)

Write a CSV file, containing all the ModelVariants defined for a ModelSuite, and also all the observables in the observablesDict.

Parameters:
  • observablesDict – a dictionary of ‘observables’, each entry in the form ‘obsName’:[obsVals for each run], e.g. “vrms”:[0.6, 0.8, 0.9].
  • fname – file name of the CSV file to create, inside the model suite’s base output path.

Note

Could be a function on the ModelSuite?

Project Versions

Table Of Contents

Previous topic

CREDO Python Source API Documentation

Next topic

Utils API

This Page