Example #1
0
        void operator()(const state_type& x, state_type& dxdt, const double& t)
        {
            for (state_type::iterator i(dxdt.begin()); i != dxdt.end(); ++i)
            {
                *i = 0.0;
            }
            // XXX
            for (reaction_container_type::const_iterator
                i(reactions_.begin()); i != reactions_.end(); i++)
            {
                // Prepare  state_array of reactants and products that contain amounts of each reactants.
                Ratelaw::state_container_type reactants_states(i->reactants.size());
                Ratelaw::state_container_type products_states(i->products.size());
                Ratelaw::state_container_type::size_type cnt(0);

                for (index_container_type::const_iterator
                    j((*i).reactants.begin()); j != (*i).reactants.end(); ++j, cnt++)
                {
                    reactants_states[cnt] = x[*j];
                }
                cnt = 0;
                for (index_container_type::const_iterator
                    j((*i).products.begin()); j != (*i).products.end(); ++j, cnt++)
                {
                    products_states[cnt] = x[*j];
                }

                double flux;
                // Get pointer of Ratelaw object and call it.
                if (i->ratelaw.expired() || i->ratelaw.lock()->is_available() == false)
                {
                    boost::scoped_ptr<Ratelaw> temporary_ratelaw_obj(new RatelawMassAction(i->k));
                    flux = temporary_ratelaw_obj->deriv_func(reactants_states, products_states, volume_);
                }
                else
                {
                    boost::shared_ptr<Ratelaw> ratelaw = (*i).ratelaw.lock();
                    flux = (*ratelaw).deriv_func(reactants_states, products_states, volume_);
                }

                // Merge each reaction's flux into whole dxdt
                for (index_container_type::const_iterator
                    j((*i).reactants.begin()); j != (*i).reactants.end(); ++j)
                {
                    dxdt[*j] -= flux;
                }
                for (index_container_type::const_iterator
                    j((*i).products.begin()); j != (*i).products.end(); ++j)
                {
                    dxdt[*j] += flux;
                }
            }
        }
void lattice( const state_type &x , state_type &dxdt , const double /* t */ )
{
    state_type::const_iterator x_begin = x.begin();
    state_type::const_iterator x_end = x.end();
    state_type::iterator dxdt_begin = dxdt.begin();

    x_end--; // stop one before last
    while( x_begin != x_end )
    {
        *(dxdt_begin++) = std::sin( *(x_begin) - *(x_begin++) );
    }
    *dxdt_begin = sin( *x_begin - *(x.begin()) ); // periodic boundary
}
Example #3
0
    void operator()(state_type const& x, state_type& dxdt, double const& t)
    {
        for (state_type::iterator i(dxdt.begin()); i != dxdt.end(); ++i)
        {
            *i = 0.0;
        }

        NetworkModel::reaction_rule_container_type const&
            reaction_rules(model_->reaction_rules());
        for (NetworkModel::reaction_rule_container_type::const_iterator
                 i(reaction_rules.begin()); i != reaction_rules.end(); ++i)
        {
            double flux((*i).k() * volume_);

            ReactionRule::reactant_container_type const&
                reactants((*i).reactants());
            ReactionRule::product_container_type const&
                products((*i).products());
            for (ReactionRule::reactant_container_type::iterator
                     j(reactants.begin()); j != reactants.end(); ++j)
            {
                flux *= x[index_map_[*j]] / volume_;
            }

            for (ReactionRule::reactant_container_type::iterator
                     j(reactants.begin()); j != reactants.end(); ++j)
            {
                dxdt[index_map_[*j]] -= flux;
            }

            for (ReactionRule::product_container_type::iterator
                     j(products.begin()); j != products.end(); ++j)
            {
                dxdt[index_map_[*j]] += flux;
            }
        }
    }
Example #4
0
        void operator()(
            const state_type& x, matrix_type& jacobi, const double& t, state_type& dfdt) const
        {
            // fill 0 into jacobi and dfdt
            for (state_type::iterator i(dfdt.begin()); i != dfdt.end(); ++i)
            {
                *i = 0.0;
            }
            for (matrix_type::array_type::iterator i(jacobi.data().begin());
                i != jacobi.data().end(); ++i)
            {
                *i = 0.0;
            }

            // Calculate jacobian for each reaction and merge it.
            for (reaction_container_type::const_iterator
                i(reactions_.begin()); i != reactions_.end(); i++)
            {
                // Calculate a reaction's jacobian.
                // prepare state_array that contain amounts of reactants
                index_container_type::size_type reactants_size(i->reactants.size());
                index_container_type::size_type products_size(i->products.size());
                Ratelaw::state_container_type reactants_states(reactants_size);
                Ratelaw::state_container_type products_states(products_size);
                Ratelaw::state_container_type::size_type cnt(0);
                for (index_container_type::const_iterator
                    j((*i).reactants.begin()); j != (*i).reactants.end(); ++j, cnt++)
                {
                    reactants_states[cnt] = x[*j];
                }
                cnt = 0;
                for (index_container_type::const_iterator
                    j((*i).products.begin()); j != (*i).products.end(); ++j, cnt++)
                {
                    products_states[cnt] = x[*j];
                }
                // prepare matrix object that will be filled with numerical differentiate.
                matrix_type::size_type row_length = reactants_size + products_size;
                matrix_type::size_type col_length = row_length;
                matrix_type mat(row_length, col_length);

                // get the pointer of Ratelaw object and call it.
                if (i->ratelaw.expired() || i->ratelaw.lock()->is_available() == false)
                {
                    boost::scoped_ptr<Ratelaw> temporary_ratelaw_obj(new RatelawMassAction(i->k));
                    temporary_ratelaw_obj->jacobi_func(mat, reactants_states, products_states, volume_);
                }
                else
                {
                    boost::shared_ptr<Ratelaw> ratelaw = (*i).ratelaw.lock();
                    (*ratelaw).jacobi_func(mat, reactants_states, products_states, volume_);
                }

                //merge jacobian
                for(matrix_type::size_type row(0); row < row_length; row++)
                {
                    matrix_type::size_type j_row(row < reactants_size ? (*i).reactants[row] : (*i).products[row - reactants_size]);
                    for(matrix_type::size_type col(0); col < col_length; col++)
                    {
                        matrix_type::size_type j_col(col < reactants_size ? (*i).reactants[col] : (*i).products[col - reactants_size]);
                        jacobi(j_row, j_col) += mat(row, col);
                    }
                }
            }
        }