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")
84 "Solver for the linear system.")
87 .make_field_descriptor_type(equation_name)),
89 "Input fields of the equation.")
91 "Variant of the interior penalty discontinuous Galerkin method.")
93 "Polynomial order for the finite element in DG method (order 0 is suitable if there is no diffusion/dispersion).")
95 "If true, use DG projection of the initial condition field."
96 "Otherwise, evaluate initial condition field directly (well suited for reading native data).")
98 EqFields().output_fields.make_output_type(equation_name,
""),
99 IT::Default(
"{ \"fields\": [ " + Model::ModelEqData::default_output_field() +
"] }"),
100 "Specification of output fields and output times.")
104 template<
class Model>
106 Input::register_class< TransportDG<Model>,
Mesh &,
const Input::Record>(std::string(Model::ModelEqData::name()) +
"_DG") +
111 template<
class Model>
115 .
name(
"fracture_sigma")
117 "Coefficient of diffusive transfer through fractures (for each substance).")
125 "Penalty parameter influencing the discontinuity of the solution (for each substance). "
126 "Its default value 1 is sufficient in most cases. Higher value diminishes the inter-element jumps.")
139 .
description(
"Subdomain ids of the domain decomposition.");
145 this->add_coords_field();
146 this->set_default_fieldset();
152 template<
class Model>
155 double h_max = 0, h_min = numeric_limits<double>::infinity();
156 for (
unsigned int i=0; i<e->
n_nodes(); i++)
157 for (
unsigned int j=i+1; j<e->
n_nodes(); j++)
159 double dist = arma::norm(*e.
node(i) - *e.
node(j));
160 h_max = max(h_max, dist);
161 h_min = min(h_min, dist);
168 template<
class Model>
177 double delta = 0, h = 0;
186 for (
unsigned int i=0; i<side.
n_nodes(); i++)
187 for (
unsigned int j=i+1; j<side.
n_nodes(); j++) {
188 double dist = arma::norm(*side.
node(i) - *side.
node(j));
195 for (
int k=0; k<K_size; k++)
196 delta += dot(K[k]*normal_vector,normal_vector);
199 gamma = 0.5*fabs(flux) + alpha/h*delta*elem_anisotropy(side.
element());
204 template<
typename Model>
206 :
Model(init_mesh, in_rec),
220 Model::init_balance(in_rec);
231 eq_data_->dg_order = in_rec.
val<
unsigned int>(
"dg_order");
233 Model::init_from_input(in_rec);
236 shared_ptr<DiscreteSpace> ds = make_shared<EqualOrderDiscreteSpace>(Model::mesh_, fe);
237 eq_data_->dh_ = make_shared<DOFHandlerMultiDim>(*Model::mesh_);
244 template<
class Model>
247 eq_fields_->set_components(eq_data_->substances_.names());
248 eq_fields_->set_input_list( input_rec.val<
Input::Array>(
"input_fields"), *(Model::time_) );
249 eq_data_->set_time_governor(Model::time_);
250 eq_data_->balance_ = this->
balance();
251 eq_fields_->initialize();
254 eq_data_->gamma.resize(eq_data_->n_substances());
255 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
256 eq_data_->gamma[sbi].resize(Model::mesh_->boundary_.size());
259 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)));
260 ret_sources.resize(eq_data_->n_substances());
261 ret_sources_prev.resize(eq_data_->n_substances());
264 if (input_rec.opt_val(
"user_fields", user_fields_arr)) {
268 eq_data_->output_vec.resize(eq_data_->n_substances());
269 eq_fields_->output_field.set_components(eq_data_->substances_.names());
270 eq_fields_->output_field.set_mesh(*Model::mesh_);
271 eq_fields_->output_fields.set_mesh(*Model::mesh_);
274 eq_fields_->output_field.setup_components();
275 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
278 auto output_field_ptr = create_field_fe< 3, FieldValue<3>::Scalar >(eq_data_->dh_);
279 eq_fields_->output_field[sbi].set(output_field_ptr, 0);
280 eq_data_->output_vec[sbi] = output_field_ptr->vec();
284 eq_fields_->output_fields.initialize(Model::output_stream_, Model::mesh_, input_rec.val<
Input::Record>(
"output"), this->
time());
287 std::string petsc_default_opts;
288 if (eq_data_->dh_->distr()->np() == 1)
289 petsc_default_opts =
"-ksp_type bcgs -pc_type ilu -pc_factor_levels 2 -ksp_diagonal_scale_fix -pc_factor_fill 6.0";
291 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";
294 eq_data_->ls =
new LinSys*[eq_data_->n_substances()];
295 eq_data_->ls_dt =
new LinSys*[eq_data_->n_substances()];
296 eq_data_->conc_fe.resize(eq_data_->n_substances());
299 shared_ptr<DiscreteSpace> ds = make_shared<EqualOrderDiscreteSpace>(Model::mesh_, fe);
300 eq_data_->dh_p0 = make_shared<DOFHandlerMultiDim>(*Model::mesh_);
301 eq_data_->dh_p0->distribute_dofs(ds);
303 stiffness_matrix.resize(eq_data_->n_substances(),
nullptr);
304 mass_matrix.resize(eq_data_->n_substances(),
nullptr);
305 rhs.resize(eq_data_->n_substances(),
nullptr);
306 mass_vec.resize(eq_data_->n_substances(),
nullptr);
307 eq_data_->ret_vec.resize(eq_data_->n_substances(),
nullptr);
309 for (
unsigned int sbi = 0; sbi < eq_data_->n_substances(); sbi++) {
310 eq_data_->ls[sbi] =
new LinSys_PETSC(eq_data_->dh_->distr().get(), petsc_default_opts);
312 eq_data_->ls[sbi]->set_solution(eq_data_->output_vec[sbi].petsc_vec());
314 eq_data_->ls_dt[sbi] =
new LinSys_PETSC(eq_data_->dh_->distr().get(), petsc_default_opts);
317 eq_data_->conc_fe[sbi] = create_field_fe< 3, FieldValue<3>::Scalar >(eq_data_->dh_p0);
319 VecDuplicate(eq_data_->ls[sbi]->get_solution(), &eq_data_->ret_vec[sbi]);
323 init_projection = input_rec.val<
bool>(
"init_projection");
337 Model::balance_->allocate(eq_data_->dh_->distr()->lsize(), mass_assembly_->eval_points()->max_size());
339 int qsize = mass_assembly_->eval_points()->max_size();
340 eq_data_->dif_coef.resize(eq_data_->n_substances());
341 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
343 eq_data_->dif_coef[sbi].resize(qsize);
346 eq_fields_->init_condition.setup_components();
347 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
354 template<
class Model>
359 if (eq_data_->gamma.size() > 0) {
362 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
364 if (eq_data_->ls !=
nullptr) {
365 delete eq_data_->ls[i];
366 delete eq_data_->ls_dt[i];
369 if (stiffness_matrix.size() > 0) {
370 if (stiffness_matrix[i])
371 chkerr(MatDestroy(&stiffness_matrix[i]));
373 chkerr(MatDestroy(&mass_matrix[i]));
375 chkerr(VecDestroy(&rhs[i]));
377 chkerr(VecDestroy(&mass_vec[i]));
378 if (eq_data_->ret_vec[i])
379 chkerr(VecDestroy(&eq_data_->ret_vec[i]));
382 if (eq_data_->ls !=
nullptr) {
383 delete[] eq_data_->ls;
384 delete[] eq_data_->ls_dt;
385 eq_data_->ls =
nullptr;
393 if (mass_assembly_ !=
nullptr) {
394 delete mass_assembly_;
395 delete stiffness_assembly_;
396 delete sources_assembly_;
397 delete bdr_cond_assembly_;
398 delete init_assembly_;
406 template<
class Model>
410 eq_fields_->mark_input_times( *(Model::time_) );
412 std::stringstream ss;
419 set_initial_condition();
420 for (
unsigned int sbi = 0; sbi < eq_data_->n_substances(); sbi++)
421 ( (
LinSys_PETSC *)eq_data_->ls[sbi] )->set_initial_guess_nonzero();
424 if (!allocation_done) preallocate();
427 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
429 Model::balance_->calculate_instant(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
432 ret_sources_prev[sbi] = 0;
439 template<
class Model>
443 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
446 eq_data_->ls[i]->start_allocation();
447 stiffness_matrix[i] = NULL;
451 eq_data_->ls_dt[i]->start_allocation();
452 mass_matrix[i] = NULL;
453 VecZeroEntries(eq_data_->ret_vec[i]);
455 stiffness_assembly_->assemble(eq_data_->dh_);
456 mass_assembly_->assemble(eq_data_->dh_);
457 sources_assembly_->assemble(eq_data_->dh_);
458 bdr_cond_assembly_->assemble(eq_data_->dh_);
459 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
461 VecAssemblyBegin(eq_data_->ret_vec[i]);
462 VecAssemblyEnd(eq_data_->ret_vec[i]);
465 allocation_done =
true;
470 template<
class Model>
475 Model::time_->next_time();
476 Model::time_->view(
"TDG");
485 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
487 eq_data_->ls_dt[i]->start_add_assembly();
488 eq_data_->ls_dt[i]->mat_zero_entries();
489 VecZeroEntries(eq_data_->ret_vec[i]);
491 mass_assembly_->assemble(eq_data_->dh_);
492 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
494 eq_data_->ls_dt[i]->finish_assembly();
495 VecAssemblyBegin(eq_data_->ret_vec[i]);
496 VecAssemblyEnd(eq_data_->ret_vec[i]);
498 if (mass_matrix[i] == NULL)
500 VecDuplicate(eq_data_->ls[i]->get_solution(), &mass_vec[i]);
501 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
502 MatConvert(*( eq_data_->ls_dt[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &mass_matrix[i]);
505 MatCopy(*( eq_data_->ls_dt[i]->get_matrix() ), mass_matrix[i], DIFFERENT_NONZERO_PATTERN);
510 if (stiffness_matrix[0] == NULL
512 || eq_fields_->flow_flux.changed())
516 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
518 eq_data_->ls[i]->start_add_assembly();
519 eq_data_->ls[i]->mat_zero_entries();
521 stiffness_assembly_->assemble(eq_data_->dh_);
522 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
524 eq_data_->ls[i]->finish_assembly();
526 if (stiffness_matrix[i] == NULL)
527 MatConvert(*( eq_data_->ls[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &stiffness_matrix[i]);
529 MatCopy(*( eq_data_->ls[i]->get_matrix() ), stiffness_matrix[i], DIFFERENT_NONZERO_PATTERN);
536 || eq_fields_->flow_flux.changed())
538 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
540 eq_data_->ls[i]->start_add_assembly();
541 eq_data_->ls[i]->rhs_zero_entries();
543 sources_assembly_->assemble(eq_data_->dh_);
544 bdr_cond_assembly_->assemble(eq_data_->dh_);
545 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
547 eq_data_->ls[i]->finish_assembly();
549 if (rhs[i] ==
nullptr) VecDuplicate(*( eq_data_->ls[i]->get_rhs() ), &rhs[i]);
550 VecCopy(*( eq_data_->ls[i]->get_rhs() ), rhs[i]);
572 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
574 MatConvert(stiffness_matrix[i], MATSAME, MAT_INITIAL_MATRIX, &m);
575 MatAXPY(m, 1./Model::time_->dt(), mass_matrix[i], SUBSET_NONZERO_PATTERN);
576 eq_data_->ls[i]->set_matrix(m, DIFFERENT_NONZERO_PATTERN);
578 VecDuplicate(rhs[i], &w);
579 VecWAXPY(w, 1./Model::time_->dt(), mass_vec[i], rhs[i]);
580 eq_data_->ls[i]->set_rhs(w);
585 eq_data_->ls[i]->solve();
588 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
599 calculate_cumulative_balance();
605 template<
class Model>
609 for (
auto cell : eq_data_->dh_->own_range() )
611 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
612 unsigned int n_dofs=loc_dof_indices.n_rows;
614 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
617 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
619 eq_data_->conc_fe[sbi]->vec().set(dof_p0, 0);
621 for (
unsigned int j=0; j<n_dofs; ++j)
622 eq_data_->conc_fe[sbi]->vec().add( dof_p0, eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]] );
624 eq_data_->conc_fe[sbi]->vec().set( dof_p0, max(eq_data_->conc_fe[sbi]->vec().get(dof_p0)/n_dofs, 0.) );
632 template<
class Model>
643 eq_fields_->output_fields.output(this->
time().step());
645 Model::output_data();
648 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
649 Model::balance_->calculate_instant(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
650 Model::balance_->output();
657 template<
class Model>
660 if (Model::balance_->cumulative())
662 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
664 Model::balance_->calculate_cumulative(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
667 VecDot(eq_data_->ret_vec[sbi], eq_data_->ls[sbi]->get_solution(), &ret_sources[sbi]);
669 Model::balance_->add_cumulative_source(eq_data_->subst_idx_[sbi], (ret_sources[sbi]-ret_sources_prev[sbi])/Model::time_->dt());
670 ret_sources_prev[sbi] = ret_sources[sbi];
678 template<
class Model>
683 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
684 eq_data_->ls[sbi]->start_allocation();
686 init_assembly_->assemble(eq_data_->dh_);
688 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
689 eq_data_->ls[sbi]->start_add_assembly();
691 init_assembly_->assemble(eq_data_->dh_);
693 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
695 eq_data_->ls[sbi]->finish_assembly();
696 eq_data_->ls[sbi]->solve();
700 init_assembly_->assemble(eq_data_->dh_);
704 template<
class Model>
707 if (solution_changed)
709 for (
auto cell : eq_data_->dh_->own_range() )
711 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
712 unsigned int n_dofs=loc_dof_indices.n_rows;
714 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
717 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
719 double old_average = 0;
720 for (
unsigned int j=0; j<n_dofs; ++j)
721 old_average += eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]];
722 old_average /= n_dofs;
724 for (
unsigned int j=0; j<n_dofs; ++j)
725 eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]]
726 += eq_data_->conc_fe[sbi]->vec().get(dof_p0) - old_average;
731 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
732 MatMult(*(eq_data_->ls_dt[sbi]->get_matrix()), eq_data_->ls[sbi]->get_solution(), mass_vec[sbi]);