14 : cOre(const_cast<
Core &>(core)) {}
25 std::vector<RandomFieldData> random_fields,
26 boost::shared_ptr<Range> ents_ptr) {
32 auto get_random_number = [](
auto &range) {
33 const auto r =
static_cast<double>(std::rand()) / RAND_MAX;
34 return range[0] +
r * (range[1] - range[0]);
42 VecGetLocalSize(
v, &size);
46 for (
auto &rf : random_fields) {
49 <<
"Set random field " << rf.first <<
" " <<
static_cast<int>(field_id);
52 auto lo = prb_ptr->getNumeredRowDofsPtr()->lower_bound(
54 const auto hi = prb_ptr->getNumeredRowDofsPtr()->upper_bound(
56 for (; lo != hi; ++lo) {
57 const auto idx = (*lo)->getPetscLocalDofIdx();
58 if (idx >= 0 && idx < size) {
59 array[idx] = get_random_number(rf.second);
63 for (
auto pit = ents_ptr->const_pair_begin();
64 pit != ents_ptr->const_pair_end(); ++pit) {
66 prb_ptr->getNumeredRowDofsPtr()->get<
Unique_mi_tag>().lower_bound(
69 prb_ptr->getNumeredRowDofsPtr()->get<
Unique_mi_tag>().upper_bound(
71 for (; lo != hi; ++lo) {
72 const auto idx = (*lo)->getPetscLocalDofIdx();
73 if (idx >= 0 && idx < size) {
74 array[idx] = get_random_number(rf.second);
81 CHKERR VecRestoreArray(
v, &array);
83 CHKERR VecGhostUpdateBegin(
v, INSERT_VALUES, SCATTER_FORWARD);
84 CHKERR VecGhostUpdateEnd(
v, INSERT_VALUES, SCATTER_FORWARD);
91 boost::shared_ptr<FEMethod> pipeline,
99 pipeline->ts_t = time;
100 pipeline->ts_dt = delta_t;
102 pipeline->ksp_ctx = KspMethod::KSPContext::CTX_SETFUNCTION;
103 pipeline->snes_ctx = SnesMethod::SNESContext::CTX_SNESSETFUNCTION;
104 pipeline->ts_ctx = TSMethod::TSContext::CTX_TSSETIFUNCTION;
106 auto [
v,
a] =
setPipelineX(pipeline, x, delta_x, delta2_x, delta_t);
114 "Run assemble pipeline");
127 auto get_norm = [](
auto f) {
129 VecNorm(
f, NORM_2, &fnorm);
141 boost::shared_ptr<FEMethod> pipeline,
152 pipeline->ts_t = time;
154 pipeline->ts_dt = delta_t;
155 pipeline->ts_a = 1. / delta_t;
156 pipeline->ts_aa = 1. / pow(delta_t, 2);
158 pipeline->ksp_ctx = KspMethod::KSPContext::CTX_OPERATORS;
159 pipeline->snes_ctx = SnesMethod::SNESContext::CTX_SNESSETJACOBIAN;
160 pipeline->ts_ctx = TSMethod::TSContext::CTX_TSSETIJACOBIAN;
162 auto [
v,
a] =
setPipelineX(pipeline, x, delta_x, delta2_x, delta_t);
170 "Run assemble pipeline");
174 auto get_norm = [](
auto m) {
176 MatNorm(
m, NORM_1, &fnorm);
193 auto axpy = [&](
auto v0,
auto diff_v,
auto eps) {
195 if (v0.use_count()) {
209 axpy(x, diff_x, -
eps),
211 axpy(delta_x, diff_x, -
eps),
213 axpy(delta2_x, diff_x, -
eps),
215 time, delta_t, cache_ptr);
219 axpy(x, diff_x,
eps),
221 axpy(delta_x, diff_x,
eps),
223 axpy(delta2_x, diff_x,
eps),
225 time, delta_t, cache_ptr);
227 auto calculate_derivative = [
eps](
auto fm1,
auto fp1) {
229 VecGetLocalSize(fp1, &size);
230 VecAXPY(fp1, -1, fm1);
232 VecGetArray(fp1, &array);
233 for (
int j = 0;
j != size; ++
j) {
236 VecRestoreArray(fp1, &array);
240 return calculate_derivative(fm1, fp1);
245 boost::shared_ptr<FEMethod> pipeline_rhs,
252 dm, fe_name, pipeline_rhs, x, delta_x, delta2_x, diff_x, time, delta_t,
255 auto m =
assembleMat(dm, fe_name, pipeline_lhs, x, delta_x, delta2_x, time,
280 VecCopy(delta_x, x_t);
281 VecScale(x_t, 1. / delta_t);
289 pipeline->x_t = PETSC_NULL;
296 VecCopy(delta2_x, x_tt);
297 VecScale(x_tt, 1. / pow(delta_t, 2));
302 pipeline->x_tt = x_tt;
305 pipeline->x_tt = PETSC_NULL;
308 return std::make_pair(x_t, x_tt);