xref: /libCEED/examples/solids/src/cl-options.c (revision 6f4486566ca58303378304fa7d7f298fb3b0a2bb) !
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 /// @file
9 /// Command line option processing for solid mechanics example using PETSc
10 
11 #include "../include/cl-options.h"
12 
13 // -----------------------------------------------------------------------------
14 // Process command line options
15 // -----------------------------------------------------------------------------
16 // Process general command line options
17 PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx) {
18   PetscErrorCode ierr;
19   PetscBool ceed_flag = PETSC_FALSE;
20 
21   PetscFunctionBeginUser;
22 
23   PetscOptionsBegin(comm, NULL,
24                     "Elasticity / Hyperelasticity in PETSc with libCEED", NULL);
25 
26   ierr = PetscOptionsString("-ceed", "CEED resource specifier",
27                             NULL, app_ctx->ceed_resource, app_ctx->ceed_resource,
28                             sizeof(app_ctx->ceed_resource), &ceed_flag);
29   CHKERRQ(ierr);
30 
31   ierr = PetscStrncpy(app_ctx->output_dir, ".", 2);
32   CHKERRQ(ierr); // Default - current directory
33   ierr = PetscOptionsString("-output_dir", "Output directory",
34                             NULL, app_ctx->output_dir, app_ctx->output_dir,
35                             sizeof(app_ctx->output_dir), NULL); CHKERRQ(ierr);
36 
37   app_ctx->degree         = 3;
38   ierr = PetscOptionsInt("-degree", "Polynomial degree of tensor product basis",
39                          NULL, app_ctx->degree, &app_ctx->degree, NULL);
40   CHKERRQ(ierr);
41 
42   app_ctx->q_extra         = 0;
43   ierr = PetscOptionsInt("-qextra", "Number of extra quadrature points",
44                          NULL, app_ctx->q_extra, &app_ctx->q_extra, NULL);
45   CHKERRQ(ierr);
46 
47   ierr = PetscOptionsString("-mesh", "Read mesh from file", NULL,
48                             app_ctx->mesh_file, app_ctx->mesh_file,
49                             sizeof(app_ctx->mesh_file), NULL); CHKERRQ(ierr);
50 
51   app_ctx->problem_choice  = ELAS_LINEAR;       // Default - Linear Elasticity
52   ierr = PetscOptionsEnum("-problem",
53                           "Solves Elasticity & Hyperelasticity Problems",
54                           NULL, problemTypes, (PetscEnum)app_ctx->problem_choice,
55                           (PetscEnum *)&app_ctx->problem_choice, NULL);
56   CHKERRQ(ierr);
57   app_ctx->name = problemTypes[app_ctx->problem_choice];
58   app_ctx->name_for_disp = problemTypesForDisp[app_ctx->problem_choice];
59 
60   app_ctx->num_increments = app_ctx->problem_choice == ELAS_LINEAR ? 1 : 10;
61   ierr = PetscOptionsInt("-num_steps", "Number of pseudo-time steps",
62                          NULL, app_ctx->num_increments, &app_ctx->num_increments,
63                          NULL); CHKERRQ(ierr);
64 
65   app_ctx->forcing_choice  = FORCE_NONE;     // Default - no forcing term
66   ierr = PetscOptionsEnum("-forcing", "Set forcing function option", NULL,
67                           forcing_types, (PetscEnum)app_ctx->forcing_choice,
68                           (PetscEnum *)&app_ctx->forcing_choice, NULL);
69   CHKERRQ(ierr);
70 
71   PetscInt max_n = 3;
72   app_ctx->forcing_vector[0] = 0;
73   app_ctx->forcing_vector[1] = -1;
74   app_ctx->forcing_vector[2] = 0;
75   ierr = PetscOptionsScalarArray("-forcing_vec",
76                                  "Direction to apply constant force", NULL,
77                                  app_ctx->forcing_vector, &max_n, NULL);
78   CHKERRQ(ierr);
79 
80   if ((app_ctx->problem_choice == ELAS_FSInitial_NH1 ||
81        app_ctx->problem_choice == ELAS_FSInitial_NH2 ||
82        app_ctx->problem_choice == ELAS_FSCurrent_NH1 ||
83        app_ctx->problem_choice == ELAS_FSCurrent_NH2 ||
84        app_ctx->problem_choice == ELAS_FSInitial_MR1) &&
85       app_ctx->forcing_choice == FORCE_CONST)
86     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP,
87             "Cannot use constant forcing and finite strain formulation. "
88             "Constant forcing in reference frame currently unavaliable.");
89 
90   // Dirichlet boundary conditions
91   app_ctx->bc_clamp_count = 16;
92   ierr = PetscOptionsIntArray("-bc_clamp",
93                               "Face IDs to apply incremental Dirichlet BC",
94                               NULL, app_ctx->bc_clamp_faces, &app_ctx->bc_clamp_count,
95                               NULL); CHKERRQ(ierr);
96   // Set vector for each clamped BC
97   for (PetscInt i = 0; i < app_ctx->bc_clamp_count; i++) {
98     // Translation vector
99     char option_name[25];
100     const size_t nclamp_params = sizeof(app_ctx->bc_clamp_max[0])/sizeof(
101                                    app_ctx->bc_clamp_max[0][0]);
102     for (PetscInt j = 0; j < nclamp_params; j++)
103       app_ctx->bc_clamp_max[i][j] = 0.;
104 
105     snprintf(option_name, sizeof option_name, "-bc_clamp_%d_translate",
106              app_ctx->bc_clamp_faces[i]);
107     max_n = 3;
108     ierr = PetscOptionsScalarArray(option_name,
109                                    "Vector to translate clamped end by", NULL,
110                                    app_ctx->bc_clamp_max[i], &max_n, NULL);
111     CHKERRQ(ierr);
112 
113     // Rotation vector
114     max_n = 5;
115     snprintf(option_name, sizeof option_name, "-bc_clamp_%d_rotate",
116              app_ctx->bc_clamp_faces[i]);
117     ierr = PetscOptionsScalarArray(option_name,
118                                    "Vector with axis of rotation and rotation, in radians",
119                                    NULL, &app_ctx->bc_clamp_max[i][3], &max_n, NULL);
120     CHKERRQ(ierr);
121 
122     // Normalize
123     PetscScalar norm = sqrt(app_ctx->bc_clamp_max[i][3]*app_ctx->bc_clamp_max[i][3]
124                             + app_ctx->bc_clamp_max[i][4]*app_ctx->bc_clamp_max[i][4]
125                             + app_ctx->bc_clamp_max[i][5]*app_ctx->bc_clamp_max[i][5]);
126     if (fabs(norm) < 1e-16)
127       norm = 1;
128     for (PetscInt j = 0; j < 3; j++)
129       app_ctx->bc_clamp_max[i][3 + j] /= norm;
130   }
131 
132   // Neumann boundary conditions
133   app_ctx->bc_traction_count = 16;
134   ierr = PetscOptionsIntArray("-bc_traction",
135                               "Face IDs to apply traction (Neumann) BC",
136                               NULL, app_ctx->bc_traction_faces,
137                               &app_ctx->bc_traction_count, NULL); CHKERRQ(ierr);
138   // Set vector for each traction BC
139   for (PetscInt i = 0; i < app_ctx->bc_traction_count; i++) {
140     // Translation vector
141     char option_name[25];
142     for (PetscInt j = 0; j < 3; j++)
143       app_ctx->bc_traction_vector[i][j] = 0.;
144 
145     snprintf(option_name, sizeof option_name, "-bc_traction_%d",
146              app_ctx->bc_traction_faces[i]);
147     max_n = 3;
148     PetscBool set = false;
149     ierr = PetscOptionsScalarArray(option_name,
150                                    "Traction vector for constrained face", NULL,
151                                    app_ctx->bc_traction_vector[i], &max_n, &set);
152     CHKERRQ(ierr);
153 
154     if (!set)
155       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP,
156               "Traction vector must be set for all traction boundary conditions.");
157   }
158 
159   app_ctx->multigrid_choice = MULTIGRID_LOGARITHMIC;
160   ierr = PetscOptionsEnum("-multigrid", "Set multigrid type option", NULL,
161                           multigrid_types, (PetscEnum)app_ctx->multigrid_choice,
162                           (PetscEnum *)&app_ctx->multigrid_choice, NULL);
163   CHKERRQ(ierr);
164 
165   app_ctx->test_mode = PETSC_FALSE;
166   ierr = PetscOptionsBool("-test",
167                           "Testing mode (do not print unless error is large)",
168                           NULL, app_ctx->test_mode, &(app_ctx->test_mode), NULL);
169   CHKERRQ(ierr);
170 
171   app_ctx->expect_final_strain = -1.;
172   ierr = PetscOptionsReal("-expect_final_strain_energy",
173                           "Expect final strain energy close to this value.",
174                           NULL, app_ctx->expect_final_strain, &app_ctx->expect_final_strain, NULL);
175   CHKERRQ(ierr);
176 
177   app_ctx->test_tol = 1e-8;
178   ierr = PetscOptionsReal("-expect_final_state_rtol",
179                           "Relative tolerance for final strain energy test",
180                           NULL, app_ctx->test_tol, &app_ctx->test_tol, NULL);
181   CHKERRQ(ierr);
182 
183   app_ctx->view_soln = PETSC_FALSE;
184   ierr = PetscOptionsBool("-view_soln", "Write out solution vector for viewing",
185                           NULL, app_ctx->view_soln, &(app_ctx->view_soln), NULL);
186   CHKERRQ(ierr);
187 
188   app_ctx->view_final_soln = PETSC_FALSE;
189   ierr = PetscOptionsBool("-view_final_soln",
190                           "Write out final solution vector for viewing",
191                           NULL, app_ctx->view_final_soln, &(app_ctx->view_final_soln),
192                           NULL); CHKERRQ(ierr);
193   CHKERRQ(ierr);
194 
195   PetscBool set;
196   char energy_viewer_filename[PETSC_MAX_PATH_LEN] = "";
197   ierr = PetscOptionsString("-strain_energy_monitor",
198                             "Print out current strain energy at every load increment",
199                             NULL, energy_viewer_filename,
200                             energy_viewer_filename, sizeof(energy_viewer_filename),
201                             &set); CHKERRQ(ierr);
202   if (set) {
203     ierr = PetscViewerASCIIOpen(comm, energy_viewer_filename,
204                                 &app_ctx->energy_viewer); CHKERRQ(ierr);
205     ierr = PetscViewerASCIIPrintf(app_ctx->energy_viewer, "increment,energy\n");
206     CHKERRQ(ierr);
207     // Initial configuration is base energy state; this may not be true if we extend in the future to
208     // initially loaded configurations (because a truly at-rest initial state may not be realizable).
209     ierr = PetscViewerASCIIPrintf(app_ctx->energy_viewer, "%f,%e\n", 0., 0.);
210     CHKERRQ(ierr);
211   }
212   PetscOptionsEnd(); // End of setting AppCtx
213 
214   // Check for all required values set
215   if (app_ctx->test_mode) {
216     if (app_ctx->forcing_choice == FORCE_NONE && !app_ctx->bc_clamp_count)
217       app_ctx->forcing_choice = FORCE_MMS;
218   }
219   if (!app_ctx->bc_clamp_count && app_ctx->forcing_choice != FORCE_MMS) {
220     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "-boundary options needed");
221   }
222 
223   // Provide default ceed resource if not specified
224   if (!ceed_flag) {
225     const char *ceed_resource = "/cpu/self";
226     strncpy(app_ctx->ceed_resource, ceed_resource, 10);
227   }
228 
229   // Determine number of levels
230   switch (app_ctx->multigrid_choice) {
231   case MULTIGRID_LOGARITHMIC:
232     app_ctx->num_levels = ceil(log(app_ctx->degree)/log(2)) + 1;
233     break;
234   case MULTIGRID_UNIFORM:
235     app_ctx->num_levels = app_ctx->degree;
236     break;
237   case MULTIGRID_NONE:
238     app_ctx->num_levels = 1;
239     break;
240   }
241 
242   // Populate array of degrees for each level for multigrid
243   ierr = PetscMalloc1(app_ctx->num_levels, &(app_ctx->level_degrees));
244   CHKERRQ(ierr);
245 
246   switch (app_ctx->multigrid_choice) {
247   case MULTIGRID_LOGARITHMIC:
248     for (int i = 0; i < app_ctx->num_levels-1; i++)
249       app_ctx->level_degrees[i] = pow(2,i);
250     app_ctx->level_degrees[app_ctx->num_levels-1] = app_ctx->degree;
251     break;
252   case MULTIGRID_UNIFORM:
253     for (int i = 0; i < app_ctx->num_levels; i++)
254       app_ctx->level_degrees[i] = i + 1;
255     break;
256   case MULTIGRID_NONE:
257     app_ctx->level_degrees[0] = app_ctx->degree;
258     break;
259   }
260 
261   PetscFunctionReturn(0);
262 };
263