示例#1
0
    def build(self, dsargs, is_stiff=False):
        # re-compute in case gen type has been changed
        self.targetlang = self._targetlangs[self.gen_type]
        if is_stiff and self.targetlang == 'python' and self.gen_type == 'vode':
            dsargs.algparams['stiff'] = True

        name = dsargs.name
        if self.gen_version == 0:
            self.gen_version = 1

        # assume it's sufficient to check if .sav file there rather than .so
        found_new = False
        while not found_new:
            filename = os.path.join(self.cwd, 'models', name + '_' + \
                                    self.gen_type + \
                                    '_ver%i'%self.gen_version+'.sav')
            if not os.path.exists(filename):
                found_new = True
            else:
                print(filename + ' already exists')
                self.gen_version += 1
        dsargs.name = name + '_' + self.gen_type + '_ver%i' % self.gen_version
        gen = self.classes[self.gen_type](dsargs)
        model = dst.embed(gen, name=self.model_name, dsi_name='gen')
        self.used_dsargs[self.gen_version] = dsargs.copy()
        self.save_gen(model, name)
        return model
示例#2
0
文件: common.py 项目: Vibhor100/fovea
    def build(self, dsargs, is_stiff=False):
        # re-compute in case gen type has been changed
        self.targetlang = self._targetlangs[self.gen_type]
        if is_stiff and self.targetlang == 'python' and self.gen_type == 'vode':
            dsargs.algparams['stiff'] = True

        name = dsargs.name
        if self.gen_version == 0:
            self.gen_version = 1

        # assume it's sufficient to check if .sav file there rather than .so
        found_new = False
        while not found_new:
            filename = os.path.join(self.cwd, 'models', name + '_' + \
                                    self.gen_type + \
                                    '_ver%i'%self.gen_version+'.sav')
            if not os.path.exists(filename):
                found_new = True
            else:
                print(filename + ' already exists')
                self.gen_version += 1
        dsargs.name = name+'_'+self.gen_type+'_ver%i'%self.gen_version
        gen = self.classes[self.gen_type](dsargs)
        model = dst.embed(gen, name=self.model_name, dsi_name='gen')
        self.used_dsargs[self.gen_version] = dsargs.copy()
        self.save_gen(model, name)
        return model
示例#3
0
def make_shooter():
    # no friction
    # cos(atan(x)) = 1/(sqrt(1+x^2))
    Fx_str = '0'  # '-speed_fn()*cos(atan2(vy,vx))'
    Fy_str = '-10'

    DSargs = dst.args()
    DSargs.varspecs = {
        'vx': Fx_str,
        'x': 'vx',
        'vy': Fy_str,
        'y': 'vy',
        'Fx_out': 'Fx(x,y)',
        'Fy_out': 'Fy(x,y)',
        'speed': 'speed_fn(vx, vy)',
        'bearing': '90-180*atan2(vy,vx)/pi'
    }

    auxfndict = {
        'Fx': (['x', 'y'], Fx_str),
        'Fy': (['x', 'y'], Fy_str),
        'speed_fn': (['vx', 'vy'], 'sqrt(vx*vx+vy*vy)'),
    }
    DSargs.auxvars = ['Fx_out', 'Fy_out', 'speed', 'bearing']

    DSargs.fnspecs = auxfndict
    DSargs.algparams = {
        'init_step': 0.001,
        'max_step': 0.1,
        'max_pts': 20000,
        'maxevtpts': 2,
        'refine': 5
    }

    ground_event = dst.Events.makeZeroCrossEvent('y',
                                                 -1, {
                                                     'name': 'ground',
                                                     'eventtol': 1e-3,
                                                     'precise': True,
                                                     'term': True
                                                 },
                                                 varnames=['y'],
                                                 targetlang='python')
    peak_event = dst.Events.makeZeroCrossEvent('vy',
                                               -1, {
                                                   'name': 'peak',
                                                   'eventtol': 1e-3,
                                                   'precise': True,
                                                   'term': False
                                               },
                                               varnames=['vy'],
                                               targetlang='python')
    DSargs.events = [ground_event, peak_event]
    DSargs.checklevel = 2
    DSargs.ics = {'x': 0, 'y': 0, 'vx': 0, 'vy': 0}
    DSargs.ics.update(make_vel_ics(5, 20))
    DSargs.name = 'cannon'
    DSargs.tdomain = [0, 100000]
    DSargs.tdata = [0, 10]
    return dst.embed(dst.Generator.Vode_ODEsystem(DSargs))
