Ejemplo n.º 1
0
void SampleTree::RecursiveOutput(int currNode, int indent) const {
    Indent(indent);

    for (int i = 0; i < 3; i++) {
        if (i) printf(" x ");
        printf("[%lf, %lf]", GetVectorComponent(this->nodes[currNode].lowerPoint, i),
                             GetVectorComponent(this->nodes[currNode].upperPoint, i));
    }
    printf(", population = %d\n", this->nodes[currNode].population);

    if (this->nodes[currNode].sampleList) {
        Indent(indent);
        printf("Leaf:");
        for (int i = 0; i < this->nodes[currNode].population; i++) {
            Vector point = this->samples[this->nodes[currNode].sampleList[i]];
            printf(" (%lf, %lf, %lf)", point.GetX(), point.GetY(), point.GetZ());
        }
        printf("\n");
    } else {
        Indent(indent);
        printf("Non-leaf:\n");
        this->RecursiveOutput(this->nodes[currNode].leftIndex, indent + 4);
        this->RecursiveOutput(this->nodes[currNode].rightIndex, indent + 4);
    }
}
double AbstractParameterisedSystem<VECTOR>::GetAnyVariable(unsigned index, double time,
                                                           VECTOR* pDerivedQuantities)
{
    if (index < mNumberOfStateVariables)
    {
        return GetVectorComponent(mStateVariables, index);
    }
    else if (index - mNumberOfStateVariables < GetVectorSize(mParameters))
    {
        return GetVectorComponent(mParameters, index - mNumberOfStateVariables);
    }
    else
    {
        unsigned offset = mNumberOfStateVariables + GetVectorSize(mParameters);
        if (index - offset < GetNumberOfDerivedQuantities())
        {
            VECTOR dqs;
            if (pDerivedQuantities == NULL)
            {
                dqs = ComputeDerivedQuantitiesFromCurrentState(time);
                pDerivedQuantities = &dqs;
            }
            double value = GetVectorComponent(*pDerivedQuantities, index - offset);
            if (pDerivedQuantities == &dqs)
            {
                DeleteVector(dqs);
            }
            return value;
        }
        else
        {
            EXCEPTION("Invalid index passed to GetAnyVariable.");
        }
    }
}
Ejemplo n.º 3
0
N_Vector ParameterisedCvode::ComputeDerivedQuantities(double time,
                                                      const N_Vector& rState)
{
    N_Vector derived_quantities = NULL;
    CreateVectorIfEmpty(derived_quantities,1);
    SetVectorComponent(derived_quantities, 0,
                       2*GetVectorComponent(mParameters,0) + GetVectorComponent(rState,0));
    return derived_quantities;
}
double AbstractParameterisedSystem<VECTOR>::GetParameter(unsigned index) const
{
    if (index >= GetVectorSize(mParameters))
    {
        EXCEPTION("The index passed in must be less than the number of parameters.");
    }
    return GetVectorComponent(mParameters, index);
}
double AbstractParameterisedSystem<VECTOR>::GetStateVariable(unsigned index) const
{
    if (index >= mNumberOfStateVariables)
    {
        EXCEPTION("The index passed in must be less than the number of state variables.");
    }
    return GetVectorComponent(mStateVariables, index);
}
Ejemplo n.º 6
0
 void CheckDerivedQuantities(AbstractParameterisedSystem<VECTOR_TYPE>& rCell,
                             const VECTOR_TYPE& rStateVec)
 {
     TS_ASSERT_EQUALS(rCell.GetNumberOfDerivedQuantities(), 2u);
     TS_ASSERT_EQUALS(rCell.GetDerivedQuantityIndex("FonRT"), 0u);
     TS_ASSERT_EQUALS(rCell.GetDerivedQuantityIndex("potassium_currents"), 1u);
     TS_ASSERT_EQUALS(rCell.GetDerivedQuantityUnits(0u), "per_millivolt");
     TS_ASSERT_EQUALS(rCell.GetDerivedQuantityUnits(1u), "microA_per_cm2");
     VECTOR_TYPE derived = rCell.ComputeDerivedQuantitiesFromCurrentState(0.0);
     const double FonRT = 0.037435728309031795;
     const double i_K_total = 1.0007;
     TS_ASSERT_EQUALS(GetVectorSize(derived), 2u);
     TS_ASSERT_DELTA(GetVectorComponent(derived, 0), FonRT, 1e-12);
     TS_ASSERT_DELTA(GetVectorComponent(derived, 1), i_K_total, 1e-4);
     DeleteVector(derived);
     derived = rCell.ComputeDerivedQuantities(0.0, rStateVec);
     TS_ASSERT_EQUALS(GetVectorSize(derived), 2u);
     TS_ASSERT_DELTA(GetVectorComponent(derived, 0), FonRT, 1e-12);
     TS_ASSERT_DELTA(GetVectorComponent(derived, 1), i_K_total, 1e-4);
     DeleteVector(derived);
 }
