55 return Selection(
"DG_variant",
"Type of penalty term.")
56 .
add_value(non_symmetric,
"non-symmetric",
"non-symmetric weighted interior penalty DG method")
57 .
add_value(incomplete,
"incomplete",
"incomplete weighted interior penalty DG method")
58 .
add_value(symmetric,
"symmetric",
"symmetric weighted interior penalty DG method")
81 std::string equation_name = std::string(Model::ModelEqData::name()) +
"_DG";
82 return Model::get_input_type(
"DG",
"Discontinuous Galerkin (DG) solver")
85 "Solver for the linear system.")
88 .make_field_descriptor_type(equation_name)),
90 "Input fields of the equation.")
92 "Variant of the interior penalty discontinuous Galerkin method.")
94 "Polynomial order for the finite element in DG method (order 0 is suitable if there is no diffusion/dispersion).")
96 "If true, use DG projection of the initial condition field."
97 "Otherwise, evaluate initial condition field directly (well suited for reading native data).")
99 EqFields().output_fields.make_output_type(equation_name,
""),
100 IT::Default(
"{ \"fields\": [ " + Model::ModelEqData::default_output_field() +
"] }"),
101 "Specification of output fields and output times.")
105 template<
class Model>
107 Input::register_class< TransportDG<Model>,
Mesh &,
const Input::Record>(std::string(Model::ModelEqData::name()) +
"_DG") +
112 template<
class Model>
116 .
name(
"fracture_sigma")
118 "Coefficient of diffusive transfer through fractures (for each substance).")
126 "Penalty parameter influencing the discontinuity of the solution (for each substance). "
127 "Its default value 1 is sufficient in most cases. Higher value diminishes the inter-element jumps.")
140 .
description(
"Subdomain ids of the domain decomposition.");
146 this->add_coords_field();
147 this->set_default_fieldset();
153 template<
class Model>
156 double h_max = 0, h_min = numeric_limits<double>::infinity();
157 for (
unsigned int i=0; i<e->
n_nodes(); i++)
158 for (
unsigned int j=i+1; j<e->
n_nodes(); j++)
160 double dist = arma::norm(*e.
node(i) - *e.
node(j));
161 h_max = max(h_max, dist);
162 h_min = min(h_min, dist);
169 template<
class Model>
178 double delta = 0, h = 0;
187 for (
unsigned int i=0; i<side.
n_nodes(); i++)
188 for (
unsigned int j=i+1; j<side.
n_nodes(); j++) {
189 double dist = arma::norm(*side.
node(i) - *side.
node(j));
196 for (
int k=0; k<K_size; k++)
197 delta += dot(K[k]*normal_vector,normal_vector);
200 gamma = 0.5*fabs(flux) + alpha/h*delta*elem_anisotropy(side.
element());
205 template<
typename Model>
207 :
Model(init_mesh, in_rec),
221 Model::init_balance(in_rec);
232 eq_data_->dg_order = in_rec.
val<
unsigned int>(
"dg_order");
234 Model::init_from_input(in_rec);
237 shared_ptr<DiscreteSpace> ds = make_shared<EqualOrderDiscreteSpace>(Model::mesh_, fe);
238 eq_data_->dh_ = make_shared<DOFHandlerMultiDim>(*Model::mesh_);
245 template<
class Model>
248 eq_fields_->set_components(eq_data_->substances_.names());
249 eq_fields_->set_input_list( input_rec.val<
Input::Array>(
"input_fields"), *(Model::time_) );
250 eq_data_->set_time_governor(Model::time_);
251 eq_data_->balance_ = this->
balance();
252 eq_fields_->initialize(input_rec);
255 eq_data_->gamma.resize(eq_data_->n_substances());
256 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
257 eq_data_->gamma[sbi].resize(Model::mesh_->boundary_.size());
260 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)));
261 ret_sources.resize(eq_data_->n_substances());
262 ret_sources_prev.resize(eq_data_->n_substances());
265 if (input_rec.opt_val(
"user_fields", user_fields_arr)) {
269 eq_data_->output_vec.resize(eq_data_->n_substances());
270 eq_fields_->output_field.set_components(eq_data_->substances_.names());
271 eq_fields_->output_field.set_mesh(*Model::mesh_);
272 eq_fields_->output_fields.set_mesh(*Model::mesh_);
275 eq_fields_->output_field.setup_components();
276 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
279 auto output_field_ptr = create_field_fe< 3, FieldValue<3>::Scalar >(eq_data_->dh_);
280 eq_fields_->output_field[sbi].set(output_field_ptr, 0);
281 eq_data_->output_vec[sbi] = output_field_ptr->vec();
285 eq_fields_->output_fields.initialize(Model::output_stream_, Model::mesh_, input_rec.val<
Input::Record>(
"output"), this->
time());
288 std::string petsc_default_opts;
289 if (eq_data_->dh_->distr()->np() == 1)
290 petsc_default_opts =
"-ksp_type bcgs -pc_type ilu -pc_factor_levels 2 -ksp_diagonal_scale_fix -pc_factor_fill 6.0";
292 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";
295 eq_data_->ls =
new LinSys*[eq_data_->n_substances()];
296 eq_data_->ls_dt =
new LinSys*[eq_data_->n_substances()];
297 eq_data_->conc_fe.resize(eq_data_->n_substances());
300 shared_ptr<DiscreteSpace> ds = make_shared<EqualOrderDiscreteSpace>(Model::mesh_, fe);
301 eq_data_->dh_p0 = make_shared<DOFHandlerMultiDim>(*Model::mesh_);
302 eq_data_->dh_p0->distribute_dofs(ds);
304 stiffness_matrix.resize(eq_data_->n_substances(),
nullptr);
305 mass_matrix.resize(eq_data_->n_substances(),
nullptr);
306 rhs.resize(eq_data_->n_substances(),
nullptr);
307 mass_vec.resize(eq_data_->n_substances(),
nullptr);
308 eq_data_->ret_vec.resize(eq_data_->n_substances(),
nullptr);
310 for (
unsigned int sbi = 0; sbi < eq_data_->n_substances(); sbi++) {
311 eq_data_->ls[sbi] =
new LinSys_PETSC(eq_data_->dh_->distr().get(), petsc_default_opts);
313 eq_data_->ls[sbi]->set_solution(eq_data_->output_vec[sbi].petsc_vec());
315 eq_data_->ls_dt[sbi] =
new LinSys_PETSC(eq_data_->dh_->distr().get(), petsc_default_opts);
318 eq_data_->conc_fe[sbi] = create_field_fe< 3, FieldValue<3>::Scalar >(eq_data_->dh_p0);
320 VecDuplicate(eq_data_->ls[sbi]->get_solution(), &eq_data_->ret_vec[sbi]);
324 init_projection = input_rec.val<
bool>(
"init_projection");
338 Model::balance_->allocate(eq_data_->dh_->distr()->lsize(), mass_assembly_->eval_points()->max_size());
340 int qsize = mass_assembly_->eval_points()->max_size();
341 eq_data_->dif_coef.resize(eq_data_->n_substances());
342 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
344 eq_data_->dif_coef[sbi].resize(qsize);
347 eq_fields_->init_condition.setup_components();
348 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
355 template<
class Model>
360 if (eq_data_->gamma.size() > 0) {
363 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
365 if (eq_data_->ls !=
nullptr) {
366 delete eq_data_->ls[i];
367 delete eq_data_->ls_dt[i];
370 if (stiffness_matrix.size() > 0) {
371 if (stiffness_matrix[i])
372 chkerr(MatDestroy(&stiffness_matrix[i]));
374 chkerr(MatDestroy(&mass_matrix[i]));
376 chkerr(VecDestroy(&rhs[i]));
378 chkerr(VecDestroy(&mass_vec[i]));
379 if (eq_data_->ret_vec[i])
380 chkerr(VecDestroy(&eq_data_->ret_vec[i]));
383 if (eq_data_->ls !=
nullptr) {
384 delete[] eq_data_->ls;
385 delete[] eq_data_->ls_dt;
386 eq_data_->ls =
nullptr;
394 if (mass_assembly_ !=
nullptr) {
395 delete mass_assembly_;
396 delete stiffness_assembly_;
397 delete sources_assembly_;
398 delete bdr_cond_assembly_;
399 delete init_assembly_;
407 template<
class Model>
411 eq_fields_->mark_input_times( *(Model::time_) );
413 std::stringstream ss;
420 set_initial_condition();
421 for (
unsigned int sbi = 0; sbi < eq_data_->n_substances(); sbi++)
422 ( (
LinSys_PETSC *)eq_data_->ls[sbi] )->set_initial_guess_nonzero();
425 if (!allocation_done) preallocate();
428 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
430 Model::balance_->calculate_instant(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
433 ret_sources_prev[sbi] = 0;
440 template<
class Model>
444 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
447 eq_data_->ls[i]->start_allocation();
448 stiffness_matrix[i] = NULL;
452 eq_data_->ls_dt[i]->start_allocation();
453 mass_matrix[i] = NULL;
454 VecZeroEntries(eq_data_->ret_vec[i]);
456 stiffness_assembly_->assemble(eq_data_->dh_);
457 mass_assembly_->assemble(eq_data_->dh_);
458 sources_assembly_->assemble(eq_data_->dh_);
459 bdr_cond_assembly_->assemble(eq_data_->dh_);
460 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
462 VecAssemblyBegin(eq_data_->ret_vec[i]);
463 VecAssemblyEnd(eq_data_->ret_vec[i]);
466 allocation_done =
true;
471 template<
class Model>
476 Model::time_->next_time();
477 Model::time_->view(
"TDG");
486 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
488 eq_data_->ls_dt[i]->start_add_assembly();
489 eq_data_->ls_dt[i]->mat_zero_entries();
490 VecZeroEntries(eq_data_->ret_vec[i]);
492 mass_assembly_->assemble(eq_data_->dh_);
493 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
495 eq_data_->ls_dt[i]->finish_assembly();
496 VecAssemblyBegin(eq_data_->ret_vec[i]);
497 VecAssemblyEnd(eq_data_->ret_vec[i]);
499 if (mass_matrix[i] == NULL)
501 VecDuplicate(eq_data_->ls[i]->get_solution(), &mass_vec[i]);
502 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
503 MatConvert(*( eq_data_->ls_dt[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &mass_matrix[i]);
506 MatCopy(*( eq_data_->ls_dt[i]->get_matrix() ), mass_matrix[i], DIFFERENT_NONZERO_PATTERN);
511 if (stiffness_matrix[0] == NULL
513 || eq_fields_->flow_flux.changed())
517 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
519 eq_data_->ls[i]->start_add_assembly();
520 eq_data_->ls[i]->mat_zero_entries();
522 stiffness_assembly_->assemble(eq_data_->dh_);
523 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
525 eq_data_->ls[i]->finish_assembly();
527 if (stiffness_matrix[i] == NULL)
528 MatConvert(*( eq_data_->ls[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &stiffness_matrix[i]);
530 MatCopy(*( eq_data_->ls[i]->get_matrix() ), stiffness_matrix[i], DIFFERENT_NONZERO_PATTERN);
537 || eq_fields_->flow_flux.changed())
539 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
541 eq_data_->ls[i]->start_add_assembly();
542 eq_data_->ls[i]->rhs_zero_entries();
544 sources_assembly_->assemble(eq_data_->dh_);
545 bdr_cond_assembly_->assemble(eq_data_->dh_);
546 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
548 eq_data_->ls[i]->finish_assembly();
550 if (rhs[i] ==
nullptr) VecDuplicate(*( eq_data_->ls[i]->get_rhs() ), &rhs[i]);
551 VecCopy(*( eq_data_->ls[i]->get_rhs() ), rhs[i]);
573 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
575 MatConvert(stiffness_matrix[i], MATSAME, MAT_INITIAL_MATRIX, &m);
576 MatAXPY(m, 1./Model::time_->dt(), mass_matrix[i], SUBSET_NONZERO_PATTERN);
577 eq_data_->ls[i]->set_matrix(m, DIFFERENT_NONZERO_PATTERN);
579 VecDuplicate(rhs[i], &w);
580 VecWAXPY(w, 1./Model::time_->dt(), mass_vec[i], rhs[i]);
581 eq_data_->ls[i]->set_rhs(w);
586 eq_data_->ls[i]->solve();
589 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
600 calculate_cumulative_balance();
606 template<
class Model>
610 for (
auto cell : eq_data_->dh_->own_range() )
612 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
613 unsigned int n_dofs=loc_dof_indices.n_rows;
615 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
618 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
620 eq_data_->conc_fe[sbi]->vec().set(dof_p0, 0);
622 for (
unsigned int j=0; j<n_dofs; ++j)
623 eq_data_->conc_fe[sbi]->vec().add( dof_p0, eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]] );
625 eq_data_->conc_fe[sbi]->vec().set( dof_p0, max(eq_data_->conc_fe[sbi]->vec().get(dof_p0)/n_dofs, 0.) );
633 template<
class Model>
644 eq_fields_->output_fields.output(this->
time().step());
646 Model::output_data();
649 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
650 Model::balance_->calculate_instant(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
651 Model::balance_->output();
658 template<
class Model>
661 if (Model::balance_->cumulative())
663 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
665 Model::balance_->calculate_cumulative(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
668 VecDot(eq_data_->ret_vec[sbi], eq_data_->ls[sbi]->get_solution(), &ret_sources[sbi]);
670 Model::balance_->add_cumulative_source(eq_data_->subst_idx_[sbi], (ret_sources[sbi]-ret_sources_prev[sbi])/Model::time_->dt());
671 ret_sources_prev[sbi] = ret_sources[sbi];
679 template<
class Model>
684 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
685 eq_data_->ls[sbi]->start_allocation();
687 init_assembly_->assemble(eq_data_->dh_);
689 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
690 eq_data_->ls[sbi]->start_add_assembly();
692 init_assembly_->assemble(eq_data_->dh_);
694 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
696 eq_data_->ls[sbi]->finish_assembly();
697 eq_data_->ls[sbi]->solve();
701 init_assembly_->assemble(eq_data_->dh_);
705 template<
class Model>
708 if (solution_changed)
710 for (
auto cell : eq_data_->dh_->own_range() )
712 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
713 unsigned int n_dofs=loc_dof_indices.n_rows;
715 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
718 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
720 double old_average = 0;
721 for (
unsigned int j=0; j<n_dofs; ++j)
722 old_average += eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]];
723 old_average /= n_dofs;
725 for (
unsigned int j=0; j<n_dofs; ++j)
726 eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]]
727 += eq_data_->conc_fe[sbi]->vec().get(dof_p0) - old_average;
732 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
733 MatMult(*(eq_data_->ls_dt[sbi]->get_matrix()), eq_data_->ls[sbi]->get_solution(), mass_vec[sbi]);