示例#4
0
def create_model():
    pars = {'g': 9.8}  #, 'pi': np.pi}

    #ODE
    ode_def = {
        'x': 'vx',
        'y': 'vy',
        'vx': '-(pi**2)*x',
        'vy': '-g',
        'tt': '1.0',
    }
    event_bounce = dst.makeZeroCrossEvent(
        'x-y',
        1, {
            'name': 'bounce',
            'eventtol': 1e-3,
            'term': True,
            'active': True,
            'eventinterval': 1,
            'eventdelay': 1e-2,
            'starttime': 0,
            'precise': True
        },
        varnames=['x', 'y'],
        targetlang='python')  # targetlang is redundant (defaults to python)

    DSargs = dst.args(name='bball_sin')  # struct-like data
    DSargs.events = [event_bounce]
    #DSargs.pars = pars
    #DSargs.tdata = [0, 10]
    #DSargs.algparams = {'max_pts': 3000, 'stiff': False}
    DSargs.algparams = {'stiff': False, 'init_step': 0.01}
    DSargs.varspecs = ode_def
    DSargs.pars = pars
    #DSargs.xdomain = {'y': [0, 100]}

    DS_fall = dst.embed(dst.Generator.Vode_ODEsystem(DSargs))
    DS_fall_MI = dst.intModelInterface(DS_fall)

    # Reset
    ev_map = dst.EvMapping({
        'y': 'x+0.001',
        'vy': '0.9*(vx-vy)'
    },
                           model=DS_fall)
    #ev_map = dst.EvMapping({'y': '10', 'x': '20'}, model=DS_fall)

    DS_BBall = dst.makeModelInfoEntry(DS_fall_MI, ['bball_sin'],
                                      [('bounce', ('bball_sin', ev_map))])

    modelInfoDict = dst.makeModelInfo([DS_BBall])
    bball_sin_model = dst.Model.HybridModel({
        'name': 'Bouncing_Ball_Sinusiodal',
        'modelInfo': modelInfoDict
    })
    return bball_sin_model
示例#5
0
def make_shooter():
    # no friction
    # cos(atan(x)) = 1/(sqrt(1+x^2))
    Fx_str = '0' # '-speed_fn()*cos(atan2(vy,vx))'
    Fy_str = '-10'

    DSargs = dst.args()
    DSargs.varspecs = {'vx': Fx_str, 'x': 'vx',
                       'vy': Fy_str, 'y': 'vy',
                       'Fx_out': 'Fx(x,y)', 'Fy_out': 'Fy(x,y)',
                       'speed': 'speed_fn(vx, vy)',
                       'bearing': '90-180*atan2(vy,vx)/pi'}

    auxfndict = {'Fx': (['x', 'y'], Fx_str),
                 'Fy': (['x', 'y'], Fy_str),
                 'speed_fn': (['vx', 'vy'], 'sqrt(vx*vx+vy*vy)'),
                 }
    DSargs.auxvars = ['Fx_out', 'Fy_out', 'speed', 'bearing']

    DSargs.fnspecs = auxfndict
    DSargs.algparams = {'init_step':0.001,
                        'max_step': 0.1,
                        'max_pts': 20000,
                        'maxevtpts': 2,
                        'refine': 5}

    ground_event = dst.Events.makeZeroCrossEvent('y', -1,
                                                 {'name': 'ground',
                                                  'eventtol': 1e-3,
                                                  'precise': True,
                                                  'term': True},
                                                 varnames=['y'],
                                                 targetlang='python')
    peak_event = dst.Events.makeZeroCrossEvent('vy', -1,
                                                 {'name': 'peak',
                                                  'eventtol': 1e-3,
                                                  'precise': True,
                                                  'term': False},
                                                 varnames=['vy'],
                                                 targetlang='python')
    DSargs.events = [ground_event, peak_event]
    DSargs.checklevel = 2
    DSargs.ics = {'x': 0, 'y': 0,
                  'vx': 0, 'vy': 0}
    DSargs.ics.update(make_vel_ics(5,20))
    DSargs.name = 'cannon'
    DSargs.tdomain = [0, 100000]
    DSargs.tdata = [0, 10]
    return dst.embed(dst.Generator.Vode_ODEsystem(DSargs))
示例#6
0
def create_model():
    pars = {'g': 9.8}#, 'pi': np.pi}

    #ODE
    ode_def = {
           'x': 'vx',
           'y': 'vy',
           'vx': '-(pi**2)*x',
           'vy': '-g',
           'tt': '1.0',
            }
    event_bounce = dst.makeZeroCrossEvent(
            'x-y', 1,
            {'name': 'bounce',
             'eventtol': 1e-3,
             'term': True,
             'active': True,
             'eventinterval': 1,
             'eventdelay': 1e-2,
             'starttime': 0,
             'precise': True
             },
            varnames=['x', 'y'],
            targetlang='python')  # targetlang is redundant (defaults to python)

    DSargs = dst.args(name='bball_sin')  # struct-like data
    DSargs.events = [event_bounce]
    #DSargs.pars = pars
    #DSargs.tdata = [0, 10]
    #DSargs.algparams = {'max_pts': 3000, 'stiff': False}
    DSargs.algparams = {'stiff': False, 'init_step': 0.01}
    DSargs.varspecs = ode_def
    DSargs.pars = pars
    #DSargs.xdomain = {'y': [0, 100]}

    DS_fall = dst.embed(dst.Generator.Vode_ODEsystem(DSargs))
    DS_fall_MI = dst.intModelInterface(DS_fall)

    # Reset
    ev_map = dst.EvMapping({'y': 'x+0.001', 'vy': '0.9*(vx-vy)'}, model=DS_fall)
    #ev_map = dst.EvMapping({'y': '10', 'x': '20'}, model=DS_fall)

    DS_BBall = dst.makeModelInfoEntry(DS_fall_MI, ['bball_sin'],
                                      [('bounce', ('bball_sin', ev_map))])

    modelInfoDict = dst.makeModelInfo([DS_BBall])
    bball_sin_model = dst.Model.HybridModel(
            {'name': 'Bouncing_Ball_Sinusiodal', 'modelInfo': modelInfoDict})
    return bball_sin_model
