template <int dim>
void nonlinear<dim>::run ()
{
unsigned int cycle_control=4;
for (unsigned int cycle=0; cycle<cycle_control; ++cycle)
{
std::cout << "Cycle " << cycle << ':' << std::endl;
if (cycle == 0)
{
GridGenerator::hyper_cube (triangulation, 0.0, 1.0);
triangulation.refine_global (1);
for (typename Triangulation<dim>::active_cell_iterator
cell=triangulation.begin_active();
cell!=triangulation.end(); ++cell)
{
for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
{
if (cell->face(f)->at_boundary() == true)
{
if ( std::abs(cell->face(f)->center()[0]-0.5)< 1e-12 )
{
cell->face(f)->set_boundary_id(10);
}
// Boundary faces
static const double tol = 1e-12;
if ( std::abs(cell->face(f)->center()[0]-0.0)< tol )
{
cell->face(f)->set_boundary_id(1);
}
if ( std::abs(cell->face(f)->center()[0]-1.0)< tol )
{
cell->face(f)->set_boundary_id(1);
}
if ( std::abs(cell->face(f)->center()[1]-0.0)< tol )
{
cell->face(f)->set_boundary_id(1);
}
if ( std::abs(cell->face(f)->center()[1]-1.0)< tol )
{
cell->face(f)->set_boundary_id(1);
}
}
}
}
std::ofstream out ("grid-1.ucd");
GridOut grid_out;
grid_out.write_ucd (triangulation, out);
}
else
refine_grid ();
std::cout << " Number of active cells: "<< triangulation.n_active_cells() << std::endl;
setup_system ();
int iteration=0;
assemble_system ();
solve ();
Vector<double> difference(dof_handler.n_dofs());
Vector<double> temp_vector(dof_handler.n_dofs());
Vector<double> temp_difference(dof_handler.n_dofs());
difference=solution; // std::cout << " ||u_k-u_{k-1}||" << difference.l2_norm() << std::endl;
previous_solution = solution ;
double omega = 0.5; //relxation control number
int success_step=0 ;
do{
assemble_system();
solve();
difference = solution;
difference -= previous_solution; //This is temporary step
temp_vector=solution;
temp_vector.add(omega,difference);
temp_difference = temp_vector;
temp_difference -= solution; //Calculate different again
if (temp_difference.l2_norm()< difference.l2_norm()) //temp_difference = temp_vector - solution , difference=solution-previous_solution
{
success_step+=1;
difference=temp_vector;
difference-=solution ;
solution=temp_vector;
}
previous_solution=solution ;
iteration+=1;
std::ofstream outerror2("iter_error.dat",std::ios::app);
outerror2 << degree << " " << triangulation.n_active_cells() << " " << iteration << " " << difference.l2_norm() << " " << error << std::endl;
}while (difference.l2_norm() > 0.00001);
std::cout << " ||u_k-u_{k-1}|| = " << difference.l2_norm() << " Iteration Number: " << iteration << std::endl;
error_evaluation();
std::ofstream outerror("error.dat",std::ios::app);
outerror << degree << " " << triangulation.n_active_cells() << " " << iteration << " " << error << std::endl;
output_results ();
}
}
template <int dim>
void nonlinear<dim>::refine_grid ()
{
Vector<float> estimated_error_per_cell (triangulation.n_active_cells());
KellyErrorEstimator<dim>::estimate (dof_handler,
QGauss<dim-1>(3),
typename FunctionMap<dim>::type(),
solution,
estimated_error_per_cell);
GridRefinement::refine_and_coarsen_fixed_number (triangulation,
estimated_error_per_cell,
0.3, 0.0);
/* You need to tranfer solution (n-1 step) domain into previous solution (n step)*/
triangulation.prepare_coarsening_and_refinement ();
SolutionTransfer<dim> solution_transfer(dof_handler);
solution_transfer.prepare_for_coarsening_and_refinement(solution);
triangulation.execute_coarsening_and_refinement(); // I think n_dof() has increased here. You need to check this if necessary
dof_handler.distribute_dofs(fe);
// Vector<double> tmp(dof_handler.n_dofs()); //tmp - n step dof
previous_solution.reinit (dof_handler.n_dofs());
solution_transfer.interpolate(solution, previous_solution); // interpolate (input, output)
solution.reinit (dof_handler.n_dofs());
}
template <int dim>
void nonlinear<dim>::setup_system (const unsigned int refinement_cycle)
{
if (refinement_cycle==0)
{ dof_handler.distribute_dofs (fe);
std::cout << " Number of degrees of freedom: "
<< dof_handler.n_dofs()
<< std::endl;
solution.reinit (dof_handler.n_dofs());
previous_solution.reinit (dof_handler.n_dofs());
system_rhs.reinit (dof_handler.n_dofs());
for (unsigned int i=0; i<dof_handler.n_dofs(); ++i)//////////
{ //////
previous_solution(i)=1;
}
DynamicSparsityPattern dsp(dof_handler.n_dofs());
DoFTools::make_sparsity_pattern (dof_handler, dsp);
sparsity_pattern.copy_from(dsp);
system_matrix.reinit (sparsity_pattern);
constraints.clear ();
DoFTools::make_hanging_node_constraints (dof_handler,
constraints);
VectorTools::interpolate_boundary_values (dof_handler,1,
BoundaryValues<dim>(),
constraints);
constraints.close ();
}
else
{
//You don't need to make previous solution. Or solution vector , (it will be done on refinement step)
//What you should do? set boundary condition again.
DynamicSparsityPattern dsp(dof_handler.n_dofs());
DoFTools::make_sparsity_pattern (dof_handler, dsp);
sparsity_pattern.copy_from(dsp);
system_matrix.reinit (sparsity_pattern);
system_rhs.reinit (dof_handler.n_dofs());
constraints.clear ();
DoFTools::make_hanging_node_constraints (dof_handler,
constraints);
VectorTools::interpolate_boundary_values (dof_handler,1,
BoundaryValues<dim>(),
constraints);
constraints.close ();
std::cout << "Set up system finished" << std::endl;
}
}
An error occurred in line <1668> of file </Users/kimjaekwang/dealii-8.4.1/include/deal.II/lac/constraint_matrix.templates.h> in function
void dealii::internals::dealiiSparseMatrix::add_value(const LocalType, const size_type, const size_type, SparseMatrixIterator &) [SparseMatrixIterator = dealii::SparseMatrixIterators::Iterator<double, false>, LocalType = double]
The violated condition was:
matrix_values->column() == column
The name and call sequence of the exception was:
typename SparseMatrix<typename SparseMatrixIterator::MatrixType::value_type>::ExcInvalidIndex(row, column)
Additional Information:
You are trying to access the matrix entry with index <0,30>, but this entry does not exist in the sparsity pattern of this matrix.
template <int dim>
void nonlinear<dim>::refine_grid ()
{
Vector<float> estimated_error_per_cell (triangulation.n_active_cells());
KellyErrorEstimator<dim>::estimate (dof_handler,
QGauss<dim-1>(3),
typename FunctionMap<dim>::type(),
solution,
estimated_error_per_cell);
GridRefinement::refine_and_coarsen_fixed_number (triangulation,
estimated_error_per_cell,
0.3, 0.0);
triangulation.prepare_coarsening_and_refinement ();
SolutionTransfer<dim> solution_transfer(dof_handler);
solution_transfer.prepare_for_coarsening_and_refinement(solution);
triangulation.execute_coarsening_and_refinement(); // I think n_dof() has increased here. You need to check this if necessary
dof_handler.distribute_dofs(fe);
previous_solution.reinit (dof_handler.n_dofs());
solution_transfer.interpolate(solution, previous_solution);
DynamicSparsityPattern dsp(dof_handler.n_dofs());
DoFTools::make_sparsity_pattern (dof_handler, dsp);
sparsity_pattern.copy_from(dsp);
solution.reinit (dof_handler.n_dofs());
system_matrix.reinit (sparsity_pattern);
system_rhs.reinit (dof_handler.n_dofs());
GridRefinement::refine_and_coarsen_fixed_number (triangulation,
estimated_error_per_cell,
1.0, 0.0);
if I input 90 cells to be refined, it goes at least one cycle,
but same or less than 80 cell, I keep receiving error message that...
Might AMR be related to my program?
An error occurred in line <1668> of file </Users/kimjaekwang/dealii-8.4.1/include/deal.II/lac/constraint_matrix.templates.h> in function
void dealii::internals::dealiiSparseMatrix::add_value(const LocalType, const size_type, const size_type, SparseMatrixIterator &) [SparseMatrixIterator = dealii::SparseMatrixIterators::Iterator<double, false>, LocalType = double]
The violated condition was:
matrix_values->column() == column
The name and call sequence of the exception was:
typename SparseMatrix<typename SparseMatrixIterator::MatrixType::value_type>::ExcInvalidIndex(row, column)
Additional Information:
You are trying to access the matrix entry with index <1,305>, but this entry does not exist in the sparsity pattern of this matrix.
The most common cause for this problem is that you used a method to build the sparsity pattern that did not (completely) take into account all of the entries you will later try to write into. An example would be building a sparsity pattern that does not include the entries you will write into due to constraints on degrees of freedom such as hanging nodes or periodic boundary conditions. In such cases, building the sparsity pattern will succeed, but you will get errors such as the current one at one point or other when trying to write into the entries of the matrix.
template <int dim>
void nonlinear<dim>::refine_grid ()
{
Vector<float> estimated_error_per_cell (triangulation.n_active_cells());
KellyErrorEstimator<dim>::estimate (dof_handler,
QGauss<dim-1>(3),
typename FunctionMap<dim>::type(),
solution,
estimated_error_per_cell);
GridRefinement::refine_and_coarsen_fixed_number (triangulation,
estimated_error_per_cell,
1.0, 0.0);
/* You need to tranfer solution (n-1 step) domain into previous solution (n step)*/
triangulation.prepare_coarsening_and_refinement ();
SolutionTransfer<dim> solution_transfer(dof_handler);
solution_transfer.prepare_for_coarsening_and_refinement(solution);
triangulation.execute_coarsening_and_refinement(); // I think n_dof() has increased here. You need to check this if necessary
dof_handler.distribute_dofs(fe);
// Vector<double> tmp(dof_handler.n_dofs()); //tmp - n step dof
previous_solution.reinit (dof_handler.n_dofs());
solution_transfer.interpolate(solution, previous_solution); // interpolate (input, output)
DynamicSparsityPattern dsp(dof_handler.n_dofs());
DoFTools::make_sparsity_pattern (dof_handler, dsp);
sparsity_pattern.copy_from(dsp);
solution.reinit (dof_handler.n_dofs());
system_matrix.reinit (sparsity_pattern);
system_rhs.reinit (dof_handler.n_dofs());
constraints.clear ();
DoFTools::make_sparsity_pattern(dof_handler,
dsp,
constraints,
/*keep_constrained_dofs = */ false);
template <int dim>
void
StokesProblem<dim>::refine_mesh () //AMR based on VELOCITY GRADIENT
{
const FEValuesExtractors::Vector velocities (0);
Vector<float> estimated_error_per_cell (triangulation.n_active_cells());
KellyErrorEstimator<dim>::estimate (dof_handler,QGauss<dim-1>(degree+1),typename FunctionMap<dim>::type(),
solution,estimated_error_per_cell, fe.component_mask(velocities));
GridRefinement::refine_and_coarsen_fixed_number (triangulation,estimated_error_per_cell,0.3, 0.0);
triangulation.prepare_coarsening_and_refinement ();
SolutionTransfer<dim> solution_transfer(dof_handler); // Here my doc hander might include all (u,v,p) solution
solution_transfer.prepare_for_coarsening_and_refinement(solution.block(0));
//since this function only takes vector type inputs... I assume that I cannot transfer whole solution (u,v,p) at once?
triangulation.execute_coarsening_and_refinement();
//Transfer solution
solution_transfer.interpolate(solution.block(0), previous_solution.block(0));
solution_transfer.prepare_for_coarsening_and_refinement(solution.block(1));
solution_transfer.interpolate(solution.block(1), previous_solution.block(1));
An error occurred in line <253> of file </Users/kimjaekwang/dealii-8.4.1/source/numerics/solution_transfer.cc> in function
void dealii::SolutionTransfer<2, dealii::Vector<double>, dealii::DoFHandler<2, 2> >::prepare_for_coarsening_and_refinement(const std::vector<VectorType> &) [dim = 2, VectorType = dealii::Vector<double>, DoFHandlerType = dealii::DoFHandler<2, 2>]
The violated condition was:
all_in[i].size()==n_dofs_old
The name and call sequence of the exception was:
ExcDimensionMismatch(all_in[i].size(),n_dofs_old)
Additional Information:
Dimension 238 not equal to 274
temperature_trans(temperature_dof_handler); // non-block vectorstokes_trans(stokes_dof_handler); // block vectortriangulation.prepare_coarsening_and_refinement();temperature_trans.prepare_for_coarsening_and_refinement(x_temperature);stokes_trans.prepare_for_coarsening_and_refinement(x_stokes);
error: use of
class template 'BlockVector' requires template arguments
SolutionTransfer<dim,BlockVector> solution_transfer(dof_handler)...
^~~~~~~~~~~
/Users/kimjaekwang/Programs/dealii/include/deal.II/n
SolutionTransfer<dim,BlockVector<double> > solution_transfer(dof_handler);