Ice Sheet System Model  4.18
Code documentation
Functions
ParseToolkitsOptionsx.h File Reference

header file for parsing petsc options file More...

#include "../../classes/classes.h"

Go to the source code of this file.

Functions

void ParseToolkitsOptionsx (Parameters *parameters, FILE *petscoptionsfid)
 

Detailed Description

header file for parsing petsc options file

Definition in file ParseToolkitsOptionsx.h.

Function Documentation

◆ ParseToolkitsOptionsx()

void ParseToolkitsOptionsx ( Parameters parameters,
FILE *  petscoptionsfid 
)

Definition at line 19 of file ParseToolkitsOptionsx.cpp.

19  {
20 
21  char line[1000];
22  char word1[1000];
23  char word2[1000];
24 
25  /*intermediary: */
26  int *analyses = NULL;
27  char **strings = NULL;
28  char *string = NULL;
29  char **toolkits = NULL;
30  char *toolkit = NULL;
31  char *newstring = NULL;
32  char *catstring = NULL;
33  int numanalyses;
34  int stringlength,toolkitlength;
35 
36  /*Get my_rank:*/
37  int my_rank=IssmComm::GetRank();
38 
39  if(my_rank==0){
40 
41  /*Now, go through lines and figure out how many analyses we have: */
42  numanalyses=0;
43  while(fgets(line, sizeof line, fid)){
44  /*skip comments and empty lines: */
45  if ((line[0]=='%') || (line[0]=='\n') || (line[0]==' ') || (line[0]=='\t') || (line[0]=='\r')) continue;
46  /*ok, first time, we should get an analysis enum, starting with a +: */
47  if (line[0]=='+'){
48  numanalyses++;
49  }
50  else{
51  continue;
52  }
53  }
54 
55  /*Now, allocate analyses and strings: */
56  analyses = xNew<int>(numanalyses);
57  strings = xNew<char*>(numanalyses);
58  toolkits = xNew<char*>(numanalyses);
59  for(int i=0;i<numanalyses;i++) strings[i] = NULL;
60  for(int i=0;i<numanalyses;i++) toolkits[i] = NULL;
61 
62  /*Go back to beginning of file:*/
63  fseek(fid,0,SEEK_SET);
64  numanalyses=0;
65  while( fgets(line, sizeof line, fid)){
66  /*skip comments and empty lines: */
67  if ((line[0]=='%') || (line[0]=='\n') || (line[0]==' ') || (line[0]=='\t') || (line[0]=='\r'))continue;
68 
69  /*Get rid of end of line: */
70  line[strlen(line)-1]='\0';
71 
72  if(line[0]=='+'){ /*this is the analysis line: */
73  analyses[numanalyses]=StringToEnumx(&line[1]); //skip the '+'
74  numanalyses++;
75  /*Now get toolkit (should be following line)*/
76  if(!fgets(line, sizeof line, fid)) _error_("could not read toolkit for analysis " << &line[1]);
77  sscanf(line,"-%s %s",&word1[0],&word2[0]);
78  if(strcmp(word1,"toolkit")!=0) _error_("toolkit option does not follow analysis");
79  toolkit=xNew<char>((strlen(word2)+1));
80  xMemCpy<char>(toolkit,word2,(strlen(word2)+1));
81  toolkits[numanalyses-1]=toolkit;
82  continue;
83  }
84  else{
85  /*this is an option corresponding to analysis numanalyses-1. Add it to the already existing options*/
86  if(strings[numanalyses-1]==NULL){
87  string=xNew<char>((strlen(line)+1));
88  xMemCpy<char>(string,line,(strlen(line)+1));
89 
90  strings[numanalyses-1]=string;
91  }
92  else{
93  string=strings[numanalyses-1];
94  newstring=xNew<char>((strlen(line)+1));
95  xMemCpy<char>(newstring,line,(strlen(line)+1));
96 
97  /*concatenate:*/
98  catstring=xNew<char>(strlen(string)+1+strlen(newstring)+1+1); //fit in a space " "
99  xMemCpy<char>(catstring,string,(strlen(string)+1));
100 
101  strcat(catstring," ");
102  strcat(catstring,newstring);
103  strings[numanalyses-1]=catstring;
104  xDelete<char>(newstring);
105  xDelete<char>(string);
106  }
107  }
108  }
109  }
110 
111  /*Ok, broadcast to other cpus: */
112  ISSM_MPI_Bcast(&numanalyses,1,ISSM_MPI_INT,0,IssmComm::GetComm());
113  if(my_rank!=0){
114  analyses = xNew<int>(numanalyses);
115  toolkits = xNew<char*>(numanalyses);
116  strings = xNew<char*>(numanalyses);
117  }
118  ISSM_MPI_Bcast(analyses,numanalyses,ISSM_MPI_INT,0,IssmComm::GetComm());
119  for(int i=0;i<numanalyses;i++){
120  char* toolkit=toolkits[i];
121  if(my_rank==0){
122  if(toolkit==NULL) _error_("PETSc options for analysis " << EnumToStringx(reCast<int>(analyses[i])) << " have been declared but were not found");
123  }
124  if(my_rank==0)toolkitlength=(strlen(toolkit)+1)*sizeof(char);
125  ISSM_MPI_Bcast(&toolkitlength,1,ISSM_MPI_INT,0,IssmComm::GetComm());
126  if(my_rank!=0)toolkit=xNew<char>(toolkitlength);
127  ISSM_MPI_Bcast(toolkit,toolkitlength,ISSM_MPI_CHAR,0,IssmComm::GetComm());
128  if(my_rank!=0)toolkits[i]=toolkit;
129 
130  char* string=strings[i];
131  if(my_rank==0){
132  if(string==NULL) _error_("PETSc options for analysis " << EnumToStringx(reCast<int>(analyses[i])) << " have been declared but were not found");
133  }
134  if(my_rank==0)stringlength=(strlen(string)+1)*sizeof(char);
135  ISSM_MPI_Bcast(&stringlength,1,ISSM_MPI_INT,0,IssmComm::GetComm());
136  if(my_rank!=0)string=xNew<char>(stringlength);
137  ISSM_MPI_Bcast(string,stringlength,ISSM_MPI_CHAR,0,IssmComm::GetComm());
138  if(my_rank!=0)strings[i]=string;
139  }
140 
141  /*Ok, out of strings and analyses and numanalyses, create parameters, and plug them into parameters container: */
142  parameters->AddObject(new StringArrayParam(ToolkitsOptionsStringsEnum,strings,numanalyses));
143  parameters->AddObject(new StringArrayParam(ToolkitsTypesEnum,toolkits,numanalyses));
144  parameters->AddObject(new IntVecParam(ToolkitsOptionsAnalysesEnum,analyses,numanalyses));
145 
146  /*Clean up and return*/
147  for(int i=0;i<numanalyses;i++) xDelete<char>(strings[i]);
148  for(int i=0;i<numanalyses;i++) xDelete<char>(toolkits[i]);
149  xDelete<char*>(strings);
150  xDelete<char*>(toolkits);
151  xDelete<int>(analyses);
152  return;
153 }
ToolkitsTypesEnum
@ ToolkitsTypesEnum
Definition: EnumDefinitions.h:441
IssmComm::GetComm
static ISSM_MPI_Comm GetComm(void)
Definition: IssmComm.cpp:30
IntVecParam
Definition: IntVecParam.h:20
Parameters::AddObject
void AddObject(Param *newparam)
Definition: Parameters.cpp:67
ISSM_MPI_INT
#define ISSM_MPI_INT
Definition: issmmpi.h:127
EnumToStringx
const char * EnumToStringx(int enum_in)
Definition: EnumToStringx.cpp:15
StringArrayParam
Definition: StringArrayParam.h:20
ISSM_MPI_Bcast
int ISSM_MPI_Bcast(void *buffer, int count, ISSM_MPI_Datatype datatype, int root, ISSM_MPI_Comm comm)
Definition: issmmpi.cpp:162
StringToEnumx
int StringToEnumx(const char *string_in, bool notfounderror=true)
Definition: StringToEnumx.cpp:14
ToolkitsOptionsAnalysesEnum
@ ToolkitsOptionsAnalysesEnum
Definition: EnumDefinitions.h:439
IssmComm::GetRank
static int GetRank(void)
Definition: IssmComm.cpp:34
ISSM_MPI_CHAR
#define ISSM_MPI_CHAR
Definition: issmmpi.h:124
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49
ToolkitsOptionsStringsEnum
@ ToolkitsOptionsStringsEnum
Definition: EnumDefinitions.h:440