14 : cOre(const_cast<
Core &>(core)) {}
25 boost::shared_ptr<Range> ents_ptr,
RowColData r) {
31 auto get_random_number = [](
auto &range) {
32 const auto r =
static_cast<double>(std::rand()) / RAND_MAX;
33 return range[0] + r * (range[1] - range[0]);
41 VecGetLocalSize(
v, &size);
45 for (
auto &rf : random_fields) {
48 <<
"Set random field " << rf.first <<
" " <<
static_cast<int>(field_id);
50 auto get_numbered_dofs = [&]() {
53 return prb_ptr->getNumeredRowDofsPtr();
55 return prb_ptr->getNumeredColDofsPtr();
57 throw std::runtime_error(
"Invalid RowColData");
62 auto lo = get_numbered_dofs()->lower_bound(
64 const auto hi = get_numbered_dofs()->upper_bound(
66 for (; lo != hi; ++lo) {
67 const auto idx = (*lo)->getPetscLocalDofIdx();
68 if (idx >= 0 && idx < size) {
69 array[idx] = get_random_number(rf.second);
73 for (
auto pit = ents_ptr->const_pair_begin();
74 pit != ents_ptr->const_pair_end(); ++pit) {
75 auto lo = get_numbered_dofs()->get<
Unique_mi_tag>().lower_bound(
77 auto hi = get_numbered_dofs()->get<
Unique_mi_tag>().upper_bound(
79 for (; lo != hi; ++lo) {
80 const auto idx = (*lo)->getPetscLocalDofIdx();
81 if (idx >= 0 && idx < size) {
82 array[idx] = get_random_number(rf.second);
89 CHKERR VecRestoreArray(
v, &array);
91 CHKERR VecGhostUpdateBegin(
v, INSERT_VALUES, SCATTER_FORWARD);
92 CHKERR VecGhostUpdateEnd(
v, INSERT_VALUES, SCATTER_FORWARD);
99 boost::shared_ptr<FEMethod> pipeline,
107 pipeline->ts_t = time;
108 pipeline->ts_dt = delta_t;
114 auto [
v,
a] =
setPipelineX(pipeline, x, delta_x, delta2_x, delta_t);
122 "Run assemble pipeline");
135 auto get_norm = [](
auto f) {
137 VecNorm(f, NORM_2, &fnorm);
149 boost::shared_ptr<FEMethod> pipeline,
160 pipeline->ts_t = time;
162 pipeline->ts_dt = delta_t;
163 pipeline->ts_a = 1. / delta_t;
164 pipeline->ts_aa = 1. / pow(delta_t, 2);
170 auto [
v,
a] =
setPipelineX(pipeline, x, delta_x, delta2_x, delta_t);
178 "Run assemble pipeline");
182 auto get_norm = [](
auto m) {
184 MatNorm(
m, NORM_1, &fnorm);
201 auto axpy = [&](
auto v0,
auto diff_v,
auto eps) {
203 if (v0.use_count()) {
217 axpy(x, diff_x, -
eps),
219 axpy(delta_x, diff_x, -
eps),
221 axpy(delta2_x, diff_x, -
eps),
223 time, delta_t, cache_ptr);
227 axpy(x, diff_x,
eps),
229 axpy(delta_x, diff_x,
eps),
231 axpy(delta2_x, diff_x,
eps),
233 time, delta_t, cache_ptr);
235 auto calculate_derivative = [
eps](
auto fm1,
auto fp1) {
237 VecGetLocalSize(fp1, &size);
238 VecAXPY(fp1, -1, fm1);
240 VecGetArray(fp1, &array);
241 for (
int j = 0;
j != size; ++
j) {
244 VecRestoreArray(fp1, &array);
248 return calculate_derivative(fm1, fp1);
253 boost::shared_ptr<FEMethod> pipeline_rhs,
260 dm, fe_name, pipeline_rhs, x, delta_x, delta2_x, diff_x, time, delta_t,
263 auto m =
assembleMat(dm, fe_name, pipeline_lhs, x, delta_x, delta2_x, time,
288 VecCopy(delta_x, x_t);
289 VecScale(x_t, 1. / delta_t);
297 pipeline->x_t = PETSC_NULLPTR;
304 VecCopy(delta2_x, x_tt);
305 VecScale(x_tt, 1. / pow(delta_t, 2));
310 pipeline->x_tt = x_tt;
313 pipeline->x_tt = PETSC_NULLPTR;
316 return std::make_pair(x_t, x_tt);
#define MOFEM_TAG_AND_LOG_C(channel, severity, tag, format,...)
Tag and log in channel.
#define MOFEM_TAG_AND_LOG(channel, severity, tag)
Tag and log in channel.
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.
#define CHKERR
Inline error check.
PetscErrorCode DMoFEMMeshToLocalVector(DM dm, Vec l, InsertMode mode, ScatterMode scatter_mode, RowColData rc=RowColData::COL)
set local (or ghosted) vector values on mesh for partition only
PetscErrorCode DMoFEMLoopFiniteElements(DM dm, const char fe_name[], MoFEM::FEMethod *method, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr())
Executes FEMethod for finite elements in DM.
auto createDMVector(DM dm, RowColData rc=RowColData::COL)
Get smart vector from DM.
auto createDMMatrix(DM dm)
Get smart matrix from DM.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
const double v
phase velocity of light in medium (cm/ns)
FTensor::Index< 'j', 3 > j
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
SmartPetscObj< Vec > vectorDuplicate(Vec vec)
Create duplicate vector of smart vector.
auto getProblemPtr(DM dm)
get problem pointer from DM
boost::weak_ptr< CacheTuple > CacheTupleWeakPtr
FTensor::Index< 'm', 3 > m
virtual FieldBitNumber get_field_bit_number(const std::string name) const =0
get field bit number
Deprecated interface functions.
static UId getLoFieldEntityUId(const FieldBitNumber bit, const EntityHandle ent)
static UId getHiFieldEntityUId(const FieldBitNumber bit, const EntityHandle ent)
static UId getHiBitNumberUId(const FieldBitNumber bit_number)
static UId getLoBitNumberUId(const FieldBitNumber bit_number)
@ CTX_SETFUNCTION
Setting up the linear system function.
@ CTX_OPERATORS
Setting up linear operators.
Calculate directional derivative of the right hand side and compare it with tangent matrix derivative...
SmartPetscObj< Mat > assembleMat(SmartPetscObj< DM > dm, std::string fe_name, boost::shared_ptr< FEMethod > pipeline, SmartPetscObj< Vec > x, SmartPetscObj< Vec > delta_x, SmartPetscObj< Vec > delta2_x, double time, double delta_t, CacheTupleWeakPtr cache_ptr)
Assemble the left hand side vector.
SmartPetscObj< Vec > checkCentralFiniteDifference(SmartPetscObj< DM > dm, std::string fe_name, boost::shared_ptr< FEMethod > pipeline_rhs, boost::shared_ptr< FEMethod > pipeline_lhs, SmartPetscObj< Vec > x, SmartPetscObj< Vec > delta_x, SmartPetscObj< Vec > delta2_x, SmartPetscObj< Vec > diff_x, double time, double delta_t, double eps, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr())
Check consistency between directional derivative with matrix.
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
OperatorsTester(const MoFEM::Core &core)
SmartPetscObj< Vec > assembleVec(SmartPetscObj< DM > dm, std::string fe_name, boost::shared_ptr< FEMethod > pipeline, SmartPetscObj< Vec > x, SmartPetscObj< Vec > delta_x, SmartPetscObj< Vec > delta2_x, double time, double delta_t, CacheTupleWeakPtr cache_ptr)
Assemble the right hand side vector.
SmartPetscObj< Vec > setRandomFields(SmartPetscObj< DM > dm, std::vector< RandomFieldData > random_fields, boost::shared_ptr< Range > ents=nullptr, RowColData r=RowColData::COL)
Generate random fields.
std::pair< SmartPetscObj< Vec >, SmartPetscObj< Vec > > setPipelineX(boost::shared_ptr< FEMethod > pipeline, SmartPetscObj< Vec > x, SmartPetscObj< Vec > delta_x, SmartPetscObj< Vec > delta2_x, double delta_t)
Set vectors, x, x_t, and x_tt to finite element instance.
SmartPetscObj< Vec > directionalCentralFiniteDifference(SmartPetscObj< DM > dm, std::string fe_name, boost::shared_ptr< FEMethod > pipeline, SmartPetscObj< Vec > x, SmartPetscObj< Vec > delta_x, SmartPetscObj< Vec > delta2_x, SmartPetscObj< Vec > diff_x, double time, double delta_t, double eps, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr())
Calculate directional directive using finite difference.
static constexpr Switches CtxSetA
Jacobian matrix switch.
static constexpr Switches CtxSetF
Residual vector switch.
@ CTX_SET_X_T
Time derivative X_t is set.
@ CTX_SET_X
Solution vector X is set.
@ CTX_SET_X_TT
Second time derivative X_tt is set.
static constexpr Switches CtxSetB
Preconditioner matrix switch.
static constexpr Switches CtxSetTime
Time value switch.
intrusive_ptr for managing petsc objects
@ CTX_SNESSETJACOBIAN
Setting up Jacobian matrix computation.
@ CTX_SNESSETFUNCTION
Setting up nonlinear function evaluation.
@ CTX_TSSETIFUNCTION
Setting up implicit function.
@ CTX_TSSETIJACOBIAN
Setting up implicit Jacobian.
base class for all interface classes