Esempio n. 1
0
    def __init__(self,param,grid):

        self.list_param=['forcing','noslip','timestepping','diffusion','Kdiff',
                         'forcing_module','gravity','isisland',
                         'customized','custom_module','additional_tracer']
        param.copy(self,self.list_param)

        # for potential energy
        self.list_param=['xr','yr','nh','Lx','msk','area','mpitools']
        grid.copy(self,self.list_param)

        # for variables
        param.varname_list=['vorticity','psi','u','v','buoyancy']
        param.tracer_list=['vorticity','buoyancy']
        param.whosetspsi=('vorticity')

        if hasattr(self,'additional_tracer'):
            for k in range(len(self.additional_tracer)):
                trac=self.additional_tracer[k]
                param.varname_list.append(trac)
                param.tracer_list.append(trac)
                print('Tracers are :',param.tracer_list)

        param.sizevar     =[grid.nyl,grid.nxl]
        self.var = Var(param)
        self.source = zeros(param.sizevar)

        # for operators
        self.ope = Operators(param,grid)

        # for timescheme
        self.tscheme = Timescheme(param,self.var.state)
        self.tscheme.set(self.dynamics, self.timestepping)

        if self.forcing:

            try:
                f = import_module(self.forcing_module)
            except:
                print('module %s for forcing cannot be found'%self.forcing_module)
                print('make sure file **%s.py** exists'%self.forcing_module)
                exit(0)

            self.forc = f.Forcing(param,grid)

        self.diags={}

        if self.customized:
            try:
                f = import_module(self.custom_module)
                print(f)
                self.extrastep = f.Step(param,grid)
            except:
                print('module %s for forcing cannot be found'%self.custom_module)
                print('make sure file **%s.py** exists'%self.custom_module)
                exit(0)
Esempio n. 2
0
    def __init__(self, param, grid):

        self.list_param = ['forcing', 'noslip', 'timestepping',
                           'forcing_module', 'additional_tracer',
                           'myrank',
                           'gravity', 'diffusion', 'Kdiff', 'f0']
        param.copy(self, self.list_param)

        # for potential energy
        self.list_param = ['xr', 'yr', 'nh',
                           'Lx', 'msk', 'area', 'mpitools', 'dx']
        grid.copy(self, self.list_param)

        # for variables
        param.varname_list = ['vorticity', 'psi',
                              'u', 'v', 'buoyancy', 'V', 'qE']
        param.tracer_list = ['vorticity', 'buoyancy', 'V']
        param.whosetspsi = ('vorticity')

        if hasattr(self, 'additional_tracer'):
            for k in range(len(self.additional_tracer)):
                trac = self.additional_tracer[k]
                param.varname_list.append(trac)
                param.tracer_list.append(trac)

        param.sizevar = [grid.nyl, grid.nxl]
        self.var = Var(param)

        # for operators
        self.ope = Operators(param, grid)

        # for timescheme
        self.tscheme = Timescheme(param, self.var.state)

        if self.forcing:
            if self.forcing_module == 'embedded':
                print('Warning: check that you have indeed added the forcing to the model')
                print('Right below the line    : model = f2d.model')
                print('you should have the line: model.forc = Forcing(param, grid)')

                pass
            else:
                try:
                    f = import_module(self.forcing_module)

                except ImportError:
                    print('module %s for forcing cannot be found'
                          % self.forcing_module)
                    print('make sure file **%s.py** exists' % self.forcing_module)
                    sys.exit(0)

                self.forc = f.Forcing(param, grid)


        self.diags = {}
