Esempio n. 1
0
    def setup(self):
        time_units = self.options['time_units']
        num_stages = self.options['num_stages']
        num_step_vars = self.options['num_step_vars']
        i_step = self.options['i_step']
        glm_B = self.options['glm_B']
        glm_V = self.options['glm_V']

        self.dy_dF = dy_dF = {}

        self.add_input('h', units=time_units)

        for state_name, state in iteritems(self.options['states']):
            size = np.prod(state['shape'])
            shape = state['shape']

            F_name = get_name('F', state_name, i_step=i_step)
            y_old_name = get_name('y_old', state_name, i_step=i_step)
            y_new_name = get_name('y_new', state_name, i_step=i_step)

            self.add_input(F_name, shape=(num_stages,) + shape,
                units=get_rate_units(state['units'], time_units))

            self.add_input(y_old_name, shape=(num_step_vars,) + shape,
                units=state['units'])

            self.add_output(y_new_name, shape=(num_step_vars,) + shape,
                units=state['units'])

            F_arange = np.arange(num_stages * size).reshape(
                (num_stages,) + shape)

            y_arange = np.arange(num_step_vars * size).reshape(
                (num_step_vars,) + shape)

            # -----------------

            # (num_step_vars, num_stages,) + shape
            rows = np.einsum('i...,j->ij...', y_arange, np.ones(num_stages, int)).flatten()

            cols = np.zeros((num_step_vars, num_stages,) + shape, int).flatten()
            self.declare_partials(y_new_name, 'h', rows=rows, cols=cols)

            cols = np.einsum('j...,i->ij...', F_arange, np.ones(num_step_vars, int)).flatten()
            self.declare_partials(y_new_name, F_name, rows=rows, cols=cols)

            # -----------------

            # (num_step_vars, num_step_vars,) + shape
            data = np.einsum('ij,...->ij...',
                glm_V, np.ones(shape)).flatten()
            rows = np.einsum('i...,j->ij...', y_arange, np.ones(num_step_vars)).flatten()
            cols = np.einsum('j...,i->ij...', y_arange, np.ones(num_step_vars)).flatten()

            self.declare_partials(y_new_name, y_old_name, val=data, rows=rows, cols=cols)
Esempio n. 2
0
    def setup(self):
        time_units = self.options['time_units']
        num_stages = self.options['num_stages']
        num_step_vars = self.options['num_step_vars']
        i_step = self.options['i_step']
        glm_B = self.options['glm_B']
        glm_V = self.options['glm_V']

        self.dy_dF = dy_dF = {}

        self.declare_partials('*', '*', dependent=False)

        self.add_input('h', units=time_units)

        for state_name, state in iteritems(self.options['states']):
            size = np.prod(state['shape'])

            y_old_name = get_name('y_old', state_name, i_step=i_step)
            y_new_name = get_name('y_new', state_name, i_step=i_step)

            for j_stage in range(num_stages):
                F_name = get_name('F',
                                  state_name,
                                  i_step=i_step,
                                  j_stage=j_stage)

                self.add_input(F_name,
                               shape=(1, ) + state['shape'],
                               units=get_rate_units(state['units'],
                                                    time_units))

            self.add_input(y_old_name,
                           shape=(num_step_vars, ) + state['shape'],
                           units=state['units'])

            self.add_output(y_new_name,
                            shape=(num_step_vars, ) + state['shape'],
                            units=state['units'])

            self.declare_partials(y_new_name, 'h', dependent=True)

            y_arange = np.arange(num_step_vars * size).reshape(
                (num_step_vars, size))

            # num_step_vars, num_step_vars, size
            data = np.einsum('ij,...->ij...', glm_V, np.ones(size)).flatten()
            rows = np.einsum('i...,j->ij...', y_arange,
                             np.ones(num_step_vars, int)).flatten()
            cols = np.einsum('j...,i->ij...', y_arange,
                             np.ones(num_step_vars, int)).flatten()

            self.declare_partials(y_new_name,
                                  y_old_name,
                                  val=data,
                                  rows=rows,
                                  cols=cols)

            for j_stage in range(num_stages):
                F_name = get_name('F',
                                  state_name,
                                  i_step=i_step,
                                  j_stage=j_stage)

                vals = np.zeros((num_step_vars, 1, size))
                rows = np.arange(num_step_vars * 1 * size)
                cols = np.zeros((num_step_vars, 1 * size), int)
                for ii_step in range(num_step_vars):
                    vals[ii_step, 0, :] = glm_B[ii_step, j_stage]
                    cols[ii_step, :] = np.arange(1 * size)
                vals = vals.flatten()
                cols = cols.flatten()

                self.declare_partials(y_new_name, F_name, rows=rows, cols=cols)
                dy_dF[state_name, j_stage] = vals
