Example #1
0
    def run(self):
        self.logger.info('Running module StaticAvailability')

        ################################################################################################################
        ## Init
        ################################################################################################################
        jmax = self.input.v('grid', 'maxIndex', 'x')
        kmax = self.input.v('grid', 'maxIndex', 'z')
        fmax = self.input.v('grid', 'maxIndex', 'f')

        c0 = self.input.v('hatc0', 'a', range(0, jmax + 1), range(0, kmax + 1),
                          range(0, fmax + 1))
        c1_a0 = self.input.v('hatc1', 'a', range(0, jmax + 1),
                             range(0, kmax + 1), range(0, fmax + 1))
        c1_a0x = self.input.v('hatc1', 'ax', range(0, jmax + 1),
                              range(0, kmax + 1), range(0, fmax + 1))
        if isinstance(c1_a0x, bool):
            c1_a0x = np.zeros((jmax + 1, kmax + 1, fmax + 1))

        d = {}

        c0_int = ny.integrate(c0, 'z', kmax, 0, self.input.slice('grid'))
        B = self.input.v('B', range(0, jmax + 1), [0], [0])
        u0 = self.input.v('u0', range(0, jmax + 1), range(0, kmax + 1),
                          range(0, fmax + 1))
        zeta0 = self.input.v('zeta0', range(0, jmax + 1), [0],
                             range(0, fmax + 1))
        Kh = self.input.v('Kh', range(0, jmax + 1), [0], [0])

        ################################################################################################################
        ## Second order closure
        ################################################################################################################
        u1 = self.input.v('u1', range(0, jmax + 1), range(0, kmax + 1),
                          range(0, fmax + 1))

        d['T'] = {}
        d['F'] = {}
        T0 = 0
        F0 = 0

        ## Transport T  ############################################################################################
        ## T.1. - u0*c1_a0
        # Total
        c1a_f0 = c1_a0
        T0 += ny.integrate(ny.complexAmplitudeProduct(u0, c1a_f0, 2), 'z',
                           kmax, 0, self.input.slice('grid'))

        # Decomposition
        for submod in self.input.getKeysOf('hatc1', 'a'):
            if submod == 'erosion':
                for subsubmod in self.input.getKeysOf('hatc1', 'a', 'erosion'):
                    c1_a0_comp = self.input.v('hatc1', 'a', submod, subsubmod,
                                              range(0, jmax + 1),
                                              range(0, kmax + 1),
                                              range(0, fmax + 1))
                    c1a_f0_comp_res = c1_a0_comp
                    d['T'] = self.dictExpand(
                        d['T'], subsubmod,
                        ['TM' + str(2 * n) for n in range(0, fmax + 1)
                         ])  # add submod index to dict if not already
                    # transport with residual availability
                    for n in range(0, fmax + 1):
                        tmp = np.zeros(c1a_f0_comp_res.shape, dtype=complex)
                        tmp[:, :, n] = c1a_f0_comp_res[:, :, n]
                        tmp = ny.integrate(
                            ny.complexAmplitudeProduct(u0, tmp, 2), 'z', kmax,
                            0, self.input.slice('grid'))[:, 0, 0]
                        if any(abs(tmp)) > 10**-14:
                            d['T'][subsubmod]['TM' + str(2 * n)] += tmp
            else:
                c1_a0_comp = self.input.v('hatc1', 'a', submod,
                                          range(0,
                                                jmax + 1), range(0, kmax + 1),
                                          range(0, fmax + 1))
                c1a_f0_comp_res = c1_a0_comp
                d['T'] = self.dictExpand(
                    d['T'], submod,
                    ['TM' + str(2 * n) for n in range(0, fmax + 1)
                     ])  # add submod index to dict if not already
                # transport with residual availability
                for n in range(0, fmax + 1):
                    tmp = np.zeros(c1a_f0_comp_res.shape, dtype=complex)
                    tmp[:, :, n] = c1a_f0_comp_res[:, :, n]
                    tmp = ny.integrate(ny.complexAmplitudeProduct(u0, tmp, 2),
                                       'z', kmax, 0,
                                       self.input.slice('grid'))[:, 0, 0]
                    if any(abs(tmp)) > 10**-14:
                        d['T'][submod]['TM' + str(2 * n)] += tmp

        ## T.2. - u1*c0
        # Total
        T0 += ny.integrate(ny.complexAmplitudeProduct(u1, c0, 2), 'z', kmax, 0,
                           self.input.slice('grid'))

        # Decomposition
        for submod in self.input.getKeysOf('u1'):
            u1_comp = self.input.v('u1', submod, range(0, jmax + 1),
                                   range(0, kmax + 1), range(0, fmax + 1))
            d['T'] = self.dictExpand(
                d['T'], submod,
                ['TM' + str(2 * n) for n in range(0, fmax + 1)
                 ])  # add submod index to dict if not already
            # transport with residual availability
            for n in range(0, fmax + 1):
                tmp = np.zeros(u1_comp.shape, dtype=complex)
                tmp[:, :, n] = u1_comp[:, :, n]
                if submod == 'stokes':
                    tmp = ny.integrate(ny.complexAmplitudeProduct(tmp, c0, 2),
                                       'z', kmax, 0,
                                       self.input.slice('grid'))[:, 0, 0]
                    if any(abs(tmp)) > 10**-14:
                        d['T'][submod] = self.dictExpand(
                            d['T'][submod], 'TM' + str(2 * n),
                            ['return', 'drift'])
                        d['T'][submod]['TM0']['return'] += tmp
                else:
                    tmp = ny.integrate(ny.complexAmplitudeProduct(tmp, c0, 2),
                                       'z', kmax, 0,
                                       self.input.slice('grid'))[:, 0, 0]
                    if any(abs(tmp)) > 10**-14:
                        d['T'][submod]['TM' + str(2 * n)] += tmp

        ## T.5. - u0*c0*zeta0
        # Total
        T0 += ny.complexAmplitudeProduct(
            ny.complexAmplitudeProduct(u0[:, [0], :], c0[:, [0], :], 2), zeta0,
            2)

        # Decomposition
        uzeta = ny.complexAmplitudeProduct(u0[:, [0], :], zeta0, 2)
        d['T'] = self.dictExpand(
            d['T'], 'stokes', ['TM' + str(2 * n) for n in range(0, fmax + 1)])
        # transport with residual availability
        for n in range(0, fmax + 1):
            tmp = np.zeros(c0[:, [0], :].shape, dtype=complex)
            tmp[:, :, n] = c0[:, [0], n]
            tmp = ny.complexAmplitudeProduct(uzeta, tmp, 2)[:, 0, 0]
            if any(abs(tmp)) > 10**-14:
                d['T']['stokes']['TM' + str(2 * n)]['drift'] += tmp

        ## T.6. - u1riv*c2rivriv
        c2 = self.input.v('hatc2', 'a', 'erosion', 'river_river',
                          range(0, jmax + 1), range(0, kmax + 1),
                          range(0, fmax + 1))
        u1riv = self.input.v('u1', 'river', range(0, jmax + 1),
                             range(0, kmax + 1), range(0, fmax + 1))
        if u1riv is not None:
            d['T'] = self.dictExpand(
                d['T'], 'river_river',
                'TM0')  # add submod index to dict if not already
            tmp = ny.integrate(ny.complexAmplitudeProduct(u1riv, c2, 2), 'z',
                               kmax, 0, self.input.slice('grid'))
            if any(abs(tmp[:, 0, 0])) > 10**-14:
                d['T']['river_river']['TM0'] = tmp[:, 0, 0]

            T0 += tmp

        ## T.7. - diffusive part
        # Total
        c0x = self.input.d('hatc0',
                           'a',
                           range(0, jmax + 1),
                           range(0, kmax + 1),
                           range(0, fmax + 1),
                           dim='x')
        T0 += -Kh * ny.integrate(c0x, 'z', kmax, 0, self.input.slice('grid'))

        c2x = self.input.d('hatc2',
                           'a',
                           'erosion',
                           'river_river',
                           range(0, jmax + 1),
                           range(0, kmax + 1),
                           range(0, fmax + 1),
                           dim='x')
        T0 += -Kh * ny.integrate(c2x, 'z', kmax, 0, self.input.slice('grid'))

        # Decomposition
        d['T'] = self.dictExpand(d['T'], 'diffusion_tide', ['TM0'])
        d['T'] = self.dictExpand(d['T'], 'diffusion_river', ['TM0'])
        # transport with residual availability
        tmp = -(Kh * ny.integrate(c0x, 'z', kmax, 0,
                                  self.input.slice('grid')))[:, 0, 0]
        if any(abs(tmp)) > 10**-14:
            d['T']['diffusion_tide']['TM0'] = tmp
        tmp = -(Kh * ny.integrate(c2x, 'z', kmax, 0,
                                  self.input.slice('grid')))[:, 0, 0]
        if any(abs(tmp)) > 10**-14:
            d['T']['diffusion_river']['TM0'] = tmp

        ## Diffusion F  ############################################################################################
        ## F.1. - u0*C1ax*f0
        # Total
        F0 += ny.integrate(ny.complexAmplitudeProduct(u0, c1_a0x, 2), 'z',
                           kmax, 0, self.input.slice('grid'))

        # Decomposition
        for submod in self.input.getKeysOf('hatc1', 'ax'):
            c1_ax0_comp = self.input.v('hatc1', 'ax', submod,
                                       range(0, jmax + 1), range(0, kmax + 1),
                                       range(0, fmax + 1))
            d['F'] = self.dictExpand(
                d['F'], submod,
                ['FM' + str(2 * n) for n in range(0, fmax + 1)
                 ])  # add submod index to dict if not already
            # transport with residual availability
            for n in range(0, fmax + 1):
                tmp = np.zeros(u0.shape, dtype=complex)
                tmp[:, :, n] = u0[:, :, n]
                tmp = ny.integrate(
                    ny.complexAmplitudeProduct(tmp, c1_ax0_comp, 2), 'z', kmax,
                    0, self.input.slice('grid'))[:, 0, 0]
                if any(abs(tmp)) > 10**-14:
                    d['F'][submod]['FM' + str(2 * n)] += tmp

        ## F.3. - diffusive part
        # Total
        F0 += -Kh * ny.integrate(c0, 'z', kmax, 0, self.input.slice('grid'))
        F0 += -Kh * ny.integrate(c2, 'z', kmax, 0, self.input.slice('grid'))

        # Decomposition
        d['F'] = self.dictExpand(d['F'], 'diffusion_tide', ['FM0'])
        d['F'] = self.dictExpand(d['F'], 'diffusion_river', ['FM0'])
        # transport with residual availability
        tmp = -(Kh * ny.integrate(c0, 'z', kmax, 0,
                                  self.input.slice('grid')))[:, 0, 0]
        if any(abs(tmp)) > 10**-14:
            d['F']['diffusion_tide']['FM0'] = tmp
        tmp = -(Kh * ny.integrate(c2, 'z', kmax, 0,
                                  self.input.slice('grid')))[:, 0, 0]
        if any(abs(tmp)) > 10**-14:
            d['F']['diffusion_river']['FM0'] = tmp

        ## Solve    ################################################################################################
        ## Add all mechanisms & compute a0c
        from src.DataContainer import DataContainer
        dc = DataContainer(d)
        dc.merge(self.input.slice('grid'))
        T_til = np.real(dc.v('T', range(0, jmax + 1)))
        F_til = np.real(dc.v('F', range(0, jmax + 1)))

        # DEBUG: CHECKS IF COMPOSITE T, F == total T, F
        # print np.max(abs((dc.v('T', range(0, jmax+1))-T0[:, 0, 0])/(T0[:, 0, 0]+10**-10)))
        # print np.max(abs((dc.v('F', range(0, jmax+1))-F0[:, 0, 0])/(F0[:, 0, 0]+10**-10)))

        integral = -ny.integrate(T_til / (F_til - 10**-6), 'x', 0,
                                 range(0, jmax + 1), self.input.slice('grid'))
        if self.input.v('Qsed') is None:
            G = 0
        else:
            G = self.input.v('Qsed') / B[-1, 0, 0]

        P = ny.integrate(G / (F_til - 10**-6) * np.exp(-integral), 'x', 0,
                         range(0, jmax + 1), self.input.slice('grid'))
        ################################################################################################################
        # Boundary condition 1
        ################################################################################################################
        if self.input.v('sedbc') == 'astar':
            astar = self.input.v('astar')
            k = astar * ny.integrate(B[:, 0, 0], 'x', 0, jmax,
                                     self.input.slice('grid')) / ny.integrate(
                                         B[:, 0, 0] * np.exp(integral), 'x', 0,
                                         jmax, self.input.slice('grid'))

            f0 = (k - P) * np.exp(integral)
            f0x = (-T_til * f0 - G) / (F_til - 10**-6)

        ################################################################################################################
        # Boundary condition 2
        ################################################################################################################
        elif self.input.v('sedbc') == 'csea':
            csea = self.input.v('csea')
            c000 = np.real(c0_int[0, 0, 0])
            k = csea / c000 * (self.input.v('grid', 'low', 'z', 0) -
                               self.input.v('grid', 'high', 'z', 0))
            f0 = (k - P) * np.exp(integral)
            f0x = (-T_til * f0 - G) / (F_til - 10**-6)

        else:
            from src.util.diagnostics.KnownError import KnownError
            raise KnownError(
                'sediment boundary sedbc not known: use astar or csea')

        ################################################################################################################
        # Store in dict
        ################################################################################################################
        d['a'] = f0
        d['c0'] = c0 * f0.reshape((jmax + 1, 1, 1))
        d['c1'] = c1_a0 * f0.reshape((jmax + 1, 1, 1)) + c1_a0x * f0x.reshape(
            (jmax + 1, 1, 1))
        d['c2'] = c2 * f0.reshape((jmax + 1, 1, 1))

        return d