Esempio n. 3
0
    def __init__(self, param, grid):
        self.list_param = [
            'forcing', 'diffusion', 'Kdiff', 'noslip', 'timestepping', 'beta',
            'Rd', 'forcing_module'
        ]
        param.copy(self, self.list_param)

        # for diagnostics
        self.list_param = ['yr', 'nh', 'msk', 'area', 'mpitools']
        grid.copy(self, self.list_param)

        # for variables
        param.varname_list = ('pv1', 'psi1', 'u1', 'v1', 'pv2', 'psi2', 'u2',
                              'v2')
        param.sizevar = [grid.nyl, grid.nxl]
        self.var = Var(param)
        self.source = zeros(param.sizevar)

        #        self.ipva = self.var.varname_list.index('pvanom')
        #        self.ipv  = self.var.varname_list.index('pv')
        #        self.ivor = self.var.varname_list.index('vorticity')
        #        self.ipsi = self.var.varname_list.index('psi')

        # background pv
        self.pvback = self.beta * (grid.yr - grid.Ly * .5) * grid.msk

        # for operators
        param.tracer_list = ['pv1', 'pv2']
        param.whosetspsi = ('pvanom')
        param.qgoperator = True
        self.ope = Operators(param, grid)

        # for timescheme
        self.tscheme = Timescheme(param, self.var.state)
        self.dx0 = self.tscheme.dx0

        self.kt = 0

        if self.forcing:

            try:
                f = import_module(self.forcing_module)
            except:
                print('module %s for forcing cannot be found' %
                      self.forcing_module)
                print('make sure file **%s.py** exists' % self.forcing_module)
                exit(0)

            self.forc = f.Forcing(param, grid)

        self.diags = {}

        self.tscheme.set(self.dynamics, self.timestepping)
Esempio n. 4
0
    def __init__(self, param, grid):

        self.list_param = ['forcing', 'noslip', 'timestepping']
        param.copy(self, self.list_param)

        # for variables
        param.varname_list = ('vorticity', 'psi', 'u', 'v', 'buoyancy', 'V')
        param.sizevar = [grid.nyl, grid.nxl]
        self.var = Var(param)

        # for operators
        param.tracer_list = ['vorticity', 'buoyancy', 'V']
        param.whosetspsi = ('vorticity')
        self.ope = Operators(param, grid)

        # for timescheme
        self.tscheme = Timescheme(param, self.var.state)
Esempio n. 5
0
    def __init__(self, param, grid):

        self.list_param = ['timestepping', 'diffusion', 'Kdiff']
        param.copy(self, self.list_param)

        self.list_grid = ['msk', 'nh', 'area', 'mpitools']
        grid.copy(self, self.list_grid)

        # for variables
        param.varname_list = ['tracer', 'psi', 'u', 'v', 'vorticity']
        param.sizevar = [grid.nyl, grid.nxl]
        self.var = Var(param)

        # for operators
        param.tracer_list = ['tracer']
        param.whosetspsi = ('tracer')
        self.ope = Operators(param, grid)

        # for timescheme
        self.tscheme = Timescheme(param, self.var.state)
        self.tscheme.set(self.advection, self.timestepping)

        self.diags = {}
