const QString CPrinter::entryLink(const KeyTreeItem& item, CSwordModuleInfo* module) { Q_ASSERT(module); if (module->type() == CSwordModuleInfo::Bible) { CSwordVerseKey vk(module); vk = item.key(); switch (item.settings().keyRenderingFace) { case KeyTreeItem::Settings::CompleteShort: return QString::fromUtf8(vk.getShortText()); case KeyTreeItem::Settings::CompleteLong: return vk.key(); case KeyTreeItem::Settings::NoKey: return QString::null; case KeyTreeItem::Settings::SimpleKey: //fall through default: return QString::number(vk.Verse()); } } return item.key(); }
QString CrossRefRendering::entryLink(const KeyTreeItem &item, const CSwordModuleInfo *module) { QString linkText; const bool isBible = module && (module->type() == CSwordModuleInfo::Bible); CSwordVerseKey vk(module); //only valid for bible modules, i.e. isBible == true if (isBible) { vk.setKey(item.key()); } switch (item.settings().keyRenderingFace) { case KeyTreeItem::Settings::NoKey: { linkText = QString::null; break; //no key is valid for all modules } case KeyTreeItem::Settings::CompleteShort: { if (isBible) { linkText = QString::fromUtf8(vk.getShortText()); break; } //fall through for non-Bible modules } case KeyTreeItem::Settings::CompleteLong: { if (isBible) { linkText = vk.key(); break; } //fall through for non-Bible modules } case KeyTreeItem::Settings::SimpleKey: { if (isBible) { linkText = QString::number(vk.getVerse()); break; } //fall through for non-Bible modules } default: { //default behaviour to return the passed key linkText = item.key(); break; } } if (!linkText.isEmpty()) { //if we have a valid link text // qWarning("rendering"); return QString("<a href=\"%1\">%2</a>") .arg( ReferenceManager::encodeHyperlink( module->name(), item.key(), ReferenceManager::typeFromModule(module->type()) ) ) .arg(linkText); } return QString::null; }
/** Copies the current chapter into the clipboard. */ void CBibleReadWindow::copyDisplayedText() { CSwordVerseKey dummy(*verseKey()); dummy.setVerse(1); CSwordVerseKey vk(*verseKey()); vk.setLowerBound(dummy); const CSwordBibleModuleInfo* bible = dynamic_cast<const CSwordBibleModuleInfo*>(modules().first()); dummy.setVerse(bible->verseCount(dummy.book(), dummy.getChapter())); vk.setUpperBound(dummy); CExportManager mgr(false, tr("Copying"), filterOptions(), displayOptions()); mgr.copyKey(&vk, CExportManager::Text, true); }
BtBookmarkItem::BtBookmarkItem(CSwordModuleInfo* module, QString key, QString& description) :m_description(description), m_moduleName(module ? module->name() : QString::null) { if (((module && (module->type() == CSwordModuleInfo::Bible)) || (module->type() == CSwordModuleInfo::Commentary)) ) { CSwordVerseKey vk(0); vk.key(key); vk.setLocale("en"); m_key = vk.key(); //the m_key member is always the english key! } else { m_key = key; }; update(); }
/** Saves the chapter as valid HTML page. */ void CBibleReadWindow::saveChapterPlain() { //saves the complete chapter to disk CSwordVerseKey vk(*verseKey()); CSwordVerseKey dummy(*verseKey()); dummy.setVerse(1); vk.setLowerBound(dummy); const CSwordBibleModuleInfo* bible = dynamic_cast<const CSwordBibleModuleInfo*>(modules().first()); dummy.setVerse(bible->verseCount(dummy.book(), dummy.getChapter())); vk.setUpperBound(dummy); CExportManager mgr(true, tr("Saving"), filterOptions(), displayOptions()); mgr.saveKey(&vk, CExportManager::Text, true); }
QString BtBookmarkItem::key() { const QString englishKeyName = englishKey(); if (!module()) { return englishKeyName; } QString returnKeyName = englishKeyName; if ((module()->type() == CSwordModuleInfo::Bible) || (module()->type() == CSwordModuleInfo::Commentary)) { CSwordVerseKey vk(0); vk.key(englishKeyName); vk.setLocale(CPointers::backend()->booknameLanguage().toLatin1() ); returnKeyName = vk.key(); //the returned key is always in the currently set bookname language } return returnKeyName; }
QString CDisplayRendering::entryLink(const KeyTreeItem &item, const CSwordModuleInfo * module) { QString linkText; const bool isBible = module && (module->type() == CSwordModuleInfo::Bible); CSwordVerseKey vk(module); //only valid for bible modules, i.e. isBible == true vk.setIntros(true); if (isBible) { vk.setKey(item.key()); } if (isBible && (vk.getVerse() == 0)) { return QString::null; //Warning: return already here } switch (item.settings().keyRenderingFace) { case KeyTreeItem::Settings::NoKey: { linkText = QString::null; break; //no key is valid for all modules } case KeyTreeItem::Settings::CompleteShort: { if (isBible) { linkText = QString::fromUtf8(vk.getShortText()); break; } //fall through for non-Bible modules } case KeyTreeItem::Settings::CompleteLong: { if (isBible) { linkText = vk.key(); break; } //fall through for non-Bible modules } case KeyTreeItem::Settings::SimpleKey: { if (isBible) { linkText = QString::number(vk.getVerse()); break; } //fall through for non-Bible modules } default: { //default behaviour to return the passed key linkText = item.key(); break; } } if (linkText.isEmpty()) { return QString("<a name=\"").append(keyToHTMLAnchor(item.key())).append("\"></a>"); } else { return QString("<a name=\"").append(keyToHTMLAnchor(item.key())).append("\" ") .append("href=\"") .append(ReferenceManager::encodeHyperlink( module->name(), item.key(), ReferenceManager::typeFromModule(module->type())) ) .append("\">").append(linkText).append("</a>\n"); } return QString::null; }
void SkinningAppVk::initRendering(void) { setGLDrawCallbacks(this); VkResult result = VK_ERROR_INITIALIZATION_FAILED; NvAssetLoaderAddSearchPath("vk10-kepler/SkinningAppVk"); mMesh.initRendering(vk()); SkinnedMesh::UBOBlock& ubo = *mMesh.mUBO; ubo.mRenderMode[2] = 0; ubo.mLightDir0 = nv::vec4f(0.267f, 0.535f, 0.802f, 0.0f); ubo.mLightDir1 = nv::vec4f(-0.408f, 0.816f, -0.408f, 0.0f); mMesh.mUBO.Update(); VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = { VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO }; pipelineLayoutCreateInfo.setLayoutCount = 1; pipelineLayoutCreateInfo.pSetLayouts = &mMesh.mDescriptorSetLayout; result = vkCreatePipelineLayout(device(), &pipelineLayoutCreateInfo, 0, &mPipelineLayout); CHECK_VK_RESULT(); // Create static state info for the mPipeline. // set dynamically VkPipelineViewportStateCreateInfo vpStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO }; vpStateInfo.pNext = 0; vpStateInfo.viewportCount = 1; vpStateInfo.scissorCount = 1; VkPipelineRasterizationStateCreateInfo rsStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO }; rsStateInfo.depthClampEnable = VK_TRUE; rsStateInfo.rasterizerDiscardEnable = VK_FALSE; rsStateInfo.polygonMode = VK_POLYGON_MODE_FILL; rsStateInfo.cullMode = VK_CULL_MODE_BACK_BIT; rsStateInfo.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; VkPipelineColorBlendAttachmentState attachments[1] = {}; attachments[0].blendEnable = VK_FALSE; attachments[0].colorWriteMask = ~0; VkPipelineColorBlendStateCreateInfo cbStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO }; cbStateInfo.logicOpEnable = VK_FALSE; cbStateInfo.attachmentCount = ARRAY_SIZE(attachments); cbStateInfo.pAttachments = attachments; VkPipelineDepthStencilStateCreateInfo dsStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO }; dsStateInfo.depthTestEnable = VK_TRUE; dsStateInfo.depthWriteEnable = VK_TRUE; dsStateInfo.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL; dsStateInfo.depthBoundsTestEnable = VK_FALSE; dsStateInfo.stencilTestEnable = VK_FALSE; dsStateInfo.minDepthBounds = 0.0f; dsStateInfo.maxDepthBounds = 1.0f; VkPipelineMultisampleStateCreateInfo msStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO }; msStateInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; msStateInfo.alphaToCoverageEnable = VK_FALSE; msStateInfo.sampleShadingEnable = VK_FALSE; msStateInfo.minSampleShading = 1.0f; uint32_t smplMask = 0x1; msStateInfo.pSampleMask = &smplMask; VkPipelineTessellationStateCreateInfo tessStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO }; tessStateInfo.patchControlPoints = 0; VkPipelineDynamicStateCreateInfo dynStateInfo; VkDynamicState dynStates[] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; memset(&dynStateInfo, 0, sizeof(dynStateInfo)); dynStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; dynStateInfo.dynamicStateCount = 2; dynStateInfo.pDynamicStates = dynStates; // Shaders VkPipelineShaderStageCreateInfo shaderStages[2]; uint32_t shaderCount = 0; #ifdef SOURCE_SHADERS shaderCount = vk().createShadersFromSourceFile( NvAssetLoadTextFile("src_shaders/skinning.glsl"), shaderStages, 2); #else { int32_t length; char* data = NvAssetLoaderRead("shaders/skinning.nvs", length); shaderCount = vk().createShadersFromBinaryFile((uint32_t*)data, length, shaderStages, 2); } #endif // Create mPipeline state VI-IA-VS-VP-RS-FS-CB VkGraphicsPipelineCreateInfo pipelineInfo = { VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO }; pipelineInfo.pVertexInputState = &mMesh.mVIStateInfo; pipelineInfo.pInputAssemblyState = &mMesh.mIAStateInfo; pipelineInfo.pViewportState = &vpStateInfo; pipelineInfo.pRasterizationState = &rsStateInfo; pipelineInfo.pColorBlendState = &cbStateInfo; pipelineInfo.pDepthStencilState = &dsStateInfo; pipelineInfo.pMultisampleState = &msStateInfo; pipelineInfo.pTessellationState = &tessStateInfo; pipelineInfo.pDynamicState = &dynStateInfo; pipelineInfo.stageCount = shaderCount; pipelineInfo.pStages = shaderStages; pipelineInfo.renderPass = vk().mainRenderTarget()->clearRenderPass(); pipelineInfo.subpass = 0; pipelineInfo.layout = mPipelineLayout; result = vkCreateGraphicsPipelines(device(), VK_NULL_HANDLE, 1, &pipelineInfo, NULL, &mPipeline); CHECK_VK_RESULT(); mMesh.UpdateDescriptorSet(vk()); }
void SkinningAppVk::draw(void) { VkResult result = VK_ERROR_INITIALIZATION_FAILED; NvSimpleUBO<SkinnedMesh::UBOBlock>& uboObj = mMesh.mUBO; SkinnedMesh::UBOBlock& ubo = *uboObj; // Compute and update the ModelViewProjection matrix nv::perspectiveLH(ubo.mMVP, 45.0f, (float)m_width / (float)m_height, 0.1f, 100.0f); ubo.mMVP *= m_transformer->getModelViewMat(); // Compute and update the bone matrices computeBones(mTime, ubo); mTime += mTimeScalar * getFrameDeltaTime(); // Update other uniforms ubo.mRenderMode[0] = (int32_t)mSingleBoneSkinning; ubo.mRenderMode[1] = (int32_t)mRenderMode; uboObj.Update(); VkCommandBuffer cmd = vk().getMainCommandBuffer(); VkRenderPassBeginInfo renderPassBeginInfo = { VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO }; renderPassBeginInfo.renderPass = vk().mainRenderTarget()->clearRenderPass(); renderPassBeginInfo.framebuffer = vk().mainRenderTarget()->frameBuffer(); renderPassBeginInfo.renderArea.offset.x = 0; renderPassBeginInfo.renderArea.offset.y = 0; renderPassBeginInfo.renderArea.extent.width = m_width; renderPassBeginInfo.renderArea.extent.height = m_height; VkClearValue clearValues[2]; clearValues[0].color.float32[0] = 0.33f; clearValues[0].color.float32[1] = 0.44f; clearValues[0].color.float32[2] = 0.66f; clearValues[0].color.float32[3] = 1.0f; clearValues[1].depthStencil.depth = 1.0f; clearValues[1].depthStencil.stencil = 0; renderPassBeginInfo.pClearValues = clearValues; renderPassBeginInfo.clearValueCount = 2; vkCmdBeginRenderPass(cmd, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); { // Bind the mPipeline state vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, mPipeline); VkViewport vp; VkRect2D sc; vp.x = 0; vp.y = 0; vp.height = (float)(m_height); vp.width = (float)(m_width); vp.minDepth = 0.0f; vp.maxDepth = 1.0f; sc.offset.x = 0; sc.offset.y = 0; sc.extent.width = vp.width; sc.extent.height = vp.height; vkCmdSetViewport(cmd, 0, 1, &vp); vkCmdSetScissor(cmd, 0, 1, &sc); mMesh.draw(mPipelineLayout, cmd); } vkCmdEndRenderPass(cmd); vk().submitMainCommandBuffer(); }
void CalculateAngleDerivativesE2(vtkPolyData* mesh, const arma::sp_mat& edge_lengths, const arma::vec& radii) { //calculate the power center for each triangle as follows //for 2 circles centered at c1 and c2 solve for p: //|p-c1|^2 - r1^2 = |p-c2|^2 - r2^2 -- this is power line //add one more condition for the third circle to get one point // Funny part: the paper says that the circle of radius sqrt(|c1-p|^2 - r1^2) // is perpendicular to all three circles. But this works only when the 3 circles do not intersect const int npts = mesh->GetNumberOfPoints(); const int ncells = mesh->GetNumberOfCells(); vtkSmartPointer<vtkIdList> cellIdList = vtkSmartPointer<vtkIdList>::New(); vtkSmartPointer<vtkIdList> pointIdList = vtkSmartPointer<vtkIdList>::New(); //for every face calculate gamma_ijk arma::vec gamma_percell(ncells); for (vtkIdType i = 0; i < ncells; i++) { pointIdList->Reset(); mesh->GetCellPoints(cellIdList->GetId(i), pointIdList); const int ind_i = pointIdList->GetId(0); const int ind_j = pointIdList->GetId(1); const int ind_k = pointIdList->GetId(2); const double ri = radii[ind_i]; const double rj = radii[ind_j]; const double rk = radii[ind_k]; arma::vec ci(3), cj(3), ck(3); mesh->GetPoint(ind_i, ci.memptr()); mesh->GetPoint(ind_j, cj.memptr()); mesh->GetPoint(ind_k, ck.memptr()); //create local 2D coordinates, and map vertices arma::vec vji = cj - ci; arma::vec v = ck - ci; arma::vec n = arma::cross(vji, v); //normal arma::vec vki = arma::cross(n, vji); //creates local y' axis const double lenX = arma::norm(vji,2); const double lenY = arma::norm(vki,2); vji /= lenX; //normalize axes vki /= lenY; //2D coordinates arma::vec vi(3,0); //(0,0) arma::vec vj(3); arma::vec vk(3); vj(0) = lenX; vj(1) = 0.0; vk(0) = arma::dot(v, vji); vk(1) = arma::dot(v, vki); //form matrices for system of linear equations and solve //A = [2*(c2-c1)'; 2*(c2-c3)']; //B = [ c2'*c2 - c1'*c1 + r1^2 - r2^2; c2'*c2 - c3'*c3 + r3^2 - r2^2]; //P = A\B; arma::mat A(2,2); arma::vec B(2); A.insert_rows( 0, 2.0*(cj-ci) ); A.insert_rows( 1, 2.0*(cj-ck) ); B(0) = arma::dot(cj, cj) - arma::dot(ci, ci) + ri*ri - rj*rj; B(1) = arma::dot(cj, cj) - arma::dot(ck, ck) + rk*rk - rj*rj; arma::vec P = arma::solve( A, B ); //calculate distance from P to all the edges: hi, hj, hk //http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html const double hi = arma::norm( arma::cross(cj-ck, ck-P), 2 ) / arma::norm(cj-ck, 2); const double hj = arma::norm( arma::cross(ci-ck, ck-P), 2 ) / arma::norm(ci-ck, 2); const double hk = arma::norm( arma::cross(cj-ci, ci-P), 2 ) / arma::norm(cj-ci, 2); const double li = edge_lengths(ind_j, ind_k); const double lj = edge_lengths(ind_i, ind_k); const double lk = edge_lengths(ind_i, ind_j); const double dTi_duj = hk/lk; const double dTj_duk = hi/li; const double dTk_dui = hj/lj; const double dTi_dui = - dTi_duj - dTk_dui; const double dTj_duj = - dTj_duk - dTi_duj; const double dTk_duk = - dTj_duk - dTk_dui; } }
int Stokhos::GMRESDivisionExpansionStrategy<ordinal_type,value_type,node_type>:: GMRES(const Teuchos::SerialDenseMatrix<int, double> & A, Teuchos::SerialDenseMatrix<int,double> & X, const Teuchos::SerialDenseMatrix<int,double> & B, int max_iter, double tolerance, int prec_iter, int order, int dim, int PrecNum, const Teuchos::SerialDenseMatrix<int, double> & M, int diag) { int n = A.numRows(); int k = 1; double resid; Teuchos::SerialDenseMatrix<int, double> P(n,n); Teuchos::SerialDenseMatrix<int, double> Ax(n,1); Ax.multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS,1.0, A, X, 0.0); Teuchos::SerialDenseMatrix<int, double> r0(B); r0-=Ax; resid=r0.normFrobenius(); //define vector v=r/norm(r) where r=b-Ax Teuchos::SerialDenseMatrix<int, double> v(n,1); r0.scale(1/resid); Teuchos::SerialDenseMatrix<int, double> h(1,1); //Matrix of orthog basis vectors V Teuchos::SerialDenseMatrix<int, double> V(n,1); //Set v=r0/norm(r0) to be 1st col of V for (int i=0; i<n; i++) { V(i,0)=r0(i,0); } //right hand side Teuchos::SerialDenseMatrix<int, double> bb(1,1); bb(0,0)=resid; Teuchos::SerialDenseMatrix<int, double> w(n,1); Teuchos::SerialDenseMatrix<int, double> c; Teuchos::SerialDenseMatrix<int, double> s; while (resid > tolerance && k < max_iter) { h.reshape(k+1,k); //Arnoldi iteration(Gram-Schmidt ) V.reshape(n,k+1); //set vk to be kth col of V Teuchos::SerialDenseMatrix<int, double> vk(Teuchos::Copy, V, n,1,0,k-1); //Preconditioning step: solve Mz=vk Teuchos::SerialDenseMatrix<int, double> z(vk); if (PrecNum == 1) { Stokhos::DiagPreconditioner precond(M); precond.ApplyInverse(vk,z,prec_iter); } else if (PrecNum == 2) { Stokhos::JacobiPreconditioner precond(M); precond.ApplyInverse(vk,z,2); } else if (PrecNum == 3) { Stokhos::GSPreconditioner precond(M,1); precond.ApplyInverse(vk,z,1); } else if (PrecNum == 4) { Stokhos::SchurPreconditioner precond(M, order, dim, diag); precond.ApplyInverse(vk,z,prec_iter); } w.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1, A, z, 0.0); Teuchos::SerialDenseMatrix<int, double> vi(n,1); Teuchos::SerialDenseMatrix<int, double> ip(1,1); for (int i=0; i<k; i++) { //set vi to be ith col of V Teuchos::SerialDenseMatrix<int, double> vi(Teuchos::Copy, V, n,1,0,i); //Calculate inner product ip.multiply(Teuchos::TRANS, Teuchos::NO_TRANS, 1.0, vi, w, 0.0); h(i,k-1)= ip(0,0); //scale vi by h(i,k-1) vi.scale(ip(0,0)); w-=vi; } h(k,k-1)=w.normFrobenius(); w.scale(1.0/h(k,k-1)); //add column vk+1=w to V for (int i=0; i<n; i++) { V(i,k)=w(i,0); } //Solve upper hessenberg least squares problem via Givens rotations //Compute previous Givens rotations for (int i=0; i<k-1; i++) { double q=c(i,0)*h(i,k-1)+s(i,0)*h(i+1,k-1); h(i+1,k-1)=-1*s(i,0)*h(i,k-1)+c(i,0)*h(i+1,k-1); h(i,k-1)=q; } //Compute next Givens rotations c.reshape(k,1); s.reshape(k,1); bb.reshape(k+1,1); double l = sqrt(h(k-1,k-1)*h(k-1,k-1)+h(k,k-1)*h(k,k-1)); c(k-1,0)=h(k-1,k-1)/l; s(k-1,0)=h(k,k-1)/l; // Givens rotation on h and bb h(k-1,k-1)=l; h(k,k-1)=0; bb(k,0)=-s(k-1,0)*bb(k-1,0); bb(k-1,0)=c(k-1,0)*bb(k-1,0); //Determine residual resid = fabs(bb(k,0)); k++; } //Extract upper triangular square matrix bb.reshape(h.numRows()-1 ,1); //Solve linear system int info; Teuchos::LAPACK<int, double> lapack; lapack.TRTRS('U', 'N', 'N', h.numRows()-1, 1, h.values(), h.stride(), bb.values(), bb.stride(),&info); Teuchos::SerialDenseMatrix<int, double> ans(X); V.reshape(n,k-1); ans.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, V, bb, 0.0); if (PrecNum == 1) { Stokhos::DiagPreconditioner precond(M); precond.ApplyInverse(ans,ans,prec_iter); } else if (PrecNum == 2) { Stokhos::JacobiPreconditioner precond(M); precond.ApplyInverse(ans,ans,2); } else if (PrecNum == 3) { Stokhos::GSPreconditioner precond(M,1); precond.ApplyInverse(ans,ans,1); } else if (PrecNum == 4) { Stokhos::SchurPreconditioner precond(M, order, dim, diag); precond.ApplyInverse(ans,ans,prec_iter); } X+=ans; std::cout << "iteration count= " << k-1 << std::endl; return 0; }
//GMRES int gmres(const Teuchos::SerialDenseMatrix<int, double> & A, Teuchos::SerialDenseMatrix<int,double> X,const Teuchos::SerialDenseMatrix<int,double> & B, int max_iter, double tolerance) { int n; int k; double resid; k=1; n=A.numRows(); std::cout << "A= " << A << std::endl; std::cout << "B= " << B << std::endl; //Teuchos::SerialDenseMatrix<int, double> Ax(n,1); //Ax.multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS,1.0, A, X, 0.0); Teuchos::SerialDenseMatrix<int, double> r0(B); //r0-=Ax; resid=r0.normFrobenius(); std::cout << "resid= " << resid << std::endl; //define vector v=r/norm(r) where r=b-Ax r0.scale(1/resid); Teuchos::SerialDenseMatrix<int, double> h(1,1); //Matrix of orthog basis vectors V Teuchos::SerialDenseMatrix<int, double> V(n,1); //Set v=r0/norm(r0) to be 1st col of V for (int i=0; i<n; i++){ V(i,0)=r0(i,0); } //right hand side Teuchos::SerialDenseMatrix<int, double> bb(1,1); bb(0,0)=resid; Teuchos::SerialDenseMatrix<int, double> w(n,1); Teuchos::SerialDenseMatrix<int, double> c; Teuchos::SerialDenseMatrix<int, double> s; while (resid > tolerance && k < max_iter){ std::cout << "k = " << k << std::endl; h.reshape(k+1,k); //Arnoldi iteration(Gram-Schmidt ) V.reshape(n,k+1); //set vk to be kth col of V Teuchos::SerialDenseMatrix<int, double> vk(Teuchos::Copy, V, n,1,0,k-1); w.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, A, vk, 0.0); Teuchos::SerialDenseMatrix<int, double> vi(n,1); Teuchos::SerialDenseMatrix<int, double> ip(1,1); for (int i=0; i<k; i++){ //set vi to be ith col of V Teuchos::SerialDenseMatrix<int, double> vi(Teuchos::Copy, V, n,1,0,i); //Calculate inner product ip.multiply(Teuchos::TRANS, Teuchos::NO_TRANS, 1.0, vi, w, 0.0); h(i,k-1)= ip(0,0); //scale vi by h(i,k-1) vi.scale(ip(0,0)); w-=vi; } h(k,k-1)=w.normFrobenius(); w.scale(1.0/w.normFrobenius()); //add column vk+1=w to V for (int i=0; i<n; i++){ V(i,k)=w(i,0); } //Solve upper hessenberg least squares problem via Givens rotations //Compute previous Givens rotations for (int i=0; i<k-1; i++){ // double hi=h(i,k-1); // double hi1=h(i+1,k-1); // h(i,k-1)=c(i,0)*h(i,k-1)+s(i,0)*h(i+1,k-1); // h(i+1,k-1)=-1*s(i,0)*h(i,k-1)+c(i,0)*h(i+1,k-1); // h(i,k-1)=c(i,0)*hi+s(i,0)*hi1; // h(i+1,k-1)=-1*s(i,0)*hi+c(i,0)*hi1; double q=c(i,0)*h(i,k-1)+s(i,0)*h(i+1,k-1); h(i+1,k-1)=-1*s(i,0)*h(i,k-1)+c(i,0)*h(i+1,k-1); h(i,k-1)=q; } //Compute next Givens rotations c.reshape(k,1); s.reshape(k,1); bb.reshape(k+1,1); double l = sqrt(h(k-1,k-1)*h(k-1,k-1)+h(k,k-1)*h(k,k-1)); c(k-1,0)=h(k-1,k-1)/l; s(k-1,0)=h(k,k-1)/l; std::cout << "c " << c(k-1,0)<<std::endl; std::cout << "s " << s(k-1,0)<<std::endl; // Givens rotation on h and bb // h(k-1,k-1)=l; // h(k,k-1)=0; double hk=h(k,k-1); double hk1=h(k-1,k-1); h(k-1,k-1)=c(k-1,0)*hk1+s(k-1,0)*hk; h(k,k-1)=-1*s(k-1,0)*hk1+c(k-1,0)*hk; std::cout << "l = " << l <<std::endl; std::cout << "h(k-1,k-1) = should be l " << h(k-1,k-1) <<std::endl; std::cout << "h(k,k-1) = should be 0 " << h(k,k-1) <<std::endl; bb(k,0)=-1*s(k-1,0)*bb(k-1,0); bb(k-1,0)=c(k-1,0)*bb(k-1,0); //Determine residual resid =fabs(bb(k,0)); std::cout << "resid = " << resid <<std::endl; k++; } //Extract upper triangular square matrix bb.reshape(h.numRows()-1 ,1); //Solve linear system int info; std::cout << "bb pre solve = " << bb << std::endl; std::cout << "h= " << h << std::endl; Teuchos::LAPACK<int, double> lapack; lapack.TRTRS('U', 'N', 'N', h.numRows()-1, 1, h.values(), h.stride(), bb.values(), bb.stride(),&info); V.reshape(n,k-1); std::cout << "V= " << V << std::endl; std::cout << "y= " << bb << std::endl; X.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, V, bb, 1.0); std::cout << "X= " << X << std::endl; //Check V is orthogoanl // Teuchos::SerialDenseMatrix<int, double> vtv(V); // vtv.multiply(Teuchos::TRANS, Teuchos::NO_TRANS, 1.0, V, V, 0.0); // std::cout << "Vtv" << vtv << std::endl; return 0; }
//Mean-Based Preconditioned GMRES int pregmres(const Teuchos::SerialDenseMatrix<int, double> & A, const Teuchos::SerialDenseMatrix<int,double> & X,const Teuchos::SerialDenseMatrix<int,double> & B, int max_iter, double tolerance) { int n; int k; double resid; k=1; n=A.numRows(); std::cout << A << std::endl; Teuchos::SerialDenseMatrix<int, double> D(n,1); //Get diagonal entries of A for (int i=0; i<n; i++){ D(i,0)=A(i,i); } Teuchos::SerialDenseMatrix<int, double> Ax(n,1); Ax.multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS,1.0, A, X, 0.0); Teuchos::SerialDenseMatrix<int, double> r0(B); r0-=Ax; resid=r0.normFrobenius(); //define vector v=r/norm(r) where r=b-Ax Teuchos::SerialDenseMatrix<int, double> v(n,1); r0.scale(1/resid); Teuchos::SerialDenseMatrix<int, double> h(1,1); //Matrix of orthog basis vectors V Teuchos::SerialDenseMatrix<int, double> V(n,1); //Set v=r0/norm(r0) to be 1st col of V for (int i=0; i<n; i++){ V(i,0)=r0(i,0); } //right hand side Teuchos::SerialDenseMatrix<int, double> bb(1,1); bb(0,0)=resid; Teuchos::SerialDenseMatrix<int, double> w(n,1); Teuchos::SerialDenseMatrix<int, double> c; Teuchos::SerialDenseMatrix<int, double> s; while (resid > tolerance && k < max_iter){ std::cout << "k = " << k << std::endl; h.reshape(k+1,k); //Arnoldi iteration(Gram-Schmidt ) V.reshape(n,k+1); //set vk to be kth col of V Teuchos::SerialDenseMatrix<int, double> vk(Teuchos::Copy, V, n,1,0,k-1); //Preconditioning step w=AMj(-1)vj w.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1/D(k-1,0), A, vk, 0.0); Teuchos::SerialDenseMatrix<int, double> vi(n,1); Teuchos::SerialDenseMatrix<int, double> ip(1,1); for (int i=0; i<k; i++){ //set vi to be ith col of V Teuchos::SerialDenseMatrix<int, double> vi(Teuchos::Copy, V, n,1,0,i); //Calculate inner product ip.multiply(Teuchos::TRANS, Teuchos::NO_TRANS, 1.0, vi, w, 0.0); h(i,k-1)= ip(0,0); //scale vi by h(i,k-1) vi.scale(ip(0,0)); w-=vi; } h(k,k-1)=w.normFrobenius(); w.scale(1.0/w.normFrobenius()); //add column vk+1=w to V for (int i=0; i<n; i++){ V(i,k)=w(i,0); } //Solve upper hessenberg least squares problem via Givens rotations //Compute previous Givens rotations for (int i=0; i<k-1; i++){ h(i,k-1)=c(i,0)*h(i,k-1)+s(i,0)*h(i+1,k-1); h(i+1,k-1)=-s(i,0)*h(i,k-1)+c(i,0)*h(i+1,k-1); } //Compute next Givens rotations c.reshape(k,1); s.reshape(k,1); bb.reshape(k+1,1); double l = sqrt(h(k-1,k-1)*h(k-1,k-1)+h(k,k-1)*h(k,k-1)); c(k-1,0)=h(k-1,k-1)/l; s(k-1,0)=h(k,k-1)/l; std::cout <<" h(k,k-1)= " << h(k,k-1) << std::endl; // Givens rotation on h and bb h(k-1,k-1)=l; h(k,k-1)=0; bb(k-1,0)=c(k-1,0)*bb(k-1,0); bb(k,0)=-s(k-1,0)*bb(k-1,0); //Determine residual resid = fabs(bb(k,0)); std::cout << "resid = " << resid << std::endl; k=k+1; } //Extract upper triangular square matrix bb.reshape(h.numRows()-1 ,1); //Solve linear system int info; Teuchos::LAPACK<int, double> lapack; lapack.TRTRS('U', 'N', 'N', h.numRows()-1, 1, h.values(), h.stride(), bb.values(), bb.stride(),&info); //Found y=Mx for (int i=0; i<k-1; i++){ bb(i,0)=bb(i,0)/D(i,0); } V.reshape(n,k-1); Teuchos::SerialDenseMatrix<int, double> ans(X); ans.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, V, bb, 1.0); std::cout << "ans= " << ans << std::endl; std::cout << "h= " << h << std::endl; return 0; }