示例#7
0
def create_model():
    pars = {'g': 1}
    icdict = {'y': 5, 'vy': 0}

    y_str = 'vy'
    vy_str = '-g'

    event_bounce = dst.makeZeroCrossEvent(
        'y',
        0, {
            'name': 'bounce',
            'eventtol': 1e-3,
            'term': True,
            'active': True
        },
        varnames=['y'],
        parnames=['g'],
        targetlang='python')  # targetlang is redundant (defaults to python)

    DSargs = dst.args(name='bball')  # struct-like data
    DSargs.events = [event_bounce]
    #DSargs.pars = pars
    #DSargs.tdata = [0, 10]
    #DSargs.algparams = {'max_pts': 3000, 'stiff': False}
    DSargs.algparams = {'stiff': False}
    DSargs.varspecs = {'y': y_str, 'vy': vy_str}
    DSargs.pars = pars
    #DSargs.xdomain = {'y': [0, 100], 'vy': [-100, 100]}

    DSargs.ics = icdict

    DS_fall = dst.embed(dst.Generator.Vode_ODEsystem(DSargs))
    DS_fall_MI = dst.intModelInterface(DS_fall)

    ev_map = dst.EvMapping({'y': 0, 'vy': '-0.75*vy'}, model=DS_fall)

    DS_BBall = dst.makeModelInfoEntry(DS_fall_MI, ['bball'],
                                      [('bounce', ('bball', ev_map))])

    modelInfoDict = dst.makeModelInfo([DS_BBall])
    bball_model = dst.Model.HybridModel({
        'name': 'Bouncing_Ball',
        'modelInfo': modelInfoDict
    })
    return bball_model
示例#8
0
def create_model():
    pars = {'g': 1}
    icdict = {'y': 5, 'vy': 0}

    y_str = 'vy'
    vy_str = '-g'

    event_bounce = dst.makeZeroCrossEvent('y', 0,
                                {'name': 'bounce',
                                 'eventtol': 1e-3,
                                 'term': True,
                                 'active': True},
                        varnames=['y'],
                        parnames=['g'],
                        targetlang='python')  # targetlang is redundant (defaults to python)

    DSargs = dst.args(name='bball')  # struct-like data
    DSargs.events = [event_bounce]
    #DSargs.pars = pars
    #DSargs.tdata = [0, 10]
    #DSargs.algparams = {'max_pts': 3000, 'stiff': False}
    DSargs.algparams = {'stiff': False}
    DSargs.varspecs = {'y': y_str, 'vy': vy_str}
    DSargs.pars = pars
    #DSargs.xdomain = {'y': [0, 100], 'vy': [-100, 100]}

    DSargs.ics = icdict

    DS_fall = dst.embed(dst.Generator.Vode_ODEsystem(DSargs))
    DS_fall_MI = dst.intModelInterface(DS_fall)

    ev_map = dst.EvMapping({'y': 0, 'vy': '-0.75*vy'}, model=DS_fall)

    DS_BBall = dst.makeModelInfoEntry(DS_fall_MI, ['bball'],
                                      [('bounce', ('bball', ev_map))])

    modelInfoDict = dst.makeModelInfo([DS_BBall])
    bball_model = dst.Model.HybridModel({'name': 'Bouncing_Ball', 'modelInfo': modelInfoDict})
    return bball_model
def build_lin():
    # make local linear system spec
    if can_cache:
        print("I'm not building this model twice!")
    DSargs = dst.args(name='lin')
    xfn_str = '(x0+yfx*y - x)/taux'
    yfn_str = '(y0+xfy*x - y)/tauy'
    DSargs.varspecs = {'x': xfn_str, 'y': yfn_str}
    DSargs.xdomain = {'x': xdom, 'y': ydom}
    DSargs.pars = {'x0': xdom_half, 'y0': ydom_half,
                   'xfy': 1, 'yfx': 1,
                   'taux': 1, 'tauy': 1}
    DSargs.algparams = {'init_step':0.001,
                        'max_step': 0.001,
                        'max_pts': 10000}
    DSargs.checklevel = 0
    DSargs.tdata = [0, 10]
    DSargs.ics = {'x': xdom_half*1.1, 'y': ydom_half*1.1}
    DSargs.fnspecs = {'Jacobian': (['t', 'x', 'y'],
                                   """[[-1/taux, yfx/taux],
                                       [xfy/tauy, -1/tauy]]""")}
    return dst.embed(dst.Generator.Vode_ODEsystem(DSargs))