Esempio n. 6
0
    def __init__(self, param, grid):

        self.list_param = [
            'forcing', 'noslip', 'timestepping', 'diffusion', 'Kdiff',
            'forcing_module', 'additional_tracer', 'enforce_momentum',
            'var_to_save', 'customized', 'custom_module', 'spongelayer'
        ]
        param.copy(self, self.list_param)

        # for diagnostics
        self.list_param = [
            'yr', 'nh', 'msk', 'area', 'mpitools', 'dx', 'xr', 'yr', 'r2',
            'x0', 'y0', 'x2', 'y2', 'isisland', 'Lx', 'ny'
        ]
        grid.copy(self, self.list_param)

        # for variables
        param.varname_list = ['vorticity', 'psi', 'u', 'v',
                              'source']  # ,'tauw','wshear']
        param.tracer_list = ['vorticity']
        param.whosetspsi = ('vorticity')

        if 'tauw' in self.var_to_save:
            param.varname_list.append('tauw')

        if 'wshear' in self.var_to_save:
            param.varname_list.append('wshear')

        if hasattr(self, 'additional_tracer'):
            for k in range(len(self.additional_tracer)):
                trac = self.additional_tracer[k]
                param.varname_list.append(trac)
                param.tracer_list.append(trac)

        param.sizevar = [grid.nyl, grid.nxl]
        self.var = Var(param)
        self.work = np.zeros(param.sizevar)

        # for timing the code
        self.timers = Timers(param)

        # for operators
        self.ope = Operators(param, grid)

        # for timescheme
        self.tscheme = Timescheme(param, self.var.state)
        self.tscheme.set(self.dynamics, self.timestepping)

        if self.forcing:
            if self.forcing_module == 'embedded':
                print(
                    'Warning: check that you have indeed added the forcing to the model'
                )
                print('Right below the line    : model = f2d.model')
                print(
                    'you should have the line: model.forc = Forcing(param, grid)'
                )

                pass
            else:
                try:
                    f = import_module(self.forcing_module)
                except ImportError:
                    print('module %s for forcing cannot be found' %
                          self.forcing_module)
                    print('make sure file **%s.py** exists' %
                          self.forcing_module)
                    exit(0)
                self.forc = f.Forcing(param, grid)

        if self.spongelayer:
            # sponge layer zone [0 = full sponge, 1 = no sponge]
            self.spongemsk = (1 - (1 + np.tanh(
                (self.xr - self.Lx) / 0.1)) * 0.5)

        self.diags = {}

        if self.customized:
            try:
                f = import_module(self.custom_module)
                self.extrastep = f.Step(param, grid)
            except ImportError:
                print('module %s for forcing cannot be found' %
                      self.custom_module)
                print('make sure file **%s.py** exists' % self.custom_module)
                exit(0)
Esempio n. 7
0
        self.kstage = 0
        self.rhs(x, t, self.dx0)
        self.x = x + (0.25 * dt) * self.dx0

        self.kstage = 1
        self.rhs(self.x, t + dt * 0.25, self.dx0)
        self.x = x + (dt / 3.) * self.dx0

        self.kstage = 2
        self.rhs(self.x, t + dt / 3., self.dx0)
        self.x = x + (dt / 2.) * self.dx0

        self.kstage = 3
        self.rhs(self.x, t + 0.5 * dt, self.dx0)
        x += dt * self.dx0


if __name__ == "__main__":

    param = Param()
    param.timestepping = 'AB2'
    param.varname_list = ['vort', 'psi', 'u']
    param.sizevar = [4, 2]

    var = Var(param)

    tscheme = Timescheme(param, var.state)

    print(tscheme.x)
    tscheme.forward()
Esempio n. 8
0
    def __init__(self, param, grid):
        self.list_param = ['forcing', 'diffusion', 'Kdiff', 'noslip',
                           'timestepping', 'beta', 'Rd', 'ageostrophic',
                           'bottom_torque',
                           'forcing_module']
        param.copy(self, self.list_param)

        # for diagnostics
        self.list_param = ['yr', 'nh', 'msk', 'area', 'mpitools',  'isisland']
        grid.copy(self, self.list_param)

        # for variables
        param.varname_list = ['pv', 'psi', 'u', 'v', 'pvanom', 'vorticity']

        if param.bottom_torque:
            param.varname_list += ['btorque']

        if param.ageostrophic:
            param.varname_list += ['ua', 'va']

        param.sizevar = [grid.nyl, grid.nxl]
        self.var = Var(param)
        self.source = np.zeros(param.sizevar)

        self.ipva = self.var.varname_list.index('pvanom')
        self.ipv = self.var.varname_list.index('pv')
        self.ivor = self.var.varname_list.index('vorticity')
        self.ipsi = self.var.varname_list.index('psi')

        # background pv
        self.pvback = self.beta*(grid.yr-grid.Ly*.5)*grid.msk

        # for operators
        param.tracer_list = ['pv']
        if param.bottom_torque:
            param.tracer_list += ['btorque']

        if param.ageostrophic:
            param.tracer_list += ['ua', 'va']

        param.whosetspsi = ('pvanom')
        param.qgoperator = True
        self.ope = Operators(param, grid)

        # for timescheme
        self.tscheme = Timescheme(param, self.var.state)
        self.dx0 = self.tscheme.dx0

        self.kt = 0

        if self.forcing:
            if self.forcing_module == 'embedded':
                print('Warning: check that you have indeed added the forcing to the model')
                print('Right below the line    : model = f2d.model')
                print('you should have the line: model.forc = Forcing(param, grid)')

                pass
            else:
                try:
                    f = import_module(self.forcing_module)

                except ImportError:
                    print('module %s for forcing cannot be found'
                          % self.forcing_module)
                    print('make sure file **%s.py** exists' % self.forcing_module)
                    sys.exit(0)

                self.forc = f.Forcing(param, grid)

        self.diags = {}

        self.tscheme.set(self.dynamics, self.timestepping)
