void TestCellwiseSourcePdeArchiving() throw(Exception) { EXIT_IF_PARALLEL; // Set up simulation time SimulationTime::Instance()->SetEndTimeAndNumberOfTimeSteps(1.0, 1); // Set up cell population HoneycombMeshGenerator generator(5, 5, 0); MutableMesh<2,2>* p_mesh = generator.GetMesh(); std::vector<CellPtr> cells; CellsGenerator<FixedDurationGenerationBasedCellCycleModel, 2> cells_generator; cells_generator.GenerateBasic(cells, p_mesh->GetNumNodes()); MeshBasedCellPopulation<2> cell_population(*p_mesh, cells); FileFinder archive_dir("archive", RelativeTo::ChasteTestOutput); std::string archive_file = "CellwiseSourcePde.arch"; ArchiveLocationInfo::SetMeshFilename("CellwiseSourcePde"); { // Create a PDE object AbstractLinearEllipticPde<2,2>* const p_pde = new CellwiseSourcePde<2>(cell_population, 0.05); // Create output archive and archive PDE object ArchiveOpener<boost::archive::text_oarchive, std::ofstream> arch_opener(archive_dir, archive_file); boost::archive::text_oarchive* p_arch = arch_opener.GetCommonArchive(); (*p_arch) << p_pde; delete p_pde; } { AbstractLinearEllipticPde<2,2>* p_pde; // Create an input archive and restore PDE object from archive ArchiveOpener<boost::archive::text_iarchive, std::ifstream> arch_opener(archive_dir, archive_file); boost::archive::text_iarchive* p_arch = arch_opener.GetCommonArchive(); (*p_arch) >> p_pde; // Test that the PDE and its member variables were archived correctly TS_ASSERT(dynamic_cast<CellwiseSourcePde<2>*>(p_pde) != NULL); CellwiseSourcePde<2>* p_static_cast_pde = static_cast<CellwiseSourcePde<2>*>(p_pde); TS_ASSERT_DELTA(p_static_cast_pde->GetCoefficient(), 0.05, 1e-6); TS_ASSERT_EQUALS(p_static_cast_pde->mrCellPopulation.GetNumRealCells(), 25u); // Avoid memory leaks delete &(p_static_cast_pde->mrCellPopulation); delete p_pde; } }
void TestArchiveAdhesionPottsUpdateRule() throw(Exception) { OutputFileHandler handler("archive", false); std::string archive_filename = handler.GetOutputDirectoryFullPath() + "AdhesionPottsUpdateRule.arch"; { AdhesionPottsUpdateRule<2> update_rule; std::ofstream ofs(archive_filename.c_str()); boost::archive::text_oarchive output_arch(ofs); // Set member variables update_rule.SetCellCellAdhesionEnergyParameter(0.5); update_rule.SetCellBoundaryAdhesionEnergyParameter(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<AdhesionPottsUpdateRule<2>*>(p_update_rule))->GetCellCellAdhesionEnergyParameter(), 0.5, 1e-6); TS_ASSERT_DELTA((static_cast<AdhesionPottsUpdateRule<2>*>(p_update_rule))->GetCellBoundaryAdhesionEnergyParameter(), 0.6, 1e-6); // Tidy up delete p_update_rule; } }
void TestDerivedQuantities() throw (Exception) { ParameterisedOde ode; boost::shared_ptr<const AbstractOdeSystemInformation> p_info = ode.GetSystemInformation(); TS_ASSERT_EQUALS(ode.GetNumberOfDerivedQuantities(), 1u); TS_ASSERT_EQUALS(ode.HasDerivedQuantity("2a_plus_y"), true); TS_ASSERT_EQUALS(ode.HasDerivedQuantity("Not_there"), false); TS_ASSERT_EQUALS(ode.GetDerivedQuantityIndex("2a_plus_y"), 0u); TS_ASSERT_EQUALS(ode.GetDerivedQuantityUnits(0u), "dimensionless"); TS_ASSERT_EQUALS(ode.rGetDerivedQuantityNames().size(), 1u); TS_ASSERT_EQUALS(ode.rGetDerivedQuantityUnits().size(), 1u); TS_ASSERT_EQUALS(ode.rGetDerivedQuantityNames()[0], "2a_plus_y"); TS_ASSERT_EQUALS(ode.rGetDerivedQuantityUnits()[0], "dimensionless"); TS_ASSERT_EQUALS(p_info->HasDerivedQuantity("2a_plus_y"), true); TS_ASSERT_EQUALS(p_info->HasDerivedQuantity("Not_there"), false); TS_ASSERT_EQUALS(p_info->GetDerivedQuantityIndex("2a_plus_y"), 0u); TS_ASSERT_EQUALS(p_info->GetDerivedQuantityUnits(0u), "dimensionless"); TS_ASSERT_EQUALS(p_info->rGetDerivedQuantityNames().size(), 1u); TS_ASSERT_EQUALS(p_info->rGetDerivedQuantityUnits().size(), 1u); TS_ASSERT_EQUALS(p_info->rGetDerivedQuantityNames()[0], "2a_plus_y"); TS_ASSERT_EQUALS(p_info->rGetDerivedQuantityUnits()[0], "dimensionless"); TS_ASSERT_EQUALS(ode.HasAnyVariable("2a_plus_y"), true); TS_ASSERT_EQUALS(ode.HasAnyVariable("Not_there"), false); TS_ASSERT_EQUALS(ode.GetAnyVariableIndex("2a_plus_y"), 2u); TS_ASSERT_EQUALS(ode.GetAnyVariableUnits(2u), "dimensionless"); TS_ASSERT_EQUALS(p_info->GetAnyVariableIndex("2a_plus_y"), 2u); TS_ASSERT_EQUALS(p_info->GetAnyVariableUnits(2u), "dimensionless"); std::vector<double> derived = ode.ComputeDerivedQuantitiesFromCurrentState(0.0); double a = ode.GetParameter(0); TS_ASSERT_EQUALS(a, 0.0); TS_ASSERT_DELTA(derived[0], 2*a, 1e-4); TS_ASSERT_DELTA(ode.GetAnyVariable(2u, 0.0), 2*a, 1e-4); TS_ASSERT_DELTA(ode.GetAnyVariable(2u, 0.0, &derived), 2*a, 1e-4); a = 1.0; ode.SetParameter(0, a); derived = ode.ComputeDerivedQuantities(0.0, ode.GetInitialConditions()); TS_ASSERT_DELTA(derived[0], 2*a, 1e-4); double y = 10.0; ode.SetStateVariable(0, y); derived = ode.ComputeDerivedQuantitiesFromCurrentState(0.0); TS_ASSERT_DELTA(derived[0], 2*a+y, 1e-4); TS_ASSERT_DELTA(ode.GetAnyVariable(2u, 1.0/* ignored for this ODE */), 2*a+y, 1e-4); // Exceptions TS_ASSERT_THROWS_THIS(ode.GetDerivedQuantityIndex("Missing"), "No derived quantity named 'Missing'."); TS_ASSERT_THROWS_THIS(ode.GetDerivedQuantityUnits(1u), "The index passed in must be less than the number of derived quantities."); TwoDimOdeSystem ode2; TS_ASSERT_THROWS_THIS(ode2.ComputeDerivedQuantitiesFromCurrentState(0.0), "This ODE system does not define derived quantities."); std::vector<double> doesnt_matter; TS_ASSERT_THROWS_THIS(ode2.ComputeDerivedQuantities(0.0, doesnt_matter), "This ODE system does not define derived quantities."); }
void TestMirams2010WntOdeSystemSetup() throw(Exception) { #ifdef CHASTE_CVODE double wnt_level = 0.5; boost::shared_ptr<AbstractCellMutationState> p_state(new WildTypeCellMutationState); Mirams2010WntOdeSystem wnt_system(wnt_level, p_state); // Solve system using CVODE solver // Matlab's strictest bit uses 0.01 below and relaxes it on flatter bits. double h_value = 0.1; CvodeAdaptor cvode_solver; OdeSolution solutions; //OdeSolution solutions2; std::vector<double> initial_conditions = wnt_system.GetInitialConditions(); double start_time, end_time, elapsed_time = 0.0; start_time = (double) std::clock(); solutions = cvode_solver.Solve(&wnt_system, initial_conditions, 0.0, 100.0, h_value, h_value); end_time = (double) std::clock(); elapsed_time = (end_time - start_time)/(CLOCKS_PER_SEC); std::cout << "1. Cvode Elapsed time = " << elapsed_time << " secs for 100 hours\n"; // Test solutions are OK for a small time increase... int end = solutions.rGetSolutions().size() - 1; // Tests the simulation is ending at the right time...(going into S phase at 7.8 hours) TS_ASSERT_DELTA(solutions.rGetTimes()[end], 100, 1e-2); // Decent results TS_ASSERT_DELTA(solutions.rGetSolutions()[end][0], 67.5011, 1e-4); TS_ASSERT_DELTA(solutions.rGetSolutions()[end][1], 67.5011, 1e-4); TS_ASSERT_DELTA(solutions.rGetSolutions()[end][2], wnt_level, 1e-4); #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 TestCellwiseSourcePdeMethods() throw(Exception) { EXIT_IF_PARALLEL; // Set up cell population HoneycombMeshGenerator generator(5, 5, 0); MutableMesh<2,2>* p_mesh = generator.GetMesh(); std::vector<CellPtr> cells; CellsGenerator<FixedDurationGenerationBasedCellCycleModel, 2> cells_generator; cells_generator.GenerateBasic(cells, p_mesh->GetNumNodes()); MeshBasedCellPopulation<2> cell_population(*p_mesh, cells); // Create a PDE object CellwiseSourcePde<2> pde(cell_population, 0.05); // Test that the member variables have been initialised correctly TS_ASSERT_EQUALS(&(pde.rGetCellPopulation()), &cell_population); TS_ASSERT_DELTA(pde.GetCoefficient(), 0.05, 1e-6); // Test methods Node<2>* p_node = cell_population.GetNodeCorrespondingToCell(*(cell_population.Begin())); TS_ASSERT_DELTA(pde.ComputeLinearInUCoeffInSourceTermAtNode(*p_node), 0.05, 1e-6); ChastePoint<2> point; c_matrix<double,2,2> diffusion_matrix = pde.ComputeDiffusionTerm(point); for (unsigned i=0; i<2; i++) { for (unsigned j=0; j<2; j++) { double value = 0.0; if (i == j) { value = 1.0; } TS_ASSERT_DELTA(diffusion_matrix(i,j), value, 1e-6); } } }
void TestMonodomainTissueGetCardiacCell() throw(Exception) { HeartConfig::Instance()->Reset(); TetrahedralMesh<1,1> mesh; mesh.ConstructRegularSlabMesh(1.0, 1.0); // [0,1] with h=1.0, ie 2 node mesh MyCardiacCellFactory cell_factory; cell_factory.SetMesh(&mesh); MonodomainTissue<1> monodomain_tissue( &cell_factory ); if (mesh.GetDistributedVectorFactory()->IsGlobalIndexLocal(0)) { AbstractCardiacCellInterface* cell = monodomain_tissue.GetCardiacCell(0); TS_ASSERT_DELTA(cell->GetStimulus(0.001),-80,1e-10); } if (mesh.GetDistributedVectorFactory()->IsGlobalIndexLocal(1)) { AbstractCardiacCellInterface* cell = monodomain_tissue.GetCardiacCell(1); TS_ASSERT_DELTA(cell->GetStimulus(0.001),0,1e-10); } }
void test_initial_settings() { TS_ASSERT_EQUALS(ch_->num_links(), NUM_MODULES); for (int i=0; i < ch_->num_links(); i++) { TS_ASSERT_DELTA(ch_->get_link(i).get_q(), INITIAL_Q, EPS); } for (int i=0; i < ch_->num_links(); i++) { ch_->get_link(i).set_q(0.0); TS_ASSERT_DELTA(ch_->get_link(i).get_q(), 0.0, EPS); TS_ASSERT((ch_->get_link(i).get_R_jts()).isApprox(Eigen::Matrix3d::Identity())); } /* Angles are now 0, so all of the modules should be * at the "initial_rotaiton" rotation (as long as * R_jts is identity for all of the modules) */ for (int i=0; i < ch_->num_links(); i++) { TS_ASSERT(ch_->get_current_R(i).isApprox(ch_->get_link(0).get_init_rotation())); } }
void TestCalculateLobeVolume() { #if defined(CHASTE_VTK) && ( (VTK_MAJOR_VERSION >= 5 && VTK_MINOR_VERSION >= 6) || VTK_MAJOR_VERSION >= 6) EXIT_IF_PARALLEL; vtkSmartPointer<vtkPolyData> sphere = CreateSphere(100); AirwayGenerator generator(sphere); //The sphere is coarsely meshed, hence relatively large tolerance TS_ASSERT_DELTA(generator.CalculateLobeVolume(), 4.0/3.0*M_PI, 1e-2); #endif }
void TestXaxisRotation3DWithMethod() { TrianglesMeshReader<3,3> mesh_reader("mesh/test/data/cube_136_elements"); TetrahedralMesh<3,3> mesh; mesh.ConstructFromMeshReader(mesh_reader); ChastePoint<3> corner_before = mesh.GetNode(6)->GetPoint(); TS_ASSERT_EQUALS(corner_before[0], 1.0); TS_ASSERT_EQUALS(corner_before[1], 1.0); TS_ASSERT_EQUALS(corner_before[2], 1.0); double mesh_volume = mesh.GetVolume(); mesh.RotateX(M_PI/2.0); double new_mesh_volume = mesh.GetVolume(); TS_ASSERT_DELTA(mesh_volume,new_mesh_volume,1e-6); ChastePoint<3> corner_after = mesh.GetNode(6)->GetPoint(); TS_ASSERT_DELTA(corner_after[0], 1.0, 1e-7); TS_ASSERT_DELTA(corner_after[1], 1.0, 1e-7); TS_ASSERT_DELTA(corner_after[2], -1.0, 1e-7); }
void TestHeatEquationForCoupledOdeSystem() { // Create PDE system object HeatEquationForCoupledOdeSystem<1> pde; ChastePoint<1> x(1.0); TS_ASSERT_DELTA(pde.ComputeDuDtCoefficientFunction(x,0), 1.0, 1e-6); c_vector<double,1> pde_solution; pde_solution(0) = 4.0; std::vector<double> ode_solution(1); ode_solution[0] = 5.0; TS_ASSERT_DELTA(pde.ComputeSourceTerm(x, pde_solution, ode_solution, 0), 0.0, 1e-6); Node<1> node(0); TS_ASSERT_DELTA(pde.ComputeSourceTermAtNode(node, pde_solution, ode_solution, 0), 0.0, 1e-6); c_matrix<double,1,1> diffusion_term = pde.ComputeDiffusionTerm(x, 0); TS_ASSERT_DELTA(diffusion_term(0,0), 1.0, 1e-6); }
void TestLoadAbstractOdeSystem() { TwoDimOdeSystem ode; TS_ASSERT_EQUALS( ode.GetNumberOfStateVariables(), 2U ); // Read archive from previous test OutputFileHandler handler("archive", false); std::string archive_filename; archive_filename = handler.GetOutputDirectoryFullPath() + "ode.arch"; std::ifstream ifs(archive_filename.c_str(), std::ios::binary); boost::archive::text_iarchive input_arch(ifs); input_arch >> ode; TS_ASSERT_EQUALS( ode.GetNumberOfStateVariables(), 2U ); std::vector<double>& r_state_variables = ode.rGetStateVariables(); TS_ASSERT_DELTA(r_state_variables[0], 7.0, 1e-12); TS_ASSERT_DELTA(r_state_variables[1], 8.0, 1e-12); }
/* * To visualize the results, open a new terminal, {{{cd}}} to the Chaste directory, * then {{{cd}}} to {{{anim}}}. Then do: {{{java Visualize2dVertexCells /tmp/$USER/testoutput/CellBasedDemo1/results_from_time_0}}}. * We may have to do: {{{javac Visualize2dVertexCells.java}}} beforehand to create the * java executable. * * EMPTYLINE * * The {{{make_a_movie}}} script can be used to generate a video based on the results of your simulation. * To do this, first visualize the results using {{{Visualize2dVertexCells}}} as described above. Click * on the box marked "Output" and play through the whole simulation to generate a sequence of {{{.png}}} * images, one for each time step. Next, still in the {{{anim}}} folder, do: {{{./make_a_movie}}}. * This reads in the {{{.png}}} files and creates a video file called {{{simulation.mpeg}}}. * * EMPTYLINE * * Results can also be visualized using Paraview. See the UserTutorials/VisualizingWithParaview tutorial for more information. * * EMPTYLINE * * == Test 2 - basic node-based simulation == * * We next show how to modify the previous test to implement a 'node-based' simulation, * in which cells are represented by overlapping spheres (actually circles, since we're * in 2D). */ void TestNodeBasedMonolayer() throw (Exception) { /* We now need to create a {{{NodesOnlyMesh}}} we do this by first creating a {{{MutableMesh}}} * and passing this to a helper method {{{ConstructNodesWithoutMesh}}} along with a interaction cut off length * that defines the connectivity in the mesh. */ HoneycombMeshGenerator generator(2, 2); //**Changed**// MutableMesh<2,2>* p_generating_mesh = generator.GetMesh(); //**Changed**// NodesOnlyMesh<2> mesh; //**Changed**// mesh.ConstructNodesWithoutMesh(*p_generating_mesh, 1.5); //**Changed**// /* We create the cells as before, only this time we need one cell per node.*/ std::vector<CellPtr> cells; MAKE_PTR(TransitCellProliferativeType, p_transit_type); CellsGenerator<StochasticDurationCellCycleModel, 2> cells_generator; cells_generator.GenerateBasicRandom(cells, mesh.GetNumNodes(), p_transit_type); //**Changed**// /* This time we create a {{{NodeBasedCellPopulation}}} as we are using a {{{NodesOnlyMesh}}}.*/ NodeBasedCellPopulation<2> cell_population(mesh, cells);//**Changed**// /* We create an {{{OffLatticeSimulation}}} object as before, all we change is the output directory * and output results more often as a larger default timestep is used for these simulations. */ OffLatticeSimulation<2> simulator(cell_population); simulator.SetOutputDirectory("CellBasedDemo2"); //**Changed**// simulator.SetSamplingTimestepMultiple(12); //**Changed**// simulator.SetEndTime(20.0); /* We use a different {{{Force}}} which is suitable for node based simulations. */ MAKE_PTR(RepulsionForce<2>, p_force); //**Changed**// simulator.AddForce(p_force); /* In all types of simulation you may specify how cells are removed from the simulation by specifying * a {{{CellKiller}}}. You create these in the same was as the {{{Force}}} and pass them to the {{{CellBasedSimulation}}}. * Note that here the constructor for {{{RandomCellKiller}}} requires some arguments to be passed to it, therefore we use the * {{{MAKE_PTR_ARGS}}} macro. */ MAKE_PTR_ARGS(RandomCellKiller<2>, p_cell_killer, (&cell_population, 0.01)); //**Changed**// simulator.AddCellKiller(p_cell_killer); /* Again we call the {{{Solve}}} method on the simulation to run the simulation.*/ simulator.Solve(); /* The next two lines are for test purposes only and are not part of this tutorial. * Again, we are checking that we reached the end time of the simulation * with the correct number of cells. */ TS_ASSERT_EQUALS(cell_population.GetNumRealCells(), 7u); TS_ASSERT_DELTA(SimulationTime::Instance()->GetTime(), 20.0, 1e-10); }
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; } }
// Testing Load() (based on previous two tests) void TestLoad() throw (Exception) { EXIT_IF_PARALLEL; // Cell based archiving doesn't work in parallel. // Load the simulation from the TestSave method above and // run it from 0.1 to 1.0 OffLatticeSimulation<2>* p_simulator1; p_simulator1 = CellBasedSimulationArchiver<2, OffLatticeSimulation<2> >::Load("TestOffLatticeSimulationWithNodeBasedCellPopulationSaveAndLoad", 0.1); // Test that the numerical method was archived correctly boost::shared_ptr<AbstractNumericalMethod<2, 2> > p_method = p_simulator1->GetNumericalMethod(); TS_ASSERT_EQUALS(p_method->HasAdaptiveTimestep(), true); p_simulator1->SetEndTime(1.0); p_simulator1->Solve(); // Save, then reload and run from 1.0 to 2.5 CellBasedSimulationArchiver<2, OffLatticeSimulation<2> >::Save(p_simulator1); OffLatticeSimulation<2>* p_simulator2 = CellBasedSimulationArchiver<2, OffLatticeSimulation<2> >::Load("TestOffLatticeSimulationWithNodeBasedCellPopulationSaveAndLoad", 1.0); p_simulator2->SetEndTime(2.5); p_simulator2->Solve(); // These results are from time 2.5 in TestStandardResultForArchivingTestBelow() (above!) std::vector<double> node_3_location = p_simulator2->GetNodeLocation(3); TS_ASSERT_DELTA(node_3_location[0], mNode3x, 1e-4); TS_ASSERT_DELTA(node_3_location[1], mNode3y, 1e-4); std::vector<double> node_4_location = p_simulator2->GetNodeLocation(4); TS_ASSERT_DELTA(node_4_location[0], mNode4x, 1e-4); TS_ASSERT_DELTA(node_4_location[1], mNode4y, 1e-4); // Tidy up delete p_simulator1; delete p_simulator2; }
void TestSimpleSystemWithOrderSwapped() { // The solution should be the same, but we'll have to construct a new CombinedOdeSystemInformation // object, because the order of subsystems has changed. SimpleOde1 ode_for_y; // dy/dt = x SimpleOde2 ode_for_x; // dx/dt = -y std::vector<AbstractOdeSystem*> ode_systems; ode_systems.push_back(&ode_for_x); ode_systems.push_back(&ode_for_y); // Create combined ODE system CombinedOdeSystem combined_ode_system(ode_systems); // Tell the combined ODE system which state variables in the first ODE system // correspond to which parameters in the second ODE system... std::map<unsigned, unsigned> variable_parameter_map; variable_parameter_map[0] = 0; combined_ode_system.Configure(variable_parameter_map, &ode_for_y, &ode_for_x); // ...and vice versa (we can re-use the map in this case) combined_ode_system.Configure(variable_parameter_map, &ode_for_x, &ode_for_y); // Test solving the combined system. // This is dy/dt = x, dx/dt = -y, y(0) = 0, x(0) = 1. // The analytic solution is y = sin(t), x = cos(t). EulerIvpOdeSolver solver; OdeSolution solutions; double h = 0.01; std::vector<double> inits = combined_ode_system.GetInitialConditions(); solutions = solver.Solve(&combined_ode_system, inits, 0.0, 2.0, h, h); double global_error = 0.5*(exp(2.0)-1)*h; TS_ASSERT_DELTA(solutions.rGetSolutions().back()[1], sin(2.0), global_error); TS_ASSERT_DELTA(solutions.rGetSolutions().back()[0], cos(2.0), global_error); }
void TestElementWithAttributes() throw (Exception) { // Create an element MutableElement<2,2> this_element(0); // Check no attributes and exception thrown if accessed TS_ASSERT_EQUALS(this_element.GetNumElementAttributes(), 0u); TS_ASSERT_THROWS_THIS(this_element.rGetElementAttributes(), "Element has no attributes associated with it. Construct attributes first"); // Add an attribute this_element.AddElementAttribute(1.23); // Check correct number and value TS_ASSERT_EQUALS(this_element.GetNumElementAttributes(), 1u); TS_ASSERT_DELTA(this_element.rGetElementAttributes()[0], 1.23, 1e-10); // Add a second attribute this_element.AddElementAttribute(4.56); // Check correct number and values TS_ASSERT_EQUALS(this_element.GetNumElementAttributes(), 2u); TS_ASSERT_DELTA(this_element.rGetElementAttributes()[0], 1.23, 1e-10); TS_ASSERT_DELTA(this_element.rGetElementAttributes()[1], 4.56, 1e-10); }
void TestGetLocation() { ChastePoint<3> point(1.0, 2.0, 3.0); c_vector<double, 3>& point_location = point.rGetLocation(); TS_ASSERT_EQUALS(point_location(1), 2.0); point_location(0) = 0; const c_vector<double, 3>& const_point_location = point.rGetLocation(); for (int i=0; i<3; i++) { TS_ASSERT_DELTA(const_point_location[i], point[i], 1e-7); } }
/** * Check that GetNextNode() returns the coordinates of the correct node. * Compares the coordinates of the first two nodes with their known * values, checks that no errors are thrown for the remaining nodes and * that an error is thrown if we try to call the function too many times. */ void TestGetNextNode() throw(Exception) { VertexMeshReader<2,2> mesh_reader("mesh/test/data/TestVertexMeshWriter/vertex_mesh_2d"); std::vector<double> first_node; first_node = mesh_reader.GetNextNode(); TS_ASSERT_DELTA(first_node[0], 0.0, 1e-6); TS_ASSERT_DELTA(first_node[1], 0.0, 1e-6) std::vector<double> next_node; next_node = mesh_reader.GetNextNode(); TS_ASSERT_DELTA(next_node[0], 1.0, 1e-6); TS_ASSERT_DELTA(next_node[1], 0.0, 1e-6); for (unsigned i=0; i<5; i++) { TS_ASSERT_THROWS_NOTHING(next_node = mesh_reader.GetNextNode()); } TS_ASSERT_THROWS_THIS(next_node = mesh_reader.GetNextNode(), "Cannot get the next line from node or element file due to incomplete data"); }
void TestLinearBasisFunction3d() { ChastePoint<3> zero(0,0,0); ChastePoint<3> zerozeroone(0,0,1); ChastePoint<3> zeroonezero(0,1,0); ChastePoint<3> onezerozero(1,0,0); std::vector<ChastePoint<3>*> evaluation_points; evaluation_points.push_back(&zero); evaluation_points.push_back(&onezerozero); evaluation_points.push_back(&zeroonezero); evaluation_points.push_back(&zerozeroone); BasisFunctionsCheckers<3> checker; checker.checkLinearBasisFunctions(evaluation_points); // Derivatives c_matrix<double, 3, 4> derivatives; LinearBasisFunction<3>::ComputeBasisFunctionDerivatives(onezerozero,derivatives); TS_ASSERT_DELTA(derivatives(0,0), -1, 1e-12); TS_ASSERT_DELTA(derivatives(0,1), 1, 1e-12); TS_ASSERT_DELTA(derivatives(0,2), 0, 1e-12); TS_ASSERT_DELTA(derivatives(0,3), 0, 1e-12); }
// Tests for Enemy void testEnemySet(void) { setUp(); Enemy *enemy = new Enemy(); enemy->set(*tileSet, 0, 0); Sprite * testSprite = new Sprite(); testSprite->setTexture(*tileSet); testSprite->setTextureRect(IntRect(0, 0, 1, 1)); TS_ASSERT_DELTA(enemy->dx, ENEMY_SPEED, 0.01); TS_ASSERT(enemy->life); delete enemy; breakDown(); }
void TestGenerateBasicWithFixedDurationGenerationBasedCellCycleModel() throw(Exception) { // Create mesh TrianglesMeshReader<2,2> mesh_reader("mesh/test/data/square_2_elements"); TetrahedralMesh<2,2> mesh; mesh.ConstructFromMeshReader(mesh_reader); // Create cells std::vector<CellPtr> cells; CellsGenerator<FixedDurationGenerationBasedCellCycleModel, 2> cells_generator; cells_generator.GenerateBasic(cells, mesh.GetNumNodes()); // Test that cells were generated correctly TS_ASSERT_EQUALS(cells.size(), mesh.GetNumNodes()); for (unsigned i=0; i<cells.size(); i++) { TS_ASSERT_DELTA(cells[i]->GetBirthTime(), -(double)(i), 1e-9); TS_ASSERT_EQUALS(cells[i]->GetCellCycleModel()->GetDimension(), 2u); } // Test with extra input argument std::vector<unsigned> location_indices; location_indices.push_back(2); location_indices.push_back(7); location_indices.push_back(9); std::vector<CellPtr> cells2; CellsGenerator<FixedDurationGenerationBasedCellCycleModel, 2> cells_generator2; cells_generator2.GenerateBasic(cells2, 3, location_indices); TS_ASSERT_EQUALS(cells2.size(), 3u); TS_ASSERT_DELTA(cells2[0]->GetBirthTime(), -2.0, 1e-4); TS_ASSERT_DELTA(cells2[1]->GetBirthTime(), -7.0, 1e-4); TS_ASSERT_DELTA(cells2[2]->GetBirthTime(), -9.0, 1e-4); }
/* == Sliding boundary conditions == * * It is common to require a Dirichlet boundary condition where the displacement/position in one dimension * is fixed, but the displacement/position in the others are free. This can be easily done when * collecting the new locations for the fixed nodes, as shown in the following example. Here, we * take a unit square, apply gravity downward, and suppose the Y=0 surface is like a frictionless boundary, * so that, for the nodes on Y=0, we specify y=0 but leave x free (Here (X,Y)=old position, (x,y)=new position). * (Note though that this wouldn't be enough to uniquely specify the final solution - an arbitrary * translation in the Y direction could be added a solution to obtain another valid solution, so we * fully fix the node at the origin.) */ void TestWithSlidingDirichletBoundaryConditions() throw(Exception) { QuadraticMesh<2> mesh; mesh.ConstructRegularSlabMesh(0.1 /*stepsize*/, 1.0 /*width*/, 1.0 /*height*/); ExponentialMaterialLaw<2> law(1.0, 0.5); // First parameter is 'a', second 'b', in W=a*exp(b(I1-3)) /* Create fixed nodes and locations... */ std::vector<unsigned> fixed_nodes; std::vector<c_vector<double,2> > locations; /* Fix node 0 (the node at the origin) */ fixed_nodes.push_back(0); locations.push_back(zero_vector<double>(2)); /* For the rest, if the node is on the Y=0 surface.. */ for (unsigned i=1; i<mesh.GetNumNodes(); i++) { if ( fabs(mesh.GetNode(i)->rGetLocation()[1])<1e-6) { /* ..add it to the list of fixed nodes.. */ fixed_nodes.push_back(i); /* ..and define y to be 0 but x is fixed */ c_vector<double,2> new_location; new_location(0) = SolidMechanicsProblemDefinition<2>::FREE; new_location(1) = 0.0; locations.push_back(new_location); } } /* Set the material law and fixed nodes, add some gravity, and solve */ SolidMechanicsProblemDefinition<2> problem_defn(mesh); problem_defn.SetMaterialLaw(INCOMPRESSIBLE,&law); problem_defn.SetFixedNodes(fixed_nodes, locations); c_vector<double,2> gravity = zero_vector<double>(2); gravity(1) = -0.5; problem_defn.SetBodyForce(gravity); IncompressibleNonlinearElasticitySolver<2> solver(mesh, problem_defn, "ElasticitySlidingBcsExample"); solver.Solve(); solver.CreateCmguiOutput(); /* Check the node at (1,0) has moved but has stayed on Y=0 */ TS_ASSERT_LESS_THAN(1.0, solver.rGetDeformedPosition()[10](0)); TS_ASSERT_DELTA(solver.rGetDeformedPosition()[10](1), 0.0, 1e-3); }
void TestTopOfAirwaysPatientDataOutflowFlux() throw (Exception) { MatrixVentilationProblem problem("lung/test/data/top_of_tree", 0u); PetscTools::SetOption("-ksp_monitor", ""); problem.SetOutflowFlux(0.001); problem.SetConstantInflowPressures(50.0); //problem.SetConstantInflowFluxes(100.0); TetrahedralMesh<1, 3>& r_mesh=problem.rGetMesh(); TS_ASSERT_EQUALS(r_mesh.GetNumNodes(), 31u); TS_ASSERT_EQUALS(r_mesh.GetNumElements(), 30u); problem.Solve(); std::vector<double> flux, pressure; problem.GetSolutionAsFluxesAndPressures(flux, pressure); TS_ASSERT_DELTA(flux[0], 0.001, 1e-5); }
void testPeriodicTimer( void ) { TEST_HEADER; DummyTimerUser timerUser; // after 1 sec, expire periodically at each sec timerUser.startTimer(1, 0, 1, 0); sleep(4); timerUser.stopTimer(); // 3 expiration (+- 1) TS_ASSERT_DELTA( timerUser.m_counter, 3, 1 ); }
void testMat42Mat23Product() { glam::mat4x2 m7a(std::make_pair(M7A, &M7A[8])); glam::mat3x4 m7b(std::make_pair(M7B, &M7B[12])); glam::mat3x2 p7 = m7a * m7b; TS_ASSERT_DELTA(p7[0][0], P7[0], 1e-6f); TS_ASSERT_DELTA(p7[0][1], P7[1], 1e-6f); TS_ASSERT_DELTA(p7[1][0], P7[2], 1e-6f); TS_ASSERT_DELTA(p7[1][1], P7[3], 1e-6f); TS_ASSERT_DELTA(p7[2][0], P7[4], 1e-6f); TS_ASSERT_DELTA(p7[2][1], P7[5], 1e-6f); }
void TestComputeTransformedBasisFunctionDerivatives() { // 1D ChastePoint<1> one(1); c_matrix<double, 1, 1> inv_J; inv_J(0,0) = 0.5; c_matrix<double, 1, 2> trans_deriv; LinearBasisFunction<1>::ComputeTransformedBasisFunctionDerivatives(one, inv_J, trans_deriv); TS_ASSERT_DELTA(trans_deriv(0,0), -0.5, 1e-12); TS_ASSERT_DELTA(trans_deriv(0,1), 0.5, 1e-12); // 2D ChastePoint<2> oneone(1,1); c_matrix<double, 2, 2> inv_J2 = 0.5 * identity_matrix<double>(2); c_matrix<double, 2, 3> trans_deriv2; LinearBasisFunction<2>::ComputeTransformedBasisFunctionDerivatives(oneone, inv_J2, trans_deriv2); TS_ASSERT_DELTA(trans_deriv2(0,0), -0.5, 1e-12); TS_ASSERT_DELTA(trans_deriv2(0,1), 0.5, 1e-12); TS_ASSERT_DELTA(trans_deriv2(0,2), 0, 1e-12); // 3D ChastePoint<3> oneoneone(1,1,1); c_matrix<double, 3, 3> inv_J3 = 0.5 * identity_matrix<double>(3); c_matrix<double, 3, 4> trans_deriv3; LinearBasisFunction<3>::ComputeTransformedBasisFunctionDerivatives(oneoneone, inv_J3, trans_deriv3); TS_ASSERT_DELTA(trans_deriv3(0,0), -0.5, 1e-12); TS_ASSERT_DELTA(trans_deriv3(0,1), 0.5, 1e-12); TS_ASSERT_DELTA(trans_deriv3(0,2), 0, 1e-12); TS_ASSERT_DELTA(trans_deriv3(0,3), 0, 1e-12); }
void TestGoldbeter1991OdeSteadyState() { // Keep running until we reach steady state SimulationTime* p_simulation_time = SimulationTime::Instance(); // run until 100, with dt=0.01 double t1=100; double dt=0.01; unsigned num_steps=(unsigned) t1/dt; p_simulation_time->SetEndTimeAndNumberOfTimeSteps(t1, num_steps+1); boost::shared_ptr<AbstractCellProperty> p_healthy_state(CellPropertyRegistry::Instance()->Get<WildTypeCellMutationState>()); boost::shared_ptr<AbstractCellProperty> p_transit_type(CellPropertyRegistry::Instance()->Get<TransitCellProliferativeType>()); // Create a cell-cycle model FixedDurationGenerationBasedCellCycleModel* p_cell_model = new FixedDurationGenerationBasedCellCycleModel(); Goldbeter1991SrnModel* p_srn_model = new Goldbeter1991SrnModel(); CellPtr p_tn_cell(new Cell(p_healthy_state, p_cell_model, p_srn_model, false, CellPropertyCollection())); p_tn_cell->SetCellProliferativeType(p_transit_type); p_tn_cell->InitialiseCellCycleModel(); p_tn_cell->InitialiseSrnModel(); // Run the cell simulation until t1 while (!p_simulation_time->IsFinished()) { p_simulation_time->IncrementTimeOneStep(); if (p_tn_cell->ReadyToDivide()) { p_tn_cell->Divide(); } } // Get final state variables double current_time = SimulationTime::Instance()->GetTime(); std::cout << "Finished ODE - " << "mSimulatedToTime : " << p_srn_model->GetSimulatedToTime() << ", current_time : " << current_time << std::endl; // Direct access to state variables double C = dynamic_cast<Goldbeter1991SrnModel*>(p_tn_cell->GetSrnModel())->GetC(); TS_ASSERT_DELTA(C, 0.5470, 1e-4); double M = dynamic_cast<Goldbeter1991SrnModel*>(p_tn_cell->GetSrnModel())->GetM(); TS_ASSERT_DELTA(M, 0.2936, 1e-4); double X = dynamic_cast<Goldbeter1991SrnModel*>(p_tn_cell->GetSrnModel())->GetX(); TS_ASSERT_DELTA(X, 0.0067, 1e-4); // Indirect access to state vector C = dynamic_cast<Goldbeter1991SrnModel*>(p_tn_cell->GetSrnModel())->GetProteinConcentrations()[0]; TS_ASSERT_DELTA(C, 0.5470, 1e-4); M = dynamic_cast<Goldbeter1991SrnModel*>(p_tn_cell->GetSrnModel())->GetProteinConcentrations()[1]; TS_ASSERT_DELTA(M, 0.2936, 1e-4); X = dynamic_cast<Goldbeter1991SrnModel*>(p_tn_cell->GetSrnModel())->GetProteinConcentrations()[2]; TS_ASSERT_DELTA(X, 0.0067, 1e-4); std::cout << "Finished ODE - " << "C : " << C << ", M : " << M << ", X : " << X << std::endl; }
/* * Failing test for ReMesh (see #1275) */ void noTestCylindricalReMeshFailingTest() throw (Exception) { // Load a problematic mesh TrianglesMeshReader<2,2> mesh_reader("cell_based/test/data/TestCylindricalMeshBug/mesh"); Cylindrical2dMesh mesh(20); mesh.ConstructFromMeshReader(mesh_reader); TS_ASSERT_DELTA(mesh.GetWidth(0), 20, 1e-3); TrianglesMeshWriter<2,2> mesh_writer("TestCylindricalMeshBug", "mesh", false); mesh_writer.WriteFilesUsingMesh(mesh); // Use showme to view this mesh NodeMap map(mesh.GetNumNodes()); mesh.ReMesh(map); }
void testEnemyCollision(void) { setUp(); Enemy *enemy = new Enemy(); enemy->set(*tileSet, 0, 0); World::initLevels(); World::setLevel(2); float dx = enemy->dx; enemy->update(1); TS_ASSERT_DELTA(enemy->dx, dx, 0.01); delete enemy; breakDown(); }
void TestGetActiveTension() { NhsModelWithBackwardSolver system; double Ca_I = GetSampleCaIValue(); system.SetIntracellularCalciumConcentration(Ca_I); system.SetStretchAndStretchRate(0.6, 0.1); system.RunDoNotUpdate(0, 1, 0.01); double Ta_at_next_time_before_update = system.GetNextActiveTension(); system.UpdateStateVariables(); double Ta = system.GetActiveTension(); TS_ASSERT_DELTA(Ta, Ta_at_next_time_before_update, 1e-12); }