26 #include <queso/GslVector.h>
27 #include <queso/Defines.h>
28 #include <gsl/gsl_sort_vector.h>
36 m_vec (gsl_vector_calloc(map.NumGlobalElements()))
60 m_vec (gsl_vector_calloc(map.NumGlobalElements()))
80 m_vec (gsl_vector_calloc(map.NumGlobalElements()))
90 for (
unsigned int i = 0; i <
m_vec->size; ++i) {
91 double alpha = (double) i / ((
double)
m_vec->size - 1.);
92 (*this)[i] = (1.-alpha)*d1 + alpha*d2;
103 m_vec (gsl_vector_calloc(v.sizeLocal()))
113 for (
unsigned int i = 0; i <
m_vec->size; ++i) {
114 double alpha = (double) i / ((
double)
m_vec->size - 1.);
115 (*this)[i] = (1. - alpha) * start + alpha * end;
126 m_vec (gsl_vector_calloc(v.sizeLocal()))
169 iRC = gsl_vector_scale(
m_vec,a);
189 for (
unsigned int i = 0; i < size1; ++i) {
190 (*this)[i] *= rhs[i];
203 for (
unsigned int i = 0; i < size1; ++i) {
204 (*this)[i] /= rhs[i];
232 return *gsl_vector_ptr(
m_vec,i);
238 return *gsl_vector_const_ptr(
m_vec,i);
246 iRC = gsl_vector_memcpy(this->
m_vec, src.
m_vec);
295 return std::sqrt(this->
norm2Sq());
304 for (
unsigned int i = 0; i < size; ++i) {
305 result += fabs((*
this)[i]);
318 for (
unsigned int i = 0; i < size; ++i) {
319 aux = fabs((*
this)[i]);
320 if (aux > result) result = aux;
331 for (
unsigned int i = 0; i < size; ++i) {
332 result += (*this)[i];
342 for (
unsigned int i = 0; i < size; ++i) {
352 for (
unsigned int i = 0; i < this->
sizeLocal(); ++i) {
362 for (
unsigned int i = 0; i < this->
sizeLocal(); ++i) {
371 for (
unsigned int i = 0; i < this->
sizeLocal(); ++i) {
384 double tmpSample = 0.;
385 for (
unsigned int i = 0; i < this->
sizeLocal(); ++i) {
391 <<
", alpha[i] = " << alpha[i]
392 <<
", beta[i] = " << beta[i]
393 <<
", sample = " << tmpSample
396 if ((alpha[i] == 1. ) &&
398 if (tmpSample == 1.) {
403 <<
", alpha[i] = " << alpha[i]
404 <<
", beta[i] = " << beta[i]
405 <<
", sample = " << tmpSample
409 std::cerr <<
"Hitting 'sample = 1' in GslVector::cwSetBeta()"
412 <<
", alpha[i] = " << alpha[i]
413 <<
", beta[i] = " << beta[i]
414 <<
", sample = " << tmpSample
418 }
while (tmpSample == 1.);
419 std::cerr <<
"Code was able to get 'sample != 1' in GslVector::cwSetBeta()"
422 <<
", alpha[i] = " << alpha[i]
423 <<
", beta[i] = " << beta[i]
424 <<
", sample = " << tmpSample
429 (*this)[i] = tmpSample;
441 for (
unsigned int i = 0; i < this->
sizeLocal(); ++i) {
454 for (
unsigned int i = 0; i < this->
sizeLocal(); ++i) {
465 for (
unsigned int i = 0; i < v1.
sizeLocal(); ++i) {
469 for (
unsigned int i = 0; i < v2.
sizeLocal(); ++i) {
479 unsigned int cummulativeSize = 0;
480 for (
unsigned int i = 0; i < vecs.size(); ++i) {
482 for (
unsigned int j = 0; j < vecs[i]->sizeLocal(); ++j) {
483 (*this)[cummulativeSize+j] = tmpVec[j];
500 for (
unsigned int i = 0; i < vec.
sizeLocal(); ++i) {
501 (*this)[initialPos+i] = vec[i];
514 for (
unsigned int i = 0; i < vec.
sizeLocal(); ++i) {
515 vec[i] = (*this)[initialPos+i];
525 for (
unsigned int i = 0; i < size; ++i) {
526 (*this)[i] = 1./(*this)[i];
536 for (
unsigned int i = 0; i < size; ++i) {
537 (*this)[i] = sqrt((*
this)[i]);
545 unsigned int firstPositionToStoreDiff,
546 double valueForRemainderPosition,
555 for (
unsigned int i = 0; i < (size-1); ++i) {
556 outputVec[firstPositionToStoreDiff+i] = (*this)[i+1]-(*this)[i];
558 if (firstPositionToStoreDiff == 0) {
559 outputVec[size-1] = valueForRemainderPosition;
562 outputVec[0] = valueForRemainderPosition;
580 for (
unsigned int i = 1; i < x1Vec.
sizeLocal(); ++i) {
584 for (
unsigned int id2 = 0; id2 < x2Vec.
sizeLocal(); ++id2) {
585 double x2 = x2Vec[id2];
586 unsigned int id1 = 0;
588 for (id1 = 0; id1 < x1Vec.
sizeLocal(); ++id1) {
589 if (x2 <= x1Vec[id1]) {
594 bool makeLinearModel =
false;
599 if (x2 == x1Vec[id1]) {
600 (*this)[id2] = y1Vec[id1];
602 else if (x2 < x1Vec[0]) {
604 makeLinearModel =
true;
610 else if (found1 ==
true) {
612 makeLinearModel =
true;
620 makeLinearModel =
true;
627 if (makeLinearModel) {
628 double rate = (yb-ya)/(xb-xa);
629 (*this)[id2] = ya + (x2-xa)*rate;
641 gsl_sort_vector(
m_vec);
650 if (bcastComm.
MyPID() < 0)
return;
656 double localNumNodes = 1.;
657 double totalNumNodes = 0.;
659 "GslVector::mpiBcast()",
660 "failed MPI.Allreduce() for numNodes");
664 double localVectorSize = this->
sizeLocal();
665 double sumOfVectorSizes = 0.;
667 "GslVector::mpiBcast()",
668 "failed MPI.Allreduce() for vectorSize");
670 if ( ((
unsigned int) sumOfVectorSizes) != ((
unsigned int)(totalNumNodes*localVectorSize)) ) {
671 std::cerr <<
"rank " << bcastComm.
MyPID()
672 <<
": sumOfVectorSizes = " << sumOfVectorSizes
673 <<
", totalNumNodes = " << totalNumNodes
674 <<
", localVectorSize = " << localVectorSize
678 queso_require_equal_to_msg(((
unsigned int) sumOfVectorSizes), ((
unsigned int)(totalNumNodes*localVectorSize)),
"inconsistent vectorSize");
681 std::vector<double> dataBuffer((
unsigned int) localVectorSize, 0.);
682 if (bcastComm.
MyPID() == srcRank) {
683 for (
unsigned int i = 0; i < dataBuffer.size(); ++i) {
684 dataBuffer[i] = (*this)[i];
689 "GslVector::mpiBcast()",
690 "failed MPI.Bcast()");
692 if (bcastComm.
MyPID() != srcRank) {
693 for (
unsigned int i = 0; i < dataBuffer.size(); ++i) {
694 (*this)[i] = dataBuffer[i];
705 if (opComm.
MyPID() < 0)
return;
710 for (
unsigned int i = 0; i < size; ++i) {
711 double srcValue = (*this)[i];
712 double resultValue = 0.;
714 "GslVector::mpiAllReduce()",
715 "failed MPI.Allreduce()");
716 resultVec[i] = resultValue;
726 if (opComm.
MyPID() < 0)
return;
733 for (
unsigned int i = 0; i < size; ++i) {
734 double auxDouble = (int) (*
this)[i];
735 std::vector<double> vecOfDoubles(opComm.
NumProc(),0.);
737 "GslVector::mpiAllQuantile()",
738 "failed MPI.Gather()");
740 std::sort(vecOfDoubles.begin(), vecOfDoubles.end());
742 double result = vecOfDoubles[(
unsigned int)( probability*((
double)(vecOfDoubles.size()-1)) )];
745 "GslVector::mpiAllQuantile()",
746 "failed MPI.Bcast()");
748 resultVec[i] = result;
765 std::ostream::fmtflags curr_fmt = os.flags();
770 unsigned int savedPrecision = os.precision();
774 for (
unsigned int i = 0; i < size; ++i) {
775 os << std::scientific << (*this)[i]
780 for (
unsigned int i = 0; i < size; ++i) {
781 os << std::scientific << (*this)[i]
786 os.precision(savedPrecision);
792 for (
unsigned int i = 0; i < size; ++i) {
793 os << std::dec << (*this)[i]
798 for (
unsigned int i = 0; i < size; ++i) {
799 os << std::dec << (*this)[i]
816 const std::string& varNamePrefix,
817 const std::string& fileName,
818 const std::string& fileType,
819 const std::set<unsigned int>& allowedSubEnvIds)
const
841 *filePtrSet.
ofsVar << *
this;
846 *filePtrSet.
ofsVar <<
"];\n";
856 const std::string& fileName,
857 const std::string& fileType,
858 const std::set<unsigned int>& allowedSubEnvIds)
872 unsigned int idOfMyFirstLine = 1;
873 unsigned int idOfMyLastLine = this->
sizeLocal();
874 unsigned int numParams = 1;
878 std::string tmpString;
881 *filePtrSet.
ifsVar >> tmpString;
885 *filePtrSet.
ifsVar >> tmpString;
890 *filePtrSet.
ifsVar >> tmpString;
892 unsigned int posInTmpString = 6;
895 char nPositionsString[tmpString.size()-posInTmpString+1];
896 unsigned int posInPositionsString = 0;
899 nPositionsString[posInPositionsString++] = tmpString[posInTmpString++];
900 }
while (tmpString[posInTmpString] !=
',');
901 nPositionsString[posInPositionsString] =
'\0';
905 char nParamsString[tmpString.size()-posInTmpString+1];
906 unsigned int posInParamsString = 0;
909 nParamsString[posInParamsString++] = tmpString[posInTmpString++];
910 }
while (tmpString[posInTmpString] !=
')');
911 nParamsString[posInParamsString] =
'\0';
914 unsigned int sizeOfVecInFile = (
unsigned int) strtod(nPositionsString,NULL);
915 unsigned int numParamsInFile = (
unsigned int) strtod(nParamsString, NULL);
919 <<
", sizeOfVecInFile = " << sizeOfVecInFile
920 <<
", numParamsInFile = " << numParamsInFile
921 <<
", this->sizeLocal() = " << this->
sizeLocal()
929 queso_require_equal_to_msg(numParamsInFile, numParams,
"number of parameters of vec in file is different than number of parameters in this vec object");
932 unsigned int maxCharsPerLine = 64*numParams;
934 unsigned int lineId = 0;
935 while (lineId < idOfMyFirstLine) {
936 filePtrSet.
ifsVar->ignore(maxCharsPerLine,
'\n');
942 <<
": beginning to read input actual data"
949 *filePtrSet.
ifsVar >> tmpString;
953 *filePtrSet.
ifsVar >> tmpString;
958 std::streampos tmpPos = filePtrSet.
ifsVar->tellg();
959 filePtrSet.
ifsVar->seekg(tmpPos+(std::streampos)2);
963 <<
": beginning to read lines with numbers only"
964 <<
", lineId = " << lineId
965 <<
", idOfMyFirstLine = " << idOfMyFirstLine
966 <<
", idOfMyLastLine = " << idOfMyLastLine
970 while (lineId <= idOfMyLastLine) {
971 *filePtrSet.
ifsVar >> (*this)[lineId - idOfMyFirstLine];
995 while ((i < size) && (result ==
false)) {
996 result = ( (*this)[i] < rhs[i] );
1008 bool result =
false;
1011 while ((i < size) && (result ==
false)) {
1012 result = ( (*this)[i] > rhs[i] );
1024 bool result =
false;
1027 while ((i < size) && (result ==
false)) {
1028 result = ( (*this)[i] <= rhs[i] );
1040 bool result =
false;
1043 while ((i < size) && (result ==
false)) {
1044 result = ( (*this)[i] >= rhs[i] );
1054 return gsl_vector_max(
m_vec );
1060 return gsl_vector_min(
m_vec );
1066 return gsl_vector_max_index(
m_vec );
1072 return gsl_vector_min_index(
m_vec );
1098 unsigned int size = abs_of_this_vec.
sizeLocal();
1100 for(
unsigned int i = 0; i < size; ++i )
1102 abs_of_this_vec[i] = std::fabs( (*
this)[i] );
1105 return abs_of_this_vec;
1157 for (
unsigned int i = 0; i < size1; ++i) {
1158 result += x[i]*y[i];
1189 for (
unsigned int i = 0; i < size1; ++i) {
1190 if (lhs[i] != rhs[i]) {
unsigned int displayVerbosity() const
GslMatrix operator*(double a, const GslMatrix &mat)
Class for vector operations (virtual).
double sumOfComponents() const
Returns the sum of the components of the vector.
int getMaxValueIndex() const
This function returns the index of the maximum value in the vector this.
int subRank() const
Access function for sub-rank.
bool atLeastOneComponentSmallerOrEqualThan(const GslVector &rhs) const
This function returns true if at least one component of this is smaller than or equal to the respecti...
GslVector & operator+=(const GslVector &rhs)
Stores in this the coordinate-wise addition of this and rhs.
void cwSetGaussian(double mean, double stdDev)
This function sets component-wise Gaussian random variates, with mean mean and standard deviation std...
GslVector abs() const
This function returns absolute value of elements in this.
void cwExtract(unsigned int initialPos, GslVector &vec) const
This function sets the values of this starting at position initialPos ans saves them in vector vec...
double getMaxValue() const
Returns the maximum value in the vector this.
bool atLeastOneComponentSmallerThan(const GslVector &rhs) const
This function returns true if at least one component of this is smaller than the respective component...
const BaseEnvironment & m_env
Environment variable.
virtual void base_copy(const Vector &src)
Copies base data from vector src to this vector.
int MyPID() const
Return my process ID.
double getMinValue() const
Returns minimum value in the vector this.
bool atLeastOneComponentBiggerThan(const GslVector &rhs) const
This function returns true if at least one component of this is bigger than the respective component ...
bool atLeastOneComponentBiggerOrEqualThan(const GslVector &rhs) const
This function returns true if at least one component of this is bigger than or equal to the respectiv...
void Barrier() const
Pause every process in *this communicator until all the processes reach this point.
int NumProc() const
Returns total number of processes.
void getMinValueAndIndex(double &value, int &index)
This function returns minimum value in the vector this and its the index.
void cwSqrt()
This function returns component-wise the square-root of this.
unsigned int sizeGlobal() const
Returns the global length of this vector.
std::ofstream * ofsVar
Provides a stream interface to write data to files.
int fullRank() const
Returns the process full rank.
double & operator[](unsigned int i)
Element access method (non-const).
Struct for handling data input and output from files.
bool m_printHorizontally
Flag for either or not print this matrix horizontally.
#define queso_require_less_equal_msg(expr1, expr2, msg)
GslVector & operator*=(double a)
Stores in this the coordinate-wise multiplication of this and a.
int NumMyElements() const
Returns the number of elements owned by the calling processor.
unsigned int sizeLocal() const
Returns the length of this vector.
void cwSetConcatenated(const GslVector &v1, const GslVector &v2)
This function concatenates GslVector v1 and GslVector v2 into this.
void cwSetInverseGamma(const GslVector &alpha, const GslVector &beta)
This function returns a random variate from the inverse gamma distribution with vector parameters alp...
std::ostream & operator<<(std::ostream &os, const BaseEnvironment &obj)
void getMaxValueAndIndex(double &value, int &index)
This function returns maximum value in the vector this and its the index.
GslVector operator/(double a, const GslVector &x)
GslMatrix operator+(const GslMatrix &m1, const GslMatrix &m2)
virtual double gaussianSample(double stdDev) const =0
Samples a value from a Gaussian distribution with standard deviation given by stdDev.
gsl_vector * m_vec
GSL vector.
const std::string & subIdString() const
Access to the attribute m_subIdString; which stores the string for the sub-environment, and it will be used, for instance, to create the output files for each sub-environment.
The QUESO MPI Communicator Class.
void closeFile(FilePtrSetStruct &filePtrSet, const std::string &fileType) const
Closes the file.
#define queso_require_equal_to_msg(expr1, expr2, msg)
void cwSetBeta(const GslVector &alpha, const GslVector &beta)
This function returns a random variate from the beta distribution, with vector parameters alpha and b...
double scalarProduct(const GslVector &x, const GslVector &y)
void matlabDiff(unsigned int firstPositionToStoreDiff, double valueForRemainderPosition, GslVector &outputVec) const
double norm2() const
Returns the 2-norm (Euclidean norm) of the vector.
void cwInvert()
This function inverts component-wise the element values of this.
#define queso_require_msg(asserted, msg)
bool getPrintHorizontally() const
Checks if vector is printed horizontally.
void mpiBcast(int srcRank, const MpiComm &bcastComm)
Class for vector operations using GSL library.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
GslVector & operator=(const GslVector &rhs)
Copies values from vector rhs to this.
#define queso_require_less_msg(expr1, expr2, msg)
virtual double gammaSample(double a, double b) const =0
Samples a value from a Gamma distribution.
void cwSet(double value)
Component-wise sets all values to this with value.
void setPrintHorizontally(bool value) const
Determines whether vector should be printed horizontally.
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.
void mpiAllReduce(RawType_MPI_Op mpiOperation, const MpiComm &opComm, GslVector &resultVec) const
void cwSetUniform(const GslVector &aVec, const GslVector &bVec)
This function sets component-wise a number uniformly distributed in the range of elements of [aVec...
bool m_printScientific
Flag for either or not print this matrix in scientific notation.
GslVector & operator-=(const GslVector &rhs)
Stores in this the coordinate-wise subtraction of this by rhs.
bool getPrintScientific() const
Checks if the vector should be printed in Scientific Notation.
int getMinValueIndex() const
This function returns the index of the minimum value in the vector this.
void sort()
This function sorts the elements of the vector this in ascending numerical order. ...
unsigned int numOfProcsForStorage() const
void setPrintScientific(bool value) const
Determines whether vector should be printed in Scientific Notation.
void subReadContents(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds)
GslVector & operator/=(double a)
Stores in this the coordinate-wise division of this by a.
GslMatrix operator-(const GslMatrix &m1, const GslMatrix &m2)
GslVector()
Default Constructor.
const RngBase * rngObject() const
Access to the RNG object.
bool openOutputFile(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds, bool writeOver, FilePtrSetStruct &filePtrSet) const
Opens an output file for each sub-environment that was chosen to send data to the file...
double norm1() const
Returns the 1-norm of the vector.
gsl_vector * data() const
void copy(const GslVector &src)
This function copies the elements of the vector src into this.
A class for partitioning vectors and matrices.
#define queso_require_greater_equal_msg(expr1, expr2, msg)
std::ifstream * ifsVar
Provides a stream interface to read data from files.
void mpiAllQuantile(double probability, const MpiComm &opComm, GslVector &resultVec) const
void matlabLinearInterpExtrap(const GslVector &x1Vec, const GslVector &y1Vec, const GslVector &x2Vec)
void print(std::ostream &os) const
Print method. Defines the behavior of the std::ostream << operator inherited from the Object class...
#define queso_require_greater_msg(expr1, expr2, msg)
double normInf() const
Returns the infinity-norm (maximum norm) of the vector.
int NumGlobalElements() const
Returns the total number of elements across all processors.
void Gather(void *sendbuf, int sendcnt, RawType_MPI_Datatype sendtype, void *recvbuf, int recvcount, RawType_MPI_Datatype recvtype, int root, const char *whereMsg, const char *whatMsg) const
Gather values from each process to collect on all processes.
#define RawValue_MPI_DOUBLE
void cwSetGamma(const GslVector &a, const GslVector &b)
This function returns a random variate from the gamma distribution with vector parameters a and b...
double norm2Sq() const
Returns the 2-norm squared of this vector.
bool operator==(const GslVector &lhs, const GslVector &rhs)
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.
const Map & m_map
Mapping variable.
void subWriteContents(const std::string &varNamePrefix, const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds) const
bool openInputFile(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds, FilePtrSetStruct &filePtrSet) const
Opens an input file.
virtual double betaSample(double alpha, double beta) const =0
Samples a value from a Beta distribution.