int main() { double /* **U, **Unew,err=0.0,*/ sta = 1.0/8.0, stb=1.0/128.0; int sa = 9, sb = 129, i/*,j*/; FILE *outs; init(&U,sa,sa); init(&Unew,sa,sa); init(&Ub,sb,sb); init_cond(&U, sa, sa, sta,sta); memcpy(&Unew,&U,sizeof(Unew)); for(i = 0; i<1000 /*&& (err>0.001 || err == 0.0)*/; i++) { //err = 0.0; iteration(&U,&Unew, sa,sa, sta,sta); memcpy(&U,&Unew,sizeof(Unew)); } outs = fopen("../output/out1.dat", "w"); output_line(U, sa,sa, sta,sta, outs); fclose(outs); //init_cond(&Ub, sb, sb, stb,stb); interpol(&Ub, sb, sb, stb, stb, &U, sa-1, sa-1, sta, sta); init_cond(&Ub, sb, sb, stb, stb); outs = fopen("../output/out2.dat", "w"); output_line(Ub, sb,sb, stb,stb, outs); fclose(outs); freeArr(&Unew, sa); //output_line(U, sa,sa, sta,sta); //output_line(Ub, sb,sb, stb,stb); //freeArr(&U, sa); init(&Unew,sb,sb); init_cond(&Unew, sb, sb, stb, stb); memcpy(&Unew,&Ub,sizeof(Ub)); //err = 0.0; for(i = 0; i<1000 /*&& (err>0.0005 || err == 0.0)*/ ; i++) { iteration(&Ub,&Unew, sb,sb, stb,stb); memcpy(&Ub,&Unew,sizeof(Unew)); } outs = fopen("../output/out3.dat", "w"); output_line(Ub, sb,sb, stb,stb, outs); fclose(outs); outs = fopen("../output/out3_mstk.dat", "w"); opt_mstk_line(Ub, sb,sb, stb,stb, outs); fclose(outs); //output_line(U, size,size, step,step); //printf("%f %f %f\n", step*5, step*5, err); return 0; }
int kernel_main() { printk("Hello kernel!\n"); page_init(); init_cond(); return 0; }
/** * 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); }
// Essential (Dirichlet) boundary condition markers. scalar essential_bc_values(double x, double y) { double dx, dy; return init_cond(x, y, dx, dy); }
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 TestSolveAndWriteResultsToFileMethod() { // Create mesh of the domain [0,1]x[0,1] TrianglesMeshReader<2,2> mesh_reader("mesh/test/data/square_128_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 (end time is not a multiple of timestep, for coverage) double t_end = 0.105; /* * 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); // Need an output folder TS_ASSERT_THROWS_THIS(solver.SolveAndWriteResultsToFile(), "SetOutputDirectory() must be called prior to SolveAndWriteResultsToFile()"); solver.SetOutputDirectory("TestHeatEquationForCoupledOdeSystemIn2dWithZeroDirichletWithOutput"); // Need a time interval TS_ASSERT_THROWS_THIS(solver.SolveAndWriteResultsToFile(), "SetTimes() must be called prior to SolveAndWriteResultsToFile()"); solver.SetTimes(0, t_end); // Need a timestep TS_ASSERT_THROWS_THIS(solver.SolveAndWriteResultsToFile(), "SetTimeStep() must be called prior to SolveAndWriteResultsToFile()"); solver.SetTimeStep(0.01); // Need sampling interval TS_ASSERT_THROWS_THIS(solver.SolveAndWriteResultsToFile(), "SetSamplingTimeStep() must be called prior to SolveAndWriteResultsToFile()"); solver.SetSamplingTimeStep(0.1); // Need initial condition TS_ASSERT_THROWS_THIS(solver.SolveAndWriteResultsToFile(), "SetInitialCondition() must be called prior to SolveAndWriteResultsToFile()"); solver.SetInitialCondition(initial_condition); solver.SolveAndWriteResultsToFile(); //#ifdef CHASTE_VTK ///\todo #1967 Check that the file was output and has expected content //#endif // CHASTE_VTK // Tidy up PetscTools::Destroy(initial_condition); }
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); }
// Essential (Dirichlet) boundary condition markers. scalar essential_bc_values(int ess_bdy_marker, double x, double y) { double dx, dy; return init_cond(x, y, dx, dy); }
int main() { double sta = 1.0/64.;//, stb = 1./512.; int sa = 65, i;//, sb = 513/*,j*/; FILE *outs; init(&U,sa,sa); init(&Unew,sa,sa); //init(&Ub,sb,sb); init_cond(&U, sa, sa, sta,sta); memcpy(&Unew,&U,sizeof(Unew)); for(i = 0; i<7000; i++) { iteration(&U,&Unew, sa,sa, sta,sta); memcpy(&U,&Unew,sizeof(Unew)); } printf("step a done\n"); //outs = fopen("../output/out1.dat", "w"); //output_line(U, sa,sa, sta,sta, outs); //fclose(outs); //interpol(&Ub, sb, sb, stb, stb, &U, sa-1, sa-1, sta, sta); //init_cond(&Ub, sb, sb, stb, stb); //freeArr(&Unew, sa); //init(&Unew,sb,sb); //init_cond(&Unew, sb, sb, stb, stb); //memcpy(&Unew,&Ub,sizeof(Ub)); //for(i = 0; i<500 /*&& (err>0.0005 || err == 0.0)*/ ; i++) //{ //printf("%d/100\n",i); //iteration(&Ub,&Unew, sb,sb, stb,stb); //memcpy(&Ub,&Unew,sizeof(Unew)); //} outs = fopen("../output/out3.dat", "w"); output_grid(U, sa,sa, sta,sta, outs); fclose(outs); printf("pot. output \n"); outs = fopen("../output/press.dat", "w"); output_field_color(U, sa,sa, sta,sta, outs); fclose(outs); printf("pressure output\n"); outs = fopen("../output/field.dat", "w"); output_field(U, sa,sa, sta,sta, outs); fclose(outs); printf("field output\n"); system("./lines_of_level"); system("./Plot_field"); system("./Plot_col"); system("./Plot"); return 0; }
// 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); }