/** * Simple Parabolic PDE u' = del squared u * * With u = 0 on the boundaries of the unit cube. Subject to the initial * condition u(0,x,y,z)=sin( PI x)sin( PI y)sin( PI z). */ void TestSimpleLinearParabolicSolver3DZeroDirich() { // read mesh on [0,1]x[0,1]x[0,1] TrianglesMeshReader<3,3> mesh_reader("mesh/test/data/cube_136_elements"); TetrahedralMesh<3,3> mesh; mesh.ConstructFromMeshReader(mesh_reader); // Instantiate PDE object HeatEquation<3> pde; // Boundary conditions - zero dirichlet everywhere on boundary BoundaryConditionsContainer<3,3,1> bcc; bcc.DefineZeroDirichletOnMeshBoundary(&mesh); // Solver SimpleLinearParabolicSolver<3,3> solver(&mesh,&pde,&bcc); /* * Choose initial condition sin(x*pi)*sin(y*pi)*sin(z*pi) as * this is an eigenfunction of the heat equation. */ std::vector<double> init_cond(mesh.GetNumNodes()); for (unsigned i=0; i<mesh.GetNumNodes(); i++) { double x = mesh.GetNode(i)->GetPoint()[0]; double y = mesh.GetNode(i)->GetPoint()[1]; double z = mesh.GetNode(i)->GetPoint()[2]; init_cond[i] = sin(x*M_PI)*sin(y*M_PI)*sin(z*M_PI); } Vec initial_condition = PetscTools::CreateVec(init_cond); double t_end = 0.1; solver.SetTimes(0, t_end); solver.SetTimeStep(0.001); solver.SetInitialCondition(initial_condition); Vec result = solver.Solve(); ReplicatableVector result_repl(result); // Check solution is u = e^{-3*t*pi*pi} sin(x*pi)*sin(y*pi)*sin(z*pi), t=0.1 for (unsigned i=0; i<result_repl.GetSize(); i++) { double x = mesh.GetNode(i)->GetPoint()[0]; double y = mesh.GetNode(i)->GetPoint()[1]; double z = mesh.GetNode(i)->GetPoint()[2]; double u = exp(-3*t_end*M_PI*M_PI)*sin(x*M_PI)*sin(y*M_PI)*sin(z*M_PI); TS_ASSERT_DELTA(result_repl[i], u, 0.1); } PetscTools::Destroy(initial_condition); PetscTools::Destroy(result); }
void TestHeatEquationWithSourceWithCoupledOdeSystemIn1dWithZeroNeumann() { // Create mesh of domain [0,1] TrianglesMeshReader<1,1> mesh_reader("mesh/test/data/1D_0_to_1_100_elements"); TetrahedralMesh<1,1> mesh; mesh.ConstructFromMeshReader(mesh_reader); // Create PDE system object HeatEquationWithSourceForCoupledOdeSystem<1> pde; // Define zero Neumann boundary conditions BoundaryConditionsContainer<1,1,1> bcc; ConstBoundaryCondition<1>* p_boundary_condition = new ConstBoundaryCondition<1>(0.0); TetrahedralMesh<1,1>::BoundaryElementIterator iter = mesh.GetBoundaryElementIteratorBegin(); bcc.AddNeumannBoundaryCondition(*iter, p_boundary_condition); iter = mesh.GetBoundaryElementIteratorEnd(); iter--; bcc.AddNeumannBoundaryCondition(*iter, p_boundary_condition); // Create the correct number of ODE systems double a = 5.0; std::vector<AbstractOdeSystemForCoupledPdeSystem*> ode_systems; for (unsigned i=0; i<mesh.GetNumNodes(); i++) { ode_systems.push_back(new OdeSystemForCoupledHeatEquationWithSource(a)); } // Create PDE system solver LinearParabolicPdeSystemWithCoupledOdeSystemSolver<1,1,1> solver(&mesh, &pde, &bcc, ode_systems); // Test setting end time and timestep TS_ASSERT_THROWS_THIS(solver.SetTimes(1.0, 0.0), "Start time has to be less than end time"); TS_ASSERT_THROWS_THIS(solver.SetTimeStep(0.0), "Time step has to be greater than zero"); // Set end time and timestep double t_end = 0.1; solver.SetTimes(0, t_end); solver.SetTimeStep(0.001); // Set initial condition u(x,0) = 1 + cos(pi*x) std::vector<double> init_cond(mesh.GetNumNodes()); for (unsigned i=0; i<mesh.GetNumNodes(); i++) { double x = mesh.GetNode(i)->GetPoint()[0]; init_cond[i] = 1 + cos(M_PI*x); } Vec initial_condition = PetscTools::CreateVec(init_cond); solver.SetInitialCondition(initial_condition); // Solve PDE system and store result Vec result = solver.Solve(); ReplicatableVector result_repl(result); /* * Test that solution is given by * * u(x,t) = 1 + (1 - exp(-a*t))/a + exp(-pi*pi*t)*cos(pi*x), * v(x,t) = exp(-a*t), * * with t = t_end. */ for (unsigned i=0; i<result_repl.GetSize(); i++) { double x = mesh.GetNode(i)->GetPoint()[0]; double u = 1 + (1 - exp(-a*t_end))/a + exp(-M_PI*M_PI*t_end)*cos(M_PI*x); TS_ASSERT_DELTA(result_repl[i], u, 0.1); double u_from_v = solver.GetOdeSystemAtNode(i)->rGetPdeSolution()[0]; TS_ASSERT_DELTA(result_repl[i], u_from_v, 0.1); double v = exp(-a*t_end); TS_ASSERT_DELTA(ode_systems[i]->rGetStateVariables()[0], v, 0.1); } // Test the method GetOdeSystemAtNode() for (unsigned i=0; i<mesh.GetNumNodes(); i++) { TS_ASSERT(solver.GetOdeSystemAtNode(i) != NULL); TS_ASSERT_DELTA(static_cast<OdeSystemForCoupledHeatEquationWithSource*>(solver.GetOdeSystemAtNode(i))->GetA(), 5.0, 1e-6); } // Tidy up PetscTools::Destroy(initial_condition); PetscTools::Destroy(result); }
void TestHeatEquationWithCoupledOdeSystemIn2dWithZeroDirichlet() { // Create mesh of the domain [0,1]x[0,1] TrianglesMeshReader<2,2> mesh_reader("mesh/test/data/square_4096_elements"); TetrahedralMesh<2,2> mesh; mesh.ConstructFromMeshReader(mesh_reader); // Create PDE system object HeatEquationForCoupledOdeSystem<2> pde; // Define zero Dirichlet boundary conditions on entire boundary BoundaryConditionsContainer<2,2,1> bcc; bcc.DefineZeroDirichletOnMeshBoundary(&mesh); // Create the correct number of ODE systems double a = 5.0; std::vector<AbstractOdeSystemForCoupledPdeSystem*> ode_systems; for (unsigned i=0; i<mesh.GetNumNodes(); i++) { ode_systems.push_back(new OdeSystemForCoupledHeatEquation(a)); } // Create PDE system solver LinearParabolicPdeSystemWithCoupledOdeSystemSolver<2,2,1> solver(&mesh, &pde, &bcc, ode_systems); // Set end time and timestep double t_end = 0.01; solver.SetTimes(0, t_end); solver.SetTimeStep(0.001); /* * Set initial condition * * u(x,y,0) = sin(pi*x)*sin(pi*y), * * which is an eigenfunction of the heat equation. */ std::vector<double> init_cond(mesh.GetNumNodes()); for (unsigned i=0; i<mesh.GetNumNodes(); i++) { double x = mesh.GetNode(i)->GetPoint()[0]; double y = mesh.GetNode(i)->GetPoint()[1]; init_cond[i] = sin(M_PI*x)*sin(M_PI*y); } Vec initial_condition = PetscTools::CreateVec(init_cond); solver.SetInitialCondition(initial_condition); // Solve PDE system and store result Vec result = solver.Solve(); ReplicatableVector result_repl(result); /* * Test that solution is given by * * u(x,y,t) = e^{-2*pi*pi*t} sin(pi*x)*sin(pi*y), * v(x,y,t) = 1 + (1 - e^{-2*pi*pi*t})*sin(pi*x)*sin(pi*y)*a/(2*pi*pi), * * with t = t_end. */ for (unsigned i=0; i<result_repl.GetSize(); i++) { double x = mesh.GetNode(i)->GetPoint()[0]; double y = mesh.GetNode(i)->GetPoint()[1]; double u = exp(-2*M_PI*M_PI*t_end)*sin(M_PI*x)*sin(M_PI*y); double v = 1.0 + (a/(2*M_PI*M_PI))*(1 - exp(-2*M_PI*M_PI*t_end))*sin(M_PI*x)*sin(M_PI*y); TS_ASSERT_DELTA(result_repl[i], u, 0.01); TS_ASSERT_DELTA(ode_systems[i]->rGetStateVariables()[0], v, 0.01); } // Tidy up PetscTools::Destroy(initial_condition); PetscTools::Destroy(result); }
void TestHeatEquationWithCoupledOdeSystemIn1dWithMixed() { // Create mesh of domain [0,1] TrianglesMeshReader<1,1> mesh_reader("mesh/test/data/1D_0_to_1_100_elements"); TetrahedralMesh<1,1> mesh; mesh.ConstructFromMeshReader(mesh_reader); // Create PDE system object HeatEquationForCoupledOdeSystem<1> pde; // Define non-zero Neumann boundary condition at x=0 BoundaryConditionsContainer<1,1,1> bcc; ConstBoundaryCondition<1>* p_boundary_condition = new ConstBoundaryCondition<1>(1.0); TetrahedralMesh<1,1>::BoundaryElementIterator iter = mesh.GetBoundaryElementIteratorBegin(); bcc.AddNeumannBoundaryCondition(*iter, p_boundary_condition); // Define zero Dirichlet boundary condition at x=1 ConstBoundaryCondition<1>* p_boundary_condition2 = new ConstBoundaryCondition<1>(0.0); TetrahedralMesh<1,1>::BoundaryNodeIterator node_iter = mesh.GetBoundaryNodeIteratorEnd(); --node_iter; bcc.AddDirichletBoundaryCondition(*node_iter, p_boundary_condition2); // Create the correct number of ODE systems double a = 5.0; std::vector<AbstractOdeSystemForCoupledPdeSystem*> ode_systems; for (unsigned i=0; i<mesh.GetNumNodes(); i++) { ode_systems.push_back(new OdeSystemForCoupledHeatEquation(a)); } // Create PDE system solver LinearParabolicPdeSystemWithCoupledOdeSystemSolver<1,1,1> solver(&mesh, &pde, &bcc, ode_systems); // Set end time and timestep double t_end = 0.1; solver.SetTimes(0, t_end); solver.SetTimeStep(0.001); // Set initial condition u(x,0) = 1 - x std::vector<double> init_cond(mesh.GetNumNodes()); for (unsigned i=0; i<mesh.GetNumNodes(); i++) { double x = mesh.GetNode(i)->GetPoint()[0]; init_cond[i] = 1 - x; } Vec initial_condition = PetscTools::CreateVec(init_cond); solver.SetInitialCondition(initial_condition); // Solve PDE system and store result Vec result = solver.Solve(); ReplicatableVector result_repl(result); /* * Test that solution is given by * * u(x,t) = 1 - x, * v(x,t) = 1 + a*(1-x)*t, * * with t = t_end. */ for (unsigned i=0; i<result_repl.GetSize(); i++) { double x = mesh.GetNode(i)->GetPoint()[0]; double u = 1 - x; TS_ASSERT_DELTA(result_repl[i], u, 0.1); double v = 1 + a*(1-x)*t_end; TS_ASSERT_DELTA(ode_systems[i]->rGetStateVariables()[0], v, 0.1); } // Tidy up PetscTools::Destroy(initial_condition); PetscTools::Destroy(result); }
void TestSolvingEllipticPde() throw(Exception) { /* First we declare a mesh reader which reads mesh data files of the 'Triangle' * format. The path given is relative to the main Chaste directory. As we are in 2d, * the reader will look for three datafiles, [name].nodes, [name].ele and [name].edge. * Note that the first template argument here is the spatial dimension of the * elements in the mesh ({{{ELEMENT_DIM}}}), and the second is the dimension of the nodes, * i.e. the dimension of the space the mesh lives in ({{{SPACE_DIM}}}). Usually * {{{ELEMENT_DIM}}} and {{{SPACE_DIM}}} will be equal. */ TrianglesMeshReader<2,2> mesh_reader("mesh/test/data/square_128_elements"); /* Now declare a tetrahedral mesh with the same dimensions... */ TetrahedralMesh<2,2> mesh; /* ... and construct the mesh using the mesh reader. */ mesh.ConstructFromMeshReader(mesh_reader); /* Next we instantiate an instance of our PDE we wish to solve. */ MyPde pde; /* A set of boundary conditions are stored in a {{{BoundaryConditionsContainer}}}. The * three template arguments are ELEMENT_DIM, SPACE_DIM and PROBLEM_DIM, the latter being * the number of unknowns we are solving for. We have one unknown (ie u is a scalar, not * a vector), so in this case {{{PROBLEM_DIM}}}=1. */ BoundaryConditionsContainer<2,2,1> bcc; /* Defining the boundary conditions is the only particularly fiddly part of solving PDEs, * unless they are very simple, such as u=0 on the boundary, which could be done * as follows: */ //bcc.DefineZeroDirichletOnMeshBoundary(&mesh); /* We want to specify u=0 on x=0 and y=0. To do this, we first create the boundary condition * object saying what the value of the condition is at any particular point in space. Here * we use the class `ConstBoundaryCondition`, a subclass of `AbstractBoundaryCondition` that * yields the same constant value (0.0 here) everywhere it is used. * * Note that the object is allocated with `new`. The `BoundaryConditionsContainer` object deals * with deleting its associated boundary condition objects. Note too that we could allocate a * separate condition object for each boundary node, but using the same object where possible is * more memory efficient. */ ConstBoundaryCondition<2>* p_zero_boundary_condition = new ConstBoundaryCondition<2>(0.0); /* We then get a boundary node iterator from the mesh... */ TetrahedralMesh<2,2>::BoundaryNodeIterator iter = mesh.GetBoundaryNodeIteratorBegin(); /* ...and loop over the boundary nodes, getting the x and y values. */ while (iter < mesh.GetBoundaryNodeIteratorEnd()) { double x = (*iter)->GetPoint()[0]; double y = (*iter)->GetPoint()[1]; /* If x=0 or y=0... */ if ((x==0) || (y==0)) { /* ...associate the zero boundary condition created above with this boundary node * ({{{*iter}}} being a pointer to a {{{Node<2>}}}). */ bcc.AddDirichletBoundaryCondition(*iter, p_zero_boundary_condition); } iter++; } /* Now we create Neumann boundary conditions for the ''surface elements'' on x=1 and y=1. Note that * Dirichlet boundary conditions are defined on nodes, whereas Neumann boundary conditions are * defined on surface elements. Note also that the natural boundary condition statement for this * PDE is (D grad u).n = g(x) (where n is the outward-facing surface normal), and g(x) is a prescribed * function, ''not'' something like du/dn=g(x). Hence the boundary condition we are specifying is * (D grad u).n = 0. * * '''Important note for 1D:''' This means that if we were solving 2u,,xx,,=f(x) in 1D, and * wanted to specify du/dx=1 on the LHS boundary, the Neumann boundary value we have to specify is * -2, as n=-1 (outward facing normal) so (D gradu).n = -2 when du/dx=1. * * To define Neumann bcs, we reuse the zero boundary condition object defined above, but apply it * at surface elements. We loop over these using another iterator provided by the mesh class. */ TetrahedralMesh<2,2>::BoundaryElementIterator surf_iter = mesh.GetBoundaryElementIteratorBegin(); while (surf_iter != mesh.GetBoundaryElementIteratorEnd()) { /* Get the x and y values of any node (here, the 0th) in the element. */ unsigned node_index = (*surf_iter)->GetNodeGlobalIndex(0); double x = mesh.GetNode(node_index)->GetPoint()[0]; double y = mesh.GetNode(node_index)->GetPoint()[1]; /* If x=1 or y=1... */ if ( (fabs(x-1.0) < 1e-6) || (fabs(y-1.0) < 1e-6) ) { /* ...associate the boundary condition with the surface element. */ bcc.AddNeumannBoundaryCondition(*surf_iter, p_zero_boundary_condition); } /* Finally increment the iterator. */ surf_iter++; } /* Next we define the solver of the PDE. * To solve an {{{AbstractLinearEllipticPde}}} (which is the type of PDE {{{MyPde}}} is), * we use a {{{SimpleLinearEllipticSolver}}}. The solver, again templated over * {{{ELEMENT_DIM}}} and {{{SPACE_DIM}}}, needs to be given (pointers to) the mesh, * pde and boundary conditions. */ SimpleLinearEllipticSolver<2,2> solver(&mesh, &pde, &bcc); /* To solve, just call {{{Solve()}}}. A PETSc vector is returned. */ Vec result = solver.Solve(); /* It is a pain to access the individual components of a PETSc vector, even when running only on * one process. A helper class called {{{ReplicatableVector}}} has been created. Create * an instance of one of these, using the PETSc {{{Vec}}} as the data. The ''i''th * component of {{{result}}} can now be obtained by simply doing {{{result_repl[i]}}}. */ ReplicatableVector result_repl(result); /* Let us write out the solution to a file. To do this, create an * {{{OutputFileHandler}}}, passing in the directory we want files written to. * This is relative to the directory defined by the CHASTE_TEST_OUTPUT environment * variable - usually `/tmp/$USER/testoutput`. Note by default the output directory * passed in is emptied by this command. To avoid this, {{{false}}} can be passed in as a second * parameter. */ OutputFileHandler output_file_handler("TestSolvingLinearPdeTutorial"); /* Create an {{{out_stream}}}, which is a stream to a particular file. An {{{out_stream}}} * is a smart pointer to a `std::ofstream`. */ out_stream p_file = output_file_handler.OpenOutputFile("linear_solution.txt"); /* Loop over the entries of the solution. */ for (unsigned i=0; i<result_repl.GetSize(); i++) { /* Get the x and y-values of the node corresponding to this entry. The method * {{{GetNode}}} on the mesh class returns a pointer to a {{{Node}}}. */ double x = mesh.GetNode(i)->rGetLocation()[0]; double y = mesh.GetNode(i)->rGetLocation()[1]; /* Get the computed solution at this node from the {{{ReplicatableVector}}}. */ double u = result_repl[i]; /* Finally, write x, y and u to the output file. The solution could then be * visualised in (eg) matlab, using the commands: * {{{sol=load('linear_solution.txt'); plot3(sol(:,1),sol(:,2),sol(:,3),'.');}}}*/ (*p_file) << x << " " << y << " " << u << "\n"; } /* All PETSc {{{Vec}}}s should be destroyed when they are no longer needed, or you will have a memory leak. */ PetscTools::Destroy(result); }
// test 2D problem - takes a long time to run. // solution is incorrect to specified tolerance. void xTestSimpleLinearParabolicSolver2DNeumannWithSmallTimeStepAndFineMesh() { // Create mesh from mesh reader FemlabMeshReader<2,2> mesh_reader("mesh/test/data/", "femlab_fine_square_nodes.dat", "femlab_fine_square_elements.dat", "femlab_fine_square_edges.dat"); TetrahedralMesh<2,2> mesh; mesh.ConstructFromMeshReader(mesh_reader); // Instantiate PDE object HeatEquation<2> pde; // Boundary conditions - zero dirichlet on boundary; BoundaryConditionsContainer<2,2,1> bcc; TetrahedralMesh<2,2>::BoundaryNodeIterator iter = mesh.GetBoundaryNodeIteratorBegin(); while (iter != mesh.GetBoundaryNodeIteratorEnd()) { double x = (*iter)->GetPoint()[0]; double y = (*iter)->GetPoint()[1]; ConstBoundaryCondition<2>* p_dirichlet_boundary_condition = new ConstBoundaryCondition<2>(x); if (fabs(y) < 0.01) { bcc.AddDirichletBoundaryCondition(*iter, p_dirichlet_boundary_condition); } if (fabs(y - 1.0) < 0.01) { bcc.AddDirichletBoundaryCondition(*iter, p_dirichlet_boundary_condition); } if (fabs(x) < 0.01) { bcc.AddDirichletBoundaryCondition(*iter, p_dirichlet_boundary_condition); } iter++; } TetrahedralMesh<2,2>::BoundaryElementIterator surf_iter = mesh.GetBoundaryElementIteratorBegin(); ConstBoundaryCondition<2>* p_neumann_boundary_condition = new ConstBoundaryCondition<2>(1.0); while (surf_iter != mesh.GetBoundaryElementIteratorEnd()) { int node = (*surf_iter)->GetNodeGlobalIndex(0); double x = mesh.GetNode(node)->GetPoint()[0]; if (fabs(x - 1.0) < 0.01) { bcc.AddNeumannBoundaryCondition(*surf_iter, p_neumann_boundary_condition); } surf_iter++; } // Solver SimpleLinearParabolicSolver<2,2> solver(&mesh,&pde,&bcc); // Initial condition u(0,x,y) = sin(0.5*M_PI*x)*sin(M_PI*y)+x std::vector<double> init_cond(mesh.GetNumNodes()); for (unsigned i=0; i<mesh.GetNumNodes(); i++) { double x = mesh.GetNode(i)->GetPoint()[0]; double y = mesh.GetNode(i)->GetPoint()[1]; init_cond[i] = sin(0.5*M_PI*x)*sin(M_PI*y)+x; } Vec initial_condition = PetscTools::CreateVec(init_cond); double t_end = 0.1; solver.SetTimes(0, t_end); solver.SetTimeStep(0.001); solver.SetInitialCondition(initial_condition); Vec result = solver.Solve(); ReplicatableVector result_repl(result); // Check solution is u = e^{-5/4*M_PI*M_PI*t} sin(0.5*M_PI*x)*sin(M_PI*y)+x, t=0.1 for (unsigned i=0; i<result_repl.GetSize(); i++) { double x = mesh.GetNode(i)->GetPoint()[0]; double y = mesh.GetNode(i)->GetPoint()[1]; double u = exp((-5/4)*M_PI*M_PI*t_end) * sin(0.5*M_PI*x) * sin(M_PI*y) + x; TS_ASSERT_DELTA(result_repl[i], u, 0.001); } PetscTools::Destroy(result); PetscTools::Destroy(initial_condition); }