Esempio n. 9
0
 def find_var(self, name):
     vars_ = [var for var in self.vars if var.name == name]
     return vars_[0] if any(vars_) else Var('nope', name, undef=True)
Esempio n. 10
0
 def add_var(self, name, type_, is_const=False):
     var = Var(type_, name, is_const)
     self._remove_var_if_exist(var)
     self.vars.append(var)
     return var
Esempio n. 11
0
    def __init__(self, param, grid):
        self.list_param = [
            'forcing', 'diffusion', 'Kdiff', 'timestepping', 'ageostrophic',
            'forcing_module', 'geometry'
        ]
        param.copy(self, self.list_param)

        # for diagnostics
        self.list_param = ['yr', 'nh', 'msk', 'area', 'mpitools']
        grid.copy(self, self.list_param)
        assert grid.geometry == "perio", "SQG imposes a biperiodic domain"

        assert param.ageostrophic == False, "Ageostrophic velocity not yet tested"

        # for variables
        param.varname_list = ['pv', 'psi', 'u', 'v', 'vorticity']

        if param.ageostrophic:
            # TODO: not yet tested for SQG
            param.varname_list += ['ua', 'va']

        param.sizevar = [grid.nyl, grid.nxl]
        self.var = Var(param)
        self.source = np.zeros(param.sizevar)

        self.ipv = self.var.varname_list.index('pv')
        self.ivor = self.var.varname_list.index('vorticity')
        self.ipsi = self.var.varname_list.index('psi')

        # for operators
        param.tracer_list = ['pv']

        if param.ageostrophic:
            param.tracer_list += ['ua', 'va']

        param.whosetspsi = ('pv')
        param.sqgoperator = True
        self.ope = Operators(param, grid)

        # for timescheme
        self.tscheme = Timescheme(param, self.var.state)
        self.dx0 = self.tscheme.dx0

        self.kt = 0

        if self.forcing:
            if self.forcing_module == 'embedded':
                print(
                    'Warning: check that you have indeed added the forcing to the model'
                )
                print('Right below the line    : model = f2d.model')
                print(
                    'you should have the line: model.forc = Forcing(param, grid)'
                )

                pass
            else:
                try:
                    f = import_module(self.forcing_module)

                except ImportError:
                    print('module %s for forcing cannot be found' %
                          self.forcing_module)
                    print('make sure file **%s.py** exists' %
                          self.forcing_module)
                    sys.exit(0)

                self.forc = f.Forcing(param, grid)

        self.diags = {}

        self.tscheme.set(self.dynamics, self.timestepping)
Esempio n. 12
0
from operators import Not, And, Or, Implies, Equivalence
from variables import T, F, Var
from checker import is_tautology, values_variations


