Esempio n. 1
0
    def test_getDofErrors_ByDof(self):
        cvgFileInfo = CvgFileInfo("./sampleData/CosineHillRotate-analysis.cvg")
        cvgFileInfo.dofColMap={0:1,1:2}
        dofErrors = stgcvg.getDofErrors_ByDof(cvgFileInfo, steps='last')
        self.assertEqual(len(dofErrors), 2)
        self.assertEqual(dofErrors[0], 0.00612235812)
        self.assertEqual(dofErrors[1], 0.053)

        dofErrorArray = stgcvg.getDofErrors_ByDof(cvgFileInfo, steps='all')
        self.assertEqual(len(dofErrorArray), 2)
        self.assertEqual(len(dofErrorArray[0]), 3)
        self.assertEqual(dofErrorArray[0][0], 0.00616)
        self.assertEqual(dofErrorArray[0][1], 0.00614)
        self.assertEqual(dofErrorArray[0][2], 0.00612235812)
        self.assertEqual(dofErrorArray[1][0], 0.055)
        self.assertEqual(dofErrorArray[1][1], 0.054)
        self.assertEqual(dofErrorArray[1][2], 0.053)

        dofErrorArray = stgcvg.getDofErrors_ByDof(cvgFileInfo, steps=(0,2))
        self.assertEqual(len(dofErrorArray), 2)
        self.assertEqual(len(dofErrorArray[0]), 2)
        self.assertEqual(dofErrorArray[0][0], 0.00616)
        self.assertEqual(dofErrorArray[0][1], 0.00614)
        self.assertEqual(dofErrorArray[1][0], 0.055)
        self.assertEqual(dofErrorArray[1][1], 0.054)
Esempio n. 2
0
 def getResult(self, modelResult):
     '''Gets the result of the operator on the given fields (as a
     :class:`.FieldComparisonResult`), given a
     modelResult (:class:`~credo.modelresult.ModelResult`) which
     refers to a directory containing field comparisons
     (i.e. cvg files, see :mod:`credo.io.stgcvg`).
     '''
     cvgIndex = stgcvg.genConvergenceFileIndex(modelResult.outputPath)
     try:
         cvgFileInfo = cvgIndex[self.name]
     except KeyError:
         # TODO: create a new exception type here?
         raise KeyError("Field '%s' not found in the known list of"\
             " convergence results (%s) for model run '%s'"\
             % (self.name, list(cvgIndex.keys()),modelResult.modelName))
     dofErrors = stgcvg.getDofErrors_ByDof(cvgFileInfo, steps="last")
     fieldResult = FieldComparisonResult(self.name, dofErrors)
     fieldResult.cvgFileInfo = cvgFileInfo
     return fieldResult
 def getResult(self, modelResult):
     '''Gets the result of the operator on the given fields (as a
     :class:`.FieldComparisonResult`), given a 
     modelResult (:class:`~credo.modelresult.ModelResult`) which
     refers to a directory containing field comparisons
     (i.e. cvg files, see :mod:`credo.io.stgcvg`).
     '''
     cvgIndex = stgcvg.genConvergenceFileIndex(modelResult.outputPath)
     try:
         cvgFileInfo = cvgIndex[self.name]
     except KeyError:     
         # TODO: create a new exception type here?
         raise KeyError("Field '%s' not found in the known list of"\
             " convergence results (%s) for model run '%s'"\
             % (self.name, cvgIndex.keys(),modelResult.modelName))
     dofErrors = stgcvg.getDofErrors_ByDof(cvgFileInfo, steps="last")
     fieldResult = FieldComparisonResult(self.name, dofErrors)
     fieldResult.cvgFileInfo = cvgFileInfo
     return fieldResult
Esempio n. 4
0
def getFieldScaleCvgData_SingleCvgFile(cvgFilePath):
    '''Given a path that CVG files reside in, returns the length scales of
    each run (as a list), and a list of field error data for each field/cvg
    info in the given path.
    Thus is a utility function for generating necessary fieldErrorData for a
    multi-res convergence analysis.

    .. Note::

       This assumes all cvg info is stored in the same
       convergence file (the default approach of the legacy SYS tests)
    '''
    cvgIndex = stgcvg.genConvergenceFileIndex(cvgFilePath)
    fieldErrorData = {}
    for fieldName, cvgFileInfo in list(cvgIndex.items()):
        #NB: assumes all cvg files and all fields have same len scales.
        lenScales = stgcvg.getRes(cvgFileInfo.filename)
        dofErrors = stgcvg.getDofErrors_ByDof(cvgFileInfo)
        fieldErrorData[fieldName] = dofErrors
    return lenScales, fieldErrorData
def getFieldScaleCvgData_SingleCvgFile(cvgFilePath):
    '''Given a path that CVG files reside in, returns the length scales of
    each run (as a list), and a list of field error data for each field/cvg
    info in the given path.
    Thus is a utility function for generating necessary fieldErrorData for a
    multi-res convergence analysis.
    
    .. Note::

       This assumes all cvg info is stored in the same 
       convergence file (the default approach of the legacy SYS tests)
    '''
    cvgIndex = stgcvg.genConvergenceFileIndex(cvgFilePath)
    fieldErrorData = {}
    for fieldName, cvgFileInfo in cvgIndex.iteritems():
        #NB: assumes all cvg files and all fields have same len scales.
        lenScales = stgcvg.getRes(cvgFileInfo.filename)
        dofErrors = stgcvg.getDofErrors_ByDof(cvgFileInfo)
        fieldErrorData[fieldName] = dofErrors
    return lenScales, fieldErrorData
