File indexing completed on 2025-08-06 08:18:18
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 #include "Mille.h"
0033
0034 #include <fstream>
0035 #include <iostream>
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 Mille::Mille(const char *outFileName, bool asBinary, bool writeZero)
0046 : myOutFile(outFileName, (asBinary ? (std::ios::binary | std::ios::out) : std::ios::out))
0047 , myAsBinary(asBinary)
0048 , myWriteZero(writeZero)
0049 , myBufferPos(-1)
0050 , myHasSpecial(false)
0051 {
0052
0053
0054 myBufferInt[0] = 0;
0055 myBufferFloat[0] = 0.;
0056
0057 if (!myOutFile.is_open())
0058 {
0059 std::cerr << "Mille::Mille: Could not open " << outFileName
0060 << " as output file." << std::endl;
0061 }
0062 }
0063
0064
0065
0066 Mille::~Mille()
0067 {
0068 myOutFile.close();
0069 }
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082 void Mille::mille(int NLC, const float *derLc,
0083 int NGL, const float *derGl, const int *label,
0084 float rMeas, float sigma)
0085 {
0086 if (sigma <= 0.)
0087 {
0088 return;
0089 }
0090 if (myBufferPos == -1)
0091 {
0092 this->newSet();
0093 }
0094 if (!this->checkBufferSize(NLC, NGL))
0095 {
0096 return;
0097 }
0098
0099
0100 ++myBufferPos;
0101 myBufferFloat[myBufferPos] = rMeas;
0102 myBufferInt[myBufferPos] = 0;
0103
0104
0105 for (int i = 0; i < NLC; ++i)
0106 {
0107 if (derLc[i] || myWriteZero)
0108 {
0109 ++myBufferPos;
0110 myBufferFloat[myBufferPos] = derLc[i];
0111 myBufferInt[myBufferPos] = i + 1;
0112 }
0113 }
0114
0115
0116 ++myBufferPos;
0117 myBufferFloat[myBufferPos] = sigma;
0118 myBufferInt[myBufferPos] = 0;
0119
0120
0121 for (int i = 0; i < NGL; ++i)
0122 {
0123 if (derGl[i] || myWriteZero)
0124 {
0125 if ((label[i] > 0 || myWriteZero) && label[i] <= myMaxLabel)
0126 {
0127 ++myBufferPos;
0128 myBufferFloat[myBufferPos] = derGl[i];
0129 myBufferInt[myBufferPos] = label[i];
0130 }
0131 else
0132 {
0133 std::cerr << "Mille::mille: Invalid label " << label[i]
0134 << " <= 0 or > " << myMaxLabel << std::endl;
0135 }
0136 }
0137 }
0138 }
0139
0140
0141
0142
0143
0144
0145
0146
0147 void Mille::special(int nSpecial, const float *floatings, const int *integers)
0148 {
0149 if (nSpecial == 0)
0150 {
0151 return;
0152 }
0153 if (myBufferPos == -1)
0154 {
0155 this->newSet();
0156 }
0157 if (myHasSpecial)
0158 {
0159 std::cerr << "Mille::special: Special values already stored for this record."
0160 << std::endl;
0161 return;
0162 }
0163 if (!this->checkBufferSize(nSpecial, 0))
0164 {
0165 return;
0166 }
0167 myHasSpecial = true;
0168
0169
0170
0171
0172
0173
0174
0175 ++myBufferPos;
0176 myBufferFloat[myBufferPos] = 0.;
0177 myBufferInt[myBufferPos] = 0;
0178
0179 ++myBufferPos;
0180 myBufferFloat[myBufferPos] = -nSpecial;
0181 myBufferInt[myBufferPos] = 0;
0182
0183 for (int i = 0; i < nSpecial; ++i)
0184 {
0185 ++myBufferPos;
0186 myBufferFloat[myBufferPos] = floatings[i];
0187 myBufferInt[myBufferPos] = integers[i];
0188 }
0189 }
0190
0191
0192
0193 void Mille::kill()
0194 {
0195 myBufferPos = -1;
0196 }
0197
0198
0199
0200 void Mille::end()
0201 {
0202
0203
0204 if (myBufferPos > 0)
0205 {
0206 const int numWordsToWrite = (myBufferPos + 1) * 2;
0207
0208 if (myAsBinary)
0209 {
0210 myOutFile.write(reinterpret_cast<const char *>(&numWordsToWrite),
0211 sizeof(numWordsToWrite));
0212 myOutFile.write(reinterpret_cast<char *>(myBufferFloat),
0213 (myBufferPos + 1) * sizeof(myBufferFloat[0]));
0214 myOutFile.write(reinterpret_cast<char *>(myBufferInt),
0215 (myBufferPos + 1) * sizeof(myBufferInt[0]));
0216 }
0217 else
0218 {
0219 myOutFile << numWordsToWrite << "\n";
0220 for (int i = 0; i < myBufferPos + 1; ++i)
0221 {
0222 myOutFile << myBufferFloat[i] << " ";
0223 }
0224 myOutFile << "\n";
0225
0226 for (int i = 0; i < myBufferPos + 1; ++i)
0227 {
0228 myOutFile << myBufferInt[i] << " ";
0229 }
0230 myOutFile << "\n";
0231 }
0232 }
0233 myBufferPos = -1;
0234
0235
0236 }
0237
0238
0239
0240 void Mille::newSet()
0241 {
0242 myBufferPos = 0;
0243 myHasSpecial = false;
0244 myBufferFloat[0] = 0.0;
0245 myBufferInt[0] = 0;
0246 }
0247
0248
0249
0250
0251
0252
0253
0254
0255 bool Mille::checkBufferSize(int nLocal, int nGlobal)
0256 {
0257 if (myBufferPos + nLocal + nGlobal + 2 >= myBufferSize)
0258 {
0259 ++(myBufferInt[0]);
0260 std::cerr << "Mille::checkBufferSize: Buffer too short ("
0261 << myBufferSize << "),"
0262 << "\n need space for nLocal (" << nLocal << ")"
0263 << "/nGlobal (" << nGlobal << ") local/global derivatives, "
0264 << myBufferPos + 1 << " already stored!"
0265 << std::endl;
0266 return false;
0267 }
0268 else
0269 {
0270 return true;
0271 }
0272 }