/** @param inname Name of workspace containing peaks @param params optimized cell parameters @param out residuals from optimization */ void OptimizeLatticeForCellType::optLattice(std::string inname, std::vector<double> ¶ms, double *out) { PeaksWorkspace_sptr ws = boost::dynamic_pointer_cast<PeaksWorkspace>( AnalysisDataService::Instance().retrieve(inname)); const std::vector<Peak> &peaks = ws->getPeaks(); size_t n_peaks = ws->getNumberPeaks(); std::vector<V3D> q_vector; std::vector<V3D> hkl_vector; for (size_t i = 0; i < params.size(); i++) params[i] = std::abs(params[i]); for (size_t i = 0; i < n_peaks; i++) { q_vector.push_back(peaks[i].getQSampleFrame()); hkl_vector.push_back(peaks[i].getHKL()); } Mantid::API::IAlgorithm_sptr alg = createChildAlgorithm("CalculateUMatrix"); alg->setPropertyValue("PeaksWorkspace", inname); alg->setProperty("a", params[0]); alg->setProperty("b", params[1]); alg->setProperty("c", params[2]); alg->setProperty("alpha", params[3]); alg->setProperty("beta", params[4]); alg->setProperty("gamma", params[5]); alg->executeAsChildAlg(); ws = alg->getProperty("PeaksWorkspace"); OrientedLattice latt = ws->mutableSample().getOrientedLattice(); DblMatrix UB = latt.getUB(); DblMatrix A = aMatrix(params); DblMatrix Bc = A; Bc.Invert(); DblMatrix U1_B1 = UB * A; OrientedLattice o_lattice; o_lattice.setUB(U1_B1); DblMatrix U1 = o_lattice.getU(); DblMatrix U1_Bc = U1 * Bc; for (size_t i = 0; i < hkl_vector.size(); i++) { V3D error = U1_Bc * hkl_vector[i] - q_vector[i] / (2.0 * M_PI); out[i] = error.norm2(); } return; }
/** Execute the algorithm. */ void SaveIsawUB::exec() { try { Workspace_sptr ws1 = getProperty("InputWorkspace"); ExperimentInfo_sptr ws; IMDEventWorkspace_sptr MDWS=boost::dynamic_pointer_cast<IMDEventWorkspace>(ws1); if (MDWS != NULL) { ws = MDWS->getExperimentInfo(0); } else { ws = boost::dynamic_pointer_cast<ExperimentInfo>(ws1); } if (!ws) throw std::invalid_argument( "Must specify either a MatrixWorkspace or a PeaksWorkspace or a MDEventWorkspace."); if (!ws->sample().hasOrientedLattice()) throw std::invalid_argument("Workspace must have an oriented lattice to save"); std::string Filename = getProperty("Filename"); ofstream out; out.open(Filename.c_str()); OrientedLattice lattice = ws->sample().getOrientedLattice(); Kernel::DblMatrix ub = lattice.getUB(); // Write the ISAW UB matrix const int beam = 2; const int up = 1; const int back = 0; out << fixed; for (size_t basis = 0; basis < 3; basis++) { out << setw(11) << setprecision(8) << ub[beam][basis] << setw(12) << setprecision(8) << ub[back][basis] << setw(12) << setprecision(8) << ub[up][basis] << " " << endl; } out << setw(11) << setprecision(4) << lattice.a() << setw(12) << setprecision(4) << lattice.b() << setw(12) << setprecision(4) << lattice.c() << setw(12) << setprecision(4) << lattice.alpha() << setw(12) << setprecision(4) << lattice.beta() << setw(12) << setprecision(4) << lattice.gamma() << setw(12) << setprecision(4) << lattice.volume() << " " << endl; double ErrorVolume =getErrorVolume(lattice); out << setw(11) << setprecision(4) << lattice.errora() << setw(12) << setprecision(4) << lattice.errorb() << setw(12) << setprecision(4) << lattice.errorc() << setw(12) << setprecision(4) << lattice.erroralpha() << setw(12) << setprecision(4) << lattice.errorbeta() << setw(12) << setprecision(4) << lattice.errorgamma() << setw(12) << setprecision(4) << ErrorVolume << " " << endl; out << endl << endl; out << "The above matrix is the Transpose of the UB Matrix. "; out << "The UB matrix maps the column" << endl; out << "vector (h,k,l ) to the column vector "; out << "(q'x,q'y,q'z)." << endl; out << "|Q'|=1/dspacing and its coordinates are a "; out << "right-hand coordinate system where" << endl; out << " x is the beam direction and z is vertically "; out << "upward.(IPNS convention)" << endl; out.close(); } catch (exception &s) { throw std::invalid_argument(s.what()); } }
/** Execute the algorithm. */ void SaveIsawUB::exec() { try { Workspace_sptr ws1 = getProperty("InputWorkspace"); ExperimentInfo_sptr ws; MultipleExperimentInfos_sptr MDWS = boost::dynamic_pointer_cast<MultipleExperimentInfos>(ws1); if (MDWS != nullptr) { ws = MDWS->getExperimentInfo(0); } else { ws = boost::dynamic_pointer_cast<ExperimentInfo>(ws1); } if (!ws) throw std::invalid_argument("Must specify either a MatrixWorkspace or a " "PeaksWorkspace or a MDWorkspace."); if (!ws->sample().hasOrientedLattice()) throw std::invalid_argument( "Workspace must have an oriented lattice to save"); std::string Filename = getProperty("Filename"); ofstream out; out.open(Filename.c_str()); OrientedLattice lattice = ws->sample().getOrientedLattice(); Kernel::DblMatrix ub = lattice.getUB(); Kernel::DblMatrix modub = lattice.getModUB(); // Write the ISAW UB matrix const int beam = 2; const int up = 1; const int back = 0; out << fixed; for (size_t basis = 0; basis < 3; basis++) { out << setw(11) << setprecision(8) << ub[beam][basis] << setw(12) << setprecision(8) << ub[back][basis] << setw(12) << setprecision(8) << ub[up][basis] << " \n"; } int ModDim = 0; for (int i = 0; i < 3; i++) { if (lattice.getModVec(i) == V3D(0, 0, 0)) continue; else ModDim++; } if (ModDim > 0) { out << "ModUB: \n"; for (size_t basis = 0; basis < 3; basis++) { out << setw(11) << setprecision(8) << modub[beam][basis] << setw(12) << setprecision(8) << modub[back][basis] << setw(12) << setprecision(8) << modub[up][basis] << " \n"; } } // out << "Lattice Parameters: \n"; out << setw(11) << setprecision(4) << lattice.a() << setw(12) << setprecision(4) << lattice.b() << setw(12) << setprecision(4) << lattice.c() << setw(12) << setprecision(4) << lattice.alpha() << setw(12) << setprecision(4) << lattice.beta() << setw(12) << setprecision(4) << lattice.gamma() << setw(12) << setprecision(4) << lattice.volume() << " \n"; double ErrorVolume = getErrorVolume(lattice); out << setw(11) << setprecision(4) << lattice.errora() << setw(12) << setprecision(4) << lattice.errorb() << setw(12) << setprecision(4) << lattice.errorc() << setw(12) << setprecision(4) << lattice.erroralpha() << setw(12) << setprecision(4) << lattice.errorbeta() << setw(12) << setprecision(4) << lattice.errorgamma() << setw(12) << setprecision(4) << ErrorVolume << " \n"; out << "\n"; if (ModDim >= 1) { out << "Modulation Vector 1: " << setw(12) << setprecision(4) << lattice.getdh(0) << setw(12) << setprecision(4) << lattice.getdk(0) << setw(12) << setprecision(4) << lattice.getdl(0) << " \n"; out << "Modulation Vector 1 error: " << setw(6) << setprecision(4) << lattice.getdherr(0) << setw(12) << setprecision(4) << lattice.getdkerr(0) << setw(12) << setprecision(4) << lattice.getdlerr(0) << " \n\n"; } if (ModDim >= 2) { out << "Modulation Vector 2: " << setw(12) << setprecision(4) << lattice.getdh(1) << setw(12) << setprecision(4) << lattice.getdk(1) << setw(12) << setprecision(4) << lattice.getdl(1) << " \n"; out << "Modulation Vector 2 error: " << setw(6) << setprecision(4) << lattice.getdherr(1) << setw(12) << setprecision(4) << lattice.getdkerr(1) << setw(12) << setprecision(4) << lattice.getdlerr(1) << " \n\n"; } if (ModDim == 3) { out << "Modulation Vector 3: " << setw(12) << setprecision(4) << lattice.getdh(2) << setw(12) << setprecision(4) << lattice.getdk(2) << setw(12) << setprecision(4) << lattice.getdl(2) << " \n"; out << "Modulation Vector 3 error: " << setw(6) << setprecision(4) << lattice.getdherr(2) << setw(12) << setprecision(4) << lattice.getdkerr(2) << setw(12) << setprecision(4) << lattice.getdlerr(2) << " \n\n"; } if (ModDim >= 1) { out << "Max Order: " << lattice.getMaxOrder() << " \n"; out << "Cross Terms: " << lattice.getCrossTerm() << " \n"; } out << "\n"; if (ModDim == 0) { out << "The above matrix is the Transpose of the UB Matrix. "; out << "The UB matrix maps the column\n"; out << "vector (h,k,l ) to the column vector "; out << "(q'x,q'y,q'z).\n"; out << "|Q'|=1/dspacing and its coordinates are a "; out << "right-hand coordinate system where\n"; out << " x is the beam direction and z is vertically "; out << "upward.(IPNS convention)\n"; } else { out << "The above matrix is the Transpose of the UB Matrix and the " "Transpose of ModUB. "; out << "The UB matrix together with ModUB maps the column vector " "(h,k,l,m,n,p) \n"; out << "to the column vector (q'x,q'y,q'z).\n"; out << "The columns of ModUB are the coordinates of modulation vectors " "in Qlab. \n"; out << "|Q'|=1/dspacing and its coordinates are a "; out << "right-hand coordinate system where"; out << " x is the beam direction and z is vertically "; out << "upward.(IPNS convention)\n"; } out.close(); } catch (exception &s) { throw std::invalid_argument(s.what()); } }