xref: /libCEED/examples/solids/problems/mooney-rivlin.c (revision 3d8e882215d238700cdceb37404f76ca7fa24eaa)
1*3d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2*3d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3*3d8e8822SJeremy L Thompson //
4*3d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5*3d8e8822SJeremy L Thompson //
6*3d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7*3d8e8822SJeremy L Thompson 
85754ecacSJeremy L Thompson #include <ceed.h>
95754ecacSJeremy L Thompson #include <petsc.h>
105754ecacSJeremy L Thompson #include "../problems/mooney-rivlin.h"
115754ecacSJeremy L Thompson 
125754ecacSJeremy L Thompson // Build libCEED context object
135754ecacSJeremy L Thompson PetscErrorCode PhysicsContext_MR(MPI_Comm comm, Ceed ceed, Units *units,
145754ecacSJeremy L Thompson                                  CeedQFunctionContext *ctx) {
155754ecacSJeremy L Thompson   PetscErrorCode ierr;
165754ecacSJeremy L Thompson   Physics_MR phys;
175754ecacSJeremy L Thompson 
185754ecacSJeremy L Thompson   PetscFunctionBegin;
195754ecacSJeremy L Thompson 
205754ecacSJeremy L Thompson   ierr = PetscMalloc1(1, units); CHKERRQ(ierr);
215754ecacSJeremy L Thompson   ierr = PetscMalloc1(1, &phys); CHKERRQ(ierr);
225754ecacSJeremy L Thompson   ierr = ProcessPhysics_MR(comm, phys, *units); CHKERRQ(ierr);
235754ecacSJeremy L Thompson   CeedQFunctionContextCreate(ceed, ctx);
245754ecacSJeremy L Thompson   CeedQFunctionContextSetData(*ctx, CEED_MEM_HOST, CEED_COPY_VALUES,
255754ecacSJeremy L Thompson                               sizeof(*phys), phys);
265754ecacSJeremy L Thompson   ierr = PetscFree(phys); CHKERRQ(ierr);
275754ecacSJeremy L Thompson 
285754ecacSJeremy L Thompson   PetscFunctionReturn(0);
295754ecacSJeremy L Thompson }
305754ecacSJeremy L Thompson 
315754ecacSJeremy L Thompson // Build libCEED smoother context object
325754ecacSJeremy L Thompson PetscErrorCode PhysicsSmootherContext_MR(MPI_Comm comm, Ceed ceed,
335754ecacSJeremy L Thompson     CeedQFunctionContext ctx, CeedQFunctionContext *ctx_smoother) {
345754ecacSJeremy L Thompson   PetscErrorCode ierr;
355754ecacSJeremy L Thompson   PetscScalar nu_smoother = 0;
365754ecacSJeremy L Thompson   PetscBool nu_flag = PETSC_FALSE;
375754ecacSJeremy L Thompson   Physics_MR phys, phys_smoother;
385754ecacSJeremy L Thompson 
395754ecacSJeremy L Thompson   PetscFunctionBegin;
405754ecacSJeremy L Thompson 
415754ecacSJeremy L Thompson   ierr = PetscOptionsBegin(comm, NULL,
425754ecacSJeremy L Thompson                            "Mooney Rivlin physical parameters for smoother", NULL);
435754ecacSJeremy L Thompson   CHKERRQ(ierr);
445754ecacSJeremy L Thompson 
455754ecacSJeremy L Thompson   ierr = PetscOptionsScalar("-nu_smoother", "Poisson's ratio for smoother",
465754ecacSJeremy L Thompson                             NULL, nu_smoother, &nu_smoother, &nu_flag);
475754ecacSJeremy L Thompson   CHKERRQ(ierr);
485754ecacSJeremy L Thompson   if (nu_smoother < 0 ||
495754ecacSJeremy L Thompson       nu_smoother >= 0.5) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP,
505754ecacSJeremy L Thompson                                     "Mooney-Rivlin model requires Poisson ratio -nu option in [0, .5)");
515754ecacSJeremy L Thompson 
525754ecacSJeremy L Thompson   ierr = PetscOptionsEnd(); CHKERRQ(ierr); // End of setting Physics
535754ecacSJeremy L Thompson 
545754ecacSJeremy L Thompson   if (nu_flag) {
555754ecacSJeremy L Thompson     // Copy context
565754ecacSJeremy L Thompson     CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &phys);
575754ecacSJeremy L Thompson     ierr = PetscMalloc1(1, &phys_smoother); CHKERRQ(ierr);
585754ecacSJeremy L Thompson     ierr = PetscMemcpy(phys_smoother, phys, sizeof(*phys)); CHKERRQ(ierr);
595754ecacSJeremy L Thompson     CeedQFunctionContextRestoreData(ctx, &phys);
605754ecacSJeremy L Thompson     // Create smoother context
615754ecacSJeremy L Thompson     CeedQFunctionContextCreate(ceed, ctx_smoother);
625754ecacSJeremy L Thompson     phys_smoother->lambda = 2 * (phys_smoother->mu_1 + phys_smoother->mu_2) *
635754ecacSJeremy L Thompson                             nu_smoother / (1 - 2*nu_smoother);
645754ecacSJeremy L Thompson     CeedQFunctionContextSetData(*ctx_smoother, CEED_MEM_HOST, CEED_COPY_VALUES,
655754ecacSJeremy L Thompson                                 sizeof(*phys_smoother), phys_smoother);
665754ecacSJeremy L Thompson     ierr = PetscFree(phys_smoother); CHKERRQ(ierr);
675754ecacSJeremy L Thompson   } else {
685754ecacSJeremy L Thompson     *ctx_smoother = NULL;
695754ecacSJeremy L Thompson   }
705754ecacSJeremy L Thompson 
715754ecacSJeremy L Thompson   PetscFunctionReturn(0);
725754ecacSJeremy L Thompson }
735754ecacSJeremy L Thompson 
745754ecacSJeremy L Thompson // Process physics options - Mooney-Rivlin
755754ecacSJeremy L Thompson PetscErrorCode ProcessPhysics_MR(MPI_Comm comm, Physics_MR phys, Units units) {
765754ecacSJeremy L Thompson   PetscErrorCode ierr;
775754ecacSJeremy L Thompson   PetscReal nu = -1;
785754ecacSJeremy L Thompson   phys->mu_1 = -1;
795754ecacSJeremy L Thompson   phys->mu_2 = -1;
805754ecacSJeremy L Thompson   phys->lambda = -1;
815754ecacSJeremy L Thompson   units->meter     = 1;        // 1 meter in scaled length units
825754ecacSJeremy L Thompson   units->second    = 1;        // 1 second in scaled time units
835754ecacSJeremy L Thompson   units->kilogram  = 1;        // 1 kilogram in scaled mass units
845754ecacSJeremy L Thompson 
855754ecacSJeremy L Thompson   PetscFunctionBeginUser;
865754ecacSJeremy L Thompson 
875754ecacSJeremy L Thompson   ierr = PetscOptionsBegin(comm, NULL, "Mooney Rivlin physical parameters", NULL);
885754ecacSJeremy L Thompson   CHKERRQ(ierr);
895754ecacSJeremy L Thompson 
905754ecacSJeremy L Thompson   ierr = PetscOptionsScalar("-mu_1", "Material Property mu_1", NULL,
915754ecacSJeremy L Thompson                             phys->mu_1, &phys->mu_1, NULL); CHKERRQ(ierr);
925754ecacSJeremy L Thompson   if (phys->mu_1 < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP,
935754ecacSJeremy L Thompson                                 "Mooney-Rivlin model requires non-negative -mu_1 option (Pa)");
945754ecacSJeremy L Thompson 
955754ecacSJeremy L Thompson   ierr = PetscOptionsScalar("-mu_2", "Material Property mu_2", NULL,
965754ecacSJeremy L Thompson                             phys->mu_2, &phys->mu_2, NULL); CHKERRQ(ierr);
975754ecacSJeremy L Thompson   if (phys->mu_2 < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP,
985754ecacSJeremy L Thompson                                 "Mooney-Rivlin model requires non-negative -mu_2 option (Pa)");
995754ecacSJeremy L Thompson 
1005754ecacSJeremy L Thompson   ierr = PetscOptionsScalar("-nu", "Poisson ratio", NULL,
1015754ecacSJeremy L Thompson                             nu, &nu, NULL); CHKERRQ(ierr);
1025754ecacSJeremy L Thompson   if (nu < 0 || nu >= 0.5) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP,
1035754ecacSJeremy L Thompson                                      "Mooney-Rivlin model requires Poisson ratio -nu option in [0, .5)");
1045754ecacSJeremy L Thompson   phys->lambda = 2 * (phys->mu_1 + phys->mu_2) * nu / (1 - 2*nu);
1055754ecacSJeremy L Thompson 
1065754ecacSJeremy L Thompson   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
1075754ecacSJeremy L Thompson                             NULL, units->meter, &units->meter, NULL);
1085754ecacSJeremy L Thompson   CHKERRQ(ierr);
1095754ecacSJeremy L Thompson   units->meter = fabs(units->meter);
1105754ecacSJeremy L Thompson 
1115754ecacSJeremy L Thompson   ierr = PetscOptionsScalar("-units_second", "1 second in scaled time units",
1125754ecacSJeremy L Thompson                             NULL, units->second, &units->second, NULL);
1135754ecacSJeremy L Thompson   CHKERRQ(ierr);
1145754ecacSJeremy L Thompson   units->second = fabs(units->second);
1155754ecacSJeremy L Thompson 
1165754ecacSJeremy L Thompson   ierr = PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units",
1175754ecacSJeremy L Thompson                             NULL, units->kilogram, &units->kilogram, NULL);
1185754ecacSJeremy L Thompson   CHKERRQ(ierr);
1195754ecacSJeremy L Thompson   units->kilogram = fabs(units->kilogram);
1205754ecacSJeremy L Thompson 
1215754ecacSJeremy L Thompson   ierr = PetscOptionsEnd(); CHKERRQ(ierr); // End of setting Physics
1225754ecacSJeremy L Thompson 
1235754ecacSJeremy L Thompson   // Define derived units
1245754ecacSJeremy L Thompson   units->Pascal = units->kilogram / (units->meter * PetscSqr(units->second));
1255754ecacSJeremy L Thompson 
1265754ecacSJeremy L Thompson   // Scale material parameters based on units of Pa
1275754ecacSJeremy L Thompson   phys->mu_1 *= units->Pascal;
1285754ecacSJeremy L Thompson   phys->mu_2 *= units->Pascal;
1295754ecacSJeremy L Thompson   phys->lambda *= units->Pascal;
1305754ecacSJeremy L Thompson 
1315754ecacSJeremy L Thompson   PetscFunctionReturn(0);
1325754ecacSJeremy L Thompson };