00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "qcggs.h"
00024
00025 QCGGS::QCGGS()
00026 {
00027 twoCompBEC = false;
00028 }
00029
00030 QCGGS::~QCGGS()
00031 {
00032
00033 }
00034
00035 void QCGGS::setNoOfImages(int number)
00036 {
00037 noOfImages = number;
00038 GSPhases.resizeAndPreserve(noOfImages);
00039 GSPhases2.resizeAndPreserve(noOfImages);
00040 Images.resizeAndPreserve(noOfImages);
00041 Images2.resizeAndPreserve(noOfImages);
00042 }
00043
00044 void QCGGS::setSystem(QCQuantumSystem<complexType,2> &system)
00045 {
00046 ptrSystem = &system;
00047 ptrField = system.accessData();
00048 int rows = system.getNoOfRows(), cols = system.getNoOfColumns();
00049 for(int j = 0; j < noOfImages; j ++)
00050 GSPhases(j).setSize(rows,cols);
00051 for(int j = 0; j < noOfImages; j ++)
00052 Images(j).setSize(rows,cols);
00053 twoCompBEC = false;
00054 }
00055
00056 void QCGGS::setSystem(QC2ComponentBEC<2> &system)
00057 {
00058 ptrSystem = &system;
00059 ptrField = system.accessComp1();
00060 ptrField2 = system.accessComp2();
00061 int rows = system.getNoOfRows(), cols = system.getNoOfColumns();
00062
00063 for(int j = 0; j < noOfImages; j ++)
00064 {
00065 GSPhases(j).setSize(rows,cols);
00066 GSPhases2(j).setSize(rows,cols);
00067 }
00068 for(int j = 0; j < noOfImages; j ++)
00069 {
00070 Images(j).setSize(rows,cols);
00071 Images2(j).setSize(rows,cols);
00072 }
00073 twoCompBEC = true;
00074 }
00075
00076 void QCGGS::setGuess(parameterType mag, parameterType phase)
00077 {
00078 if(ptrField != NULL)
00079 {
00080 if(!twoCompBEC)
00081 {
00082 for(int k = 0; k < ptrField->rows(); k ++)
00083 for(int j = 0; j < ptrField->cols(); j ++)
00084 (*ptrField)(j,k) = mag*exp(i*phase);
00085 }
00086 else
00087 {
00088 for(int k = 0; k < ptrField->rows(); k ++)
00089 for(int j = 0; j < ptrField->cols(); j ++)
00090 (*ptrField)(j,k) = (*ptrField2)(j,k) = mag*exp(i*phase);
00091 }
00092 }
00093 else
00094 exit(0);
00095 }
00096
00097 void QCGGS::loadImages(string extension)
00098 {
00099 string filename;
00100 char digit;
00101
00102 for(int j = 0; j < noOfImages; j ++)
00103 {
00104 digit = static_cast<char> (j + 48);
00105 if(!twoCompBEC)
00106 {
00107 filename = filePrefix + digit + extension;
00108 Images(j).readFromFile(filename.c_str());
00109 }
00110 else
00111 {
00112 filename = filePrefix + '1' + digit + extension;
00113 Images(j).readFromFile(filename.c_str());
00114 filename = filePrefix + '2' + digit + extension;
00115 Images2(j).readFromFile(filename.c_str());
00116 }
00117 }
00118 }
00119
00120 void QCGGS::writeResults(string extension)
00121 {
00122 QCPhase Phase;
00123 string filename;
00124 char digit;
00125
00126 for(int j = 0; j < noOfImages; j ++)
00127 {
00128 digit = static_cast<char> (j + 48);
00129 if(!twoCompBEC)
00130 {
00131 filename = filePrefix + digit + extension;
00132 Phase.outputPhaseFunction(GSPhases(j),filename.c_str());
00133 }
00134 else
00135 {
00136 filename = filePrefix + '1' + digit + extension;
00137 Phase.outputPhaseFunction(GSPhases(j),filename.c_str());
00138 filename = filePrefix + '2' + digit + extension;
00139 Phase.outputPhaseFunction(GSPhases2(j),filename.c_str());
00140 }
00141 }
00142 }
00143
00144 void QCGGS::writeOriginals(string extension)
00145 {
00146 QCPhase Phase;
00147 string filename;
00148 char digit;
00149
00150 for(int j = 0; j < noOfImages; j ++)
00151 {
00152 digit = static_cast<char> (j + 48);
00153 if(!twoCompBEC)
00154 {
00155 filename = filePrefix + digit + extension;
00156 Phase.outputPhaseFunction(Images(j),filename.c_str());
00157 }
00158 else
00159 {
00160 filename = filePrefix + '1' + digit + extension;
00161 Phase.outputPhaseFunction(Images(j),filename.c_str());
00162 filename = filePrefix + '2' + digit + extension;
00163 Phase.outputPhaseFunction(Images2(j),filename.c_str());
00164 }
00165 }
00166 }
00167
00168 void QCGGS::copyField(QCComplexField<2>& dest, QCComplexField<2>& source)
00169 {
00170 for(int k = 0; k < source.noOfColumns(); k ++)
00171 for(int j = 0; j < source.noOfRows(); j ++)
00172 dest.assignElement(j,k,source.retrieveElement(j,k));
00173 }
00174
00175 void QCGGS::copyField(QCComplexField<2>& dest, Array<complexType,2> &source)
00176 {
00177 for(int k = 0; k < source.cols(); k ++)
00178 for(int j = 0; j < source.rows(); j ++)
00179 dest.assignElement(j,k,source(j,k));
00180 }
00181
00182 void QCGGS::updateMagnitude(QCComplexField<2>& image, QCComplexField<2> &result)
00183 {
00184 double currentPhase = 0.0;
00185 int buffer = 0;
00186
00187 for(int k = buffer; k < result.noOfColumns()-buffer; k ++)
00188 for(int j = buffer; j < result.noOfRows()-buffer; j ++)
00189 {
00190 currentPhase = atan2(result.retrieveElement(j,k).imag(),result.retrieveElement(j,k).real());
00191 result.assignElement(j,k,sqrt(norm(image.retrieveElement(j,k)))*exp(i*currentPhase));
00192 }
00193 }
00194
00195 void QCGGS::updateMagnitude(QCComplexField<2>& image, Array<complexType,2>& result)
00196 {
00197 double currentPhase = 0.0;
00198 int buffer = 0;
00199
00200 for(int k = buffer; k < result.cols()-buffer; k ++)
00201 for(int j = buffer; j < result.rows()-buffer; j ++)
00202 {
00203 currentPhase = atan2(result(j,k).imag(),result(j,k).real());
00204 result(j,k) = sqrt(norm(image.retrieveElement(j,k)))*exp(i*currentPhase);
00205 }
00206 }
00207
00208 parameterType QCGGS::RMSError(QCComplexField<2>& original, QCComplexField<2> &result)
00209 {
00210 parameterType sigma = 0.0, totalNorm = 0.0, tmp = 0.0;
00211 int frame = result.noOfRows()/10;
00212
00213 for(int k = frame; k < original.noOfColumns()-frame; k ++)
00214 for(int j = frame; j < original.noOfRows()-frame; j ++)
00215 {
00216 tmp = sqrt(norm(original.retrieveElement(j,k))) - sqrt(norm(result.retrieveElement(j,k)));
00217 sigma = sigma + tmp*tmp;
00218 totalNorm = totalNorm + norm(original.retrieveElement(j,k));
00219 }
00220
00221 sigma = sqrt(sigma/totalNorm);
00222 return sigma;
00223 }
00224
00225 parameterType QCGGS::RMSError(QCComplexField<2>& original, Array<complexType,2> &result)
00226 {
00227 parameterType sigma = 0.0, totalNorm = 0.0, tmp = 0.0;
00228 int frame = result.rows()/10;
00229
00230 for(int k = frame; k < original.noOfColumns()-frame; k ++)
00231 for(int j = frame; j < original.noOfRows()-frame; j ++)
00232 {
00233 tmp = sqrt(norm(original.retrieveElement(j,k))) - sqrt(norm(result(j,k)));
00234 sigma = sigma + tmp*tmp;
00235 totalNorm = totalNorm + norm(original.retrieveElement(j,k));
00236 }
00237
00238 sigma = sqrt(sigma/totalNorm);
00239 return sigma;
00240 }
00241
00242 parameterType QCGGS::iterate(QCBEC<2> &bec)
00243 {
00244
00245 for(int k = 0; k < noOfImages-1; k ++)
00246 {
00247 updateMagnitude(Images(k),*ptrField);
00248 copyField(GSPhases(k),*ptrField);
00249 for(int j = 0; j < maxTimeSteps; j ++)
00250 bec.evolve(true);
00251 }
00252 rmsError = RMSError(Images(noOfImages-1),*ptrField);
00253
00254 for(int k = noOfImages-1; k > 0; k --)
00255 {
00256 updateMagnitude(Images(k),*ptrField);
00257 copyField(GSPhases(k),*ptrField);
00258 for(int j = 0; j < maxTimeSteps; j ++)
00259 bec.evolve(false);
00260 }
00261 iterationNo ++;
00262 return rmsError;
00263 }
00264
00265 parameterType QCGGS::iterate(QC2ComponentBEC<2> &bec)
00266 {
00267
00268 for(int k = 0; k < noOfImages-1; k ++)
00269 {
00270 updateMagnitude(Images(k),*ptrField);
00271 updateMagnitude(Images2(k),*ptrField2);
00272 copyField(GSPhases(k),*ptrField);
00273 copyField(GSPhases2(k),*ptrField2);
00274 for(int j = 0; j < maxTimeSteps; j ++)
00275 bec.evolve(true);
00276 }
00277 rmsError = RMSError(Images(noOfImages-1),*ptrField);
00278
00279 for(int k = noOfImages-1; k > 0; k --)
00280 {
00281 updateMagnitude(Images(k),*ptrField);
00282 updateMagnitude(Images2(k),*ptrField2);
00283 copyField(GSPhases(k),*ptrField);
00284 copyField(GSPhases2(k),*ptrField2);
00285 for(int j = 0; j < maxTimeSteps; j ++)
00286 bec.evolve(false);
00287 }
00288 iterationNo ++;
00289 return rmsError;
00290 }