xref: /libCEED/examples/solids/problems/neo-hookean.c (revision a71fcd9fac4e7a8dfa69a197fd7b41b8f31fd6a3)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed.h>
9 #include <petsc.h>
10 #include "../problems/neo-hookean.h"
11 
12 // Build libCEED context object
13 PetscErrorCode PhysicsContext_NH(MPI_Comm comm, Ceed ceed, Units *units,
14                                  CeedQFunctionContext *ctx) {
15   PetscErrorCode ierr;
16   Physics_NH phys;
17 
18   PetscFunctionBegin;
19 
20   ierr = PetscMalloc1(1, units); CHKERRQ(ierr);
21   ierr = PetscMalloc1(1, &phys); CHKERRQ(ierr);
22   ierr = ProcessPhysics_NH(comm, phys, *units); CHKERRQ(ierr);
23   CeedQFunctionContextCreate(ceed, ctx);
24   CeedQFunctionContextSetData(*ctx, CEED_MEM_HOST, CEED_COPY_VALUES,
25                               sizeof(*phys), phys);
26   ierr = PetscFree(phys); CHKERRQ(ierr);
27 
28   PetscFunctionReturn(0);
29 }
30 
31 // Build libCEED smoother context object
32 PetscErrorCode PhysicsSmootherContext_NH(MPI_Comm comm, Ceed ceed,
33     CeedQFunctionContext ctx, CeedQFunctionContext *ctx_smoother) {
34   PetscErrorCode ierr;
35   PetscScalar nu_smoother = 0;
36   PetscBool nu_flag = PETSC_FALSE;
37   Physics_NH phys, phys_smoother;
38 
39   PetscFunctionBegin;
40 
41   PetscOptionsBegin(comm, NULL, "Neo-Hookean physical parameters for smoother",
42                     NULL);
43 
44   ierr = PetscOptionsScalar("-nu_smoother", "Poisson's ratio for smoother",
45                             NULL, nu_smoother, &nu_smoother, &nu_flag);
46   CHKERRQ(ierr);
47 
48   PetscOptionsEnd(); // End of setting Physics
49 
50   if (nu_flag) {
51     // Copy context
52     CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &phys);
53     ierr = PetscMalloc1(1, &phys_smoother); CHKERRQ(ierr);
54     ierr = PetscMemcpy(phys_smoother, phys, sizeof(*phys)); CHKERRQ(ierr);
55     CeedQFunctionContextRestoreData(ctx, &phys);
56     // Create smoother context
57     CeedQFunctionContextCreate(ceed, ctx_smoother);
58     phys_smoother->nu = nu_smoother;
59     CeedQFunctionContextSetData(*ctx_smoother, CEED_MEM_HOST, CEED_COPY_VALUES,
60                                 sizeof(*phys_smoother), phys_smoother);
61     ierr = PetscFree(phys_smoother); CHKERRQ(ierr);
62   } else {
63     *ctx_smoother = NULL;
64   }
65 
66   PetscFunctionReturn(0);
67 }
68 
69 // Process physics options - Neo-Hookean
70 PetscErrorCode ProcessPhysics_NH(MPI_Comm comm, Physics_NH phys, Units units) {
71   PetscErrorCode ierr;
72   PetscBool nu_flag = PETSC_FALSE;
73   PetscBool Young_flag = PETSC_FALSE;
74   phys->nu = 0;
75   phys->E = 0;
76   units->meter     = 1;        // 1 meter in scaled length units
77   units->second    = 1;        // 1 second in scaled time units
78   units->kilogram  = 1;        // 1 kilogram in scaled mass units
79 
80   PetscFunctionBeginUser;
81 
82   PetscOptionsBegin(comm, NULL, "Neo-Hookean physical parameters", NULL);
83 
84   ierr = PetscOptionsScalar("-nu", "Poisson's ratio", NULL, phys->nu, &phys->nu,
85                             &nu_flag); CHKERRQ(ierr);
86 
87   ierr = PetscOptionsScalar("-E", "Young's Modulus", NULL, phys->E, &phys->E,
88                             &Young_flag); CHKERRQ(ierr);
89 
90   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
91                             NULL, units->meter, &units->meter, NULL);
92   CHKERRQ(ierr);
93   units->meter = fabs(units->meter);
94 
95   ierr = PetscOptionsScalar("-units_second", "1 second in scaled time units",
96                             NULL, units->second, &units->second, NULL);
97   CHKERRQ(ierr);
98   units->second = fabs(units->second);
99 
100   ierr = PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units",
101                             NULL, units->kilogram, &units->kilogram, NULL);
102   CHKERRQ(ierr);
103   units->kilogram = fabs(units->kilogram);
104 
105   PetscOptionsEnd(); // End of setting Physics
106 
107   // Check for all required options to be set
108   if (!nu_flag) {
109     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "-nu option needed");
110   }
111   if (!Young_flag) {
112     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "-E option needed");
113   }
114 
115   // Define derived units
116   units->Pascal = units->kilogram / (units->meter * PetscSqr(units->second));
117 
118   // Scale E to Pa
119   phys->E *= units->Pascal;
120 
121   PetscFunctionReturn(0);
122 };