xref: /libCEED/examples/solids/problems/neo-hookean.c (revision 67490bc6de48fa3615ca135c74fbc2a7c9584dee)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
33d8e8822SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
53d8e8822SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
73d8e8822SJeremy L Thompson 
85754ecacSJeremy L Thompson #include <ceed.h>
95754ecacSJeremy L Thompson #include <petsc.h>
105754ecacSJeremy L Thompson #include "../problems/neo-hookean.h"
115754ecacSJeremy L Thompson 
125754ecacSJeremy L Thompson // Build libCEED context object
135754ecacSJeremy L Thompson PetscErrorCode PhysicsContext_NH(MPI_Comm comm, Ceed ceed, Units *units,
145754ecacSJeremy L Thompson                                  CeedQFunctionContext *ctx) {
155754ecacSJeremy L Thompson   PetscErrorCode ierr;
165754ecacSJeremy L Thompson   Physics_NH 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_NH(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_NH(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_NH phys, phys_smoother;
385754ecacSJeremy L Thompson 
395754ecacSJeremy L Thompson   PetscFunctionBegin;
405754ecacSJeremy L Thompson 
41*67490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Neo-Hookean physical parameters for smoother",
42*67490bc6SJeremy L Thompson                     NULL);
435754ecacSJeremy L Thompson 
445754ecacSJeremy L Thompson   ierr = PetscOptionsScalar("-nu_smoother", "Poisson's ratio for smoother",
455754ecacSJeremy L Thompson                             NULL, nu_smoother, &nu_smoother, &nu_flag);
465754ecacSJeremy L Thompson   CHKERRQ(ierr);
475754ecacSJeremy L Thompson 
48*67490bc6SJeremy L Thompson   PetscOptionsEnd(); // End of setting Physics
495754ecacSJeremy L Thompson 
505754ecacSJeremy L Thompson   if (nu_flag) {
515754ecacSJeremy L Thompson     // Copy context
525754ecacSJeremy L Thompson     CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &phys);
535754ecacSJeremy L Thompson     ierr = PetscMalloc1(1, &phys_smoother); CHKERRQ(ierr);
545754ecacSJeremy L Thompson     ierr = PetscMemcpy(phys_smoother, phys, sizeof(*phys)); CHKERRQ(ierr);
555754ecacSJeremy L Thompson     CeedQFunctionContextRestoreData(ctx, &phys);
565754ecacSJeremy L Thompson     // Create smoother context
575754ecacSJeremy L Thompson     CeedQFunctionContextCreate(ceed, ctx_smoother);
585754ecacSJeremy L Thompson     phys_smoother->nu = nu_smoother;
595754ecacSJeremy L Thompson     CeedQFunctionContextSetData(*ctx_smoother, CEED_MEM_HOST, CEED_COPY_VALUES,
605754ecacSJeremy L Thompson                                 sizeof(*phys_smoother), phys_smoother);
615754ecacSJeremy L Thompson     ierr = PetscFree(phys_smoother); CHKERRQ(ierr);
625754ecacSJeremy L Thompson   } else {
635754ecacSJeremy L Thompson     *ctx_smoother = NULL;
645754ecacSJeremy L Thompson   }
655754ecacSJeremy L Thompson 
665754ecacSJeremy L Thompson   PetscFunctionReturn(0);
675754ecacSJeremy L Thompson }
685754ecacSJeremy L Thompson 
695754ecacSJeremy L Thompson // Process physics options - Neo-Hookean
705754ecacSJeremy L Thompson PetscErrorCode ProcessPhysics_NH(MPI_Comm comm, Physics_NH phys, Units units) {
715754ecacSJeremy L Thompson   PetscErrorCode ierr;
725754ecacSJeremy L Thompson   PetscBool nu_flag = PETSC_FALSE;
735754ecacSJeremy L Thompson   PetscBool Young_flag = PETSC_FALSE;
745754ecacSJeremy L Thompson   phys->nu = 0;
755754ecacSJeremy L Thompson   phys->E = 0;
765754ecacSJeremy L Thompson   units->meter     = 1;        // 1 meter in scaled length units
775754ecacSJeremy L Thompson   units->second    = 1;        // 1 second in scaled time units
785754ecacSJeremy L Thompson   units->kilogram  = 1;        // 1 kilogram in scaled mass units
795754ecacSJeremy L Thompson 
805754ecacSJeremy L Thompson   PetscFunctionBeginUser;
815754ecacSJeremy L Thompson 
82*67490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Neo-Hookean physical parameters", NULL);
835754ecacSJeremy L Thompson 
845754ecacSJeremy L Thompson   ierr = PetscOptionsScalar("-nu", "Poisson's ratio", NULL, phys->nu, &phys->nu,
855754ecacSJeremy L Thompson                             &nu_flag); CHKERRQ(ierr);
865754ecacSJeremy L Thompson 
875754ecacSJeremy L Thompson   ierr = PetscOptionsScalar("-E", "Young's Modulus", NULL, phys->E, &phys->E,
885754ecacSJeremy L Thompson                             &Young_flag); CHKERRQ(ierr);
895754ecacSJeremy L Thompson 
905754ecacSJeremy L Thompson   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
915754ecacSJeremy L Thompson                             NULL, units->meter, &units->meter, NULL);
925754ecacSJeremy L Thompson   CHKERRQ(ierr);
935754ecacSJeremy L Thompson   units->meter = fabs(units->meter);
945754ecacSJeremy L Thompson 
955754ecacSJeremy L Thompson   ierr = PetscOptionsScalar("-units_second", "1 second in scaled time units",
965754ecacSJeremy L Thompson                             NULL, units->second, &units->second, NULL);
975754ecacSJeremy L Thompson   CHKERRQ(ierr);
985754ecacSJeremy L Thompson   units->second = fabs(units->second);
995754ecacSJeremy L Thompson 
1005754ecacSJeremy L Thompson   ierr = PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units",
1015754ecacSJeremy L Thompson                             NULL, units->kilogram, &units->kilogram, NULL);
1025754ecacSJeremy L Thompson   CHKERRQ(ierr);
1035754ecacSJeremy L Thompson   units->kilogram = fabs(units->kilogram);
1045754ecacSJeremy L Thompson 
105*67490bc6SJeremy L Thompson   PetscOptionsEnd(); // End of setting Physics
1065754ecacSJeremy L Thompson 
1075754ecacSJeremy L Thompson   // Check for all required options to be set
1085754ecacSJeremy L Thompson   if (!nu_flag) {
1095754ecacSJeremy L Thompson     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "-nu option needed");
1105754ecacSJeremy L Thompson   }
1115754ecacSJeremy L Thompson   if (!Young_flag) {
1125754ecacSJeremy L Thompson     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "-E option needed");
1135754ecacSJeremy L Thompson   }
1145754ecacSJeremy L Thompson 
1155754ecacSJeremy L Thompson   // Define derived units
1165754ecacSJeremy L Thompson   units->Pascal = units->kilogram / (units->meter * PetscSqr(units->second));
1175754ecacSJeremy L Thompson 
1185754ecacSJeremy L Thompson   // Scale E to Pa
1195754ecacSJeremy L Thompson   phys->E *= units->Pascal;
1205754ecacSJeremy L Thompson 
1215754ecacSJeremy L Thompson   PetscFunctionReturn(0);
1225754ecacSJeremy L Thompson };