Esempio n. 3
0
    def setup(self):
        time_units = self.options['time_units']
        num_times = self.options['num_times']
        num_stages = self.options['num_stages']
        num_step_vars = self.options['num_step_vars']
        glm_A = self.options['glm_A']
        glm_U = self.options['glm_U']

        h_arange = np.arange(num_times - 1)

        self.add_input('h_vec', shape=(num_times - 1), units=time_units)

        for state_name, state in iteritems(self.options['states']):
            size = np.prod(state['shape'])
            shape = state['shape']

            F_name = get_name('F', state_name)
            y_name = get_name('y', state_name)
            Y_out_name = get_name('Y_out', state_name)
            Y_in_name = get_name('Y_in', state_name)

            Y_arange = np.arange((num_times - 1) * num_stages * size).reshape(
                (num_times - 1, num_stages,) + shape)

            F_arange = np.arange((num_times - 1) * num_stages * size).reshape(
                (num_times - 1, num_stages,) + shape)

            y_arange = np.arange(num_times * num_step_vars * size).reshape(
                (num_times, num_step_vars,) + shape)

            self.add_input(F_name,
                shape=(num_times - 1, num_stages,) + shape,
                units=get_rate_units(state['units'], time_units))

            self.add_input(y_name,
                shape=(num_times, num_step_vars,) + shape,
                units=state['units'])

            self.add_input(Y_in_name, val=0.,
                shape=(num_times - 1, num_stages,) + shape,
                units=state['units'])

            self.add_output(Y_out_name,
                shape=(num_times - 1, num_stages,) + shape,
                units=state['units'])

            # -----------------

            ones = -np.ones((num_times - 1) * num_stages * size)
            arange = np.arange((num_times - 1) * num_stages * size)
            self.declare_partials(Y_out_name, Y_in_name, val=ones, rows=arange, cols=arange)

            # -----------------

            # (num_times - 1, num_stages, num_stages,) + shape
            rows = np.einsum('ij...,k->ijk...', Y_arange, np.ones(num_stages, int)).flatten()

            cols = np.einsum('jk...,i->ijk...',
                np.ones((num_stages, num_stages,) + shape, int), h_arange).flatten()
            self.declare_partials(Y_out_name, 'h_vec', rows=rows, cols=cols)

            cols = np.einsum('ik...,j->ijk...', F_arange, np.ones(num_stages, int)).flatten()
            self.declare_partials(Y_out_name, F_name, rows=rows, cols=cols)

            # -----------------

            # (num_times - 1, num_stages, num_step_vars,) + shape
            data = np.einsum('jk,i...->ijk...',
                glm_U, np.ones((num_times - 1,) + shape)).flatten()
            rows = np.einsum('ij...,k->ijk...', Y_arange, np.ones(num_step_vars)).flatten()
            cols = np.einsum('ik...,j->ijk...', y_arange[:-1, :, :], np.ones(num_stages)).flatten()

            self.declare_partials(Y_out_name, y_name, val=data, rows=rows, cols=cols)
