void TestCellBetaCateninWriterArchiving() throw (Exception)
    {
        // The purpose of this test is to check that archiving can be done for this class
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "CellBetaCateninWriter.arch";

        {
            AbstractCellBasedWriter<2,2>* const p_cell_writer = new CellBetaCateninWriter<2,2>();

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            output_arch << p_cell_writer;

            delete p_cell_writer;
        }

        {
            AbstractCellBasedWriter<2,2>* p_cell_writer_2;

            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            input_arch >> p_cell_writer_2;

            delete p_cell_writer_2;
       }
    }
    /**
     * HOW_TO_TAG General/Archiving
     * Use a binary rather than ascii boost archive format, for speed and smaller file sizes.
     *
     * (NB this file is actually larger, but when there's lots of data it should be smaller!)
     *
     * Note that a binary archive could become machine architecture-specific.
     * But it can be helpful to use them for speed. In this case we have had
     * success in storing an ascii one, loading and re-saving as binary.
     * Loading code could then look for the binary one, and revert to ascii if
     * it is not there.
     *
     * The test below is identical to the one above, apart from the two lines indicated.
     */
    void TestUsingABinaryArchive() throw (Exception)
    {
        OutputFileHandler handler("archive", false);
        std::string archive_filename;
        archive_filename = handler.GetOutputDirectoryFullPath() + "subchild_binary.arch";

        // Save
        {
            // Create an output archive
            std::ofstream ofs(archive_filename.c_str(), std::ios::binary);
            boost::archive::binary_oarchive output_arch(ofs); // LINE CHANGED!

            boost::shared_ptr<BaseClass> p_base(new SubChildClass());

            p_base->mTagInBaseClass = 6;

            output_arch << p_base;
        }

        // Load
        {
            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::binary_iarchive input_arch(ifs); // LINE CHANGED!

            boost::shared_ptr<BaseClass> p_base;

            input_arch >> p_base;

            TS_ASSERT_EQUALS(p_base->mTagInBaseClass, 6u);
            TS_ASSERT(dynamic_cast<ChildClass*>(p_base.get()));
            TS_ASSERT(dynamic_cast<SubChildClass*>(p_base.get()));
        }
    }
    void TestArchiveTysonNovakCellCycleModels()
    {
        // Set up
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "TysonNovakCellCycleModel.arch";

        {
            // We must set up SimulationTime to avoid memory leaks
            SimulationTime::Instance()->SetEndTimeAndNumberOfTimeSteps(1.0, 1);

            // As usual, we archive via a pointer to the most abstract class possible
            AbstractCellCycleModel* const p_model = new TysonNovakCellCycleModel;

            p_model->SetDimension(3);
            p_model->SetBirthTime(-1.5);
            static_cast<TysonNovakCellCycleModel*>(p_model)->SetDt(0.085);

            // We must create a cell to be able to initialise the cell cycle model's ODE system
            MAKE_PTR(WildTypeCellMutationState, p_healthy_state);
            MAKE_PTR(StemCellProliferativeType, p_stem_type);
            CellPtr p_cell(new Cell(p_healthy_state, p_model));
            p_cell->SetCellProliferativeType(p_stem_type);
            p_cell->InitialiseCellCycleModel();

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            output_arch << p_model;

            // Note that here, deletion of the cell-cycle model is handled by the cell destructor
            SimulationTime::Destroy();
        }

        {
            // We must set SimulationTime::mStartTime here to avoid tripping an assertion
            SimulationTime::Instance()->SetStartTime(0.0);

            AbstractCellCycleModel* p_model2;

            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            input_arch >> p_model2;

            TS_ASSERT_EQUALS(p_model2->GetDimension(), 3u);
            TS_ASSERT_DELTA(p_model2->GetBirthTime(), -1.5, 1e-12);
            TS_ASSERT_DELTA(static_cast<TysonNovakCellCycleModel*>(p_model2)->GetDt(), 0.085, 1e-3);

            TysonNovakCellCycleModel* p_static_cast_model =
                static_cast<TysonNovakCellCycleModel*>(p_model2);

            TysonNovak2001OdeSystem* p_ode_system =
                static_cast<TysonNovak2001OdeSystem*>(p_static_cast_model->GetOdeSystem());

            TS_ASSERT(p_ode_system != NULL);

            // Avoid memory leaks
            delete p_model2;
        }
    }
    void TestArchivingShovingCaBasedDivisionRule()
    {
        EXIT_IF_PARALLEL; // Beware of processes overwriting the identical archives of other processes
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "ShovingCaBasedDivisionRule.arch";

        {
            ShovingCaBasedDivisionRule<2> division_rule;

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Serialize via pointer to most abstract class possible
            AbstractCaBasedDivisionRule<2>* const p_division_rule = &division_rule;
            output_arch << p_division_rule;
        }

        {
            AbstractCaBasedDivisionRule<2>* p_division_rule;

            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            // Restore from the archive
            input_arch >> p_division_rule;

            TS_ASSERT(p_division_rule != NULL);

            // Tidy up
            delete p_division_rule;
        }
    }
    void TestArchivingBoostSharedPtrToChildUsingBaseClass() throw (Exception)
    {
        OutputFileHandler handler("archive", false);
        std::string archive_filename;
        archive_filename = handler.GetOutputDirectoryFullPath() + "shared_ptr_abs.arch";

        // Save
        {
            // Create an output archive
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            boost::shared_ptr<BaseClass> p_base(new ChildClass());

            p_base->mTagInBaseClass = 6;

            output_arch << p_base;
        }

        // Load
        {
            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            boost::shared_ptr<BaseClass> p_base;

            input_arch >> p_base;

            TS_ASSERT_EQUALS(p_base->mTagInBaseClass, 6u);
        }
    }
    void TestArchivingNullPointer() throw (Exception)
    {
        EXIT_IF_PARALLEL;

        OutputFileHandler handler("TestElementAttributes", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "null_element_attributes.arch";

        {
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            ElementAttributes<2,2>* const p_element_attributes = NULL;

            output_arch << p_element_attributes;

            delete p_element_attributes;
        }

        {
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            ElementAttributes<2,2>* p_element_attributes;
            input_arch >> p_element_attributes;

            TS_ASSERT(!p_element_attributes);

            delete p_element_attributes;
        }
    }
    void TestArchiveTimeStepper()
    {
        OutputFileHandler handler("TestTimeStepper_Archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "time.arch";

        // Create and archive time-stepper
        {

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            TimeStepper stepper(0.0, 1.0, 0.1, false);

            //Run for 5 steps
            for (int i=0; i<5; i++)
            {
                stepper.AdvanceOneTimeStep();
            }

            TS_ASSERT_DELTA(stepper.GetTime(), 0.5, 1e-10);
            TS_ASSERT_DELTA(stepper.GetNextTime(), 0.6, 1e-10);

            TimeStepper* const p_stepper_for_archiving = &stepper;
            output_arch << p_stepper_for_archiving;


            //Exhibit normal behaviour after archive snapshot
            stepper.AdvanceOneTimeStep();
            TS_ASSERT_DELTA(stepper.GetTime(), 0.6, 1e-10);
            TS_ASSERT_DELTA(stepper.GetNextTime(), 0.7, 1e-10);
            stepper.AdvanceOneTimeStep();
            TS_ASSERT_DELTA(stepper.GetTime(), 0.7, 1e-10);
            TS_ASSERT_DELTA(stepper.GetNextTime(), 0.8, 1e-10);

        }

        // Restore
        {
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            TimeStepper* p_stepper;
            input_arch >> p_stepper;

            TS_ASSERT_DELTA(p_stepper->GetTime(), 0.5, 1e-10);
            TS_ASSERT_DELTA(p_stepper->GetNextTime(), 0.6, 1e-10);

            p_stepper->AdvanceOneTimeStep();
            TS_ASSERT_DELTA(p_stepper->GetTime(), 0.6, 1e-10);
            TS_ASSERT_DELTA(p_stepper->GetNextTime(), 0.7, 1e-10);

            p_stepper->AdvanceOneTimeStep();
            TS_ASSERT_DELTA(p_stepper->GetTime(), 0.7, 1e-10);
            TS_ASSERT_DELTA(p_stepper->GetNextTime(), 0.8, 1e-10);

            delete p_stepper;
        }
    }
    void TestArchiving()
    {
        EXIT_IF_PARALLEL;
#ifdef CHASTE_CVODE
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "mirams_ode.arch";

        {
            double wnt_level = 0.5;
            boost::shared_ptr<AbstractCellMutationState> p_state(new WildTypeCellMutationState);
            Mirams2010WntOdeSystem ode_system(wnt_level, p_state);

            TS_ASSERT_DELTA(ode_system.GetWntLevel(), 0.50, 1e-6);
            TS_ASSERT_EQUALS(ode_system.GetMutationState()->IsType<WildTypeCellMutationState>(), true);

            std::vector<double> initial_conditions = ode_system.GetInitialConditions();
            TS_ASSERT_EQUALS(initial_conditions.size(), 3u);
            TS_ASSERT_DELTA(initial_conditions[0], 64.1863, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[1], 64.1863, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[2], 0.5, 1e-6);

            // Create an output archive
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Archive ODE system
            AbstractOdeSystem* const p_const_ode_system = &ode_system;
            output_arch << p_const_ode_system;
        }

        {
            AbstractOdeSystem* p_ode_system;

            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            // Restore from the archive
            input_arch >> p_ode_system;

            // Check that archiving worked correctly
            TS_ASSERT_DELTA(static_cast<Mirams2010WntOdeSystem*>(p_ode_system)->GetWntLevel(), 0.50, 1e-6);
            TS_ASSERT_EQUALS(static_cast<Mirams2010WntOdeSystem*>(p_ode_system)->GetMutationState()->IsType<WildTypeCellMutationState>(), true);

            std::vector<double> initial_conditions = p_ode_system->GetInitialConditions();
            TS_ASSERT_EQUALS(initial_conditions.size(), 3u);
            TS_ASSERT_DELTA(initial_conditions[0], 64.1863, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[1], 64.1863, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[2], 0.5, 1e-6);

            // Tidy up
            delete p_ode_system;
        }
#else
        std::cout << "CVODE is not enabled. " << std::endl;
        std::cout << "If required please install and alter your hostconfig settings to switch on chaste support." << std::endl;
#endif //CHASTE_CVODE
    }
    void TestSetGetFunctionsInAbstractOdeSystem()
    {
        TwoDimOdeSystem ode;

        std::vector<double> initial_conditions = ode.GetInitialConditions();
        std::vector<double>& r_state_variables = ode.rGetStateVariables();

        TS_ASSERT_DELTA(initial_conditions[0], 1.0, 1e-12);
        TS_ASSERT_DELTA(initial_conditions[1], 2.0, 1e-12);
        TS_ASSERT_DELTA(r_state_variables[0], 3.0, 1e-12);
        TS_ASSERT_DELTA(r_state_variables[1], 4.0, 1e-12);

        std::vector<double> new_initial_conditions;
        new_initial_conditions.push_back(5.0);
        new_initial_conditions.push_back(6.0);

        std::vector<double> new_state_variables;
        new_state_variables.push_back(7.0);
        new_state_variables.push_back(8.0);

        ode.SetDefaultInitialConditions(new_initial_conditions);
        ode.SetStateVariables(new_state_variables);

        initial_conditions = ode.GetInitialConditions();

        TS_ASSERT_DELTA(initial_conditions[0], 5.0, 1e-12);
        TS_ASSERT_DELTA(initial_conditions[1], 6.0, 1e-12);
        TS_ASSERT_DELTA(r_state_variables[0], 7.0, 1e-12);
        TS_ASSERT_DELTA(r_state_variables[1], 8.0, 1e-12);

        ode.SetDefaultInitialCondition(1, 9.0);
        initial_conditions = ode.GetInitialConditions();
        TS_ASSERT_DELTA(initial_conditions[0], 5.0, 1e-12);
        TS_ASSERT_DELTA(initial_conditions[1], 9.0, 1e-12);

        // Archive the ODE system
        OutputFileHandler handler("archive", false);
        std::string archive_filename;
        archive_filename = handler.GetOutputDirectoryFullPath() + "ode.arch";
        std::ofstream ofs(archive_filename.c_str());
        boost::archive::text_oarchive output_arch(ofs);

        output_arch <<  static_cast<const TwoDimOdeSystem&>(ode);
        TS_ASSERT_DELTA(r_state_variables[0], 7.0, 1e-12);
        TS_ASSERT_DELTA(r_state_variables[1], 8.0, 1e-12);

        ode.SetStateVariable(0, 2.0);
        ode.SetStateVariable(1, 5.0);

        TS_ASSERT_THROWS_THIS(ode.SetStateVariable(2, 1.0),
                "The index passed in must be less than the number of state variables."); //cover exception

        TS_ASSERT_DELTA(r_state_variables[0], 2.0, 1e-12);
        TS_ASSERT_DELTA(r_state_variables[1], 5.0, 1e-12);
    }
    void TestArchiveCellPropertyCollection() throw (Exception)
    {
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "properties.arch";

        // Archive a cell property collection
        {
            // Create a cell property collection
            CellPropertyCollection collection;

            NEW_PROP(BetaCateninOneHitCellMutationState, p_bcat1_mutation);
            collection.AddProperty(p_bcat1_mutation);
            NEW_PROP(ApcOneHitCellMutationState, p_apc1_mutation);
            collection.AddProperty(p_apc1_mutation);

            // Create an output archive
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Write the cell to the archive
            output_arch << static_cast<const CellPropertyCollection>(collection);
        }

        // Restore cell property collection
        {
            CellPropertyCollection collection;

            // Restore the cell property collection
            std::ifstream ifs(archive_filename.c_str());
            boost::archive::text_iarchive input_arch(ifs);

            input_arch >> collection;

            // Test that the collection was archived correctly
            TS_ASSERT_EQUALS(collection.GetSize(), 2u);

            TS_ASSERT_EQUALS(collection.HasProperty<BetaCateninOneHitCellMutationState>(), true);
            TS_ASSERT_EQUALS(collection.HasProperty<ApcOneHitCellMutationState>(), true);
            TS_ASSERT_EQUALS(collection.HasProperty<ApcTwoHitCellMutationState>(), false);

            NEW_PROP(ApcOneHitCellMutationState, p_apc1_mutation_2);
            TS_ASSERT_EQUALS(collection.HasProperty(p_apc1_mutation_2), false);

            TS_ASSERT_EQUALS(collection.HasPropertyType<AbstractCellProperty>(), true);
            TS_ASSERT_EQUALS(collection.HasPropertyType<AbstractCellMutationState>(), true);

            for (CellPropertyCollection::Iterator it = collection.Begin(); it != collection.End(); ++it)
            {
                TS_ASSERT_EQUALS(collection.HasProperty(*it), true);
                TS_ASSERT((*it)->IsType<BetaCateninOneHitCellMutationState>() || (*it)->IsType<ApcOneHitCellMutationState>());
                TS_ASSERT_EQUALS((*it)->IsSubType<AbstractCellMutationState>(), true);
            }
        }
    }
    void TestArchivingLinkedChildAndParent() throw (Exception)
    {
        /*
         * This test is an abstraction of archiving a cyclically linked parent-child pair.
         * The parent represents a Cell and the child represents an AbstractCellCycleModel.
         */

        OutputFileHandler handler("archive",false);
        std::string archive_filename;
        archive_filename = handler.GetOutputDirectoryFullPath() + "linked_classes.arch";

        // Save
        {
            // Create an output archive
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            ChildClass* p_child = new ChildClass;
            ParentClass* p_parent = new ParentClass(p_child);

            p_child->mTag = 11;
            p_parent->mTag = 10;

            ParentClass* const p_parent_for_archiving = p_parent;
            //ChildClass* const p_child_for_archiving = p_child;

            //output_arch << p_child_for_archiving;
            output_arch << p_parent_for_archiving;

            delete p_child;
            delete p_parent;
        }

        // Load
        {
            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            //ChildClass* p_child;
            ParentClass* p_parent;

            input_arch >> p_parent;

            TS_ASSERT_EQUALS(p_parent->mTag, 10u);
            TS_ASSERT_EQUALS(p_parent->mpChild->mTag, 11u);
            TS_ASSERT_EQUALS(p_parent->mpChild->mpParent, p_parent);

            // Tidy up
            delete p_parent->mpChild;
            delete p_parent;
        }
    }
    void TestArchivingRkfSolver() throw(Exception)
    {
        OutputFileHandler handler("archive",false);
        std::string archive_filename;
        archive_filename = handler.GetOutputDirectoryFullPath() + "rkf_solver.arch";

        Ode5Jacobian ode_system;
        OdeSolution solutions;
        double h_value = 0.1;
        double end_time = 1.0;

        // Create and archive simulation time
        {
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Set up a solver
            AbstractIvpOdeSolver* const p_rkf_ode_solver = new RungeKuttaFehlbergIvpOdeSolver;


            // Should always archive a pointer
            output_arch << p_rkf_ode_solver;

            // Change stimulus a bit
            delete p_rkf_ode_solver;
        }

        // Restore
        {
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            // Create a pointer
            AbstractIvpOdeSolver* p_rkf;
            input_arch >> p_rkf;

            std::vector<double> state_variables = ode_system.GetInitialConditions();

            solutions = p_rkf->Solve(&ode_system, state_variables, 0.0, end_time, h_value, 1e-5);
            unsigned last = solutions.GetNumberOfTimeSteps();

            double numerical_solution;
            numerical_solution = solutions.rGetSolutions()[last][0];

            // The tests
            double analytical_solution = 1.0/(1.0+4.0*exp(-100.0*end_time));

            TS_ASSERT_DELTA(numerical_solution,analytical_solution,1.0e-3);

            delete p_rkf;
        }
    }
    void TestArchiving()
    {
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "tn_ode.arch";

        {
            TysonNovak2001OdeSystem ode_system;

            ode_system.SetDefaultInitialCondition(2, 3.25);

            std::vector<double> initial_conditions = ode_system.GetInitialConditions();
            TS_ASSERT_EQUALS(initial_conditions.size(), 6u);
            TS_ASSERT_DELTA(initial_conditions[0], 0.0999, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[1], 0.9890, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[2], 3.2500, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[3], 1.4211, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[4], 0.6728, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[5], 0.4854, 1e-4);

            // Create an output archive
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Archive ODE system
            AbstractOdeSystem* const p_const_ode_system = &ode_system;
            output_arch << p_const_ode_system;
        }

        {
            AbstractOdeSystem* p_ode_system;

            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            // Restore from the archive
            input_arch >> p_ode_system;

            // Check that archiving worked correctly
            std::vector<double> initial_conditions = p_ode_system->GetInitialConditions();
            TS_ASSERT_EQUALS(initial_conditions.size(), 6u);
            TS_ASSERT_DELTA(initial_conditions[0], 0.0999, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[1], 0.9890, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[2], 3.2500, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[3], 1.4211, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[4], 0.6728, 1e-4);
            TS_ASSERT_DELTA(initial_conditions[5], 0.4854, 1e-4);

            // Tidy up
            delete p_ode_system;
        }
    }
Example #14
0
    void TestArchivingPoint()
    {
        OutputFileHandler handler("archive",false);
        std::string archive_filename;
        archive_filename = handler.GetOutputDirectoryFullPath() + "points.arch";

        // Create and archive
        {
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            ChastePoint<3>* const p_point_3d = new ChastePoint<3>(-3.0, -2.0, -1.0);
            ChastePoint<2>* const p_point_2d = new ChastePoint<2>(-33.0, -22.0);
            ChastePoint<1>* const p_point_1d = new ChastePoint<1>(-185.0);

            // Should always archive a pointer
            output_arch << p_point_3d;
            output_arch << p_point_2d;
            output_arch << p_point_1d;

            delete p_point_3d;
            delete p_point_2d;
            delete p_point_1d;

        }

        // Restore
        {
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            // Create pointer to regions
            ChastePoint<3>* p_point_3d;
            ChastePoint<2>* p_point_2d;
            ChastePoint<1>* p_point_1d;
            input_arch >> p_point_3d;
            input_arch >> p_point_2d;
            input_arch >> p_point_1d;

            TS_ASSERT_EQUALS((*p_point_3d)[0], -3.0);
            TS_ASSERT_EQUALS((*p_point_3d)[1], -2.0);
            TS_ASSERT_EQUALS((*p_point_3d)[2], -1.0);
            TS_ASSERT_EQUALS((*p_point_2d)[0], -33.0);
            TS_ASSERT_EQUALS((*p_point_2d)[1], -22.0);
            TS_ASSERT_EQUALS((*p_point_1d)[0], -185.0);

            delete p_point_3d;
            delete p_point_2d;
            delete p_point_1d;
        }
    }
    void TestArchivingOfRadialSloughingCellKiller() throw (Exception)
    {
        // Set up
        OutputFileHandler handler("archive", false);    // don't erase contents of folder
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "radial_killer.arch";

        c_vector<double,2> centre(2);
        centre[0] = 0.1;
        centre[1] = 0.2;

        double radius = 0.4;

        {
            // Create an output archive
            RadialSloughingCellKiller cell_killer(NULL, centre, radius);

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Serialize via pointer
            RadialSloughingCellKiller* const p_cell_killer = &cell_killer;
            output_arch << p_cell_killer;

            TS_ASSERT_DELTA(p_cell_killer->GetCentre()[0], 0.1, 1e-9);
            TS_ASSERT_DELTA(p_cell_killer->GetCentre()[1], 0.2, 1e-9);
            TS_ASSERT_DELTA(p_cell_killer->GetRadius(), 0.4, 1e-9);
        }

        // Change centre and radius prior to restoring the cell killer
        centre[0] = 0.0;
        centre[1] = 0.0;
        radius = 0.0;

        {
            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            RadialSloughingCellKiller* p_cell_killer;

            // Restore from the archive
            input_arch >> p_cell_killer;

            // Test we have restored the sloughing properties correctly
            TS_ASSERT_DELTA(p_cell_killer->GetCentre()[0], 0.1, 1e-9);
            TS_ASSERT_DELTA(p_cell_killer->GetCentre()[1], 0.2, 1e-9);
            TS_ASSERT_DELTA(p_cell_killer->GetRadius(), 0.4, 1e-9);

            delete p_cell_killer;
        }
    }
    void TestArchivingSolver() throw(Exception)
    {
        OutputFileHandler handler("archive", false);
        ArchiveLocationInfo::SetArchiveDirectory(handler.FindFile(""));
        std::string archive_filename = ArchiveLocationInfo::GetProcessUniqueFilePath("backward_euler_solver.arch");

        VanDerPolOde ode_system;

        double h_value = 0.01;
        double end_time = 100.0;

        // Create and archive simulation time
        {
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Set up a solver
            AbstractIvpOdeSolver* const p_backward_euler_solver = new BackwardEulerIvpOdeSolver(ode_system.GetNumberOfStateVariables());

            // Should always archive a pointer
            output_arch << p_backward_euler_solver;

            // Change stimulus a bit
            delete p_backward_euler_solver;
        }

        // Restore
        {
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            // Create a pointer
            AbstractIvpOdeSolver* p_backward_euler;
            input_arch >> p_backward_euler;
            OdeSolution solutions;

            std::vector<double> state_variables = ode_system.GetInitialConditions();

            solutions = p_backward_euler->Solve(&ode_system, state_variables, 0.0, end_time, h_value, 5*h_value);
            unsigned last = solutions.GetNumberOfTimeSteps();

            // assert that we are within a [-2,2] in x and [-2,2] in y (on limit cycle)
            TS_ASSERT_DELTA(solutions.rGetSolutions()[last][0], 0, 2);
            TS_ASSERT_DELTA(solutions.rGetSolutions()[last][1], 0, 2);

            delete p_backward_euler;
        }
    }
    void TestArchivingMockEulerSolver() throw(Exception)
    {
        EXIT_IF_PARALLEL;

        OutputFileHandler handler("archive", false);
        std::string archive_filename;
        archive_filename = handler.GetOutputDirectoryFullPath() + "mock_euler_solver.arch";

        // Create and archive simulation time
        {
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Set up a solver
            AbstractIvpOdeSolver* const p_mock_euler_ivp_ode_solver = new MockEulerIvpOdeSolver;


            Ode1 ode_system;
            p_mock_euler_ivp_ode_solver->SolveAndUpdateStateVariable(&ode_system, 0, 1, 0.01);
            TS_ASSERT_DELTA(ode_system.rGetStateVariables()[0], 1.0, 1e-2);

            // Should always archive a pointer
            output_arch << p_mock_euler_ivp_ode_solver;

            // Change stimulus a bit
            delete p_mock_euler_ivp_ode_solver;
        }

        // Restore
        {
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            // Create a pointer
            AbstractIvpOdeSolver* p_mock_euler;
            input_arch >> p_mock_euler;

            Ode1 ode_system;
            p_mock_euler->SolveAndUpdateStateVariable(&ode_system, 0, 1, 0.01);
            TS_ASSERT_DELTA(ode_system.rGetStateVariables()[0], 1.0, 1e-2);

            TS_ASSERT_EQUALS(static_cast<MockEulerIvpOdeSolver&> (*p_mock_euler).GetCallCount(), 2u);

            delete p_mock_euler;
        }
    }
    void TestArchiving(void) throw(Exception)
    {
        //Archive
        OutputFileHandler handler("archive", false);
        handler.SetArchiveDirectory();
        std::string archive_filename =  ArchiveLocationInfo::GetProcessUniqueFilePath("GI.arch");

        // Save
        {

            boost::shared_ptr<SimpleStimulus> p_stimulus(new SimpleStimulus(0.0,1.0,0.5));
            boost::shared_ptr<EulerIvpOdeSolver> p_solver(new EulerIvpOdeSolver);
            double time_step = 0.01;
            HeartConfig::Instance()->SetOdePdeAndPrintingTimeSteps(time_step, time_step, time_step);

            // icc and smc
            AbstractCardiacCell* const p_smc = new CorriasBuistSMCModified(p_solver, p_stimulus);
            AbstractCardiacCell* const p_icc = new CorriasBuistICCModified(p_solver, p_stimulus);

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            output_arch <<  p_smc;
            output_arch <<  p_icc;

            delete p_smc;
            delete p_icc;
        }
        // Load
        {
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            AbstractCardiacCell* p_smc;
            AbstractCardiacCell* p_icc;
            input_arch >> p_smc;
            input_arch >> p_icc;

            TS_ASSERT_EQUALS( p_smc->GetNumberOfStateVariables(), 14U );
            TS_ASSERT_EQUALS( p_icc->GetNumberOfStateVariables(), 18U );

            delete p_smc;
            delete p_icc;
        }
     }
    void TestArchiveCellMutationState() throw(Exception)
    {
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "cell_mutation_state.arch";

        // Archive a mutation state
        {
            ApcOneHitCellMutationState* p_state = new ApcOneHitCellMutationState();
            p_state->IncrementCellCount();

            TS_ASSERT_EQUALS(p_state->GetCellCount(), 1u);
            TS_ASSERT_EQUALS(p_state->GetColour(), 3u);

            // Create an output archive
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Write the cell to the archive
            const AbstractCellProperty* const p_const_state = p_state;
            output_arch << p_const_state;

            delete p_state;
        }

        // Restore mutation state
        {
            AbstractCellProperty* p_state;

            // Restore the mutation state
            std::ifstream ifs(archive_filename.c_str());
            boost::archive::text_iarchive input_arch(ifs);

            input_arch >> p_state;

            TS_ASSERT_EQUALS(p_state->GetCellCount(), 1u);

            ApcOneHitCellMutationState* p_real_state = dynamic_cast<ApcOneHitCellMutationState*>(p_state);
            TS_ASSERT(p_real_state != NULL);
            TS_ASSERT_EQUALS(p_real_state->GetColour(), 3u);

            // Tidy up
            delete p_state;
        }
    }
Example #20
0
    void TestArchiveDifferentialAdhesionPottsUpdateRule() throw(Exception)
    {
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "AdhesionPottsUpdateRule.arch";

        {
            DifferentialAdhesionPottsUpdateRule<2> update_rule;

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Set member variables
            update_rule.SetLabelledCellLabelledCellAdhesionEnergyParameter(0.3);
            update_rule.SetLabelledCellCellAdhesionEnergyParameter(0.4);
            update_rule.SetCellCellAdhesionEnergyParameter(0.5);
            update_rule.SetLabelledCellBoundaryAdhesionEnergyParameter(0.6);
            update_rule.SetCellBoundaryAdhesionEnergyParameter(0.7);

            // Serialize via pointer to most abstract class possible
            AbstractPottsUpdateRule<2>* const p_update_rule = &update_rule;
            output_arch << p_update_rule;
        }

        {
            AbstractPottsUpdateRule<2>* p_update_rule;

            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            // Restore from the archive
            input_arch >> p_update_rule;

            // Test the member data
            TS_ASSERT_DELTA((static_cast<DifferentialAdhesionPottsUpdateRule<2>*>(p_update_rule))->GetLabelledCellLabelledCellAdhesionEnergyParameter(), 0.3, 1e-6);
            TS_ASSERT_DELTA((static_cast<DifferentialAdhesionPottsUpdateRule<2>*>(p_update_rule))->GetLabelledCellCellAdhesionEnergyParameter(), 0.4, 1e-6);
            TS_ASSERT_DELTA((static_cast<DifferentialAdhesionPottsUpdateRule<2>*>(p_update_rule))->GetCellCellAdhesionEnergyParameter(), 0.5, 1e-6);
            TS_ASSERT_DELTA((static_cast<DifferentialAdhesionPottsUpdateRule<2>*>(p_update_rule))->GetLabelledCellBoundaryAdhesionEnergyParameter(), 0.6, 1e-6);
            TS_ASSERT_DELTA((static_cast<DifferentialAdhesionPottsUpdateRule<2>*>(p_update_rule))->GetCellBoundaryAdhesionEnergyParameter(), 0.7, 1e-6);

            // Tidy up
            delete p_update_rule;
        }
    }
    void TestArchivingOfSloughingCellKiller() throw (Exception)
    {
        // Set up singleton classes
        OutputFileHandler handler("archive", false);    // don't erase contents of folder
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "sloughing_killer.arch";

        {
            // Create an output archive
            SloughingCellKiller<2> cell_killer(NULL, 10.0, true, 5.0);

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Serialize via pointer
            SloughingCellKiller<2>* const p_cell_killer = &cell_killer;
            output_arch << p_cell_killer;

            TS_ASSERT_EQUALS(p_cell_killer->GetSloughSides(), true);
            TS_ASSERT_DELTA(p_cell_killer->GetSloughHeight(), 10.0, 1e-9);
            TS_ASSERT_DELTA(p_cell_killer->GetSloughWidth(), 5.0, 1e-9);
        }

        {
            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            SloughingCellKiller<2>* p_cell_killer;

            // Restore from the archive
            input_arch >> p_cell_killer;

            // Test we have restored the sloughing properties correctly
            TS_ASSERT_EQUALS(p_cell_killer->GetSloughSides(), true);
            TS_ASSERT_DELTA(p_cell_killer->GetSloughHeight(), 10.0, 1e-9);
            TS_ASSERT_DELTA(p_cell_killer->GetSloughWidth(), 5.0, 1e-9);

            delete p_cell_killer;
        }
    }
    void TestCellProliferativeTypesCountWriterArchiving() throw (Exception)
    {
        // The purpose of this test is to check that archiving can be done for this class
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "CellProliferativeTypesCountWriter.arch";

        {
            AbstractCellBasedWriter<2,2>* const p_population_writer = new CellProliferativeTypesCountWriter<2,2>();
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);
            output_arch << p_population_writer;
            delete p_population_writer;
        }
        PetscTools::Barrier(); //Processes read after last process has (over-)written archive
        {
            AbstractCellBasedWriter<2,2>* p_population_writer_2;
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);
            input_arch >> p_population_writer_2;
            delete p_population_writer_2;
       }
    }
    void TestArchiving() throw(Exception)
    {
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "ode_solver.arch";

        typedef CellCycleModelOdeSolver<Alarcon2004OxygenBasedCellCycleModel, BackwardEulerIvpOdeSolver> EulerSolver;

        // Create an output archive
        {
            boost::shared_ptr<EulerSolver> p_solver = EulerSolver::Instance();
            p_solver->SetSizeOfOdeSystem(4);
            p_solver->Initialise();

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            output_arch << p_solver;

            TS_ASSERT_EQUALS(p_solver->GetSizeOfOdeSystem(), 4u);
            p_solver->Reset();
            TS_ASSERT_EQUALS(p_solver->GetSizeOfOdeSystem(), UNSIGNED_UNSET);
        }

        {
            boost::shared_ptr<EulerSolver> p_solver = EulerSolver::Instance();

            TS_ASSERT_EQUALS(p_solver->GetSizeOfOdeSystem(), UNSIGNED_UNSET);

            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            // Restore from the archive
            input_arch >> p_solver;

            TS_ASSERT_EQUALS(p_solver->GetSizeOfOdeSystem(), 4u);
        }
    }
