| 1 |
/* |
| 2 |
* The MIT License |
| 3 |
|
| 4 |
Conograph (powder auto-indexing program) |
| 5 |
|
| 6 |
Copyright (c) <2012> <Ryoko Oishi-Tomiyasu, KEK> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in |
| 16 |
all copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
THE SOFTWARE. |
| 25 |
* |
| 26 |
*/ |
| 27 |
#include "gather_additional_Q.hh" |
| 28 |
#include "../utility_func/zmath.hh" |
| 29 |
#include "../zlog/zlog.hh" |
| 30 |
#include "../ControlParam.hh" |
| 31 |
#include "../PeakPosData.hh" |
| 32 |
#include "LatticeFigureOfMeritToDisplay.hh" |
| 33 |
|
| 34 |
|
| 35 |
LatticeFigureOfMeritToDisplay::LatticeFigureOfMeritToDisplay() : m_showsTicks(false) |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
void LatticeFigureOfMeritToDisplay::putStandardMillerIndicesToFit(vector< VecDat3<Int4> >& hkl_to_fit, |
| 41 |
vector<bool>& fix_or_fit_flag) const |
| 42 |
{ |
| 43 |
const Int4 num_Q = m_associated_hkl_tray.size(); |
| 44 |
const Double cv2 = m_latfom.putCriticalValueSquare(); |
| 45 |
|
| 46 |
fix_or_fit_flag.resize(num_Q); |
| 47 |
hkl_to_fit.resize(num_Q); |
| 48 |
|
| 49 |
vector< multimap<Double, vector<HKL_Q>::const_iterator> >::const_iterator it = m_associated_hkl_tray.begin(); |
| 50 |
Double diff; |
| 51 |
for(Int4 i=0; i<num_Q; i++, it++) |
| 52 |
{ |
| 53 |
if( it->empty() ) |
| 54 |
{ |
| 55 |
hkl_to_fit[i] = 0; |
| 56 |
fix_or_fit_flag[i] = false; |
| 57 |
continue; |
| 58 |
} |
| 59 |
|
| 60 |
diff = m_qdata[i].q - it->begin()->second->Q(); |
| 61 |
hkl_to_fit[i] = it->begin()->second->HKL(); |
| 62 |
|
| 63 |
if( diff * diff <= cv2 * m_qdata[i].q_var ) fix_or_fit_flag[i] = true; |
| 64 |
else fix_or_fit_flag[i] = false; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
void LatticeFigureOfMeritToDisplay::resetMillerIndicesInRange(const Int4& num_fit_data) |
| 70 |
{ |
| 71 |
assert( num_fit_data <= (Int4)m_qdata.size() ); |
| 72 |
|
| 73 |
const Double cv2 = m_latfom.putCriticalValueSquare(); |
| 74 |
|
| 75 |
range<Double> qobs_range; |
| 76 |
qobs_range.begin = 0.0; |
| 77 |
qobs_range.end = m_qdata.rbegin()->q + sqrt(cv2 * m_qdata.rbegin()->q_var); |
| 78 |
|
| 79 |
m_latfom.putMillerIndicesInRange(qobs_range.begin, qobs_range.end, m_cal_hkl_tray); |
| 80 |
m_associated_hkl_tray.clear(); |
| 81 |
m_associated_hkl_tray.resize(num_fit_data); |
| 82 |
|
| 83 |
vector< multimap<Double, vector<HKL_Q>::const_iterator> >::iterator it = m_associated_hkl_tray.begin(); |
| 84 |
vector<HKL_Q>::const_iterator it2_begin, it2_end; |
| 85 |
for(Int4 i=0; i<num_fit_data; i++, it++) |
| 86 |
{ |
| 87 |
const Double diff = sqrt(cv2 * m_qdata[i].q_var); |
| 88 |
it2_begin = lower_bound( m_cal_hkl_tray.begin(), m_cal_hkl_tray.end(), HKL_Q(0, m_qdata[i].q - diff) ); |
| 89 |
it2_end = upper_bound( it2_begin, (vector<HKL_Q>::const_iterator)m_cal_hkl_tray.end(), HKL_Q(0, m_qdata[i].q + diff) ); |
| 90 |
if( it2_begin < it2_end ) |
| 91 |
{ |
| 92 |
for(vector<HKL_Q>::const_iterator it2=it2_begin; it2<it2_end; it2++) |
| 93 |
{ |
| 94 |
it->insert( multimap<Double, vector<HKL_Q>::const_iterator>::value_type(fabs(m_qdata[i].q-it2->Q()), it2) ); |
| 95 |
} |
| 96 |
} |
| 97 |
else |
| 98 |
{ |
| 99 |
if( m_cal_hkl_tray.begin() < it2_begin ) |
| 100 |
{ |
| 101 |
it->insert( multimap<Double, vector<HKL_Q>::const_iterator>::value_type(fabs(m_qdata[i].q - (it2_begin-1)->Q()), it2_begin-1) ); |
| 102 |
} |
| 103 |
if( it2_begin < m_cal_hkl_tray.end() ) |
| 104 |
{ |
| 105 |
it->insert( multimap<Double, vector<HKL_Q>::const_iterator>::value_type(fabs(m_qdata[i].q - it2_begin->Q()), it2_begin) ); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
Int4 LatticeFigureOfMeritToDisplay::fitLatticeParameter(const PeakPosData& pdata, |
| 114 |
const vector<etype_ID>& fitflag, const Int4& Max_ITNUM, const Double& limiter) |
| 115 |
{ |
| 116 |
vector< VecDat3<Int4> > hkl_to_fit = m_hkl_to_fit; |
| 117 |
vector<bool> fix_or_fit_flag = m_fix_or_fit_flag; // 0:fix, 1:fit. |
| 118 |
Int4 itnum = 0; |
| 119 |
try{ |
| 120 |
do{ |
| 121 |
const Double old_fom = m_latfom.putFiguresOfMerit().putFigureOfMeritWolff(); |
| 122 |
ZLOG_INFO( "\n" + num2str( ++itnum ) + ") Initial " + m_latfom.putFiguresOfMerit().putLabel_FigureOfMeritWolff() + " = " + num2str(old_fom) + "\n" ); |
| 123 |
|
| 124 |
vector<QData> qdata_modified; |
| 125 |
pair<bool, ZErrorMessage> ans = m_latfom.fitLatticeParameter(m_qdata, |
| 126 |
hkl_to_fit, fix_or_fit_flag, true, |
| 127 |
pdata, fitflag, Max_ITNUM, |
| 128 |
VCData::putPeakQData().size(), qdata_modified); |
| 129 |
|
| 130 |
if( ans.second.putErrorType() != ZErrorNoError ) |
| 131 |
{ |
| 132 |
throw ans.second; |
| 133 |
} |
| 134 |
else if( !ans.first ) |
| 135 |
{ |
| 136 |
ZLOG_INFO( num2str( m_latfom.putFiguresOfMerit().putLabel_FigureOfMeritWolff() ) + " was not improved.\n\n" ); |
| 137 |
break; |
| 138 |
} |
| 139 |
else |
| 140 |
{ |
| 141 |
// Optimization has succeeded. |
| 142 |
const Double& new_fom = m_latfom.putFiguresOfMerit().putFigureOfMeritWolff(); |
| 143 |
ZLOG_INFO( num2str( m_latfom.putFiguresOfMerit().putLabel_FigureOfMeritWolff() ) + " of optimized solution = " + num2str(new_fom) + "\n" ); |
| 144 |
m_qdata = qdata_modified; |
| 145 |
resetMillerIndicesInRange(m_associated_hkl_tray.size()); |
| 146 |
putStandardMillerIndicesToFit(hkl_to_fit, fix_or_fit_flag); |
| 147 |
if( (new_fom - old_fom) < old_fom * limiter ) break; |
| 148 |
} |
| 149 |
} while(true); |
| 150 |
} |
| 151 |
catch(const ZErrorMessage& zerr) |
| 152 |
{ |
| 153 |
ZLOG_ERROR( zerr.printErrorLog() ); |
| 154 |
} |
| 155 |
|
| 156 |
if( itnum > 2 ) |
| 157 |
{ |
| 158 |
m_hkl_to_fit = hkl_to_fit; |
| 159 |
m_fix_or_fit_flag = fix_or_fit_flag; |
| 160 |
} |
| 161 |
|
| 162 |
return itnum-1; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
void LatticeFigureOfMeritToDisplay::putCalculatedPeakPosInRange(const ControlParam& cdata, |
| 167 |
Vec_DP& cal_pos_tray) const |
| 168 |
{ |
| 169 |
static const Double MAX_DP = numeric_limits<Double>::max(); |
| 170 |
const vector<ZParawError>& peak_shift_param_rad = m_latfom.putPeakShiftParamRadian(); |
| 171 |
|
| 172 |
cal_pos_tray.clear(); |
| 173 |
if( cdata.IsAngleDispersion() ) |
| 174 |
{ |
| 175 |
for(vector<HKL_Q>::const_iterator it=m_cal_hkl_tray.begin(); it<m_cal_hkl_tray.end(); it++) |
| 176 |
{ |
| 177 |
if( cdata.putWaveLength() * cdata.putWaveLength() * it->Q() > 4.0 ) break; |
| 178 |
cal_pos_tray.push_back( cal_theta2_deg(peak_shift_param_rad, cdata.putWaveLength(), sqrt(it->Q()) ) ); |
| 179 |
} |
| 180 |
|
| 181 |
const Int4 isize = m_cal_hkl_tray.size(); |
| 182 |
cal_pos_tray.resize(isize, -MAX_DP); |
| 183 |
} |
| 184 |
else |
| 185 |
{ |
| 186 |
for(vector<HKL_Q>::const_iterator it=m_cal_hkl_tray.begin(); it<m_cal_hkl_tray.end(); it++) |
| 187 |
{ |
| 188 |
cal_pos_tray.push_back( put_polynomial_value(cdata.putConvParam(), 1.0 / sqrt(it->Q()) ) ); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
void LatticeFigureOfMeritToDisplay::putPeakPosToFit(const ControlParam& cdata, |
| 195 |
Vec_DP& cal_q_tray, Vec_DP& cal_pos_tray) const |
| 196 |
{ |
| 197 |
const Vec_DP& conv_param = cdata.putConvParam(); |
| 198 |
const vector<ZParawError>& peak_shift_param_rad = m_latfom.putPeakShiftParamRadian(); |
| 199 |
const Double& wave_length = cdata.putWaveLength(); |
| 200 |
|
| 201 |
const vector< VecDat3<Int4> >& hkl_to_fit = putMillerIndicesToFit(); |
| 202 |
const ArrayIndex num_obsQ = hkl_to_fit.size(); |
| 203 |
const SymMat<Double>& dbl_S = m_latfom.putOptimizedForm().first; |
| 204 |
|
| 205 |
cal_q_tray.clear(); |
| 206 |
cal_pos_tray.clear(); |
| 207 |
cal_q_tray.resize(num_obsQ); |
| 208 |
cal_pos_tray.resize(num_obsQ); |
| 209 |
for(ArrayIndex l=0; l<num_obsQ; l++) |
| 210 |
{ |
| 211 |
const VecDat3<Int4>& hkl = hkl_to_fit.at(l); |
| 212 |
const Double q_cal = norm(hkl, dbl_S); |
| 213 |
cal_q_tray.at(l) = q_cal; |
| 214 |
|
| 215 |
if( cdata.IsAngleDispersion() ) |
| 216 |
{ |
| 217 |
cal_pos_tray.at(l) = cal_theta2_deg(peak_shift_param_rad, wave_length, sqrt(q_cal)); |
| 218 |
} |
| 219 |
else |
| 220 |
{ |
| 221 |
cal_pos_tray.at(l) = put_polynomial_value(conv_param, 1.0 / sqrt(q_cal)); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
void LatticeFigureOfMeritToDisplay::printIndexingResult(const ControlParam& cdata, |
| 230 |
const PeakPosData& pdata, |
| 231 |
const Int4& label_start0, |
| 232 |
ostream* os) const |
| 233 |
{ |
| 234 |
static const Double DegRad = 180.0 / PI(); |
| 235 |
|
| 236 |
Int4 label_start = label_start0; |
| 237 |
os->width(label_start); |
| 238 |
*os << "" << "<IndexingResults>\n"; |
| 239 |
label_start++; |
| 240 |
|
| 241 |
const Vec_DP& posdata = pdata.putPeakPosXData(); |
| 242 |
const Vec_DP& posdata_err = pdata.putPeakWidthData(); |
| 243 |
|
| 244 |
const vector<Int4>& pos_qindex = VCData::putPeakQIndex(); |
| 245 |
|
| 246 |
const vector< VecDat3<Int4> >& hkl_to_fit = putMillerIndicesToFit(); |
| 247 |
const vector<bool>& fix_or_fit_flag = putFittingIDs(); |
| 248 |
const ArrayIndex num_obsQ = hkl_to_fit.size(); |
| 249 |
|
| 250 |
Vec_DP cal_q_tray, cal_pos_tray; |
| 251 |
this->putPeakPosToFit(cdata, cal_q_tray, cal_pos_tray); |
| 252 |
|
| 253 |
os->width(label_start); |
| 254 |
*os << "" << "<!-- q_obs, q_err, q_cal, peak_pos, peak_width, pos_cal, hkl, fix_or_fit.-->\n"; |
| 255 |
for(ArrayIndex l=0; l<num_obsQ; l++) |
| 256 |
{ |
| 257 |
os->width(15); |
| 258 |
*os << m_qdata.at(l).q; |
| 259 |
|
| 260 |
os->width(15); |
| 261 |
*os << sqrt(m_qdata.at(l).q_var); |
| 262 |
|
| 263 |
os->width(15); |
| 264 |
*os << cal_q_tray.at(l); |
| 265 |
|
| 266 |
os->width(15); |
| 267 |
*os << posdata[pos_qindex.at(l)]; |
| 268 |
|
| 269 |
os->width(15); |
| 270 |
*os << posdata_err[pos_qindex.at(l)]; |
| 271 |
|
| 272 |
os->width(15); |
| 273 |
*os << cal_pos_tray.at(l); |
| 274 |
|
| 275 |
const VecDat3<Int4>& hkl = hkl_to_fit.at(l); |
| 276 |
|
| 277 |
os->width(15); |
| 278 |
*os << "[" + num2str<Int4>( hkl[0] ) + "," |
| 279 |
+ num2str<Int4>( hkl[1] ) + "," |
| 280 |
+ num2str<Int4>( hkl[2] ) + "]"; |
| 281 |
|
| 282 |
os->width(5); |
| 283 |
if( fix_or_fit_flag.at(l) ) *os << "1"; |
| 284 |
else *os << "0"; |
| 285 |
|
| 286 |
*os << endl; |
| 287 |
} |
| 288 |
|
| 289 |
label_start--; |
| 290 |
os->width(label_start); |
| 291 |
*os << "" << "</IndexingResults>\n\n"; |
| 292 |
|
| 293 |
if( !cdata.IsAngleDispersion() ) return; |
| 294 |
|
| 295 |
label_start = label_start0; |
| 296 |
os->width(label_start); |
| 297 |
*os << "" << "<OptimizedZeroShitParameters>\n"; |
| 298 |
label_start++; |
| 299 |
|
| 300 |
vector<ZParawError> peak_shift_param_rad = m_latfom.putPeakShiftParamRadian(); |
| 301 |
const Int4 param_num = peak_shift_param_rad.size(); |
| 302 |
for(Int4 i=0; i<param_num; i++) |
| 303 |
{ |
| 304 |
os->width(label_start); |
| 305 |
*os << "" << "<Parameter>"; |
| 306 |
os->width(15); |
| 307 |
*os << peak_shift_param_rad[i].value * DegRad; |
| 308 |
*os << " </Parameter>"; |
| 309 |
|
| 310 |
*os << " <Error>"; |
| 311 |
os->width(15); |
| 312 |
*os << peak_shift_param_rad[i].error * DegRad; |
| 313 |
*os << " </Error>\n"; |
| 314 |
} |
| 315 |
|
| 316 |
label_start--; |
| 317 |
os->width(label_start); |
| 318 |
*os << "" << "</OptimizedZeroShitParameters>\n"; |
| 319 |
} |