Esempio n. 4
0
    def setup(self):
        time_units = self.options['time_units']
        num_times = self.options['num_times']
        num_stages = self.options['num_stages']
        num_step_vars = self.options['num_step_vars']
        glm_B = self.options['glm_B']
        glm_V = self.options['glm_V']

        self.dy_dy = dy_dy = {}
        self.dy_dy_inv = dy_dy_inv = {}

        h_arange = np.arange(num_times - 1)

        self.add_input('h_vec', shape=(num_times - 1), units=time_units)

        for state_name, state in iteritems(self.options['states']):
            size = np.prod(state['shape'])
            shape = state['shape']

            F_name = get_name('F', state_name)
            y0_name = get_name('y0', state_name)
            y_name = get_name('y', state_name)

            y0_arange = np.arange(num_step_vars *
                                  size).reshape((num_step_vars, ) + shape)

            y_arange = np.arange(num_times * num_step_vars * size).reshape((
                num_times,
                num_step_vars,
            ) + shape)

            F_arange = np.arange((num_times - 1) * num_stages * size).reshape((
                num_times - 1,
                num_stages,
            ) + shape)

            self.add_input(F_name,
                           shape=(
                               num_times - 1,
                               num_stages,
                           ) + shape,
                           units=get_rate_units(state['units'], time_units))

            self.add_input(y0_name,
                           shape=(num_step_vars, ) + shape,
                           units=state['units'])

            self.add_output(y_name,
                            shape=(
                                num_times,
                                num_step_vars,
                            ) + shape,
                            units=state['units'])

            # -----------------

            # (num_times, num_step_vars,) + shape
            data1 = np.ones(num_times * num_step_vars * size)
            rows1 = np.arange(num_times * num_step_vars * size)
            cols1 = np.arange(num_times * num_step_vars * size)

            # (num_times - 1, num_step_vars, num_step_vars,) + shape
            data2 = np.einsum('i...,jk->ijk...',
                              np.ones((num_times - 1, ) + shape),
                              -glm_V).flatten()
            rows2 = np.einsum('ij...,k->ijk...', y_arange[1:, :, :],
                              np.ones(num_step_vars)).flatten()
            cols2 = np.einsum('ik...,j->ijk...', y_arange[:-1, :, :],
                              np.ones(num_step_vars)).flatten()

            data = np.concatenate([data1, data2])
            rows = np.concatenate([rows1, rows2])
            cols = np.concatenate([cols1, cols2])

            dy_dy[state_name] = scipy.sparse.csc_matrix(
                (data, (rows, cols)),
                shape=(num_times * num_step_vars * size,
                       num_times * num_step_vars * size))

            dy_dy_inv[state_name] = scipy.sparse.linalg.splu(dy_dy[state_name])

            self.declare_partials(y_name,
                                  y_name,
                                  val=data,
                                  rows=rows,
                                  cols=cols)

            # -----------------

            # (num_step_vars,) + shape
            data = -np.ones((num_step_vars, ) + shape).flatten()
            rows = y_arange[0, :, :].flatten()
            cols = y0_arange.flatten()

            self.declare_partials(y_name,
                                  y0_name,
                                  val=data,
                                  rows=rows,
                                  cols=cols)

            # -----------------

            # (num_times - 1, num_step_vars, num_stages,) + shape
            rows = np.einsum('ij...,k->ijk...', y_arange[1:, :, :],
                             np.ones(num_stages)).flatten()

            cols = np.einsum('jk...,i->ijk...',
                             np.ones((
                                 num_step_vars,
                                 num_stages,
                             ) + shape), h_arange).flatten()
            self.declare_partials(y_name, 'h_vec', rows=rows, cols=cols)

            cols = np.einsum('ik...,j->ijk...', F_arange,
                             np.ones(num_step_vars)).flatten()
            self.declare_partials(y_name, F_name, rows=rows, cols=cols)