void AbstractParameterisedSystem<VECTOR>::SetStateVariables(const VECTOR& rStateVariables)
{

    if ( mNumberOfStateVariables != GetVectorSize(rStateVariables) )
    {
        EXCEPTION("The size of the passed in vector must be that of the number of state variables.");
    }

    CreateVectorIfEmpty(mStateVariables, mNumberOfStateVariables);
    for (unsigned i=0; i<mNumberOfStateVariables; i++)
    {
        SetVectorComponent(mStateVariables, i, GetVectorComponent(rStateVariables, i));
    }
}
Ejemplo n.º 8
0
void SampleTree::RecursiveBuildByIntersectionRate(SampleTree::Node &currNode, int &cnt, int fr, int to, int *sortArray, int *assistArray, double *accumulativeTop, double *accumulativeBottom) {
    currNode.population = to - fr + 1;
    currNode.lowerPoint = Vector(samples[sortArray[fr]].GetX(),
                                 samples[sortArray[this->numOfSamples + fr]].GetY(),
                                 samples[sortArray[(this->numOfSamples << 1) + fr]].GetZ());
    currNode.upperPoint = Vector(samples[sortArray[to]].GetX(),
                                 samples[sortArray[this->numOfSamples + to]].GetY(),
                                 samples[sortArray[(this->numOfSamples << 1) + to]].GetZ());
    if (currNode.population <= this->maxNodePopulation) {
        currNode.leftIndex = currNode.rightIndex = -1;
        currNode.sampleList = new int [currNode.population];
        for (int i = fr; i <= to; i++)
            currNode.sampleList[i - fr] = sortArray[i];
        return;
    }

    currNode.sampleList = NULL;
    currNode.leftIndex = ++cnt;
    currNode.rightIndex = ++cnt;

    double spanLength[] = {currNode.upperPoint.GetX() - currNode.lowerPoint.GetX(),
                           currNode.upperPoint.GetY() - currNode.lowerPoint.GetY(),
                           currNode.upperPoint.GetZ() - currNode.lowerPoint.GetZ()};
    int dim = 0;
    for (int i = 1; i < 3; i++)
        if (spanLength[i] > spanLength[dim])
            dim = i;

    double splittingPosition = this->GetSplittingPositionByIntersectionRate(fr, to, sortArray + dim * this->numOfSamples, dim,
                                                                            currNode.lowerPoint, currNode.upperPoint,
                                                                            accumulativeTop, accumulativeBottom);

    int l1, l2;
    for (int d = 0; d < 3; d++) {
        l1 = l2 = 0;
        int *arr = sortArray + d * this->numOfSamples;
        for (int i = fr; i <= to; i++)
            if (GetVectorComponent(this->samples[arr[i]], dim) <= splittingPosition)
                arr[fr + l1++] = arr[i];
            else
                assistArray[fr + l2++] = arr[i];
        for (int i = 0; i < l2; i++)
            arr[fr + l1++] = assistArray[fr + i];
    }

    this->RecursiveBuildByIntersectionRate(nodes[currNode.leftIndex], cnt, fr, to - l2, sortArray, assistArray, accumulativeTop, accumulativeBottom);
    this->RecursiveBuildByIntersectionRate(nodes[currNode.rightIndex], cnt, to - l2 + 1, to, sortArray, assistArray, accumulativeTop, accumulativeBottom);
}
std::string AbstractParameterisedSystem<VECTOR>::GetStateMessage(const std::string& rMessage, VECTOR Y)
{
    std::stringstream res;
    res << rMessage << std::endl << "State:" << std::endl;
    assert(rGetStateVariableNames().size()==GetVectorSize(Y));
    const std::vector<std::string>& r_units = rGetStateVariableUnits();
    for (unsigned i=0; i<GetVectorSize(Y); i++)
    {
        res << "\t" << rGetStateVariableNames()[i] << ":" << GetVectorComponent(Y, i);
        if (!r_units.empty())
        {
            res << " " << r_units[i];
        }
        res << std::endl;
    }
    return res.str();
}
Ejemplo n.º 10
0
double SampleTree::GetSplittingPositionByIntersectionRate(int fr, int to, int *sortArray, int dim, const Vector &lowerPoint, const Vector &upperPoint, double *accumulativeTop, double *accumulativeBottom) {
    accumulativeTop[to] = accumulativeBottom[to] = GetVectorComponent(this->samples[sortArray[to]], (dim + 1) % 3);
    accumulativeTop[to + this->numOfSamples] = accumulativeBottom[to + this->numOfSamples] = GetVectorComponent(this->samples[sortArray[to]], (dim + 2) % 3);
    for (int i = to - 1; i >= fr; i--) {
        double v1 = GetVectorComponent(this->samples[sortArray[i]], (dim + 1) % 3);
        double v2 = GetVectorComponent(this->samples[sortArray[i]], (dim + 2) % 3);

        accumulativeTop[i] = std::max(accumulativeTop[i + 1], v1);
        accumulativeBottom[i] = std::min(accumulativeBottom[i + 1], v1);

        accumulativeTop[i + this->numOfSamples] = std::max(accumulativeTop[i + 1 + this->numOfSamples], v2);
        accumulativeBottom[i + this->numOfSamples] = std::min(accumulativeBottom[i + 1 + this->numOfSamples], v2);
    }

    int lowerBound, upperBound;
    double top[2], bottom[2];
    top[0] = bottom[0] = GetVectorComponent(samples[sortArray[fr]], (dim + 1) % 3);
    top[1] = bottom[1] = GetVectorComponent(samples[sortArray[fr]], (dim + 2) % 3);

    double bestExpectation = -1;
    int bestUpperBound;

    for (lowerBound = fr; lowerBound < to; lowerBound = upperBound) {
        double pos = GetVectorComponent(this->samples[sortArray[lowerBound]], dim);
        for (upperBound = lowerBound; upperBound <= to && GetVectorComponent(this->samples[sortArray[upperBound]], dim) == pos; upperBound++) {
            double v1 = GetVectorComponent(this->samples[sortArray[upperBound]], (dim + 1) % 3);
            double v2 = GetVectorComponent(this->samples[sortArray[upperBound]], (dim + 2) % 3);

            top[0] = std::max(top[0], v1);
            bottom[0] = std::min(bottom[0], v1);

            top[1] = std::max(top[1], v2);
            bottom[1] = std::max(bottom[1], v2);
        }
        if (upperBound > to) break;

        double p1 = Sqr(GetVectorComponent(upperPoint, dim) - GetVectorComponent(lowerPoint, dim)) - 
                    Sqr(GetVectorComponent(this->samples[sortArray[fr]], dim) - GetVectorComponent(lowerPoint, dim)) -
                    Sqr(GetVectorComponent(upperPoint, dim) - GetVectorComponent(this->samples[sortArray[upperBound - 1]], dim));

        double p2 = Sqr(GetVectorComponent(upperPoint, (dim + 1) % 3) - GetVectorComponent(lowerPoint, (dim + 1) % 3)) -
                    Sqr(bottom[0] - GetVectorComponent(lowerPoint, (dim + 1) % 3)) -
                    Sqr(GetVectorComponent(upperPoint, (dim + 1) % 3) - top[0]);

        double p3 = Sqr(GetVectorComponent(upperPoint, (dim + 2) % 3) - GetVectorComponent(lowerPoint, (dim + 2) % 3)) -
                    Sqr(bottom[1] - GetVectorComponent(lowerPoint, (dim + 2) % 3)) -
                    Sqr(GetVectorComponent(upperPoint, (dim + 2) % 3) - top[1]);

        double expectation1 = p1 * p2 * p3 * (upperBound - fr);

        p1 = Sqr(GetVectorComponent(upperPoint, dim) - GetVectorComponent(lowerPoint, dim)) -
             Sqr(GetVectorComponent(this->samples[sortArray[upperBound]], dim) - GetVectorComponent(lowerPoint, dim)) -
             Sqr(GetVectorComponent(upperPoint, dim) - GetVectorComponent(this->samples[sortArray[to]], dim));

        p2 = Sqr(GetVectorComponent(upperPoint, (dim + 1) % 3) - GetVectorComponent(lowerPoint, (dim + 1) % 3)) -
                 Sqr(accumulativeBottom[upperBound] - GetVectorComponent(lowerPoint, (dim + 1) % 3)) -
                 Sqr(GetVectorComponent(upperPoint, (dim + 1) % 3) - accumulativeTop[upperBound]);

        p3 = Sqr(GetVectorComponent(upperPoint, (dim + 2) % 3) - GetVectorComponent(lowerPoint, (dim + 2) % 3)) -
                 Sqr(accumulativeBottom[upperBound + this->numOfSamples] - GetVectorComponent(lowerPoint, (dim + 2) % 3)) -
                 Sqr(GetVectorComponent(upperPoint, (dim + 2) % 3) - accumulativeTop[upperBound + this->numOfSamples]);

        double expectation2 = p1 * p2 * p3 * (to - upperBound + 1);

        double expectation = expectation1 + expectation2;
        if (bestExpectation < 0 || expectation < bestExpectation) {
            bestExpectation = expectation;
            bestUpperBound = upperBound;
        }
    }

    return GetVectorComponent(this->samples[sortArray[bestUpperBound - 1]], dim);
}
Ejemplo n.º 11
0
void ParameterisedCvode::EvaluateYDerivatives(double time, const N_Vector rY, N_Vector rDY)
{
    NV_Ith_S(rDY, 0) = GetVectorComponent(mParameters,0);
}