Example #2
0
class NumericalFunctionBase(FunctionBase):
    #Variables

    #Methods
    def __init__(self, dimNames):
        FunctionBase.__init__(self, dimNames)
        self.dataContainer = DataContainer()
        self.valueSize = 0
        return

    def function(self, **kwargs):
        if len([
                i for i in kwargs.keys()
                if i in self.dataContainer.v('grid', 'dimensions')
        ]) == 0:
            return self.__setReturnReference(kwargs.get('operation'))

        # evaluate function
        try:
            returnval = self.__evaluateFunction(**kwargs)
        except FunctionEvaluationError:
            returnval = self.__setReturnReference(kwargs.get('operation'))
        return returnval

    def __evaluateFunction(self, **kwargs):
        """Overrides the function method of FunctionBase, but is very similar.
        The difference is only that FunctionBase transfers kwargs to args before calling the actual functions
        Here we keep the kwargs as the actual functions also use this.
        """
        requestSize = sum(
            [dim in kwargs for dim in self.dimNames]
        )  # count the number of dimensions in kwargs (this makes sure that other parameters or irrelevant dimensions are ignored)

        operation = kwargs.get('operation')
        try:
            kwargs.pop('operation')
        except:
            pass
        # direct to actual function
        if operation is None:
            returnval = self.value(**kwargs)
        elif operation == 'd':
            returnval = self.derivative(**kwargs)
        elif operation == 'n':
            returnval = -self.value(**kwargs)
        elif operation == 'dn':
            returnval = -self.derivative(**kwargs)
        else:
            raise FunctionEvaluationError
        return returnval

    def __setReturnReference(self, operation=None):
        '''No difference with FunctionBase, but required here to refer to its own functions
        '''
        if not operation:
            returnval = self.function
        elif operation == 'n':
            returnval = self.negfunction
        elif operation == 'd':
            returnval = self.derfunction
        elif operation == 'dn':
            returnval = self.dnfunction
        else:
            raise KnownError(
                'Function called with unknown operation. (This error indicates an incorrectly defined function)'
            )
        return returnval

    def negfunction(self, **kwargs):
        """No difference with FunctionBase, but required here to refer to its own functions
        """
        # reset operations
        if kwargs.get(
                'operation'
        ) == 'n':  # if the negative of a negfunction is called, return to function.
            kwargs.pop('operation')
        elif kwargs.get('operation') == 'd':
            kwargs['operation'] = 'dn'
        else:
            kwargs['operation'] = 'n'

        # evaluate
        returnval = self.function(**kwargs)
        return returnval

    def addGrid(self, gridData, gridName='grid'):
        # set own DataContainer containing grid data
        data = gridData.slice(gridName, excludeKey=True)
        self.dataContainer.addData(
            'grid', data.data
        )  # improper use of the DataContainer by accessing its data directly
        return

    def addValue(self, value):
        """Add a variable 'value' to the numerical function

        Parameters:
            value (ndarray) - value to be put in the internal DataContainer
        """
        self.dataContainer.addData('value', value)
        self.valueSize = len(value.shape)
        return

    def addDerivative(self, derivative, dim):
        self.dataContainer.merge({'derivative': {dim: derivative}})
        return

    ### Depreciated v2.2 [dep01] ###
    def addSecondDerivative(self, derivative, dim):
        """ Depreciated v2.2
        """
        self.dataContainer.merge({'secondDerivative': {dim: derivative}})
        return

    ### End ###

    def value(self, **kwargs):
        """Return the value of the variable in this numerical function.

        Parameters:
            kwargs (dict) - coordinates

        Returns:
            Array value using DataContainer interpolation.
        """
        return self.dataContainer.v('value', **kwargs)

    def derivative(self, **kwargs):
        """Similar to .value(). Returns the derivative uploaded to the numerical function or makes a call
        to a numerical derivation method if no derivative is uploaded.
        """
        # kwargs.pop('operation')       # obsolete
        dim = kwargs.get('dim')
        v = self.dataContainer.v(
            'derivative', dim,
            **kwargs)  # try if analytical derivative is available
        if v is None:
            v = self.dataContainer.d(
                'value', **kwargs)  # else take numerical derivative
        return v

    ### Depreciated v2.2 [dep01] ###
    def secondDerivative(self, **kwargs):
        """See .derivative(). This method does the same for the second derivative
        Depreciated 2.2 [dep01]
        """
        # kwargs.pop('operation')       # obsolete
        dim = kwargs.get('dim')
        v = self.dataContainer.v('secondDerivative', dim, **kwargs)
        if v is None:
            v = self.dataContainer.dd('value', **kwargs)
        return v