Esempio n. 5
0
    def setup(self):
        time_units = self.options['time_units']
        num_stages = self.options['num_stages']
        num_step_vars = self.options['num_step_vars']
        i_stage = self.options['i_stage']
        i_step = self.options['i_step']
        glm_A = self.options['glm_A']
        glm_U = self.options['glm_U']

        self.declare_partials('*', '*', dependent=False)

        self.add_input('h', units=time_units)

        for state_name, state in iteritems(self.options['states']):
            size = np.prod(state['shape'])

            y_old_name = get_name('y_old',
                                  state_name,
                                  i_step=i_step,
                                  i_stage=i_stage)
            Y_name = get_name('Y', state_name, i_step=i_step, i_stage=i_stage)

            for j_stage in range(i_stage):
                F_name = get_name('F',
                                  state_name,
                                  i_step=i_step,
                                  i_stage=i_stage,
                                  j_stage=j_stage)

                self.add_input(F_name,
                               shape=(1, ) + state['shape'],
                               units=get_rate_units(state['units'],
                                                    time_units))

            self.add_input(y_old_name,
                           shape=(num_step_vars, ) + state['shape'],
                           units=state['units'])

            self.add_output(Y_name,
                            shape=(1, ) + state['shape'],
                            units=state['units'])

            vals = np.zeros((num_step_vars, size))
            rows = np.zeros((num_step_vars, size), int)
            cols = np.arange(num_step_vars * size)
            for ii_step in range(num_step_vars):
                vals[ii_step, :] = glm_U[i_stage, ii_step]
                rows[ii_step, :] = np.arange(size)
            vals = vals.flatten()
            rows = rows.flatten()

            self.declare_partials(Y_name, 'h', dependent=True)

            self.declare_partials(Y_name,
                                  y_old_name,
                                  val=vals,
                                  rows=rows,
                                  cols=cols)

            for j_stage in range(i_stage):
                F_name = get_name('F',
                                  state_name,
                                  i_step=i_step,
                                  i_stage=i_stage,
                                  j_stage=j_stage)

                arange = np.arange(size)
                self.declare_partials(Y_name, F_name, rows=arange, cols=arange)
Esempio n. 6
0
    def setup(self):
        time_units = self.options['time_units']
        num_times = self.options['num_times']
        num_stages = self.options['num_stages']
        num_step_vars = self.options['num_step_vars']
        glm_B = self.options['glm_B']
        glm_V = self.options['glm_V']

        self.mtx_lu_dict = {}
        self.mtx_y0_dict = {}
        self.mtx_h_dict = {}
        self.mtx_hf_dict = {}

        self.num_y0_dict = {}
        self.num_F_dict = {}
        self.num_y_dict = {}

        h_arange = np.arange(num_times - 1)
        num_h = num_times - 1

        self.add_input('h_vec', shape=(num_times - 1), units=time_units)

        for state_name, state in iteritems(self.options['states']):
            size = np.prod(state['shape'])
            shape = state['shape']

            y0_name = get_name('y0', state_name)
            F_name = get_name('F', state_name)
            y_name = get_name('y', state_name)

            # --------------------------------------------------------------------------------

            y0_arange = np.arange(num_step_vars *
                                  size).reshape((num_step_vars, ) + shape)

            F_arange = np.arange((num_times - 1) * num_stages * size).reshape((
                num_times - 1,
                num_stages,
            ) + shape)

            y_arange = np.arange(num_times * num_step_vars * size).reshape((
                num_times,
                num_step_vars,
            ) + shape)

            num_y0 = np.prod(y0_arange.shape)
            num_F = np.prod(F_arange.shape)
            num_y = np.prod(y_arange.shape)

            # --------------------------------------------------------------------------------

            self.add_input(y0_name,
                           shape=(num_step_vars, ) + shape,
                           units=state['units'])

            self.add_input(F_name,
                           shape=(
                               num_times - 1,
                               num_stages,
                           ) + shape,
                           units=get_rate_units(state['units'], time_units))

            self.add_output(y_name,
                            shape=(
                                num_times,
                                num_step_vars,
                            ) + shape,
                            units=state['units'])

            # --------------------------------------------------------------------------------

            data_list = []
            rows_list = []
            cols_list = []

            # y identity
            data = np.ones(num_y)
            rows = np.arange(num_y)
            cols = np.arange(num_y)
            data_list.append(data)
            rows_list.append(rows)
            cols_list.append(cols)

            # V blocks: (num_times - 1) x num_step_var x num_step_var x ...
            data = np.einsum('jk,i...->ijk...', -glm_V,
                             np.ones((num_times - 1, ) + shape)).flatten()
            rows = np.einsum('ij...,k->ijk...', y_arange[1:, :, :],
                             np.ones(num_step_vars, int)).flatten()
            cols = np.einsum('ik...,j->ijk...', y_arange[:-1, :, :],
                             np.ones(num_step_vars, int)).flatten()
            data_list.append(data)
            rows_list.append(rows)
            cols_list.append(cols)

            # concatenate
            data = np.concatenate(data_list)
            rows = np.concatenate(rows_list)
            cols = np.concatenate(cols_list)

            mtx = scipy.sparse.csc_matrix((data, (rows, cols)),
                                          shape=(num_y, num_y))

            self.mtx_lu_dict[state_name] = scipy.sparse.linalg.splu(mtx)

            # --------------------------------------------------------------------------------

            data = np.ones(num_y0)
            rows = y_arange[0, :, :].flatten()
            cols = np.arange(num_y0)
            self.mtx_y0_dict[state_name] = scipy.sparse.csc_matrix(
                (data, (rows, cols)), shape=(num_y, num_y0))

            # --------------------------------------------------------------------------------

            data = np.ones(num_F)
            rows = np.arange(num_F)
            cols = np.einsum('i,j...->ij...', h_arange,
                             np.ones((num_stages, ) + shape, int)).flatten()
            self.mtx_h_dict[state_name] = scipy.sparse.csc_matrix(
                (data, (rows, cols)), shape=(num_F, num_h))

            # --------------------------------------------------------------------------------

            # B blocks: (num_times - 1) x num_step_vars x num_stage x ...
            data = np.einsum('jk,i...->ijk...', glm_B,
                             np.ones((num_times - 1, ) + shape)).flatten()
            rows = np.einsum('ij...,k->ijk...', y_arange[1:, :, :],
                             np.ones(num_stages, int)).flatten()
            cols = np.einsum('ik...,j->ijk...', F_arange,
                             np.ones(num_step_vars, int)).flatten()

            self.mtx_hf_dict[state_name] = scipy.sparse.csc_matrix(
                (data, (rows, cols)), shape=(num_y, num_F))
