11 -my_file mesh file name\n\
12 -my_sr reduction of step size\n\
13 -my_ms maximal number of steps\n\n";
16 using namespace MoFEM;
23 int main(
int argc,
char *argv[]) {
25 const string default_options =
"-ksp_type fgmres \n"
27 "-pc_factor_mat_solver_type mumps \n"
28 "-mat_mumps_icntl_20 0 \n"
32 "-snes_type newtonls \n"
33 "-snes_linesearch_type basic \n"
40 string param_file =
"param_file.petsc";
41 if (!
static_cast<bool>(ifstream(param_file))) {
42 std::ofstream file(param_file.c_str(), std::ios::ate);
44 file << default_options;
57 ParallelComm *pcomm = ParallelComm::get_pcomm(&moab,
MYPCOMM_INDEX);
59 boost::make_shared<WrapMPIComm>(PETSC_COMM_WORLD,
false);
61 pcomm =
new ParallelComm(&moab, moab_comm_wrap->get_comm());
63 PetscBool flg = PETSC_TRUE;
67 if (flg != PETSC_TRUE) {
68 SETERRQ(PETSC_COMM_SELF, 1,
"*** ERROR -my_file (MESH FILE NEEDED)");
74 if (flg != PETSC_TRUE) {
80 PetscBool is_partitioned = PETSC_FALSE;
82 &is_partitioned, &flg);
84 if (is_partitioned == PETSC_TRUE) {
87 option =
"PARALLEL=BCAST_DELETE;PARALLEL_RESOLVE_SHARED_ENTS;PARTITION="
88 "PARALLEL_PARTITION;";
97 Tag th_step_size, th_step;
98 double def_step_size = 1;
99 CHKERR moab.tag_get_handle(
"_STEPSIZE", 1, MB_TYPE_DOUBLE, th_step_size,
100 MB_TAG_CREAT | MB_TAG_MESH, &def_step_size);
101 if (
rval == MB_ALREADY_ALLOCATED)
105 CHKERR moab.tag_get_handle(
"_STEP", 1, MB_TYPE_INTEGER, th_step,
106 MB_TAG_CREAT | MB_TAG_MESH, &def_step);
107 if (
rval == MB_ALREADY_ALLOCATED)
110 const void *tag_data_step_size[1];
112 CHKERR moab.tag_get_by_ptr(th_step_size, &root, 1, tag_data_step_size);
113 double &step_size = *(
double *)tag_data_step_size[0];
114 const void *tag_data_step[1];
115 CHKERR moab.tag_get_by_ptr(th_step, &root, 1, tag_data_step);
116 int &step = *(
int *)tag_data_step[0];
118 CHKERR PetscPrintf(PETSC_COMM_WORLD,
119 "Start step %D and step_size = %6.4e\n", step,
128 std::vector<BitRefLevel> bit_levels;
134 problem_bit_level = bit_levels.back();
155 "MESH_NODE_POSITIONS");
164 "ELASTIC",
"LAMBDA");
168 "ELASTIC",
"MESH_NODE_POSITIONS");
206 const double coords[] = {0, 0, 0};
208 Range range_no_field_vertex;
209 range_no_field_vertex.insert(no_field_vertex);
214 range_no_field_vertex);
219 CHKERR moab.create_meshset(MESHSET_SET, meshset_fe_arc_length);
220 CHKERR moab.add_entities(meshset_fe_arc_length, &no_field_vertex, 1);
226 meshset_fe_arc_length,
"ARC_LENGTH",
false);
254 "NEUMANN_FE",
"MESH_NODE_POSITIONS");
260 CHKERR moab.get_entities_by_type(it->meshset, MBTRI, tris,
true);
267 CHKERR moab.get_entities_by_type(it->meshset, MBTRI, tris,
true);
279 boost::shared_ptr<FaceElementForcesAndSourcesCore> fe_spring_lhs_ptr(
281 boost::shared_ptr<FaceElementForcesAndSourcesCore> fe_spring_rhs_ptr(
285 m_field, fe_spring_lhs_ptr, fe_spring_rhs_ptr,
"SPATIAL_POSITION",
286 "MESH_NODE_POSITIONS");
301 false,
false,
false);
312 std::map<int, NonlinearElasticElement::BlockData>::iterator sit =
325 "MESH_NODE_POSITIONS");
326 CHKERR m_field.
loop_dofs(
"MESH_NODE_POSITIONS", ent_method_material, 0);
332 1.,
"MESH_NODE_POSITIONS",
"SPATIAL_POSITION");
348 if (is_partitioned) {
349 SETERRQ(PETSC_COMM_SELF, 1,
350 "Not implemented, problem with arc-length force multiplayer");
369 "ELASTIC_MECHANICS",
COL, &
F);
374 ->createMPIAIJWithArrays<PetscGlobalIdx_mi_tag>(
"ELASTIC_MECHANICS",
377 boost::shared_ptr<ArcLengthCtx> arc_ctx = boost::shared_ptr<ArcLengthCtx>(
383 CHKERR MatGetLocalSize(Aij, &
m, &
n);
384 boost::scoped_ptr<ArcLengthMatShell> mat_ctx(
388 CHKERR MatCreateShell(PETSC_COMM_WORLD,
m,
n,
M,
N, mat_ctx.get(),
390 CHKERR MatShellSetOperation(ShellAij, MATOP_MULT,
393 ArcLengthSnesCtx snes_ctx(m_field,
"ELASTIC_MECHANICS", arc_ctx);
401 CHKERR moab.get_entities_by_type(meshset, MBVERTEX, nodes,
true);
403 node_set.merge(nodes);
405 PetscPrintf(PETSC_COMM_WORLD,
"Nb. nodes in load path: %u\n",
410 double scaled_reference_load = 1;
411 double *scale_lhs = &(arc_ctx->getFieldData());
412 double *scale_rhs = &(scaled_reference_load);
414 m_field, Aij, arc_ctx->F_lambda, scale_lhs, scale_rhs);
421 fe_neumann.
uSeF =
true;
431 boost::shared_ptr<FEMethod> my_dirichlet_bc =
433 m_field,
"SPATIAL_POSITION", Aij,
D,
F));
435 &(my_dirichlet_bc->problemPtr));
439 struct AssembleRhsVectors :
public FEMethod {
441 boost::shared_ptr<ArcLengthCtx> arcPtr;
444 AssembleRhsVectors(boost::shared_ptr<ArcLengthCtx> &arc_ptr,
446 : arcPtr(arc_ptr), nodeSet(node_set) {}
453 case CTX_SNESSETFUNCTION: {
454 CHKERR VecZeroEntries(snes_f);
455 CHKERR VecGhostUpdateBegin(snes_f, INSERT_VALUES, SCATTER_FORWARD);
456 CHKERR VecGhostUpdateEnd(snes_f, INSERT_VALUES, SCATTER_FORWARD);
457 CHKERR VecZeroEntries(arcPtr->F_lambda);
458 CHKERR VecGhostUpdateBegin(arcPtr->F_lambda, INSERT_VALUES,
460 CHKERR VecGhostUpdateEnd(arcPtr->F_lambda, INSERT_VALUES,
464 SETERRQ(PETSC_COMM_SELF, 1,
"not implemented");
473 case CTX_SNESSETFUNCTION: {
475 CHKERR VecGhostUpdateBegin(snes_f, ADD_VALUES, SCATTER_REVERSE);
476 CHKERR VecGhostUpdateEnd(snes_f, ADD_VALUES, SCATTER_REVERSE);
477 CHKERR VecAssemblyBegin(snes_f);
478 CHKERR VecAssemblyEnd(snes_f);
481 SETERRQ(PETSC_COMM_SELF, 1,
"not implemented");
488 boost::shared_ptr<NumeredDofEntity_multiIndex> numered_dofs_rows =
489 problemPtr->getNumeredRowDofsPtr();
490 Range::iterator nit = nodeSet.begin();
491 for (; nit != nodeSet.end(); nit++) {
492 NumeredDofEntityByEnt::iterator dit, hi_dit;
493 dit = numered_dofs_rows->get<
Ent_mi_tag>().lower_bound(*nit);
494 hi_dit = numered_dofs_rows->get<
Ent_mi_tag>().upper_bound(*nit);
495 for (; dit != hi_dit; dit++) {
496 PetscPrintf(PETSC_COMM_WORLD,
"%s [ %d ] %6.4e -> ",
"LAMBDA", 0,
497 arcPtr->getFieldData());
498 PetscPrintf(PETSC_COMM_WORLD,
"%s [ %d ] %6.4e\n",
499 dit->get()->getName().c_str(),
500 dit->get()->getDofCoeffIdx(),
501 dit->get()->getFieldData());
508 struct AddLambdaVectorToFInternal :
public FEMethod {
510 boost::shared_ptr<ArcLengthCtx> arcPtr;
511 boost::shared_ptr<DirichletSpatialPositionsBc> bC;
513 AddLambdaVectorToFInternal(boost::shared_ptr<ArcLengthCtx> &arc_ptr,
514 boost::shared_ptr<FEMethod> &bc)
516 bC(boost::shared_ptr<DirichletSpatialPositionsBc>(
530 case CTX_SNESSETFUNCTION: {
532 CHKERR VecGhostUpdateBegin(arcPtr->F_lambda, ADD_VALUES,
534 CHKERR VecGhostUpdateEnd(arcPtr->F_lambda, ADD_VALUES,
536 CHKERR VecAssemblyBegin(arcPtr->F_lambda);
537 CHKERR VecAssemblyEnd(arcPtr->F_lambda);
538 for (std::vector<int>::iterator vit = bC->dofsIndices.begin();
539 vit != bC->dofsIndices.end(); vit++) {
540 CHKERR VecSetValue(arcPtr->F_lambda, *vit, 0, INSERT_VALUES);
542 CHKERR VecAssemblyBegin(arcPtr->F_lambda);
543 CHKERR VecAssemblyEnd(arcPtr->F_lambda);
544 CHKERR VecDot(arcPtr->F_lambda, arcPtr->F_lambda, &arcPtr->F_lambda2);
545 PetscPrintf(PETSC_COMM_WORLD,
"\tFlambda2 = %6.4e\n",
548 CHKERR VecAXPY(snes_f, arcPtr->getFieldData(), arcPtr->F_lambda);
549 PetscPrintf(PETSC_COMM_WORLD,
"\tlambda = %6.4e\n",
550 arcPtr->getFieldData());
552 CHKERR VecNorm(snes_f, NORM_2, &fnorm);
553 PetscPrintf(PETSC_COMM_WORLD,
"\tfnorm = %6.4e\n", fnorm);
556 SETERRQ(PETSC_COMM_SELF, 1,
"not implemented");
562 AssembleRhsVectors pre_post_method(arc_ctx, node_set);
563 AddLambdaVectorToFInternal assemble_F_lambda(arc_ctx, my_dirichlet_bc);
566 CHKERR SNESCreate(PETSC_COMM_WORLD, &snes);
567 CHKERR SNESSetApplicationContext(snes, &snes_ctx);
569 CHKERR SNESSetJacobian(snes, ShellAij, Aij,
SnesMat, &snes_ctx);
570 CHKERR SNESSetFromOptions(snes);
575 if (flg == PETSC_TRUE) {
576 PetscReal atol, rtol, stol;
577 PetscInt maxit, maxf;
578 CHKERR SNESGetTolerances(snes, &atol, &rtol, &stol, &maxit, &maxf);
581 CHKERR SNESSetTolerances(snes, atol, rtol, stol, maxit, maxf);
585 CHKERR SNESGetKSP(snes, &ksp);
587 CHKERR KSPGetPC(ksp, &pc);
588 boost::scoped_ptr<PCArcLengthCtx> pc_ctx(
590 CHKERR PCSetType(pc, PCSHELL);
591 CHKERR PCShellSetContext(pc, pc_ctx.get());
595 if (flg == PETSC_TRUE) {
596 PetscReal rtol, atol, dtol;
598 CHKERR KSPGetTolerances(ksp, &rtol, &atol, &dtol, &maxits);
599 atol = my_tol * 1e-2;
601 CHKERR KSPSetTolerances(ksp, rtol, atol, dtol, maxits);
605 snes_ctx.getComputeRhs();
606 snes_ctx.getPreProcComputeRhs().push_back(my_dirichlet_bc);
607 snes_ctx.getPreProcComputeRhs().push_back(&pre_post_method);
608 loops_to_do_Rhs.push_back(
611 loops_to_do_Rhs.push_back(
615 loops_to_do_Rhs.push_back(
619 boost::ptr_map<std::string, EdgeForce> edge_forces;
620 string fe_name_str =
"FORCE_FE";
621 edge_forces.insert(fe_name_str,
new EdgeForce(m_field));
624 CHKERR edge_forces.at(fe_name_str)
625 .addForce(
"SPATIAL_POSITION", arc_ctx->F_lambda, it->getMeshsetId());
627 for (boost::ptr_map<std::string, EdgeForce>::iterator eit =
629 eit != edge_forces.end(); eit++) {
630 loops_to_do_Rhs.push_back(
635 boost::ptr_map<std::string, NodalForce> nodal_forces;
637 nodal_forces.insert(fe_name_str,
new NodalForce(m_field));
640 CHKERR nodal_forces.at(fe_name_str)
641 .addForce(
"SPATIAL_POSITION", arc_ctx->F_lambda, it->getMeshsetId());
643 for (boost::ptr_map<std::string, NodalForce>::iterator fit =
644 nodal_forces.begin();
645 fit != nodal_forces.end(); fit++) {
646 loops_to_do_Rhs.push_back(
651 loops_to_do_Rhs.push_back(
653 loops_to_do_Rhs.push_back(
655 snes_ctx.getPostProcComputeRhs().push_back(&pre_post_method);
656 snes_ctx.getPostProcComputeRhs().push_back(my_dirichlet_bc);
659 snes_ctx.getSetOperators();
660 snes_ctx.getPreProcSetOperators().push_back(my_dirichlet_bc);
661 loops_to_do_Mat.push_back(
664 loops_to_do_Mat.push_back(
667 loops_to_do_Mat.push_back(
669 loops_to_do_Mat.push_back(
671 snes_ctx.getPostProcSetOperators().push_back(my_dirichlet_bc);
674 "ELASTIC_MECHANICS",
COL,
D, INSERT_VALUES, SCATTER_FORWARD);
675 CHKERR VecGhostUpdateBegin(
D, INSERT_VALUES, SCATTER_FORWARD);
676 CHKERR VecGhostUpdateEnd(
D, INSERT_VALUES, SCATTER_FORWARD);
678 PetscScalar step_size_reduction;
680 &step_size_reduction, &flg);
681 if (flg != PETSC_TRUE) {
682 step_size_reduction = 1.;
688 if (flg != PETSC_TRUE) {
695 if (flg != PETSC_TRUE) {
698 PetscScalar max_reduction = 10, min_reduction = 0.1;
700 &max_reduction, &flg);
702 &min_reduction, &flg);
704 double gamma = 0.5, reduction = 1;
707 step_size = step_size_reduction;
709 reduction = step_size_reduction;
712 double step_size0 = step_size;
716 "ELASTIC_MECHANICS",
"SPATIAL_POSITION",
"X0_SPATIAL_POSITION",
COL,
717 arc_ctx->x0, INSERT_VALUES, SCATTER_FORWARD);
719 CHKERR VecNorm(arc_ctx->x0, NORM_2, &x0_nrm);
720 CHKERR PetscPrintf(PETSC_COMM_WORLD,
721 "\tRead x0_nrm = %6.4e dlambda = %6.4e\n", x0_nrm,
723 CHKERR arc_ctx->setAlphaBeta(1, 0);
725 CHKERR arc_ctx->setS(step_size);
726 CHKERR arc_ctx->setAlphaBeta(0, 1);
733 CHKERR VecDuplicate(arc_ctx->x0, &x00);
734 bool converged_state =
false;
736 for (
int jj = 0; step < max_steps; step++, jj++) {
739 CHKERR VecCopy(arc_ctx->x0, x00);
743 CHKERR PetscPrintf(PETSC_COMM_WORLD,
"Load Step %D step_size = %6.4e\n",
745 CHKERR arc_ctx->setS(step_size);
746 CHKERR arc_ctx->setAlphaBeta(0, 1);
747 CHKERR VecCopy(
D, arc_ctx->x0);
752 }
else if (step == 2) {
754 CHKERR arc_ctx->setAlphaBeta(1, 0);
757 step_size0 = step_size;
758 CHKERR arc_ctx->setS(step_size);
759 double dlambda = arc_ctx->dLambda;
761 CHKERR VecNorm(arc_ctx->dx, NORM_2, &dx_nrm);
762 CHKERR PetscPrintf(PETSC_COMM_WORLD,
763 "Load Step %D step_size = %6.4e dlambda0 = %6.4e "
764 "dx_nrm = %6.4e dx2 = %6.4e\n",
765 step, step_size, dlambda, dx_nrm, arc_ctx->dx2);
766 CHKERR VecCopy(
D, arc_ctx->x0);
767 CHKERR VecAXPY(
D, 1., arc_ctx->dx);
773 step_size0 = step_size;
777 step_size *= reduction;
778 if (step_size > max_reduction * step_size0) {
779 step_size = max_reduction * step_size0;
780 }
else if (step_size < min_reduction * step_size0) {
781 step_size = min_reduction * step_size0;
783 CHKERR arc_ctx->setS(step_size);
784 double dlambda = reduction * arc_ctx->dLambda;
786 CHKERR VecScale(arc_ctx->dx, reduction);
787 CHKERR VecNorm(arc_ctx->dx, NORM_2, &dx_nrm);
788 CHKERR PetscPrintf(PETSC_COMM_WORLD,
789 "Load Step %D step_size = %6.4e dlambda0 = %6.4e "
790 "dx_nrm = %6.4e dx2 = %6.4e\n",
791 step, step_size, dlambda, dx_nrm, arc_ctx->dx2);
792 CHKERR VecCopy(
D, arc_ctx->x0);
793 CHKERR VecAXPY(
D, 1., arc_ctx->dx);
797 CHKERR SNESSolve(snes, PETSC_NULL,
D);
799 CHKERR SNESGetIterationNumber(snes, &its);
800 CHKERR PetscPrintf(PETSC_COMM_WORLD,
"number of Newton iterations = %D\n",
803 SNESConvergedReason reason;
804 CHKERR SNESGetConvergedReason(snes, &reason);
808 CHKERR VecCopy(x00, arc_ctx->x0);
811 CHKERR VecNorm(arc_ctx->x0, NORM_2, &x0_nrm);
812 CHKERR PetscPrintf(PETSC_COMM_WORLD,
813 "\tRead x0_nrm = %6.4e dlambda = %6.4e\n", x0_nrm,
815 CHKERR arc_ctx->setAlphaBeta(1, 0);
818 converged_state =
false;
824 if (step > 1 && converged_state) {
826 reduction = pow((
double)its_d / (
double)(its + 1), gamma);
827 if (step_size >= max_reduction * step_size0 && reduction > 1) {
829 }
else if (step_size <= min_reduction * step_size0 && reduction < 1) {
832 CHKERR PetscPrintf(PETSC_COMM_WORLD,
"reduction step_size = %6.4e\n",
838 "ELASTIC_MECHANICS",
COL,
D, INSERT_VALUES, SCATTER_REVERSE);
840 "ELASTIC_MECHANICS",
"SPATIAL_POSITION",
"X0_SPATIAL_POSITION",
COL,
841 arc_ctx->x0, INSERT_VALUES, SCATTER_REVERSE);
842 converged_state =
true;
858 std::ostringstream o1;
859 o1 <<
"out_" << step <<
".h5m";
863 CHKERR pre_post_method.potsProcessLoadPath();
873 CHKERR MatDestroy(&ShellAij);
874 CHKERR SNESDestroy(&snes);