Example #24
0
    void TestArchiveSurfaceAreaConstraintPottsUpdateRule() throw(Exception)
    {
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "SurfaceAreaConstraintPottsUpdateRule.arch";

        {
            SurfaceAreaConstraintPottsUpdateRule<2> update_rule;

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Set member variables
            update_rule.SetDeformationEnergyParameter(0.5);
            update_rule.SetMatureCellTargetSurfaceArea(0.6);

            // Serialize via pointer to most abstract class possible
            AbstractPottsUpdateRule<2>* const p_update_rule = &update_rule;
            output_arch << p_update_rule;
        }

        {
            AbstractPottsUpdateRule<2>* p_update_rule;

            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            // Restore from the archive
            input_arch >> p_update_rule;

            // Test the member data
            TS_ASSERT_DELTA((static_cast<SurfaceAreaConstraintPottsUpdateRule<2>*>(p_update_rule))->GetDeformationEnergyParameter(), 0.5, 1e-6);
            TS_ASSERT_DELTA((static_cast<SurfaceAreaConstraintPottsUpdateRule<2>*>(p_update_rule))->GetMatureCellTargetSurfaceArea(), 0.6, 1e-6);

            // Tidy up
            delete p_update_rule;
        }
    }
    void TestSimpleTargetAreaModifierArchiving()
    {
        // Create a file for archiving
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "SimpleTargetAreaModifier.arch";

        // Separate scope to write the archive
        {
            // Initialise a growth modifier and set a non-standard mature target area
            AbstractCellBasedSimulationModifier<2,2>* const p_modifier = new SimpleTargetAreaModifier<2>();
            (static_cast<SimpleTargetAreaModifier<2>*>(p_modifier))->SetReferenceTargetArea(14.3);

            // Create an output archive
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Serialize via pointer
            output_arch << p_modifier;
            delete p_modifier;
        }

        // Separate scope to read the archive
        {
            AbstractCellBasedSimulationModifier<2,2>* p_modifier2;

            // Restore the modifier
            std::ifstream ifs(archive_filename.c_str());
            boost::archive::text_iarchive input_arch(ifs);

            input_arch >> p_modifier2;

            // See whether we read out the correct target area
            double mature_target_area = (static_cast<SimpleTargetAreaModifier<2>*>(p_modifier2))->GetReferenceTargetArea();
            TS_ASSERT_DELTA(mature_target_area, 14.3, 1e-9);

            delete p_modifier2;
        }
    }
    void TestArchiving() throw (Exception)
    {
        EXIT_IF_PARALLEL;

        OutputFileHandler handler("TestElementAttributes", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "element_attributes.arch";

        {
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            ElementAttributes<2,2>* p_element_attributes = new ElementAttributes<2,2>();

            p_element_attributes->AddAttribute(2.34);
            p_element_attributes->AddAttribute(5.67);

            ElementAttributes<2,2>* const p_const_element_attributes = p_element_attributes;

            output_arch << p_const_element_attributes;

            delete p_element_attributes;
        }

        {
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            ElementAttributes<2,2>* p_element_attributes;
            input_arch >> p_element_attributes;

            TS_ASSERT_EQUALS(p_element_attributes->rGetAttributes().size(), 2u);
            TS_ASSERT_DELTA(p_element_attributes->rGetAttributes()[0], 2.34, 1e-10);
            TS_ASSERT_DELTA(p_element_attributes->rGetAttributes()[1], 5.67, 1e-10);

            delete p_element_attributes;
        }
    }
Example #27
0
    void TestArchiveChemotaxisPottsUpdateRule() throw(Exception)
    {
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "ChemotaxisPottsUpdateRule.arch";

        {
            ChemotaxisPottsUpdateRule<2> update_rule;

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Set member variables
            // Currently none to test

            // Serialize via pointer to most abstract class possible
            AbstractPottsUpdateRule<2>* const p_update_rule = &update_rule;
            output_arch << p_update_rule;
        }

        {
            AbstractPottsUpdateRule<2>* p_update_rule;

            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            // Restore from the archive
            input_arch >> p_update_rule;

            // Test the member data
            // Currently none to test

            // Tidy up
            delete p_update_rule;
        }
    }
    void TestArchiving2d() throw (Exception)
    {
        // Set up singleton classes
        OutputFileHandler handler("archive", false); // don't erase contents of folder
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "CryptSimulationBoundaryCondition2.arch";

        {
            // Create an output archive
            CryptSimulationBoundaryCondition<2> boundary_condition(NULL);
            boundary_condition.SetUseJiggledBottomCells(true);

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Serialize via pointer
            AbstractCellPopulationBoundaryCondition<2>* const p_boundary_condition = &boundary_condition;
            output_arch << p_boundary_condition;
        }

        {
            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            AbstractCellPopulationBoundaryCondition<2>* p_boundary_condition;

            // Restore from the archive
            input_arch >> p_boundary_condition;

            // Test we have restored the plane geometry correctly
            TS_ASSERT_EQUALS(static_cast<CryptSimulationBoundaryCondition<2>*>(p_boundary_condition)->GetUseJiggledBottomCells(), true);

            // Tidy up
            delete p_boundary_condition;
       }
    }
Example #29
0
    void TestArchiveRandomCaSwitchingUpdateRule()
    {
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "RandomCaSwitchingUpdateRule.arch";

        {
            RandomCaSwitchingUpdateRule<2> update_rule;

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            // Set member variables
            update_rule.SetSwitchingParameter(1.0);

            // Serialize via pointer to most abstract class possible
            AbstractCaSwitchingUpdateRule<2>* const p_update_rule = &update_rule;
            output_arch << p_update_rule;
        }

        {
            AbstractCaSwitchingUpdateRule<2>* p_update_rule;

            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            // Restore from the archive
            input_arch >> p_update_rule;

            // Test the member data
            TS_ASSERT_DELTA((static_cast<RandomCaSwitchingUpdateRule<2>*>(p_update_rule))->GetSwitchingParameter(), 1.0, 1e-6);

            // Tidy up
            delete p_update_rule;
        }
    }
    void TestArchivingBoostSharedPtrToChild() throw (Exception)
    {
        OutputFileHandler handler("archive",false);
        std::string archive_filename;
        archive_filename = handler.GetOutputDirectoryFullPath() + "shared_ptr.arch";

        // Save
        {
            // Create an output archive
            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);

            boost::shared_ptr<ChildClass> p_child(new ChildClass);

            p_child->mTag = 11;
            p_child->mTagInBaseClass = 3;

            boost::shared_ptr<ChildClass> const p_child_for_archiving = p_child;

            output_arch << p_child_for_archiving;
        }

        // Load
        {
            // Create an input archive
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);

            boost::shared_ptr<ChildClass> p_child;

            input_arch >> p_child;

            TS_ASSERT_EQUALS(p_child->mTag, 11u);
            TS_ASSERT_EQUALS(p_child->mTagInBaseClass, 3u);
        }
    }