|
| 1 | +static char help[] = "User example calling OPFLOW with the DCOPF model.\n\n"; |
| 2 | + |
| 3 | +#include <opflow.h> |
| 4 | +#include <exago_config.h> |
| 5 | +#include <utils.h> |
| 6 | +#include <string> |
| 7 | +#include <private/opflowimpl.h> |
| 8 | + |
| 9 | +int main(int argc, char **argv) { |
| 10 | + PetscErrorCode ierr; |
| 11 | + OPFLOW dcopf; |
| 12 | + char file[PETSC_MAX_PATH_LEN], gicfile[PETSC_MAX_PATH_LEN], |
| 13 | + output_name[PETSC_MAX_PATH_LEN]; |
| 14 | + PetscBool flg = PETSC_FALSE, print_output = PETSC_FALSE, |
| 15 | + save_output = PETSC_FALSE; |
| 16 | + PetscLogStage stages[3]; |
| 17 | + char appname[] = "dcopflow"; |
| 18 | + const char default_output_name[] = "dcopflowout"; |
| 19 | + MPI_Comm comm = MPI_COMM_WORLD; |
| 20 | + |
| 21 | + /** Use `ExaGOLogSetLoggingFileName("dcopflow-logfile");` to log the output. |
| 22 | + */ |
| 23 | + ierr = ExaGOInitialize(comm, &argc, &argv, appname, help); |
| 24 | + if (ierr) { |
| 25 | + fprintf(stderr, "Could not initialize ExaGO application %s.\n", appname); |
| 26 | + return ierr; |
| 27 | + } |
| 28 | + |
| 29 | + ierr = PetscOptionsGetBool(NULL, NULL, "-print_output", &print_output, NULL); |
| 30 | + ExaGOCheckError(ierr); |
| 31 | + ierr = PetscOptionsGetString(NULL, NULL, "-save_output", &output_name[0], |
| 32 | + PETSC_MAX_PATH_LEN, &save_output); |
| 33 | + ExaGOCheckError(ierr); |
| 34 | + |
| 35 | + /* Register stages for profiling application code sections */ |
| 36 | + ierr = PetscLogStageRegister("Reading Data", &stages[0]); |
| 37 | + ExaGOCheckError(ierr); |
| 38 | + ierr = PetscLogStageRegister("Set up", &stages[1]); |
| 39 | + ExaGOCheckError(ierr); |
| 40 | + ierr = PetscLogStageRegister("Solve", &stages[2]); |
| 41 | + ExaGOCheckError(ierr); |
| 42 | + |
| 43 | + /* Stage 1 - Application creation and reading data */ |
| 44 | + ierr = PetscLogStagePush(stages[0]); |
| 45 | + ExaGOCheckError(ierr); |
| 46 | + |
| 47 | + ExaGOLog(EXAGO_LOG_INFO, "{}", "Creating DCOPF\n"); |
| 48 | + |
| 49 | + /* Create OPFLOW object */ |
| 50 | + ierr = OPFLOWCreate(comm, &dcopf); |
| 51 | + ExaGOCheckError(ierr); |
| 52 | + |
| 53 | + /* Get network data file from command line */ |
| 54 | + ierr = PetscOptionsGetString(NULL, NULL, "-netfile", file, PETSC_MAX_PATH_LEN, |
| 55 | + &flg); |
| 56 | + ExaGOCheckError(ierr); |
| 57 | + if (flg) { |
| 58 | + if (strcasestr(file, ".raw") != NULL) { |
| 59 | + ierr = OPFLOWReadPSSERawData(dcopf, file); |
| 60 | + ExaGOCheckError(ierr); |
| 61 | + } else { |
| 62 | + ierr = OPFLOWReadMatPowerData(dcopf, file); |
| 63 | + ExaGOCheckError(ierr); |
| 64 | + } |
| 65 | + } else { |
| 66 | + ierr = OPFLOWReadMatPowerData(dcopf, "datafiles/case9/case9mod.m"); |
| 67 | + ExaGOCheckError(ierr); |
| 68 | + } |
| 69 | + |
| 70 | + /* Get gic data file from command line */ |
| 71 | + ierr = PetscOptionsGetString(NULL, NULL, "-gicfile", gicfile, |
| 72 | + PETSC_MAX_PATH_LEN, &flg); |
| 73 | + |
| 74 | + ExaGOCheckError(ierr); |
| 75 | + if (flg) { |
| 76 | + ierr = OPFLOWSetGICData(dcopf, gicfile); |
| 77 | + ExaGOCheckError(ierr); |
| 78 | + } |
| 79 | + |
| 80 | + ierr = PetscLogStagePop(); |
| 81 | + ExaGOCheckError(ierr); |
| 82 | + |
| 83 | + /* Stage 2 - Set up OPFLOW application */ |
| 84 | + ierr = PetscLogStagePush(stages[1]); |
| 85 | + ExaGOCheckError(ierr); |
| 86 | + |
| 87 | + /* Set options with options for DCOPF */ |
| 88 | + ierr = PetscOptionsSetValue(NULL, "-opflow_model", "DCOPF"); |
| 89 | + ExaGOCheckError(ierr); |
| 90 | + |
| 91 | + ierr = PetscOptionsSetValue(NULL, "-opflow_initialization", "DCOPF"); |
| 92 | + ExaGOCheckError(ierr); |
| 93 | + |
| 94 | + /* Set up */ |
| 95 | + ierr = OPFLOWSetUp(dcopf); |
| 96 | + ExaGOCheckError(ierr); |
| 97 | + |
| 98 | + ierr = PetscLogStagePop(); |
| 99 | + ExaGOCheckError(ierr); |
| 100 | + |
| 101 | + /* Stage 3 - Solve */ |
| 102 | + ierr = PetscLogStagePush(stages[2]); |
| 103 | + ExaGOCheckError(ierr); |
| 104 | + |
| 105 | + /* Solve */ |
| 106 | + ierr = OPFLOWSolve(dcopf); |
| 107 | + ExaGOCheckError(ierr); |
| 108 | + |
| 109 | + ierr = PetscLogStagePop(); |
| 110 | + ExaGOCheckError(ierr); |
| 111 | + |
| 112 | + if (print_output) { |
| 113 | + ierr = OPFLOWPrintSolution(dcopf); |
| 114 | + ExaGOCheckError(ierr); |
| 115 | + } |
| 116 | + |
| 117 | + if (save_output) { |
| 118 | + // deal with "-save_output 0/1" (the way it used to be) |
| 119 | + if (strlen(output_name) == 1 && output_name[0] == '1') { |
| 120 | + strncpy(output_name, default_output_name, PETSC_MAX_PATH_LEN); |
| 121 | + } else if (strlen(output_name) == 1 && output_name[0] == '0') { |
| 122 | + save_output = PETSC_FALSE; |
| 123 | + } else if (strlen(output_name) == 0) { |
| 124 | + strncpy(output_name, default_output_name, PETSC_MAX_PATH_LEN); |
| 125 | + } |
| 126 | + if (save_output) { |
| 127 | + ierr = OPFLOWSaveSolutionDefault(dcopf, output_name); |
| 128 | + ExaGOCheckError(ierr); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /* Destroy OPFLOW object */ |
| 133 | + ierr = OPFLOWDestroy(&dcopf); |
| 134 | + ExaGOCheckError(ierr); |
| 135 | + |
| 136 | + ExaGOFinalize(); |
| 137 | + return 0; |
| 138 | +} |
0 commit comments