Example #3
0
    def run(self):
        """invoke the saveData() method to save by using Pickle.

        Returns:
            Empty dictionary.
        """
        self.logger.info('Saving output')

        ################################################################################################################
        # Make all variables from config, input and modules available (note, config not available in output module, see Program.py)
        ################################################################################################################
        # read input file
        reader = Reader()
        reader.open(self.input.v('inputFile'))
        data = reader.read('module')
        reader.close()

        # merge the datacontainers of all modules & make the module tags into a list of modules
        inputvars = DataContainer()
        module = []
        for d in data:
            module.append(d.v('module'))
            inputvars.merge(d)
        inputvars.addData('module', module)

        # merge input vars with self.input in hierarchy config, input, input for this module, vars calculated in other modules (low - high)
        # + make a list of all keys of input and config vars; these are saved always and later appended by selected module calc. vars.
        data = self.__loadConfig()
        data.merge(inputvars)
        outputKeys = self.__checkInputOverrides(
            data
        )  # checks if input is overwritten and provides the keys of not or correctly overwritten input vars
        data.merge(self.input)
        del inputvars, reader
        # now all variables from config, input and modules are in 'data'

        ################################################################################################################
        # Isolate part of DC to write; put this in saveData
        ################################################################################################################
        saveData = DataContainer()

        # vars to save
        outputVariables = toList(self.input.v('requirements'))
        outputKeys = list(
            set(outputKeys + self.__getSubmoduleRequirements(outputVariables))
        )  # convert the requested output variables to key tuples including submodule requirements
        for key in outputKeys:
            if len(key) > 1:
                saveData.merge({key[0]: data.slice(*key).data})
            else:
                saveData.merge(data.slice(*key))

        # add grid and outputgrid if available; needed for interpolating data to outputgrid later
        saveData.merge(self.input.slice('grid'))
        saveData.merge(self.input.slice(self.outputgridName))

        # add reference level to outputgrid
        if self.input.v('R') is not None:
            self.input.merge({
                self.outputgridName: {
                    'low': {
                        'z':
                        self.input.v('R',
                                     x=self.input.v(self.outputgridName,
                                                    'axis', 'x'))
                    }
                }
            })  # add reference level to outputgrid

        # make a deepcopy of the data to be saved
        # NB. very memory inefficient, but needed not to overwrite existing data
        saveData = deepcopy(saveData)

        ################################################################################################################
        # Convert data using output grid (if this is provided)
        ################################################################################################################
        grid = saveData.slice('grid')
        outputgrid = saveData.slice(self.outputgridName)
        saveAnalytical = toList(self.input.v('saveAnalytical')) or []
        dontConvert = toList(self.input.v('dontConvert')) or []
        if 'all' in saveAnalytical:
            saveAnalytical = outputVariables
        if 'all' in dontConvert:
            dontConvert = outputVariables
        self._convertData(saveData, grid, outputgrid, saveAnalytical,
                          dontConvert)

        # rename the outputgrid to grid and replace the original grid in saveData
        saveData.addData('grid', saveData.data[self.outputgridName])
        saveData.data.pop(self.outputgridName)

        ################################################################################################################
        # Make the output directory if it doesnt exist
        ################################################################################################################
        cwdpath = cfm.CWD  # path to working directory
        self.path = os.path.join(cwdpath, self.input.v('path'))
        if not self.path[-1] == '/':
            self.path += '/'
        if not os.path.exists(self.path):
            os.makedirs(self.path)

        ################################################################################################################
        # set file name and save data
        ################################################################################################################
        filename = self.__makeFileName()

        # write
        filepath = (self.path + filename + self.ext)
        try:
            with open(filepath, 'wb') as fp:
                pickle.dump(saveData.data,
                            fp,
                            protocol=pickle.HIGHEST_PROTOCOL)
        except:
            raise

        ################################################################################################################
        # return
        ################################################################################################################
        d = {}
        d['outputDirectory'] = self.path
        return d