53 return Selection(
"DG_variant",
"Type of penalty term.")
54 .
add_value(non_symmetric,
"non-symmetric",
"non-symmetric weighted interior penalty DG method")
55 .
add_value(incomplete,
"incomplete",
"incomplete weighted interior penalty DG method")
56 .
add_value(symmetric,
"symmetric",
"symmetric weighted interior penalty DG method")
79 std::string equation_name = std::string(Model::ModelEqData::name()) +
"_DG";
80 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.");
144 this->add_coords_field();
145 this->set_default_fieldset();
151 template<
class Model>
154 double h_max = 0, h_min = numeric_limits<double>::infinity();
155 for (
unsigned int i=0; i<e->
n_nodes(); i++)
156 for (
unsigned int j=i+1; j<e->
n_nodes(); j++)
158 double dist = arma::norm(*e.
node(i) - *e.
node(j));
159 h_max = max(h_max, dist);
160 h_min = min(h_min, dist);
167 template<
class Model>
176 double delta = 0, h = 0;
185 for (
unsigned int i=0; i<side.
n_nodes(); i++)
186 for (
unsigned int j=i+1; j<side.
n_nodes(); j++) {
187 double dist = arma::norm(*side.
node(i) - *side.
node(j));
194 for (
int k=0; k<K_size; k++)
195 delta += dot(K[k]*normal_vector,normal_vector);
198 gamma = 0.5*fabs(flux) + alpha/h*delta*elem_anisotropy(side.
element());
203 template<
typename Model>
205 :
Model(init_mesh, in_rec),
219 Model::init_balance(in_rec);
230 eq_data_->dg_order = in_rec.
val<
unsigned int>(
"dg_order");
232 Model::init_from_input(in_rec);
235 shared_ptr<DiscreteSpace> ds = make_shared<EqualOrderDiscreteSpace>(Model::mesh_, fe);
236 eq_data_->dh_ = make_shared<DOFHandlerMultiDim>(*Model::mesh_);
243 template<
class Model>
246 eq_fields_->set_components(eq_data_->substances_.names());
247 eq_fields_->set_input_list( input_rec.val<
Input::Array>(
"input_fields"), *(Model::time_) );
248 eq_data_->set_time_governor(Model::time_);
249 eq_data_->balance_ = this->
balance();
250 eq_fields_->initialize();
253 eq_data_->gamma.resize(eq_data_->n_substances());
254 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
255 eq_data_->gamma[sbi].resize(Model::mesh_->boundary_.size());
258 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)));
259 ret_sources.resize(eq_data_->n_substances());
260 ret_sources_prev.resize(eq_data_->n_substances());
263 if (input_rec.opt_val(
"user_fields", user_fields_arr)) {
267 eq_data_->output_vec.resize(eq_data_->n_substances());
268 eq_fields_->output_field.set_components(eq_data_->substances_.names());
269 eq_fields_->output_field.set_mesh(*Model::mesh_);
270 eq_fields_->output_fields.set_mesh(*Model::mesh_);
273 eq_fields_->output_field.setup_components();
274 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
277 auto output_field_ptr = create_field_fe< 3, FieldValue<3>::Scalar >(eq_data_->dh_);
278 eq_fields_->output_field[sbi].set(output_field_ptr, 0);
279 eq_data_->output_vec[sbi] = output_field_ptr->vec();
283 eq_fields_->output_fields.initialize(Model::output_stream_, Model::mesh_, input_rec.val<
Input::Record>(
"output"), this->
time());
286 std::string petsc_default_opts;
287 if (eq_data_->dh_->distr()->np() == 1)
288 petsc_default_opts =
"-ksp_type bcgs -pc_type ilu -pc_factor_levels 2 -ksp_diagonal_scale_fix -pc_factor_fill 6.0";
290 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";
293 eq_data_->ls =
new LinSys*[eq_data_->n_substances()];
294 eq_data_->ls_dt =
new LinSys*[eq_data_->n_substances()];
295 eq_data_->conc_fe.resize(eq_data_->n_substances());
298 shared_ptr<DiscreteSpace> ds = make_shared<EqualOrderDiscreteSpace>(Model::mesh_, fe);
299 eq_data_->dh_p0 = make_shared<DOFHandlerMultiDim>(*Model::mesh_);
300 eq_data_->dh_p0->distribute_dofs(ds);
302 stiffness_matrix.resize(eq_data_->n_substances(),
nullptr);
303 mass_matrix.resize(eq_data_->n_substances(),
nullptr);
304 rhs.resize(eq_data_->n_substances(),
nullptr);
305 mass_vec.resize(eq_data_->n_substances(),
nullptr);
306 eq_data_->ret_vec.resize(eq_data_->n_substances(),
nullptr);
308 for (
unsigned int sbi = 0; sbi < eq_data_->n_substances(); sbi++) {
309 eq_data_->ls[sbi] =
new LinSys_PETSC(eq_data_->dh_->distr().get(), petsc_default_opts);
311 eq_data_->ls[sbi]->set_solution(eq_data_->output_vec[sbi].petsc_vec());
313 eq_data_->ls_dt[sbi] =
new LinSys_PETSC(eq_data_->dh_->distr().get(), petsc_default_opts);
316 eq_data_->conc_fe[sbi] = create_field_fe< 3, FieldValue<3>::Scalar >(eq_data_->dh_p0);
318 VecDuplicate(eq_data_->ls[sbi]->get_solution(), &eq_data_->ret_vec[sbi]);
322 init_projection = input_rec.val<
bool>(
"init_projection");
336 Model::balance_->allocate(eq_data_->dh_->distr()->lsize(), mass_assembly_->eval_points()->max_size());
338 int qsize = mass_assembly_->eval_points()->max_size();
339 eq_data_->dif_coef.resize(eq_data_->n_substances());
340 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
342 eq_data_->dif_coef[sbi].resize(qsize);
345 eq_fields_->init_condition.setup_components();
346 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
353 template<
class Model>
358 if (eq_data_->gamma.size() > 0) {
361 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
363 if (eq_data_->ls !=
nullptr) {
364 delete eq_data_->ls[i];
365 delete eq_data_->ls_dt[i];
368 if (stiffness_matrix.size() > 0) {
369 if (stiffness_matrix[i])
370 chkerr(MatDestroy(&stiffness_matrix[i]));
372 chkerr(MatDestroy(&mass_matrix[i]));
374 chkerr(VecDestroy(&rhs[i]));
376 chkerr(VecDestroy(&mass_vec[i]));
377 if (eq_data_->ret_vec[i])
378 chkerr(VecDestroy(&eq_data_->ret_vec[i]));
381 if (eq_data_->ls !=
nullptr) {
382 delete[] eq_data_->ls;
383 delete[] eq_data_->ls_dt;
384 eq_data_->ls =
nullptr;
392 if (mass_assembly_ !=
nullptr) {
393 delete mass_assembly_;
394 delete stiffness_assembly_;
395 delete sources_assembly_;
396 delete bdr_cond_assembly_;
397 delete init_assembly_;
405 template<
class Model>
409 eq_fields_->mark_input_times( *(Model::time_) );
411 std::stringstream ss;
418 set_initial_condition();
419 for (
unsigned int sbi = 0; sbi < eq_data_->n_substances(); sbi++)
420 ( (
LinSys_PETSC *)eq_data_->ls[sbi] )->set_initial_guess_nonzero();
423 if (!allocation_done) preallocate();
426 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
428 Model::balance_->calculate_instant(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
431 ret_sources_prev[sbi] = 0;
438 template<
class Model>
442 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
445 eq_data_->ls[i]->start_allocation();
446 stiffness_matrix[i] = NULL;
450 eq_data_->ls_dt[i]->start_allocation();
451 mass_matrix[i] = NULL;
452 VecZeroEntries(eq_data_->ret_vec[i]);
454 stiffness_assembly_->assemble(eq_data_->dh_);
455 mass_assembly_->assemble(eq_data_->dh_);
456 sources_assembly_->assemble(eq_data_->dh_);
457 bdr_cond_assembly_->assemble(eq_data_->dh_);
458 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
460 VecAssemblyBegin(eq_data_->ret_vec[i]);
461 VecAssemblyEnd(eq_data_->ret_vec[i]);
464 allocation_done =
true;
469 template<
class Model>
474 Model::time_->next_time();
475 Model::time_->view(
"TDG");
484 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
486 eq_data_->ls_dt[i]->start_add_assembly();
487 eq_data_->ls_dt[i]->mat_zero_entries();
488 VecZeroEntries(eq_data_->ret_vec[i]);
490 mass_assembly_->assemble(eq_data_->dh_);
491 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
493 eq_data_->ls_dt[i]->finish_assembly();
494 VecAssemblyBegin(eq_data_->ret_vec[i]);
495 VecAssemblyEnd(eq_data_->ret_vec[i]);
497 if (mass_matrix[i] == NULL)
499 VecDuplicate(eq_data_->ls[i]->get_solution(), &mass_vec[i]);
500 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
501 MatConvert(*( eq_data_->ls_dt[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &mass_matrix[i]);
504 MatCopy(*( eq_data_->ls_dt[i]->get_matrix() ), mass_matrix[i], DIFFERENT_NONZERO_PATTERN);
509 if (stiffness_matrix[0] == NULL
511 || eq_fields_->flow_flux.changed())
515 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
517 eq_data_->ls[i]->start_add_assembly();
518 eq_data_->ls[i]->mat_zero_entries();
520 stiffness_assembly_->assemble(eq_data_->dh_);
521 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
523 eq_data_->ls[i]->finish_assembly();
525 if (stiffness_matrix[i] == NULL)
526 MatConvert(*( eq_data_->ls[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &stiffness_matrix[i]);
528 MatCopy(*( eq_data_->ls[i]->get_matrix() ), stiffness_matrix[i], DIFFERENT_NONZERO_PATTERN);
535 || eq_fields_->flow_flux.changed())
537 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
539 eq_data_->ls[i]->start_add_assembly();
540 eq_data_->ls[i]->rhs_zero_entries();
542 sources_assembly_->assemble(eq_data_->dh_);
543 bdr_cond_assembly_->assemble(eq_data_->dh_);
544 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
546 eq_data_->ls[i]->finish_assembly();
548 if (rhs[i] ==
nullptr) VecDuplicate(*( eq_data_->ls[i]->get_rhs() ), &rhs[i]);
549 VecCopy(*( eq_data_->ls[i]->get_rhs() ), rhs[i]);
571 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
573 MatConvert(stiffness_matrix[i], MATSAME, MAT_INITIAL_MATRIX, &m);
574 MatAXPY(m, 1./Model::time_->dt(), mass_matrix[i], SUBSET_NONZERO_PATTERN);
575 eq_data_->ls[i]->set_matrix(m, DIFFERENT_NONZERO_PATTERN);
577 VecDuplicate(rhs[i], &w);
578 VecWAXPY(w, 1./Model::time_->dt(), mass_vec[i], rhs[i]);
579 eq_data_->ls[i]->set_rhs(w);
584 eq_data_->ls[i]->solve();
587 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
598 calculate_cumulative_balance();
604 template<
class Model>
608 for (
auto cell : eq_data_->dh_->own_range() )
610 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
611 unsigned int n_dofs=loc_dof_indices.n_rows;
613 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
616 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
618 eq_data_->conc_fe[sbi]->vec().set(dof_p0, 0);
620 for (
unsigned int j=0; j<n_dofs; ++j)
621 eq_data_->conc_fe[sbi]->vec().add( dof_p0, eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]] );
623 eq_data_->conc_fe[sbi]->vec().set( dof_p0, max(eq_data_->conc_fe[sbi]->vec().get(dof_p0)/n_dofs, 0.) );
631 template<
class Model>
642 eq_fields_->output_fields.output(this->
time().step());
644 Model::output_data();
647 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
648 Model::balance_->calculate_instant(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
649 Model::balance_->output();
656 template<
class Model>
659 if (Model::balance_->cumulative())
661 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
663 Model::balance_->calculate_cumulative(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
666 VecDot(eq_data_->ret_vec[sbi], eq_data_->ls[sbi]->get_solution(), &ret_sources[sbi]);
668 Model::balance_->add_cumulative_source(eq_data_->subst_idx_[sbi], (ret_sources[sbi]-ret_sources_prev[sbi])/Model::time_->dt());
669 ret_sources_prev[sbi] = ret_sources[sbi];
677 template<
class Model>
682 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
683 eq_data_->ls[sbi]->start_allocation();
685 init_assembly_->assemble(eq_data_->dh_);
687 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
688 eq_data_->ls[sbi]->start_add_assembly();
690 init_assembly_->assemble(eq_data_->dh_);
692 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
694 eq_data_->ls[sbi]->finish_assembly();
695 eq_data_->ls[sbi]->solve();
699 init_assembly_->assemble(eq_data_->dh_);
703 template<
class Model>
706 if (solution_changed)
708 for (
auto cell : eq_data_->dh_->own_range() )
710 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
711 unsigned int n_dofs=loc_dof_indices.n_rows;
713 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
716 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
718 double old_average = 0;
719 for (
unsigned int j=0; j<n_dofs; ++j)
720 old_average += eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]];
721 old_average /= n_dofs;
723 for (
unsigned int j=0; j<n_dofs; ++j)
724 eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]]
725 += eq_data_->conc_fe[sbi]->vec().get(dof_p0) - old_average;
730 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
731 MatMult(*(eq_data_->ls_dt[sbi]->get_matrix()), eq_data_->ls[sbi]->get_solution(), mass_vec[sbi]);