Esempio n. 6
0
    def plotOverTime(self, save=True, show=False, dofIndex=None, path="."):
        """Plot the result of a FieldComparison over all timesteps of a model.
        Requires the cvgFileInfo paramater to have been set to give access to
        the cvg info of this field.

        .. Note::
           Requires you to have the
           `Matplotlib <http://matplotlib.sourceforge.net/>`_ library installed.

        'show', 'save' and 'path' parameters are the same as for
        :meth:`credo.io.stgfreq.FreqOutput.plotOverTime`. The optional 'dofIndex'
        parameter allows you to only plot a particular DOF of the field,
        otherwise all dofs will be plotted on separate graphs."""
        try:
            import matplotlib
            matplotlib.use('Agg')
            import matplotlib.pyplot as plt
        except ImportError:
            print("Error, to use CREDO built-in plot functions, please "\
                " install the matplotlib python library.")
            return

        assert self.cvgFileInfo

        dofErrorsArray = stgcvg.getDofErrors_ByDof(self.cvgFileInfo)

        numDofs = len(self.dofErrors)

        if dofIndex is not None:
            dofRange = [0]
            dofIndices = [dofIndex]
        else:
            dofRange = list(range(numDofs))
            dofIndices = list(range(numDofs))

        plt.subplots_adjust(wspace=0.4)

        for dofI in dofRange:
            plt.subplot(1, len(dofRange), dofI + 1)
            plot = plt.plot(dofErrorsArray[dofIndices[dofI]])
            # TODO: only the within tolerance test should add this.
            #plt.axhline(y=self.tol, label='tolerance', linewidth=3, color='r')

            plt.xlabel("Timestep")
            # TODO: title should change depending on analytic or reference
            plt.ylabel("Dof %d: Error vs analytic soln" % dofIndices[dofI])
            # Only display the title once on left
            if len(dofRange) == 1:
                plt.title("Convergence of field '%s' with analytic solution,"\
                    " for DOF %d" % (self.fieldName, dofIndices[dofI]))
            else:
                if dofI == 0:
                    plt.suptitle("Convergence of field '%s' with analytic"\
                        " solution" % (self.fieldName), fontsize=14)
                plt.title("DOF %d" % dofI)

        if save:
            filename = os.path.join(path, self.fieldName + "-cvg.png")
            plt.savefig(filename, format="png")
            self.plottedCvgFilename = filename
        if show: plt.show()
    def plotOverTime(self, save=True, show=False, dofIndex=None, path="."):
        """Plot the result of a FieldComparison over all timesteps of a model.
        Requires the cvgFileInfo paramater to have been set to give access to
        the cvg info of this field.

        .. Note::
           Requires you to have the
           `Matplotlib <http://matplotlib.sourceforge.net/>`_ library installed.
        
        'show', 'save' and 'path' parameters are the same as for
        :meth:`credo.io.stgfreq.FreqOutput.plotOverTime`. The optional 'dofIndex'
        parameter allows you to only plot a particular DOF of the field,
        otherwise all dofs will be plotted on separate graphs."""
        try:
            import matplotlib
            matplotlib.use('Agg')
            import matplotlib.pyplot as plt
        except ImportError:
            print "Error, to use CREDO built-in plot functions, please "\
                " install the matplotlib python library."
            return    
        
        assert self.cvgFileInfo

        dofErrorsArray = stgcvg.getDofErrors_ByDof(self.cvgFileInfo)

        numDofs = len(self.dofErrors)

        if dofIndex is not None:
            dofRange = [0]
            dofIndices = [dofIndex]
        else:
            dofRange = range(numDofs)    
            dofIndices = range(numDofs)
            
        plt.subplots_adjust(wspace=0.4)

        for dofI in dofRange:
            plt.subplot(1,len(dofRange),dofI+1)
            plot = plt.plot(dofErrorsArray[dofIndices[dofI]])
            # TODO: only the within tolerance test should add this.
            #plt.axhline(y=self.tol, label='tolerance', linewidth=3, color='r')

            plt.xlabel("Timestep")
            # TODO: title should change depending on analytic or reference
            plt.ylabel("Dof %d: Error vs analytic soln" % dofIndices[dofI])
            # Only display the title once on left
            if len(dofRange) == 1:
                plt.title("Convergence of field '%s' with analytic solution,"\
                    " for DOF %d" % (self.fieldName, dofIndices[dofI]))
            else:
                if dofI == 0:
                    plt.suptitle("Convergence of field '%s' with analytic"\
                        " solution" % (self.fieldName), fontsize=14)
                plt.title("DOF %d" % dofI)

        if save:
            filename = os.path.join(path, self.fieldName+"-cvg.png")
            plt.savefig(filename, format="png")
            self.plottedCvgFilename = filename
        if show: plt.show()