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]);
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 ((
LinSys_PETSC*)eq_data_->ls[i])->preallocate_matrix(*eq_data_->dh_);
463 VecAssemblyBegin(eq_data_->ret_vec[i]);
464 VecAssemblyEnd(eq_data_->ret_vec[i]);
467 allocation_done =
true;
472 template<
class Model>
477 Model::time_->next_time();
478 Model::time_->view(
"TDG");
487 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
489 eq_data_->ls_dt[i]->start_add_assembly();
490 eq_data_->ls_dt[i]->mat_zero_entries();
491 VecZeroEntries(eq_data_->ret_vec[i]);
493 mass_assembly_->assemble(eq_data_->dh_);
494 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
496 eq_data_->ls_dt[i]->finish_assembly();
497 VecAssemblyBegin(eq_data_->ret_vec[i]);
498 VecAssemblyEnd(eq_data_->ret_vec[i]);
500 if (mass_matrix[i] == NULL)
502 VecDuplicate(eq_data_->ls[i]->get_solution(), &mass_vec[i]);
503 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
504 MatConvert(*( eq_data_->ls_dt[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &mass_matrix[i]);
507 MatCopy(*( eq_data_->ls_dt[i]->get_matrix() ), mass_matrix[i], DIFFERENT_NONZERO_PATTERN);
512 if (stiffness_matrix[0] == NULL
514 || eq_fields_->flow_flux.changed())
518 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
520 eq_data_->ls[i]->start_add_assembly();
521 eq_data_->ls[i]->mat_zero_entries();
523 stiffness_assembly_->assemble(eq_data_->dh_);
524 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
526 eq_data_->ls[i]->finish_assembly();
528 if (stiffness_matrix[i] == NULL)
529 MatConvert(*( eq_data_->ls[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &stiffness_matrix[i]);
531 MatCopy(*( eq_data_->ls[i]->get_matrix() ), stiffness_matrix[i], DIFFERENT_NONZERO_PATTERN);
538 || eq_fields_->flow_flux.changed())
540 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
542 eq_data_->ls[i]->start_add_assembly();
543 eq_data_->ls[i]->rhs_zero_entries();
545 sources_assembly_->assemble(eq_data_->dh_);
546 bdr_cond_assembly_->assemble(eq_data_->dh_);
547 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
549 eq_data_->ls[i]->finish_assembly();
551 if (rhs[i] ==
nullptr) VecDuplicate(*( eq_data_->ls[i]->get_rhs() ), &rhs[i]);
552 VecCopy(*( eq_data_->ls[i]->get_rhs() ), rhs[i]);
574 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
576 MatConvert(stiffness_matrix[i], MATSAME, MAT_INITIAL_MATRIX, &m);
577 MatAXPY(m, 1./Model::time_->dt(), mass_matrix[i], SUBSET_NONZERO_PATTERN);
578 eq_data_->ls[i]->set_matrix(m, DIFFERENT_NONZERO_PATTERN);
580 VecDuplicate(rhs[i], &w);
581 VecWAXPY(w, 1./Model::time_->dt(), mass_vec[i], rhs[i]);
582 eq_data_->ls[i]->set_rhs(w);
587 eq_data_->ls[i]->solve();
590 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
601 calculate_cumulative_balance();
607 template<
class Model>
611 for (
auto cell : eq_data_->dh_->own_range() )
613 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
614 unsigned int n_dofs=loc_dof_indices.n_rows;
616 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
619 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
621 eq_data_->conc_fe[sbi]->vec().set(dof_p0, 0);
623 for (
unsigned int j=0; j<n_dofs; ++j)
624 eq_data_->conc_fe[sbi]->vec().add( dof_p0, eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]] );
626 eq_data_->conc_fe[sbi]->vec().set( dof_p0, max(eq_data_->conc_fe[sbi]->vec().get(dof_p0)/n_dofs, 0.) );
634 template<
class Model>
645 eq_fields_->output_fields.output(this->
time().step());
647 Model::output_data();
650 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
651 Model::balance_->calculate_instant(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
652 Model::balance_->output();
659 template<
class Model>
662 if (Model::balance_->cumulative())
664 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
666 Model::balance_->calculate_cumulative(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
669 VecDot(eq_data_->ret_vec[sbi], eq_data_->ls[sbi]->get_solution(), &ret_sources[sbi]);
671 Model::balance_->add_cumulative_source(eq_data_->subst_idx_[sbi], (ret_sources[sbi]-ret_sources_prev[sbi])/Model::time_->dt());
672 ret_sources_prev[sbi] = ret_sources[sbi];
680 template<
class Model>
685 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
686 eq_data_->ls[sbi]->start_allocation();
688 init_assembly_->assemble(eq_data_->dh_);
690 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
691 eq_data_->ls[sbi]->start_add_assembly();
693 init_assembly_->assemble(eq_data_->dh_);
695 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
697 eq_data_->ls[sbi]->finish_assembly();
698 eq_data_->ls[sbi]->solve();
702 init_assembly_->assemble(eq_data_->dh_);
706 template<
class Model>
709 if (solution_changed)
711 for (
auto cell : eq_data_->dh_->own_range() )
713 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
714 unsigned int n_dofs=loc_dof_indices.n_rows;
716 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
719 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
721 double old_average = 0;
722 for (
unsigned int j=0; j<n_dofs; ++j)
723 old_average += eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]];
724 old_average /= n_dofs;
726 for (
unsigned int j=0; j<n_dofs; ++j)
727 eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]]
728 += eq_data_->conc_fe[sbi]->vec().get(dof_p0) - old_average;
733 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
734 MatMult(*(eq_data_->ls_dt[sbi]->get_matrix()), eq_data_->ls[sbi]->get_solution(), mass_vec[sbi]);