def test_for_all_values(formula, vars_list):
    print("\nTESTING %s" % formula)
    for val_list in values_variations(len(vars_list)):
        vars_values = dict(zip(vars_list, val_list))
        print("%s - %s" % (vars_values, formula.evaluate(vars_values)))
    print("Tautology - %s\n" % is_tautology(formula, vars_list))


task_form = Implies(Var('p'), And(Var('q'), T()))
test_for_all_values(task_form, ['p', 'q'])

simple_tautology = Or(Var('p'), Not(Var('p')))
test_for_all_values(simple_tautology, ['p'])

test_form_1 = Implies(Var('p'), Or(Var('p'), Var('q')))
test_for_all_values(test_form_1, ['p', 'q'])

test_form_2 = Implies(Implies(Var('p'), Var('q')), Var('q'))
test_for_all_values(test_form_2, ['p', 'q'])

test_form_3 = Equivalence(Not(Implies(Var('p'), Var('q'))),
                          And(Var('p'), Not(Var('q'))))
test_for_all_values(test_form_3, ['p', 'q'])
Esempio n. 13
0
    def __init__(self, param, grid):

        self.list_param = [
            'forcing', 'noslip', 'timestepping', 'diffusion', 'Kdiff',
            'myrank', 'forcing_module', 'gravity', 'isisland', 'customized',
            'custom_module', 'additional_tracer'
        ]
        param.copy(self, self.list_param)

        # for potential energy
        self.list_param = ['xr', 'yr', 'nh', 'Lx', 'msk', 'area', 'mpitools']
        grid.copy(self, self.list_param)

        # for variables
        param.varname_list = [
            'vorticity', 'psi', 'u', 'v', 'buoyancy', 'banom'
        ]
        param.tracer_list = ['vorticity', 'buoyancy']
        param.whosetspsi = ('vorticity')

        if hasattr(self, 'additional_tracer'):
            for k in range(len(self.additional_tracer)):
                trac = self.additional_tracer[k]
                param.varname_list.append(trac)
                param.tracer_list.append(trac)

        param.sizevar = [grid.nyl, grid.nxl]
        self.var = Var(param)
        bref = self.var.get('buoyancy').copy()
        self.bref = bref
        self.source = np.zeros(param.sizevar)

        # for operators
        self.ope = Operators(param, grid)

        # for timescheme
        self.tscheme = Timescheme(param, self.var.state)
        self.tscheme.set(self.dynamics, self.timestepping)

        if self.forcing:
            if self.forcing_module == 'embedded':
                self.msg_forcing = (
                    'To make Fluid2d aware of your embedded forcing\n' +
                    'you need to add in the user script \n' +
                    'model.forc = Forcing(param, grid)\n' +
                    'right below the line: model = f2d.model')

                pass
            else:
                try:
                    f = import_module(self.forcing_module)
                except ImportError:
                    print('module %s for forcing cannot be found' %
                          self.forcing_module)
                    print('make sure file **%s.py** exists' %
                          self.forcing_module)
                    exit(0)
                self.forc = f.Forcing(param, grid)

        self.diags = {}

        if self.customized:
            try:
                f = import_module(self.custom_module)
                print(f)
                self.extrastep = f.Step(param, grid)
            except ImportError:
                print('module %s for forcing cannot be found' %
                      self.custom_module)
                print('make sure file **%s.py** exists' % self.custom_module)
                exit(0)