Esempio n. 7
0
    def setup(self):
        time_units = self.options['time_units']
        num_times = self.options['num_times']
        num_stages = self.options['num_stages']
        num_step_vars = self.options['num_step_vars']
        glm_A = self.options['glm_A']
        glm_U = self.options['glm_U']
        glm_B = self.options['glm_B']
        glm_V = self.options['glm_V']

        self.mtx_y0_dict = {}
        self.mtx_dict = {}
        self.mtx_h_dict = {}

        h_arange = np.arange(num_times - 1)
        num_h = num_times - 1

        self.add_input('h_vec', shape=(num_times - 1), units=time_units)

        for state_name, state in iteritems(self.options['states']):
            size = np.prod(state['shape'])
            shape = state['shape']

            y0_name = get_name('y0', state_name)
            F_name = get_name('F', state_name)
            Y_in_name = get_name('Y_in', state_name)
            Y_out_name = get_name('Y_out', state_name)

            # --------------------------------------------------------------------------------

            y0_arange = np.arange(num_step_vars * size).reshape((num_step_vars,) + shape)

            F_arange = np.arange((num_times - 1) * num_stages * size).reshape(
                (num_times - 1, num_stages,) + shape)

            Y_arange = np.arange((num_times - 1) * num_stages * size).reshape(
                (num_times - 1, num_stages,) + shape)

            y_arange = np.arange(num_times * num_step_vars * size).reshape(
                (num_times, num_step_vars,) + shape)

            num_y0 = np.prod(y0_arange.shape)
            num_F = np.prod(F_arange.shape)
            num_Y = np.prod(Y_arange.shape)
            num_y = np.prod(y_arange.shape)

            # --------------------------------------------------------------------------------

            self.add_input(y0_name,
                shape=(num_step_vars,) + shape,
                units=state['units'])

            self.add_input(F_name,
                shape=(num_times - 1, num_stages,) + shape,
                units=get_rate_units(state['units'], time_units))

            self.add_input(Y_in_name, val=0.,
                shape=(num_times - 1, num_stages,) + shape,
                units=state['units'])

            self.add_output(Y_out_name,
                shape=(num_times - 1, num_stages,) + shape,
                units=state['units'])

            # -----------------

            self.declare_partials(Y_out_name, 'h_vec')
            self.declare_partials(Y_out_name, y0_name)
            self.declare_partials(Y_out_name, F_name)

            # -----------------

            ones = -np.ones((num_times - 1) * num_stages * size)
            arange = np.arange((num_times - 1) * num_stages * size)
            self.declare_partials(Y_out_name, Y_in_name, val=ones, rows=arange, cols=arange)

            # --------------------------------------------------------------------------------
            # mtx_y0: num_stages x num_step_vars x ...

            data = np.ones((num_step_vars,) + shape).flatten()
            rows = y_arange[0, :, :].flatten()
            cols = y0_arange.flatten()
            mtx_y0 = scipy.sparse.csc_matrix((data, (rows, cols)), shape=(num_y, num_y0)).toarray()

            # --------------------------------------------------------------------------------
            # mtx_A: (num_times - 1) x num_stages x num_stages x ...

            data = np.einsum('jk,i...->ijk...',
                glm_A, np.ones((num_times - 1,) + shape)).flatten()
            rows = np.einsum('ij...,k->ijk...',
                Y_arange, np.ones(num_stages, int)).flatten()
            cols = np.einsum('ik...,j->ijk...',
                F_arange, np.ones(num_stages, int)).flatten()
            mtx_A = scipy.sparse.csc_matrix((data, (rows, cols)), shape=(num_Y, num_F)).toarray()

            # --------------------------------------------------------------------------------
            # mtx_B: (num_times - 1) x num_step_vars x num_stages x ...

            data = np.einsum('jk,i...->ijk...',
                glm_B, np.ones((num_times - 1,) + shape)).flatten()
            rows = np.einsum('ij...,k->ijk...',
                y_arange[1:, :, :], np.ones(num_stages, int)).flatten()
            cols = np.einsum('ik...,j->ijk...',
                F_arange, np.ones(num_step_vars, int)).flatten()
            mtx_B = scipy.sparse.csc_matrix((data, (rows, cols)), shape=(num_y, num_F)).toarray()

            # --------------------------------------------------------------------------------
            # mtx_U: (num_times - 1) x num_stages x num_step_vars x ...

            data = np.einsum('jk,i...->ijk...',
                glm_U, np.ones((num_times - 1,) + shape)).flatten()
            rows = np.einsum('ij...,k->ijk...',
                Y_arange, np.ones(num_step_vars, int)).flatten()
            cols = np.einsum('ik...,j->ijk...',
                y_arange[:-1, :, :], np.ones(num_stages, int)).flatten()
            mtx_U = scipy.sparse.csc_matrix((data, (rows, cols)), shape=(num_Y, num_y)).toarray()

            # --------------------------------------------------------------------------------
            # mtx_y

            data_list = []
            rows_list = []
            cols_list = []

            # identity
            data = np.ones(num_y)
            rows = np.arange(num_y)
            cols = np.arange(num_y)
            data_list.append(data); rows_list.append(rows); cols_list.append(cols)

            # (num_times - 1) x num_step_var x num_step_var x ...
            data = np.einsum('jk,i...->ijk...',
                -glm_V, np.ones((num_times - 1,) + shape)).flatten()
            rows = np.einsum('ij...,k->ijk...',
                y_arange[1:, :, :], np.ones(num_step_vars, int)).flatten()
            cols = np.einsum('ik...,j->ijk...',
                y_arange[:-1, :, :], np.ones(num_step_vars, int)).flatten()
            data_list.append(data); rows_list.append(rows); cols_list.append(cols)

            # concatenate
            data = np.concatenate(data_list)
            rows = np.concatenate(rows_list)
            cols = np.concatenate(cols_list)

            mtx_y = scipy.sparse.csc_matrix((data, (rows, cols)), shape=(num_y, num_y))
            mtx_y_inv = scipy.sparse.linalg.splu(mtx_y)

            # --------------------------------------------------------------------------------
            # mtx_h

            data = np.ones(num_F)
            rows = np.arange(num_F)
            cols = np.einsum('i,j...->ij...',
                h_arange, np.ones((num_stages,) + shape, int)).flatten()
            mtx_h = scipy.sparse.csc_matrix((data, (rows, cols)), shape=(num_F, num_h)).toarray()

            # --------------------------------------------------------------------------------
            self.mtx_y0_dict[state_name] = mtx_U.dot(mtx_y_inv.solve(mtx_y0))
            self.mtx_dict[state_name] = mtx_A + mtx_U.dot(mtx_y_inv.solve(mtx_B))
            self.mtx_h_dict[state_name] = mtx_h