26 #include <queso/Defines.h>
27 #include <queso/Miscellaneous.h>
28 #include <queso/GslVector.h>
29 #include <queso/GslMatrix.h>
41 const std::string& inputString,
42 std::vector<double>& outputDoubles)
47 outputDoubles.clear();
48 bool aDoubleIsBeingRead =
false;
49 std::string::size_type positionOfFirstChar = 0;
50 std::string::size_type numberOfChars = 0;
51 for (std::string::size_type i = 0; i < inputString.size(); ++i) {
53 if (inputString[i] ==
' ') {
54 if (aDoubleIsBeingRead ==
true) {
56 char tmpVar[numberOfChars+1];
57 for (std::string::size_type j = 0; j < numberOfChars; ++j) {
58 tmpVar[j] = inputString[positionOfFirstChar+j];
60 tmpVar[numberOfChars] =
'\0';
61 outputDoubles.push_back(strtod(tmpVar,NULL));
64 aDoubleIsBeingRead =
false;
65 positionOfFirstChar = 0;
70 if (aDoubleIsBeingRead ==
false) {
71 aDoubleIsBeingRead =
true;
72 positionOfFirstChar = i;
77 if (aDoubleIsBeingRead ==
true) {
79 char tmpVar[numberOfChars+1];
80 for (std::string::size_type j = 0; j < numberOfChars; ++j) {
81 tmpVar[j] = inputString[positionOfFirstChar+j];
83 tmpVar[numberOfChars] =
'\0';
84 outputDoubles.push_back(strtod(tmpVar,NULL));
86 std::vector<double>(outputDoubles).swap(outputDoubles);
93 const std::string& inputString,
94 std::vector<std::string>& outputWords)
100 bool aWordIsBeingRead =
false;
101 std::string::size_type positionOfFirstChar = 0;
102 std::string::size_type numberOfChars = 0;
103 for (std::string::size_type i = 0; i < inputString.size(); ++i) {
105 if (inputString[i] ==
' ') {
106 if (aWordIsBeingRead ==
true) {
108 char tmpVar[numberOfChars+1];
109 for (std::string::size_type j = 0; j < numberOfChars; ++j) {
110 tmpVar[j] = inputString[positionOfFirstChar+j];
112 tmpVar[numberOfChars] =
'\0';
113 outputWords.push_back(tmpVar);
116 aWordIsBeingRead =
false;
117 positionOfFirstChar = 0;
122 if (aWordIsBeingRead ==
false) {
123 aWordIsBeingRead =
true;
124 positionOfFirstChar = i;
129 if (aWordIsBeingRead ==
true) {
131 char tmpVar[numberOfChars+1];
132 for (std::string::size_type j = 0; j < numberOfChars; ++j) {
133 tmpVar[j] = inputString[positionOfFirstChar+j];
135 tmpVar[numberOfChars] =
'\0';
136 outputWords.push_back(tmpVar);
138 std::vector<std::string>(outputWords).swap(outputWords);
162 std::string& termString,
168 if ((ifs.rdstate() & std::ifstream::failbit)) {
171 else if (termValue) {
172 if (termString == std::string(
"inf")) {
173 *termValue = INFINITY;
175 else if (termString == std::string(
"-inf")) {
176 *termValue = -INFINITY;
178 else if (termString == std::string(
"nan")) {
179 *termValue = nan(
"");
182 *termValue = strtod(termString.c_str(),NULL);
193 std::string& termString,
195 bool& endOfLineAchieved)
198 endOfLineAchieved =
false;
203 if ((ifs.rdstate() & std::ifstream::failbit)) {
210 unsigned int pos = 0;
213 while ((pos < 512) && (c !=
'\n') && (c !=
'\0') && (c !=
' ')) {
215 if ((ifs.rdstate() & std::ifstream::failbit)) {
224 if (c ==
'\n') endOfLineAchieved =
true;
229 if (termString == std::string(
"inf")) {
230 *termValue = INFINITY;
232 else if (termString == std::string(
"-inf")) {
233 *termValue = -INFINITY;
235 else if (termString == std::string(
"nan")) {
236 *termValue = nan(
"");
239 *termValue = strtod(termString.c_str(),NULL);
259 double c = 1./std::sqrt(9.*d);
270 double compValue = 1.-0.0331*std::pow(x,4.);
271 if (u < compValue)
break;
272 compValue = 0.5*std::pow(x,2.)+d*(1.-w+log(w));
273 if (log(u) < compValue)
break;
286 struct timeval timevalNow;
288 gettimeofday(&timevalNow, NULL);
290 result = (double) (timevalNow.tv_sec - timeval0->tv_sec );
292 result += (double) (timevalNow.tv_usec - timeval0->tv_usec);
300 double angle = 2.*M_PI*((double) j)/((double) N);
301 double result = 0.53836 - 0.46164*cos(angle);
308 double sigma2 = sigma*sigma;
311 return (1./std::sqrt(2*M_PI*sigma2))*std::exp(-.5*diff*diff/sigma2);
319 std::cout <<
"Passing in MiscUintDebugMessage(), value = " << value <<
", message = " << message << std::endl;
329 std::cout <<
"Passing in MiscIntDebugMessage(), value = " << value <<
", message = " << message << std::endl;
339 std::cout <<
"Passing in MiscDoubleDebugMessage(), value = " << value <<
", message = " << message << std::endl;
368 return(grvy_check_file_path(pathname));
371 const int MAX_DEPTH = 50;
381 pathlocal = strdup(pathname);
382 dirstring = strdup(pathname);
383 parents = dirname(pathlocal);
385 if(strcmp(parents,
".") == 0)
394 bool abs_path =
false;
396 std::string leading_char(
"");
397 std::string path_to_check;
399 if(strncmp(parents,
"/",1) == 0)
407 if( (token = strtok(parents,
"/")) != NULL )
409 path_to_check += leading_char + token;
421 sprintf(dirstring,
"/%s",token);
423 sprintf(dirstring,
"%s",token);
425 while ( (token = strtok(0,
"/")) && (depth < MAX_DEPTH) )
427 dirstring = strcat(dirstring,
"/");
438 if(depth >= MAX_DEPTH )
440 std::cerr << __func__ <<
": error - Max directory depth exceeded, limit = " << MAX_DEPTH << std::endl;
460 if(stat(dirname,&st) != 0)
462 if( mkdir(dirname,0700) != 0 )
464 std::cerr << __func__ <<
": error - unable to create directory " << dirname << std::endl;
468 else if (!S_ISDIR(st.st_mode))
470 std::cerr << __func__ <<
": error - entry exists, but is not a directory " << dirname << std::endl;
480 double acceptableTreshold,
482 const char* whereString)
485 if (comm.
MyPID() < 0)
return true;
487 double localValue = (double) inputValue;
488 double sumValue = 0.;
491 "failed MPI on 'sumValue' inside MiscCheckForSameValueInAllNodes()");
493 double totalNumNodes = (double) comm.
NumProc();
494 double testValue = fabs(1. - localValue/(sumValue/totalNumNodes));
495 unsigned int boolSum = 0;
497 unsigned int boolResult = 0;
498 if (testValue > acceptableTreshold) boolResult = 1;
501 "failed MPI on 'boolSum' inside MiscCheckForSameValueInAllNodes()");
505 for (
int i = 0; i < comm.
NumProc(); ++i) {
506 if (i == comm.
MyPID()) {
507 std::cerr <<
"WARNING, "
509 <<
", inside MiscCheckForSameValueInAllNodes()"
510 <<
", rank (in this communicator) = " << i
511 <<
": boolSum = " << boolSum
512 <<
", localValue = " << localValue
513 <<
", sumValue = " << sumValue
514 <<
", totalNumNodes = " << totalNumNodes
515 <<
", avgValue = " << (sumValue/totalNumNodes)
516 <<
", relativeTest = " << testValue
525 "failed MPI on 'boolSum' inside MiscCheckForSameValueInAllNodes()");
526 inputValue = localValue;
529 queso_require_less_equal_msg(testValue, acceptableTreshold,
"not all nodes have the same value inside MiscCheckForSameValueInAllNodes()");
532 return (boolSum == 0);
539 std::vector<V*>& positions)
542 switch (positions.size()) {
548 positions[0] =
new V((1. - factor) * minValues + factor * maxValues);
552 for (
unsigned int i = 0; i < positions.size(); ++i) {
553 factor = ((double) i)/(((double) positions.size()) - 1.);
554 positions[i] =
new V((1. - factor) * minValues + factor * maxValues);
562 template <
class V1,
class V2>
571 "only 1 processor (per sub environment) should be necessary for the storage of a parameter vector");
573 "only 1 processor (per sub environment) should be necessary for the storage of a parameter vector");
579 if ((vec1.numOfProcsForStorage() == 1) &&
580 (vec2.numOfProcsForStorage() == 1)) {
583 else if ((vec1.numOfProcsForStorage() == numProcsPerSubEnvironment) &&
584 (vec2.numOfProcsForStorage() == numProcsPerSubEnvironment)) {
588 queso_error_msg(
"number of processors required for a vector storage should be equal to either 1 or to the number of processors in the sub environment");
592 queso_error_msg(
"number of processors per sub environment is less than 1!");
601 template bool QUESO::MiscCheckForSameValueInAllNodes<bool>(
bool&, double,
QUESO::MpiComm const&,
char const*);
602 template bool QUESO::MiscCheckForSameValueInAllNodes<double>(
double&, double,
QUESO::MpiComm const&,
char const*);
const MpiComm & subComm() const
Access function for MpiComm sub communicator.
double MiscGaussianDensity(double x, double mu, double sigma)
int MiscIntDebugMessage(int value, const char *message)
int subRank() const
Access function for sub-rank.
int MyPID() const
Return my process ID.
int GRVY_CheckDir(const char *dirname)
const int UQ_FAILED_READING_FILE_RC
void Barrier() const
Pause every process in *this communicator until all the processes reach this point.
double MiscHammingWindow(unsigned int N, unsigned int j)
int NumProc() const
Returns total number of processes.
#define queso_require_not_equal_to_msg(expr1, expr2, msg)
void MiscComputePositionsBetweenMinMax(V minValues, V maxValues, std::vector< V * > &positions)
#define queso_require_less_equal_msg(expr1, expr2, msg)
#define queso_error_msg(msg)
const MpiComm & fullComm() const
Access function for MpiComm full communicator.
double MiscDoubleDebugMessage(double value, const char *message)
void MiscCheckTheParallelEnvironment(const V1 &vec1, const V2 &vec2)
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
virtual double gaussianSample(double stdDev) const =0
Samples a value from a Gaussian distribution with standard deviation given by stdDev.
The QUESO MPI Communicator Class.
double MiscGammar(double a, double b, const RngBase *rngObject)
#define queso_require_equal_to_msg(expr1, expr2, msg)
double MiscGetEllapsedSeconds(struct timeval *timeval0)
Class for random number generation (base class for either GSL or Boost RNG).
Class for vector operations using GSL library.
unsigned int MiscUintDebugMessage(unsigned int value, const char *message)
int CheckFilePath(const char *path)
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
void Bcast(void *buffer, int count, RawType_MPI_Datatype datatype, int root, const char *whereMsg, const char *whatMsg) const
Broadcast values from the root process to the slave processes.
int MiscReadCharsAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue, bool &endOfLineAchieved)
void MiscReadDoublesFromString(const std::string &inputString, std::vector< double > &outputDoubles)
int MiscReadStringAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue)
void MiscReadWordsFromString(const std::string &inputString, std::vector< std::string > &outputWords)
bool MiscCheckForSameValueInAllNodes(T &inputValue, double acceptableTreshold, const MpiComm &comm, const char *whereString)
#define RawValue_MPI_DOUBLE
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
virtual double uniformSample() const =0
Samples a value from a uniform distribution.
#define RawValue_MPI_UNSIGNED