Esempio n. 14
0
    def __init__(self, param, grid):

        self.list_param = [
            'forcing', 'noslip', 'timestepping', 'alphaT', 'betaS',
            'diffusion', 'Kdiff', 'myrank', 'forcing_module', 'gravity',
            'isisland', 'customized', 'custom_module', 'additional_tracer'
        ]
        param.copy(self, self.list_param)

        # for potential energy
        self.list_param = ['xr', 'yr', 'nh', 'Lx', 'msk', 'area', 'mpitools']
        grid.copy(self, self.list_param)

        # for variables
        param.varname_list = [
            'vorticity', 'psi', 'u', 'v', 'density', 'danom', 'T', 'S'
        ]
        param.tracer_list = ['vorticity', 'T', 'S']
        param.whosetspsi = ('vorticity')

        if hasattr(self, 'additional_tracer'):
            for k in range(len(self.additional_tracer)):
                trac = self.additional_tracer[k]
                param.varname_list.append(trac)
                param.tracer_list.append(trac)

        self.varname_list = param.varname_list

        param.sizevar = [grid.nyl, grid.nxl]
        self.var = Var(param)
        dref = self.var.get('density').copy()
        self.dref = dref
        self.source = np.zeros(param.sizevar)

        # for operators
        self.ope = Operators(param, grid)

        # for timescheme
        self.tscheme = Timescheme(param, self.var.state)
        self.tscheme.set(self.dynamics, self.timestepping)

        if self.forcing:
            if self.forcing_module == 'embedded':
                print(
                    'Warning: check that you have indeed added the forcing to the model'
                )
                print('Right below the line    : model = f2d.model')
                print(
                    'you should have the line: model.forc = Forcing(param, grid)'
                )

                pass
            else:
                try:
                    f = import_module(self.forcing_module)

                except ImportError:
                    print('module %s for forcing cannot be found' %
                          self.forcing_module)
                    print('make sure file **%s.py** exists' %
                          self.forcing_module)
                    sys.exit(0)

                self.forc = f.Forcing(param, grid)

        self.diags = {}

        if self.customized:
            try:
                f = import_module(self.custom_module)
                print(f)
                self.extrastep = f.Step(param, grid)
            except ImportError:
                print('module %s for forcing cannot be found' %
                      self.custom_module)
                print('make sure file **%s.py** exists' % self.custom_module)
                exit(0)
Esempio n. 15
0
    def __init__(self, param, grid):

        self.list_param = [
            'forcing', 'noslip', 'timestepping', 'diffusion', 'Kdiff',
            'myrank', 'forcing_module', 'gravity', 'isisland', 'customized',
            'custom_module', 'additional_tracer'
        ]
        param.copy(self, self.list_param)

        # for potential energy
        self.list_param = ['xr', 'yr', 'nh', 'Lx', 'msk', 'area', 'mpitools']
        grid.copy(self, self.list_param)

        # for variables
        param.varname_list = [
            'vorticity', 'psi', 'u', 'v', 'buoyancy', 'banom'
        ]
        param.tracer_list = ['vorticity', 'buoyancy']
        param.whosetspsi = ('vorticity')

        if hasattr(self, 'additional_tracer'):
            for k in range(len(self.additional_tracer)):
                trac = self.additional_tracer[k]
                param.varname_list.append(trac)
                param.tracer_list.append(trac)

        param.sizevar = [grid.nyl, grid.nxl]
        self.var = Var(param)
        bref = self.var.get('buoyancy').copy()
        self.bref = bref
        self.source = np.zeros(param.sizevar)

        # for operators
        self.ope = Operators(param, grid)

        # for timescheme
        self.tscheme = Timescheme(param, self.var.state)
        self.tscheme.set(self.dynamics, self.timestepping)

        if self.forcing:
            if hasattr(self, 'forcing_module'):
                f = import_module(self.forcing_module)
                self.forc = f.Forcing(param, grid)
            else:
                if self.myrank == 0:
                    print('-' * 50)
                    print('did not find an external forcing module')
                    print(
                        'make sure file you define a class Forcing() in your script'
                    )
                    print('and that you attach it.')
                    print(
                        'You should have the following line before f2d.loop()')
                    print('model.forc = Forcing(param, grid)')

        self.diags = {}

        if self.customized:
            try:
                f = import_module(self.custom_module)
                print(f)
                self.extrastep = f.Step(param, grid)
            except ImportError:
                print('module %s for forcing cannot be found' %
                      self.custom_module)
                print('make sure file **%s.py** exists' % self.custom_module)
                exit(0)