54 return Selection(
"DG_variant",
"Type of penalty term.")
55 .
add_value(non_symmetric,
"non-symmetric",
"non-symmetric weighted interior penalty DG method")
56 .
add_value(incomplete,
"incomplete",
"incomplete weighted interior penalty DG method")
57 .
add_value(symmetric,
"symmetric",
"symmetric weighted interior penalty DG method")
80 std::string equation_name = std::string(Model::ModelEqData::name()) +
"_DG";
81 return Model::get_input_type(
"DG",
"Discontinuous Galerkin (DG) solver")
83 "Solver for the linear system.")
86 .make_field_descriptor_type(equation_name)),
88 "Input fields of the equation.")
90 "Variant of the interior penalty discontinuous Galerkin method.")
92 "Polynomial order for the finite element in DG method (order 0 is suitable if there is no diffusion/dispersion).")
94 "If true, use DG projection of the initial condition field."
95 "Otherwise, evaluate initial condition field directly (well suited for reading native data).")
97 EqFields().output_fields.make_output_type(equation_name,
""),
98 IT::Default(
"{ \"fields\": [ " + Model::ModelEqData::default_output_field() +
"] }"),
99 "Specification of output fields and output times.")
103 template<
class Model>
105 Input::register_class< TransportDG<Model>,
Mesh &,
const Input::Record>(std::string(Model::ModelEqData::name()) +
"_DG") +
110 template<
class Model>
114 .
name(
"fracture_sigma")
116 "Coefficient of diffusive transfer through fractures (for each substance).")
124 "Penalty parameter influencing the discontinuity of the solution (for each substance). "
125 "Its default value 1 is sufficient in most cases. Higher value diminishes the inter-element jumps.")
138 .
description(
"Subdomain ids of the domain decomposition.");
149 template<
class Model>
152 double h_max = 0, h_min = numeric_limits<double>::infinity();
153 for (
unsigned int i=0; i<e->
n_nodes(); i++)
154 for (
unsigned int j=i+1; j<e->
n_nodes(); j++)
156 double dist = arma::norm(*e.
node(i) - *e.
node(j));
157 h_max = max(h_max, dist);
158 h_min = min(h_min, dist);
165 template<
class Model>
174 double delta = 0, h = 0;
183 for (
unsigned int i=0; i<side.
n_nodes(); i++)
184 for (
unsigned int j=i+1; j<side.
n_nodes(); j++) {
185 double dist = arma::norm(*side.
node(i) - *side.
node(j));
192 for (
int k=0; k<K_size; k++)
193 delta += dot(K[k]*normal_vector,normal_vector);
196 gamma = 0.5*fabs(flux) + alpha/h*delta*elem_anisotropy(side.
element());
201 template<
typename Model>
203 :
Model(init_mesh, in_rec),
217 Model::init_balance(in_rec);
228 eq_data_->dg_order = in_rec.
val<
unsigned int>(
"dg_order");
230 Model::init_from_input(in_rec);
233 shared_ptr<DiscreteSpace> ds = make_shared<EqualOrderDiscreteSpace>(Model::mesh_, fe);
234 eq_data_->dh_ = make_shared<DOFHandlerMultiDim>(*Model::mesh_);
241 template<
class Model>
244 eq_fields_->set_components(eq_data_->substances_.names());
245 eq_fields_->set_input_list( input_rec.val<
Input::Array>(
"input_fields"), *(Model::time_) );
246 eq_data_->set_time_governor(Model::time_);
247 eq_data_->balance_ = this->
balance();
248 eq_fields_->initialize();
251 eq_data_->gamma.resize(eq_data_->n_substances());
252 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
253 eq_data_->gamma[sbi].resize(Model::mesh_->boundary_.size());
256 eq_data_->max_edg_sides = max(Model::mesh_->max_edge_sides(1), max(Model::mesh_->max_edge_sides(2), Model::mesh_->max_edge_sides(3)));
257 ret_sources.resize(eq_data_->n_substances());
258 ret_sources_prev.resize(eq_data_->n_substances());
260 eq_data_->output_vec.resize(eq_data_->n_substances());
261 eq_fields_->output_field.set_components(eq_data_->substances_.names());
262 eq_fields_->output_field.set_mesh(*Model::mesh_);
265 eq_fields_->output_field.setup_components();
266 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
269 auto output_field_ptr = create_field_fe< 3, FieldValue<3>::Scalar >(eq_data_->dh_);
270 eq_fields_->output_field[sbi].set(output_field_ptr, 0);
271 eq_data_->output_vec[sbi] = output_field_ptr->vec();
275 eq_fields_->output_fields.initialize(Model::output_stream_, Model::mesh_, input_rec.val<
Input::Record>(
"output"), this->
time());
278 std::string petsc_default_opts;
279 if (eq_data_->dh_->distr()->np() == 1)
280 petsc_default_opts =
"-ksp_type bcgs -pc_type ilu -pc_factor_levels 2 -ksp_diagonal_scale_fix -pc_factor_fill 6.0";
282 petsc_default_opts =
"-ksp_type bcgs -ksp_diagonal_scale_fix -pc_type asm -pc_asm_overlap 4 -sub_pc_type ilu -sub_pc_factor_levels 3 -sub_pc_factor_fill 6.0";
285 eq_data_->ls =
new LinSys*[eq_data_->n_substances()];
286 eq_data_->ls_dt =
new LinSys*[eq_data_->n_substances()];
287 eq_data_->conc_fe.resize(eq_data_->n_substances());
290 shared_ptr<DiscreteSpace> ds = make_shared<EqualOrderDiscreteSpace>(Model::mesh_, fe);
291 eq_data_->dh_p0 = make_shared<DOFHandlerMultiDim>(*Model::mesh_);
292 eq_data_->dh_p0->distribute_dofs(ds);
294 stiffness_matrix.resize(eq_data_->n_substances(),
nullptr);
295 mass_matrix.resize(eq_data_->n_substances(),
nullptr);
296 rhs.resize(eq_data_->n_substances(),
nullptr);
297 mass_vec.resize(eq_data_->n_substances(),
nullptr);
298 eq_data_->ret_vec.resize(eq_data_->n_substances(),
nullptr);
300 for (
unsigned int sbi = 0; sbi < eq_data_->n_substances(); sbi++) {
301 eq_data_->ls[sbi] =
new LinSys_PETSC(eq_data_->dh_->distr().get(), petsc_default_opts);
303 eq_data_->ls[sbi]->set_solution(eq_data_->output_vec[sbi].petsc_vec());
305 eq_data_->ls_dt[sbi] =
new LinSys_PETSC(eq_data_->dh_->distr().get(), petsc_default_opts);
308 eq_data_->conc_fe[sbi] = create_field_fe< 3, FieldValue<3>::Scalar >(eq_data_->dh_p0);
310 VecDuplicate(eq_data_->ls[sbi]->get_solution(), &eq_data_->ret_vec[sbi]);
314 init_projection = input_rec.val<
bool>(
"init_projection");
328 Model::balance_->allocate(eq_data_->dh_->distr()->lsize(), mass_assembly_->eval_points()->max_size());
330 int qsize = mass_assembly_->eval_points()->max_size();
331 eq_data_->dif_coef.resize(eq_data_->n_substances());
332 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
334 eq_data_->dif_coef[sbi].resize(qsize);
337 eq_fields_->init_condition.setup_components();
338 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
345 template<
class Model>
350 if (eq_data_->gamma.size() > 0) {
353 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
355 delete eq_data_->ls[i];
356 delete eq_data_->ls_dt[i];
358 if (stiffness_matrix[i])
359 chkerr(MatDestroy(&stiffness_matrix[i]));
361 chkerr(MatDestroy(&mass_matrix[i]));
363 chkerr(VecDestroy(&rhs[i]));
365 chkerr(VecDestroy(&mass_vec[i]));
366 if (eq_data_->ret_vec[i])
367 chkerr(VecDestroy(&eq_data_->ret_vec[i]));
369 delete[] eq_data_->ls;
370 delete[] eq_data_->ls_dt;
377 delete mass_assembly_;
378 delete stiffness_assembly_;
379 delete sources_assembly_;
380 delete bdr_cond_assembly_;
381 delete init_assembly_;
387 template<
class Model>
391 eq_fields_->mark_input_times( *(Model::time_) );
393 std::stringstream ss;
400 set_initial_condition();
401 for (
unsigned int sbi = 0; sbi < eq_data_->n_substances(); sbi++)
402 ( (
LinSys_PETSC *)eq_data_->ls[sbi] )->set_initial_guess_nonzero();
405 if (!allocation_done) preallocate();
408 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
410 Model::balance_->calculate_instant(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
413 ret_sources_prev[sbi] = 0;
420 template<
class Model>
424 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
427 eq_data_->ls[i]->start_allocation();
428 stiffness_matrix[i] = NULL;
432 eq_data_->ls_dt[i]->start_allocation();
433 mass_matrix[i] = NULL;
434 VecZeroEntries(eq_data_->ret_vec[i]);
436 stiffness_assembly_->assemble(eq_data_->dh_);
437 mass_assembly_->assemble(eq_data_->dh_);
438 sources_assembly_->assemble(eq_data_->dh_);
439 bdr_cond_assembly_->assemble(eq_data_->dh_);
440 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
442 VecAssemblyBegin(eq_data_->ret_vec[i]);
443 VecAssemblyEnd(eq_data_->ret_vec[i]);
446 allocation_done =
true;
451 template<
class Model>
456 Model::time_->next_time();
457 Model::time_->view(
"TDG");
466 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
468 eq_data_->ls_dt[i]->start_add_assembly();
469 eq_data_->ls_dt[i]->mat_zero_entries();
470 VecZeroEntries(eq_data_->ret_vec[i]);
472 mass_assembly_->assemble(eq_data_->dh_);
473 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
475 eq_data_->ls_dt[i]->finish_assembly();
476 VecAssemblyBegin(eq_data_->ret_vec[i]);
477 VecAssemblyEnd(eq_data_->ret_vec[i]);
479 if (mass_matrix[i] == NULL)
481 VecDuplicate(eq_data_->ls[i]->get_solution(), &mass_vec[i]);
482 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
483 MatConvert(*( eq_data_->ls_dt[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &mass_matrix[i]);
486 MatCopy(*( eq_data_->ls_dt[i]->get_matrix() ), mass_matrix[i], DIFFERENT_NONZERO_PATTERN);
491 if (stiffness_matrix[0] == NULL
493 || eq_fields_->flow_flux.changed())
497 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
499 eq_data_->ls[i]->start_add_assembly();
500 eq_data_->ls[i]->mat_zero_entries();
502 stiffness_assembly_->assemble(eq_data_->dh_);
503 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
505 eq_data_->ls[i]->finish_assembly();
507 if (stiffness_matrix[i] == NULL)
508 MatConvert(*( eq_data_->ls[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &stiffness_matrix[i]);
510 MatCopy(*( eq_data_->ls[i]->get_matrix() ), stiffness_matrix[i], DIFFERENT_NONZERO_PATTERN);
517 || eq_fields_->flow_flux.changed())
519 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
521 eq_data_->ls[i]->start_add_assembly();
522 eq_data_->ls[i]->rhs_zero_entries();
524 sources_assembly_->assemble(eq_data_->dh_);
525 bdr_cond_assembly_->assemble(eq_data_->dh_);
526 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
528 eq_data_->ls[i]->finish_assembly();
530 if (rhs[i] ==
nullptr) VecDuplicate(*( eq_data_->ls[i]->get_rhs() ), &rhs[i]);
531 VecCopy(*( eq_data_->ls[i]->get_rhs() ), rhs[i]);
553 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
555 MatConvert(stiffness_matrix[i], MATSAME, MAT_INITIAL_MATRIX, &m);
556 MatAXPY(m, 1./Model::time_->dt(), mass_matrix[i], SUBSET_NONZERO_PATTERN);
557 eq_data_->ls[i]->set_matrix(m, DIFFERENT_NONZERO_PATTERN);
559 VecDuplicate(rhs[i], &w);
560 VecWAXPY(w, 1./Model::time_->dt(), mass_vec[i], rhs[i]);
561 eq_data_->ls[i]->set_rhs(w);
566 eq_data_->ls[i]->solve();
569 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
580 calculate_cumulative_balance();
586 template<
class Model>
590 for (
auto cell : eq_data_->dh_->own_range() )
592 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
593 unsigned int n_dofs=loc_dof_indices.n_rows;
595 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
598 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
600 eq_data_->conc_fe[sbi]->vec().set(dof_p0, 0);
602 for (
unsigned int j=0; j<n_dofs; ++j)
603 eq_data_->conc_fe[sbi]->vec().add( dof_p0, eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]] );
605 eq_data_->conc_fe[sbi]->vec().set( dof_p0, max(eq_data_->conc_fe[sbi]->vec().get(dof_p0)/n_dofs, 0.) );
613 template<
class Model>
624 eq_fields_->output_fields.output(this->
time().step());
626 Model::output_data();
629 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
630 Model::balance_->calculate_instant(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
631 Model::balance_->output();
638 template<
class Model>
641 if (Model::balance_->cumulative())
643 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
645 Model::balance_->calculate_cumulative(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
648 VecDot(eq_data_->ret_vec[sbi], eq_data_->ls[sbi]->get_solution(), &ret_sources[sbi]);
650 Model::balance_->add_cumulative_source(eq_data_->subst_idx_[sbi], (ret_sources[sbi]-ret_sources_prev[sbi])/Model::time_->dt());
651 ret_sources_prev[sbi] = ret_sources[sbi];
659 template<
class Model>
664 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
665 eq_data_->ls[sbi]->start_allocation();
667 init_assembly_->assemble(eq_data_->dh_);
669 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
670 eq_data_->ls[sbi]->start_add_assembly();
672 init_assembly_->assemble(eq_data_->dh_);
674 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
676 eq_data_->ls[sbi]->finish_assembly();
677 eq_data_->ls[sbi]->solve();
681 init_assembly_->assemble(eq_data_->dh_);
685 template<
class Model>
688 el_4_loc = Model::mesh_->get_el_4_loc();
689 el_ds = Model::mesh_->get_el_ds();
693 template<
class Model>
696 if (solution_changed)
698 for (
auto cell : eq_data_->dh_->own_range() )
700 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
701 unsigned int n_dofs=loc_dof_indices.n_rows;
703 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
706 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
708 double old_average = 0;
709 for (
unsigned int j=0; j<n_dofs; ++j)
710 old_average += eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]];
711 old_average /= n_dofs;
713 for (
unsigned int j=0; j<n_dofs; ++j)
714 eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]]
715 += eq_data_->conc_fe[sbi]->vec().get(dof_p0) - old_average;
720 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
721 MatMult(*(eq_data_->ls_dt[sbi]->get_matrix()), eq_data_->ls[sbi]->get_solution(), mass_vec[sbi]);
724 template<
class Model>
727 return Model::mesh_->get_row_4_el();