| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2010 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_theta_timestepping_templates_h |
| 16 |
#define dealii_theta_timestepping_templates_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/algorithms/theta_timestepping.h> |
| 22 |
|
| 23 |
#include <deal.II/base/logstream.h> |
| 24 |
#include <deal.II/base/parameter_handler.h> |
| 25 |
#include <deal.II/base/signaling_nan.h> |
| 26 |
|
| 27 |
#include <deal.II/lac/vector_memory.h> |
| 28 |
|
| 29 |
DEAL_II_NAMESPACE_OPEN |
| 30 |
|
| 31 |
namespace Algorithms |
| 32 |
{ |
| 33 |
template <typename VectorType> |
| 34 |
ThetaTimestepping<VectorType>::ThetaTimestepping(OperatorBase &e, |
| 35 |
OperatorBase &i) |
| 36 |
: vtheta(0.5) |
| 37 |
, adaptive(false) |
| 38 |
, op_explicit(&e) |
| 39 |
, op_implicit(&i) |
| 40 |
{ |
| 41 |
d_explicit.step = numbers::signaling_nan<double>(); |
| 42 |
d_explicit.time = numbers::signaling_nan<double>(); |
| 43 |
|
| 44 |
d_implicit.step = numbers::signaling_nan<double>(); |
| 45 |
d_implicit.time = numbers::signaling_nan<double>(); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
template <typename VectorType> |
| 50 |
void |
| 51 |
ThetaTimestepping<VectorType>::notify(const Event &e) |
| 52 |
{ |
| 53 |
op_explicit->notify(e); |
| 54 |
op_implicit->notify(e); |
| 55 |
} |
| 56 |
|
| 57 |
template <typename VectorType> |
| 58 |
void |
| 59 |
ThetaTimestepping<VectorType>::declare_parameters(ParameterHandler ¶m) |
| 60 |
{ |
| 61 |
param.enter_subsection("ThetaTimestepping"); |
| 62 |
TimestepControl::declare_parameters(param); |
| 63 |
param.declare_entry("Theta", ".5", Patterns::Double(0., 1.)); |
| 64 |
param.declare_entry("Adaptive", "false", Patterns::Bool()); |
| 65 |
param.leave_subsection(); |
| 66 |
} |
| 67 |
|
| 68 |
template <typename VectorType> |
| 69 |
void |
| 70 |
ThetaTimestepping<VectorType>::parse_parameters(ParameterHandler ¶m) |
| 71 |
{ |
| 72 |
param.enter_subsection("ThetaTimestepping"); |
| 73 |
control.parse_parameters(param); |
| 74 |
vtheta = param.get_double("Theta"); |
| 75 |
adaptive = param.get_bool("Adaptive"); |
| 76 |
param.leave_subsection(); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
template <typename VectorType> |
| 81 |
void |
| 82 |
ThetaTimestepping<VectorType>::operator()(AnyData &out, const AnyData &in) |
| 83 |
{ |
| 84 |
Assert(!adaptive, ExcNotImplemented()); |
| 85 |
|
| 86 |
LogStream::Prefix prefix("Theta"); |
| 87 |
|
| 88 |
VectorType &solution = *out.entry<VectorType *>(0); |
| 89 |
GrowingVectorMemory<VectorType> mem; |
| 90 |
typename VectorMemory<VectorType>::Pointer aux(mem); |
| 91 |
aux->reinit(solution); |
| 92 |
|
| 93 |
control.restart(); |
| 94 |
|
| 95 |
d_explicit.time = control.now(); |
| 96 |
|
| 97 |
// The data used to compute the |
| 98 |
// vector associated with the old |
| 99 |
// timestep |
| 100 |
AnyData src1; |
| 101 |
src1.add<const VectorType *>(&solution, "Previous iterate"); |
| 102 |
src1.add<const double *>(&d_explicit.time, "Time"); |
| 103 |
src1.add<const double *>(&d_explicit.step, "Timestep"); |
| 104 |
src1.add<const double *>(&vtheta, "Theta"); |
| 105 |
src1.merge(in); |
| 106 |
|
| 107 |
AnyData src2; |
| 108 |
|
| 109 |
AnyData out1; |
| 110 |
out1.add<VectorType *>(aux.get(), "Solution"); |
| 111 |
// The data provided to the inner solver |
| 112 |
src2.add<const VectorType *>(aux.get(), "Previous time"); |
| 113 |
src2.add<const VectorType *>(&solution, "Previous iterate"); |
| 114 |
src2.add<const double *>(&d_implicit.time, "Time"); |
| 115 |
src2.add<const double *>(&d_implicit.step, "Timestep"); |
| 116 |
src2.add<const double *>(&vtheta, "Theta"); |
| 117 |
src2.merge(in); |
| 118 |
|
| 119 |
if (output != nullptr) |
| 120 |
(*output) << 0U << out; |
| 121 |
|
| 122 |
// When using nvcc 12.6 with C++20, the compiler has trouble determining the |
| 123 |
// type of d_explicit.time. We need to use a static_cast to help it. |
| 124 |
for (unsigned int count = 1; |
| 125 |
static_cast<double>(d_explicit.time) < control.final(); |
| 126 |
++count) |
| 127 |
{ |
| 128 |
const bool step_change = control.advance(); |
| 129 |
d_implicit.time = control.now(); |
| 130 |
d_explicit.step = (1. - vtheta) * control.step(); |
| 131 |
d_implicit.step = vtheta * control.step(); |
| 132 |
deallog << "Time step:" << d_implicit.time << std::endl; |
| 133 |
|
| 134 |
op_explicit->notify(Events::new_time); |
| 135 |
op_implicit->notify(Events::new_time); |
| 136 |
if (step_change) |
| 137 |
{ |
| 138 |
op_explicit->notify(Events::new_timestep_size); |
| 139 |
op_implicit->notify(Events::new_timestep_size); |
| 140 |
} |
| 141 |
|
| 142 |
// Compute |
| 143 |
// (I + (1-theta)dt A) u |
| 144 |
(*op_explicit)(out1, src1); |
| 145 |
(*op_implicit)(out, src2); |
| 146 |
|
| 147 |
if (output != nullptr && control.print()) |
| 148 |
(*output) << count << out; |
| 149 |
|
| 150 |
d_explicit.time = control.now(); |
| 151 |
} |
| 152 |
} |
| 153 |
} // namespace Algorithms |
| 154 |
|
| 155 |
DEAL_II_NAMESPACE_CLOSE |
| 156 |
|
| 157 |
#endif |