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 .get_user_field(equation_name)),
88 "Input fields of the equation defined by user.")
91 .make_field_descriptor_type(equation_name)),
93 "Input fields of the equation.")
95 "Variant of the interior penalty discontinuous Galerkin method.")
97 "Polynomial order for the finite element in DG method (order 0 is suitable if there is no diffusion/dispersion).")
99 "If true, use DG projection of the initial condition field."
100 "Otherwise, evaluate initial condition field directly (well suited for reading native data).")
102 EqFields().output_fields.make_output_type(equation_name,
""),
103 IT::Default(
"{ \"fields\": [ " + Model::ModelEqData::default_output_field() +
"] }"),
104 "Specification of output fields and output times.")
108 template<
class Model>
110 Input::register_class< TransportDG<Model>,
Mesh &,
const Input::Record>(std::string(Model::ModelEqData::name()) +
"_DG") +
115 template<
class Model>
119 .
name(
"fracture_sigma")
121 "Coefficient of diffusive transfer through fractures (for each substance).")
129 "Penalty parameter influencing the discontinuity of the solution (for each substance). "
130 "Its default value 1 is sufficient in most cases. Higher value diminishes the inter-element jumps.")
143 .
description(
"Subdomain ids of the domain decomposition.");
154 template<
class Model>
157 double h_max = 0, h_min = numeric_limits<double>::infinity();
158 for (
unsigned int i=0; i<e->
n_nodes(); i++)
159 for (
unsigned int j=i+1; j<e->
n_nodes(); j++)
161 double dist = arma::norm(*e.
node(i) - *e.
node(j));
162 h_max = max(h_max, dist);
163 h_min = min(h_min, dist);
170 template<
class Model>
179 double delta = 0, h = 0;
188 for (
unsigned int i=0; i<side.
n_nodes(); i++)
189 for (
unsigned int j=i+1; j<side.
n_nodes(); j++) {
190 double dist = arma::norm(*side.
node(i) - *side.
node(j));
197 for (
int k=0; k<K_size; k++)
198 delta += dot(K[k]*normal_vector,normal_vector);
201 gamma = 0.5*fabs(flux) + alpha/h*delta*elem_anisotropy(side.
element());
206 template<
typename Model>
208 :
Model(init_mesh, in_rec),
222 Model::init_balance(in_rec);
233 eq_data_->dg_order = in_rec.
val<
unsigned int>(
"dg_order");
235 Model::init_from_input(in_rec);
238 shared_ptr<DiscreteSpace> ds = make_shared<EqualOrderDiscreteSpace>(Model::mesh_, fe);
239 eq_data_->dh_ = make_shared<DOFHandlerMultiDim>(*Model::mesh_);
246 template<
class Model>
249 eq_fields_->set_components(eq_data_->substances_.names());
250 eq_fields_->set_input_list( input_rec.val<
Input::Array>(
"input_fields"), *(Model::time_) );
251 eq_data_->set_time_governor(Model::time_);
252 eq_data_->balance_ = this->
balance();
253 eq_fields_->initialize();
256 eq_data_->gamma.resize(eq_data_->n_substances());
257 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
258 eq_data_->gamma[sbi].resize(Model::mesh_->boundary_.size());
261 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)));
262 ret_sources.resize(eq_data_->n_substances());
263 ret_sources_prev.resize(eq_data_->n_substances());
265 eq_data_->output_vec.resize(eq_data_->n_substances());
266 eq_fields_->output_field.set_components(eq_data_->substances_.names());
267 eq_fields_->output_field.set_mesh(*Model::mesh_);
268 eq_fields_->output_fields.set_mesh(*Model::mesh_);
271 eq_fields_->output_field.setup_components();
272 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
275 auto output_field_ptr = create_field_fe< 3, FieldValue<3>::Scalar >(eq_data_->dh_);
276 eq_fields_->output_field[sbi].set(output_field_ptr, 0);
277 eq_data_->output_vec[sbi] = output_field_ptr->vec();
281 eq_fields_->output_fields.initialize(Model::output_stream_, Model::mesh_, input_rec.val<
Input::Record>(
"output"), this->
time());
284 std::string petsc_default_opts;
285 if (eq_data_->dh_->distr()->np() == 1)
286 petsc_default_opts =
"-ksp_type bcgs -pc_type ilu -pc_factor_levels 2 -ksp_diagonal_scale_fix -pc_factor_fill 6.0";
288 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";
291 eq_data_->ls =
new LinSys*[eq_data_->n_substances()];
292 eq_data_->ls_dt =
new LinSys*[eq_data_->n_substances()];
293 eq_data_->conc_fe.resize(eq_data_->n_substances());
296 shared_ptr<DiscreteSpace> ds = make_shared<EqualOrderDiscreteSpace>(Model::mesh_, fe);
297 eq_data_->dh_p0 = make_shared<DOFHandlerMultiDim>(*Model::mesh_);
298 eq_data_->dh_p0->distribute_dofs(ds);
300 stiffness_matrix.resize(eq_data_->n_substances(),
nullptr);
301 mass_matrix.resize(eq_data_->n_substances(),
nullptr);
302 rhs.resize(eq_data_->n_substances(),
nullptr);
303 mass_vec.resize(eq_data_->n_substances(),
nullptr);
304 eq_data_->ret_vec.resize(eq_data_->n_substances(),
nullptr);
306 for (
unsigned int sbi = 0; sbi < eq_data_->n_substances(); sbi++) {
307 eq_data_->ls[sbi] =
new LinSys_PETSC(eq_data_->dh_->distr().get(), petsc_default_opts);
309 eq_data_->ls[sbi]->set_solution(eq_data_->output_vec[sbi].petsc_vec());
311 eq_data_->ls_dt[sbi] =
new LinSys_PETSC(eq_data_->dh_->distr().get(), petsc_default_opts);
314 eq_data_->conc_fe[sbi] = create_field_fe< 3, FieldValue<3>::Scalar >(eq_data_->dh_p0);
316 VecDuplicate(eq_data_->ls[sbi]->get_solution(), &eq_data_->ret_vec[sbi]);
320 init_projection = input_rec.val<
bool>(
"init_projection");
334 Model::balance_->allocate(eq_data_->dh_->distr()->lsize(), mass_assembly_->eval_points()->max_size());
336 int qsize = mass_assembly_->eval_points()->max_size();
337 eq_data_->dif_coef.resize(eq_data_->n_substances());
338 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
340 eq_data_->dif_coef[sbi].resize(qsize);
343 eq_fields_->init_condition.setup_components();
344 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
350 if (input_rec.opt_val(
"user_fields", user_fields_arr)) {
351 eq_fields_->set_user_fields_map(user_fields_arr);
356 template<
class Model>
361 if (eq_data_->gamma.size() > 0) {
364 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
366 delete eq_data_->ls[i];
367 delete eq_data_->ls_dt[i];
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]));
380 delete[] eq_data_->ls;
381 delete[] eq_data_->ls_dt;
388 delete mass_assembly_;
389 delete stiffness_assembly_;
390 delete sources_assembly_;
391 delete bdr_cond_assembly_;
392 delete init_assembly_;
398 template<
class Model>
402 eq_fields_->mark_input_times( *(Model::time_) );
404 std::stringstream ss;
411 set_initial_condition();
412 for (
unsigned int sbi = 0; sbi < eq_data_->n_substances(); sbi++)
413 ( (
LinSys_PETSC *)eq_data_->ls[sbi] )->set_initial_guess_nonzero();
416 if (!allocation_done) preallocate();
419 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
421 Model::balance_->calculate_instant(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
424 ret_sources_prev[sbi] = 0;
431 template<
class Model>
435 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
438 eq_data_->ls[i]->start_allocation();
439 stiffness_matrix[i] = NULL;
443 eq_data_->ls_dt[i]->start_allocation();
444 mass_matrix[i] = NULL;
445 VecZeroEntries(eq_data_->ret_vec[i]);
447 stiffness_assembly_->assemble(eq_data_->dh_);
448 mass_assembly_->assemble(eq_data_->dh_);
449 sources_assembly_->assemble(eq_data_->dh_);
450 bdr_cond_assembly_->assemble(eq_data_->dh_);
451 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
453 VecAssemblyBegin(eq_data_->ret_vec[i]);
454 VecAssemblyEnd(eq_data_->ret_vec[i]);
457 allocation_done =
true;
462 template<
class Model>
467 Model::time_->next_time();
468 Model::time_->view(
"TDG");
477 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
479 eq_data_->ls_dt[i]->start_add_assembly();
480 eq_data_->ls_dt[i]->mat_zero_entries();
481 VecZeroEntries(eq_data_->ret_vec[i]);
483 mass_assembly_->assemble(eq_data_->dh_);
484 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
486 eq_data_->ls_dt[i]->finish_assembly();
487 VecAssemblyBegin(eq_data_->ret_vec[i]);
488 VecAssemblyEnd(eq_data_->ret_vec[i]);
490 if (mass_matrix[i] == NULL)
492 VecDuplicate(eq_data_->ls[i]->get_solution(), &mass_vec[i]);
493 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
494 MatConvert(*( eq_data_->ls_dt[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &mass_matrix[i]);
497 MatCopy(*( eq_data_->ls_dt[i]->get_matrix() ), mass_matrix[i], DIFFERENT_NONZERO_PATTERN);
502 if (stiffness_matrix[0] == NULL
504 || eq_fields_->flow_flux.changed())
508 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
510 eq_data_->ls[i]->start_add_assembly();
511 eq_data_->ls[i]->mat_zero_entries();
513 stiffness_assembly_->assemble(eq_data_->dh_);
514 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
516 eq_data_->ls[i]->finish_assembly();
518 if (stiffness_matrix[i] == NULL)
519 MatConvert(*( eq_data_->ls[i]->get_matrix() ), MATSAME, MAT_INITIAL_MATRIX, &stiffness_matrix[i]);
521 MatCopy(*( eq_data_->ls[i]->get_matrix() ), stiffness_matrix[i], DIFFERENT_NONZERO_PATTERN);
528 || eq_fields_->flow_flux.changed())
530 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
532 eq_data_->ls[i]->start_add_assembly();
533 eq_data_->ls[i]->rhs_zero_entries();
535 sources_assembly_->assemble(eq_data_->dh_);
536 bdr_cond_assembly_->assemble(eq_data_->dh_);
537 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
539 eq_data_->ls[i]->finish_assembly();
541 if (rhs[i] ==
nullptr) VecDuplicate(*( eq_data_->ls[i]->get_rhs() ), &rhs[i]);
542 VecCopy(*( eq_data_->ls[i]->get_rhs() ), rhs[i]);
564 for (
unsigned int i=0; i<eq_data_->n_substances(); i++)
566 MatConvert(stiffness_matrix[i], MATSAME, MAT_INITIAL_MATRIX, &m);
567 MatAXPY(m, 1./Model::time_->dt(), mass_matrix[i], SUBSET_NONZERO_PATTERN);
568 eq_data_->ls[i]->set_matrix(m, DIFFERENT_NONZERO_PATTERN);
570 VecDuplicate(rhs[i], &w);
571 VecWAXPY(w, 1./Model::time_->dt(), mass_vec[i], rhs[i]);
572 eq_data_->ls[i]->set_rhs(w);
577 eq_data_->ls[i]->solve();
580 MatMult(*(eq_data_->ls_dt[i]->get_matrix()), eq_data_->ls[i]->get_solution(), mass_vec[i]);
591 calculate_cumulative_balance();
597 template<
class Model>
601 for (
auto cell : eq_data_->dh_->own_range() )
603 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
604 unsigned int n_dofs=loc_dof_indices.n_rows;
606 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
609 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
611 eq_data_->conc_fe[sbi]->vec().set(dof_p0, 0);
613 for (
unsigned int j=0; j<n_dofs; ++j)
614 eq_data_->conc_fe[sbi]->vec().add( dof_p0, eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]] );
616 eq_data_->conc_fe[sbi]->vec().set( dof_p0, max(eq_data_->conc_fe[sbi]->vec().get(dof_p0)/n_dofs, 0.) );
624 template<
class Model>
635 eq_fields_->output_fields.output(this->
time().step());
637 Model::output_data();
640 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
641 Model::balance_->calculate_instant(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
642 Model::balance_->output();
649 template<
class Model>
652 if (Model::balance_->cumulative())
654 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
656 Model::balance_->calculate_cumulative(eq_data_->subst_idx_[sbi], eq_data_->ls[sbi]->get_solution());
659 VecDot(eq_data_->ret_vec[sbi], eq_data_->ls[sbi]->get_solution(), &ret_sources[sbi]);
661 Model::balance_->add_cumulative_source(eq_data_->subst_idx_[sbi], (ret_sources[sbi]-ret_sources_prev[sbi])/Model::time_->dt());
662 ret_sources_prev[sbi] = ret_sources[sbi];
670 template<
class Model>
675 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
676 eq_data_->ls[sbi]->start_allocation();
678 init_assembly_->assemble(eq_data_->dh_);
680 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
681 eq_data_->ls[sbi]->start_add_assembly();
683 init_assembly_->assemble(eq_data_->dh_);
685 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); sbi++)
687 eq_data_->ls[sbi]->finish_assembly();
688 eq_data_->ls[sbi]->solve();
692 init_assembly_->assemble(eq_data_->dh_);
696 template<
class Model>
699 if (solution_changed)
701 for (
auto cell : eq_data_->dh_->own_range() )
703 LocDofVec loc_dof_indices = cell.get_loc_dof_indices();
704 unsigned int n_dofs=loc_dof_indices.n_rows;
706 DHCellAccessor dh_p0_cell = eq_data_->dh_p0->cell_accessor_from_element(cell.elm_idx());
709 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
711 double old_average = 0;
712 for (
unsigned int j=0; j<n_dofs; ++j)
713 old_average += eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]];
714 old_average /= n_dofs;
716 for (
unsigned int j=0; j<n_dofs; ++j)
717 eq_data_->ls[sbi]->get_solution_array()[loc_dof_indices[j]]
718 += eq_data_->conc_fe[sbi]->vec().get(dof_p0) - old_average;
723 for (
unsigned int sbi=0; sbi<eq_data_->n_substances(); ++sbi)
724 MatMult(*(eq_data_->ls_dt[sbi]->get_matrix()), eq_data_->ls[sbi]->get_solution(), mass_vec[sbi]);