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_);
263 eq_fields_->output_fields.set_mesh(*Model::mesh_);
266 eq_fields_->output_field.setup_components();
267 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
270 auto output_field_ptr = create_field_fe< 3, FieldValue<3>::Scalar >(eq_data_->dh_);
271 eq_fields_->output_field[sbi].set(output_field_ptr, 0);
272 eq_data_->output_vec[sbi] = output_field_ptr->vec();
276 eq_fields_->output_fields.initialize(Model::output_stream_, Model::mesh_, input_rec.val<
Input::Record>(
"output"), this->
time());
279 std::string petsc_default_opts;
280 if (eq_data_->dh_->distr()->np() == 1)
281 petsc_default_opts =
"-ksp_type bcgs -pc_type ilu -pc_factor_levels 2 -ksp_diagonal_scale_fix -pc_factor_fill 6.0";
283 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";
286 eq_data_->ls =
new LinSys*[eq_data_->n_substances()];
287 eq_data_->ls_dt =
new LinSys*[eq_data_->n_substances()];
288 eq_data_->conc_fe.resize(eq_data_->n_substances());
291 shared_ptr<DiscreteSpace> ds = make_shared<EqualOrderDiscreteSpace>(Model::mesh_, fe);
292 eq_data_->dh_p0 = make_shared<DOFHandlerMultiDim>(*Model::mesh_);
293 eq_data_->dh_p0->distribute_dofs(ds);
295 stiffness_matrix.resize(eq_data_->n_substances(),
nullptr);
296 mass_matrix.resize(eq_data_->n_substances(),
nullptr);
297 rhs.resize(eq_data_->n_substances(),
nullptr);
298 mass_vec.resize(eq_data_->n_substances(),
nullptr);
299 eq_data_->ret_vec.resize(eq_data_->n_substances(),
nullptr);
301 for (
unsigned int sbi = 0; sbi < eq_data_->n_substances(); sbi++) {
302 eq_data_->ls[sbi] =
new LinSys_PETSC(eq_data_->dh_->distr().get(), petsc_default_opts);
304 eq_data_->ls[sbi]->set_solution(eq_data_->output_vec[sbi].petsc_vec());
306 eq_data_->ls_dt[sbi] =
new LinSys_PETSC(eq_data_->dh_->distr().get(), petsc_default_opts);
309 eq_data_->conc_fe[sbi] = create_field_fe< 3, FieldValue<3>::Scalar >(eq_data_->dh_p0);
311 VecDuplicate(eq_data_->ls[sbi]->get_solution(), &eq_data_->ret_vec[sbi]);
315 init_projection = input_rec.val<
bool>(
"init_projection");
329 Model::balance_->allocate(eq_data_->dh_->distr()->lsize(), mass_assembly_->eval_points()->max_size());
331 int qsize = mass_assembly_->eval_points()->max_size();
332 eq_data_->dif_coef.resize(eq_data_->n_substances());
333 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
335 eq_data_->dif_coef[sbi].resize(qsize);
338 eq_fields_->init_condition.setup_components();
339 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
346 template<
class Model>
351 if (eq_data_->gamma.size() > 0) {
354 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
356 delete eq_data_->ls[i];
357 delete eq_data_->ls_dt[i];
359 if (stiffness_matrix[i])
360 chkerr(MatDestroy(&stiffness_matrix[i]));
362 chkerr(MatDestroy(&mass_matrix[i]));
364 chkerr(VecDestroy(&rhs[i]));
366 chkerr(VecDestroy(&mass_vec[i]));
367 if (eq_data_->ret_vec[i])
368 chkerr(VecDestroy(&eq_data_->ret_vec[i]));
370 delete[] eq_data_->ls;
371 delete[] eq_data_->ls_dt;
378 delete mass_assembly_;
379 delete stiffness_assembly_;
380 delete sources_assembly_;
381 delete bdr_cond_assembly_;
382 delete init_assembly_;
388 template<
class Model>
392 eq_fields_->mark_input_times( *(Model::time_) );
394 std::stringstream ss;
401 set_initial_condition();
402 for (
unsigned int sbi = 0; sbi < eq_data_->n_substances(); sbi++)
403 ( (
LinSys_PETSC *)eq_data_->ls[sbi] )->set_initial_guess_nonzero();
406 if (!allocation_done) preallocate();
409 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
411 Model::balance_->calculate_instant(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
414 ret_sources_prev[sbi] = 0;
421 template<
class Model>
425 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
428 eq_data_->ls[i]->start_allocation();
429 stiffness_matrix[i] = NULL;
433 eq_data_->ls_dt[i]->start_allocation();
434 mass_matrix[i] = NULL;
435 VecZeroEntries(eq_data_->ret_vec[i]);
437 stiffness_assembly_->assemble(eq_data_->dh_);
438 mass_assembly_->assemble(eq_data_->dh_);
439 sources_assembly_->assemble(eq_data_->dh_);
440 bdr_cond_assembly_->assemble(eq_data_->dh_);
441 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
443 VecAssemblyBegin(eq_data_->ret_vec[i]);
444 VecAssemblyEnd(eq_data_->ret_vec[i]);
447 allocation_done =
true;
452 template<
class Model>
457 Model::time_->next_time();
458 Model::time_->view(
"TDG");
467 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
469 eq_data_->ls_dt[i]->start_add_assembly();
470 eq_data_->ls_dt[i]->mat_zero_entries();
471 VecZeroEntries(eq_data_->ret_vec[i]);
473 mass_assembly_->assemble(eq_data_->dh_);
474 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
476 eq_data_->ls_dt[i]->finish_assembly();
477 VecAssemblyBegin(eq_data_->ret_vec[i]);
478 VecAssemblyEnd(eq_data_->ret_vec[i]);
480 if (mass_matrix[i] == NULL)
482 VecDuplicate(eq_data_->ls[i]->get_solution(), &mass_vec[i]);
483 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
484 MatConvert(*( eq_data_->ls_dt[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &mass_matrix[i]);
487 MatCopy(*( eq_data_->ls_dt[i]->get_matrix() ), mass_matrix[i], DIFFERENT_NONZERO_PATTERN);
492 if (stiffness_matrix[0] == NULL
494 || eq_fields_->flow_flux.changed())
498 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
500 eq_data_->ls[i]->start_add_assembly();
501 eq_data_->ls[i]->mat_zero_entries();
503 stiffness_assembly_->assemble(eq_data_->dh_);
504 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
506 eq_data_->ls[i]->finish_assembly();
508 if (stiffness_matrix[i] == NULL)
509 MatConvert(*( eq_data_->ls[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &stiffness_matrix[i]);
511 MatCopy(*( eq_data_->ls[i]->get_matrix() ), stiffness_matrix[i], DIFFERENT_NONZERO_PATTERN);
518 || eq_fields_->flow_flux.changed())
520 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
522 eq_data_->ls[i]->start_add_assembly();
523 eq_data_->ls[i]->rhs_zero_entries();
525 sources_assembly_->assemble(eq_data_->dh_);
526 bdr_cond_assembly_->assemble(eq_data_->dh_);
527 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
529 eq_data_->ls[i]->finish_assembly();
531 if (rhs[i] ==
nullptr) VecDuplicate(*( eq_data_->ls[i]->get_rhs() ), &rhs[i]);
532 VecCopy(*( eq_data_->ls[i]->get_rhs() ), rhs[i]);
554 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
556 MatConvert(stiffness_matrix[i], MATSAME, MAT_INITIAL_MATRIX, &m);
557 MatAXPY(m, 1./Model::time_->dt(), mass_matrix[i], SUBSET_NONZERO_PATTERN);
558 eq_data_->ls[i]->set_matrix(m, DIFFERENT_NONZERO_PATTERN);
560 VecDuplicate(rhs[i], &w);
561 VecWAXPY(w, 1./Model::time_->dt(), mass_vec[i], rhs[i]);
562 eq_data_->ls[i]->set_rhs(w);
567 eq_data_->ls[i]->solve();
570 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
581 calculate_cumulative_balance();
587 template<
class Model>
591 for (
auto cell : eq_data_->dh_->own_range() )
593 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
594 unsigned int n_dofs=loc_dof_indices.n_rows;
596 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
599 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
601 eq_data_->conc_fe[sbi]->vec().set(dof_p0, 0);
603 for (
unsigned int j=0; j<n_dofs; ++j)
604 eq_data_->conc_fe[sbi]->vec().add( dof_p0, eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]] );
606 eq_data_->conc_fe[sbi]->vec().set( dof_p0, max(eq_data_->conc_fe[sbi]->vec().get(dof_p0)/n_dofs, 0.) );
614 template<
class Model>
625 eq_fields_->output_fields.output(this->
time().step());
627 Model::output_data();
630 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
631 Model::balance_->calculate_instant(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
632 Model::balance_->output();
639 template<
class Model>
642 if (Model::balance_->cumulative())
644 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
646 Model::balance_->calculate_cumulative(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
649 VecDot(eq_data_->ret_vec[sbi], eq_data_->ls[sbi]->get_solution(), &ret_sources[sbi]);
651 Model::balance_->add_cumulative_source(eq_data_->subst_idx_[sbi], (ret_sources[sbi]-ret_sources_prev[sbi])/Model::time_->dt());
652 ret_sources_prev[sbi] = ret_sources[sbi];
660 template<
class Model>
665 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
666 eq_data_->ls[sbi]->start_allocation();
668 init_assembly_->assemble(eq_data_->dh_);
670 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
671 eq_data_->ls[sbi]->start_add_assembly();
673 init_assembly_->assemble(eq_data_->dh_);
675 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
677 eq_data_->ls[sbi]->finish_assembly();
678 eq_data_->ls[sbi]->solve();
682 init_assembly_->assemble(eq_data_->dh_);
686 template<
class Model>
689 el_4_loc = Model::mesh_->get_el_4_loc();
690 el_ds = Model::mesh_->get_el_ds();
694 template<
class Model>
697 if (solution_changed)
699 for (
auto cell : eq_data_->dh_->own_range() )
701 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
702 unsigned int n_dofs=loc_dof_indices.n_rows;
704 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
707 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
709 double old_average = 0;
710 for (
unsigned int j=0; j<n_dofs; ++j)
711 old_average += eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]];
712 old_average /= n_dofs;
714 for (
unsigned int j=0; j<n_dofs; ++j)
715 eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]]
716 += eq_data_->conc_fe[sbi]->vec().get(dof_p0) - old_average;
721 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
722 MatMult(*(eq_data_->ls_dt[sbi]->get_matrix()), eq_data_->ls[sbi]->get_solution(), mass_vec[sbi]);
725 template<
class Model>
728 return Model::mesh_->get_row_4_el();