void VanderPolME2::solve() { if (mpd_->appExistsOnProcess(eq2_)) { // take a tentative time step TEUCHOS_ASSERT(true); tentativeTime_ = currentTime_ + currentTimeStepSize_; // Solve for x2 at new time using Newton's method with backward // Euler. //const double& t = tentativeTime_; const double relTol = 1.0e-5; const double absTol = 1.0e-8; bool converged = false; const int maxIters = 20; int iter = 0; double f = computeF(x2_); *mpd_->getApplicationOStream(eq2_) << "Solving for x2_ in me2, x1 = " << p_x1_ << std::endl; *mpd_->getApplicationOStream(eq2_) << iter << ": x2=" << x2_ << ", ||f||=" << std::abs(f) << std::endl; while (!converged && (iter < maxIters) ) { double dx = -f / computeJ(x2_); x2_ = dx + x2_; f = computeF(x2_); ++iter; if ( (std::abs(dx) < relTol) && (std::abs(f) < absTol) ) converged = true; *mpd_->getApplicationOStream(eq2_) << iter << ": x2=" << x2_ << ", ||f||=" << std::abs(f) << std::endl; } if (converged) isLocallyConverged_ = true; else isLocallyConverged_ = false; *mpd_->getApplicationOStream(eq2_) << "me2::solve(), x2_ = " << x2_ << std::endl; } Teuchos::broadcast(*mpd_->getGlobalComm(), 1, const_cast<double*>(&x2_)); // Send convergence status from proc 1 where eq 2 lives to all // procs. Use int for mpi commmunication. int zeroMeansConverged = isLocallyConverged_ ? 0 : 1; Teuchos::broadcast(*mpd_->getGlobalComm(), 1, Teuchos::ptrFromRef(zeroMeansConverged)); isLocallyConverged_ = (zeroMeansConverged == 0); sovledTentativeStep_ = true; }
NOX::Abstract::Group::ReturnType LOCA::Homotopy::Group::computeNewton(Teuchos::ParameterList& params) { if (isValidNewton) return NOX::Abstract::Group::Ok; string callingFunction = "LOCA::Homotopy::Group::computeNewton()"; NOX::Abstract::Group::ReturnType status, finalStatus; if (newtonVecPtr == Teuchos::null) newtonVecPtr = gVecPtr->clone(NOX::ShapeCopy); finalStatus = computeF(); globalData->locaErrorCheck->checkReturnType(finalStatus, callingFunction); status = computeJacobian(); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); status = applyJacobianInverse(params, *gVecPtr, *newtonVecPtr); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); newtonVecPtr->scale(-1.0); isValidNewton = true; return finalStatus; }
NOX::Abstract::Group::ReturnType LOCA::Homotopy::Group::computeGradient() { if (isValidGradient) return NOX::Abstract::Group::Ok; string callingFunction = "LOCA::Homotopy::Group::computeGradient()"; NOX::Abstract::Group::ReturnType status, finalStatus; if (gradVecPtr == Teuchos::null) gradVecPtr = gVecPtr->clone(NOX::ShapeCopy); finalStatus = computeF(); globalData->locaErrorCheck->checkReturnType(finalStatus, callingFunction); status = computeJacobian(); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); status = applyJacobianTranspose(*gVecPtr, *gradVecPtr); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); return finalStatus; }
/** * Performs the kalman filter prediction step */ void PointCell::predict() { stateCopy = stateVector; computeF(stateVector); computeCovarianceF(stateVector, F); P = F*P*F.getTranspose() + Q; }
void DerivativeFunctionMaterialBase::computeProperties() { for (_qp = 0; _qp < _qrule->n_points(); _qp++) { // set function value if (_prop_F) (*_prop_F)[_qp] = computeF(); for (unsigned int i = 0; i < _nargs; ++i) { // set first derivatives if (_prop_dF[i]) (*_prop_dF[i])[_qp] = computeDF(_arg_numbers[i]); // second derivatives for (unsigned int j = i; j < _nargs; ++j) { if (_prop_d2F[i][j]) (*_prop_d2F[i][j])[_qp] = computeD2F(_arg_numbers[i], _arg_numbers[j]); // third derivatives if (_third_derivatives) { for (unsigned int k = j; k < _nargs; ++k) if (_prop_d3F[i][j][k]) (*_prop_d3F[i][j][k])[_qp] = computeD3F(_arg_numbers[i], _arg_numbers[j], _arg_numbers[k]); } } } } }
NOX::Abstract::Group::ReturnType LOCA::Homotopy::DeflatedGroup:: computeNewton(Teuchos::ParameterList& params) { if (isValidNewton) return NOX::Abstract::Group::Ok; string callingFunction = "LOCA::Homotopy::DeflatedGroup::computeNewton()"; NOX::Abstract::Group::ReturnType finalStatus = NOX::Abstract::Group::Ok; NOX::Abstract::Group::ReturnType status; // Make sure F is valid if (!isF()) { status = computeF(); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); } // Make sure Jacobian is valid if (!isJacobian()) { status = computeJacobian(); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); } // zero out newton vec -- used as initial guess for some linear solvers newtonMultiVec.init(0.0); status = applyJacobianInverseMultiVector(params, fMultiVec, newtonMultiVec); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); newtonMultiVec.scale(-1.0); isValidNewton = true; return finalStatus; }
NOX::Abstract::Group::ReturnType LOCA::TurningPoint::MooreSpence::ExtendedGroup::computeNewton( Teuchos::ParameterList& params) { if (isValidNewton) return NOX::Abstract::Group::Ok; string callingFunction = "LOCA::TurningPoint::MooreSpence::ExtendedGroup::computeNewton()"; NOX::Abstract::Group::ReturnType finalStatus = NOX::Abstract::Group::Ok; NOX::Abstract::Group::ReturnType status; // Make sure F is valid if (!isF()) { status = computeF(); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); } // Make sure Jacobian is valid if (!isJacobian()) { status = computeJacobian(); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); } // zero out newton vec -- used as initial guess for some linear solvers newtonMultiVec.init(0.0); // solve using contiguous status = solverStrategy->solve(params, *ffMultiVec, newtonMultiVec); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); newtonMultiVec.scale(-1.0); isValidNewton = true; return finalStatus; }
void ReducedStVKCubatureForceModel::computeReducedEnergy(const double *q,double &energy) const { energy=0.0; //computeF(reduced_dis);//compute F for all cubica elements for(int cubica_idx=0;cubica_idx<cubica_num_;++cubica_idx) { Mat3d F=computeF(cubica_idx,q); Mat3d I(1.0); Mat3d temp=trans(F)*F; Mat3d E=0.5*(temp-I); double trace_E=E[0][0]+E[1][1]+E[2][2]; double doubleE=0.0; for(int i=0;i<3;++i) for(int j=0;j<3;++j) doubleE+=E[i][j]*E[i][j]; double element_energy=0.5*lamda_*trace_E*trace_E+mu_*doubleE; energy += cubica_weights_[cubica_idx]*element_energy; } }
/* Newton迭代法解非线性方程组 @para x,y,t,u,v,w 是方程中元素 @para M 是最大迭代步数 @para ep 是精度 */ static void newtonMethod(const double * x, const double * y, double * t, double * u, double * v, double * w, int M, double ep) { double xi[4] = { 1.0, 1.0, 1.0, 1.0 }; // 初始迭代向量 double F[4] = { 0.0 }; // F = - F(x) double deta[4] = { 0.0 }; //偏移矢量 // 4 * 4的Jacobi矩阵 double * * J = new double *[4]; for (int i = 0; i < 4; i++) { J[i] = new double[4]; } // 迭代 for (int i = 0; i < M; i++) { // 计算F = -F(x) computeF(F, *x, *y, xi[0], xi[1], xi[2], xi[3]); // 计算Jacobi矩阵 computeJacobi(J, xi[0], xi[1], xi[2], xi[3]); // 计算偏移矢量J * dtea = F gaussElimPiv(4, J, F, deta); // 判断是否得到终值 if (norm(deta) / norm(xi) <= ep) { *t = xi[0]; *u = xi[1]; *v = xi[2]; *w = xi[3]; break; } else { for (int i = 0; i < 4; i++) { xi[i] += deta[i]; } } } // 释放内存 delete[] J; }
NOX::Abstract::Group::ReturnType LOCA::Homotopy::DeflatedGroup:: computeGradient() { if (isValidGradient) return NOX::Abstract::Group::Ok; string callingFunction = "LOCA::Homotopy::DeflatedGroup::computeGradient()"; NOX::Abstract::Group::ReturnType finalStatus = NOX::Abstract::Group::Ok; NOX::Abstract::Group::ReturnType status; // Make sure F is valid if (!isF()) { status = computeF(); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); } // Make sure Jacobian is valid if (!isJacobian()) { status = computeJacobian(); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); } // Compute J^T*f for homotopy group status = applyJacobianTranspose(*fVec, *gradientVec); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); isValidGradient = true; return finalStatus; }
void ReducedStVKCubatureForceModel::computeReducedInternalForce(const double *q,double *forces) const {//q:r*1 // PerformanceCounter counter2; // counter2.StartCounter(); memset(forces,0.0,sizeof(double)*r_); // double total_time=0.0,other_total_time=0.0,F_time=0.0,assemble_f=0.0; for(int cubica_idx=0;cubica_idx<cubica_num_;++cubica_idx) { int ele=cubica_elements_[cubica_idx]; Mat3d F=computeF(cubica_idx,q); Mat3d P=firstPiolaKirchhoff(F); Mat3d temp1=trans(computeDmInv(ele)); Mat3d temp=P*temp1; double *ele_force=new double[12]; memset(ele_force,0.0,sizeof(double)*12); for(int i=0;i<4;++i) { for(int j=0;j<3;++j) { if(i==3) ele_force[3*i+j]=(-1.0)*(temp[j][0]+temp[j][1]+temp[j][2]); else ele_force[3*i+j]=temp[j][i]; } } memset(gf_,0.0,sizeof(double)*r_); for(int i=0;i<r_;++i) for(int j=0;j<12;++j) gf_[i]+=cubica_subBasis_[cubica_idx][j][i]*ele_force[j]; delete[] ele_force; for(int i=0;i<r_;++i) forces[i] += cubica_weights_[cubica_idx]*gf_[i]; } if(add_gravity_) for(int i=0;i<r_;++i) forces[i] -= gravity_force_[i]; // counter2.StopCounter(); // std::cout<<"integrator compute internal force:"<<counter2.GetElapsedTime()<<"\n"; }
/*----------------------------------------------------------------------* | (public) m.gee 01/05| | iterate on f with initial guess x numiter times | | returns absolut value of result in x | | returns f matching x | *----------------------------------------------------------------------*/ bool ML_NOX::ML_Nox_NonlinearLevel::applySmoother(Epetra_Vector* f, Epetra_Vector* x, int numiter) { if (!thislevel_prec_) { cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::applySmoother:\n" << "**ERR**: thislevel_prec_ is NULL\n" << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; } int i; Epetra_Vector out(Copy,*x,0); for (i=0; i<numiter; i++) { thislevel_prec_->ApplyInverse(*f,out); x->Update(1.0,out,1.0); computeF(*x,*f,NOX::EpetraNew::Interface::Required::Residual); } return true; }
int main(int argc, char **argv){ int i,j,value; char *cad1, *cad2; int m,n; PRINT_RESULT = 1; DEBUG=1; // For debugging: //char cadena1[] = "CGAGACGT"; //2,1,0,1,0,2,1,3,3, //char cadena2[] = "AGACTAGTTAC";//0,1,0,2,3,0,1,3,3,0,2,2,0 if (argc<3) { printf("\n Usage: ./cadenas-adn filey.adn filex.adn \n\n"); exit(EXIT_FAILURE); } printf("\n ---------------- Begin of Needleman-Wunsch implementation -------------------------\n"); // char *cad1=readFile(FILENAME1); // char *cad2=readFile(FILENAME2); cad1=readFile(argv[1]); cad2=readFile(argv[2]); m = strlen(cad1)-1; n = strlen(cad2)-1; a = malloc(sizeof(int) * m); b = malloc(sizeof(int) * n); DEBUG=0; if (DEBUG) printf("\n a[]="); for(i=0;i<m;i++){ a[i] = convNucleotido(cad1[i]); if (DEBUG) printf("%d,",a[i]); } if (DEBUG) printf("\n"); if (DEBUG) printf("\n b[]="); for(j=0;j<n;j++){ b[j] = convNucleotido(cad2[j]); if (DEBUG) printf("%d,",b[j]); } if (DEBUG) printf("\n"); //DEBUG=1; int *f = computeF(m+1,n+1); if (DEBUG) printF(f,m,n); genAlignment(f,m+1,n+1); printf("\n ---------------- End of Needleman-Wunsch implementation -------------------------\n"); exit(EXIT_SUCCESS); }
// Receive an unoccupied psi, and split off the computation of all associated f // vectors across the node using CkLoop. void PsiCache::computeFs(PsiMessage* msg) { double start = CmiWallTimer(); if (msg->spin_index != 0) { CkAbort("Error: We don't support multiple spins yet!\n"); } CkAssert(msg->size == psi_size); // Compute ikq index and the associated umklapp factor // TODO: This should just be a table lookup unsigned ikq; int umklapp[3]; kqIndex(msg->k_index, ikq, umklapp); bool uproc = false; if (umklapp[0] != 0 || umklapp[1] != 0 || umklapp[2] != 0) { uproc = true; computeUmklappFactor(umklapp); } GWBSE* gwbse = GWBSE::get(); double*** e_occ = gwbse->gw_epsilon.Eocc; double*** e_occ_shifted = gwbse->gw_epsilon.Eocc_shifted; double*** e_unocc = gwbse->gw_epsilon.Eunocc; // Create the FComputePacket for this set of f vectors and start CkLoop f_packet.size = psi_size; f_packet.unocc_psi = msg->psi; if ( qindex == 0 ) { f_packet.occ_psis = psis_shifted[ikq]; f_packet.e_occ = e_occ_shifted[msg->spin_index][ikq]; } else { f_packet.occ_psis = psis[ikq]; f_packet.e_occ = e_occ[msg->spin_index][ikq]; } f_packet.e_unocc = e_unocc[msg->spin_index][msg->k_index][msg->state_index-L]; f_packet.fs = fs + (L*psi_size*(received_chunks%pipeline_stages)); if (uproc) { f_packet.umklapp_factor = umklapp_factor; } else { f_packet.umklapp_factor = NULL; } #ifdef USE_CKLOOP CkLoop_Parallelize(computeF, 1, &f_packet, L, 0, L - 1); #else for (int l = 0; l < L; l++) { computeF(l,l,NULL,1,&f_packet); } #endif received_chunks++; #ifdef TESTING { FVectorCache *fvec_cache = fvector_cache_proxy.ckLocalBranch(); fvec_cache->computeFTilde(fs); // fvec_cache->applyCutoff(msg->accept_size, msg->accept); // fvec_cache->init(140); //compute ftilde first - similar to ckloop above for all L's fvec_cache->putFVec(msg->state_index-L, fs); } #endif // Let the matrix chares know that the f vectors are ready CkCallback cb(CkReductionTarget(PMatrix, applyFs), pmatrix2D_proxy); contribute(cb); // Cleanup delete msg; total_time += CmiWallTimer() - start; }
NOX::Abstract::Group::ReturnType LOCA::MultiContinuation::ConstrainedGroup::computeGradient() { if (isValidGradient) return NOX::Abstract::Group::Ok; std::string callingFunction = "LOCA::MultiContinuation::ConstrainedGroup::computeGradient()"; NOX::Abstract::Group::ReturnType finalStatus = NOX::Abstract::Group::Ok; NOX::Abstract::Group::ReturnType status; // Make sure F is valid if (!isF()) { status = computeF(); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); } // Make sure Jacobian is valid if (!isJacobian()) { status = computeJacobian(); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); } // Compute underlying gradient if (!grpPtr->isGradient()) { status = grpPtr->computeGradient(); finalStatus = globalData->locaErrorCheck->combineAndCheckReturnTypes(status, finalStatus, callingFunction); } // Get grad f *gradientVec->getXVec() = grpPtr->getGradient(); // compute grad f + dg/dx^T * g constraintsPtr->addDX(Teuchos::TRANS, 1.0, constraintsPtr->getConstraints(), 1.0, *gradientMultiVec.getXMultiVec()); // compute df/dp^T * f ffMultiVec->getXMultiVec()->multiply(1.0, *dfdpMultiVec->getXMultiVec(), *gradientMultiVec.getScalars()); // compute df/dp^T * f + dg/dp^T * g gradientMultiVec.getScalars()->multiply( Teuchos::TRANS, Teuchos::NO_TRANS, 1.0, *dfdpMultiVec->getScalars(), constraintsPtr->getConstraints(), 1.0); isValidGradient = true; return finalStatus; }
void ReducedStVKCubatureForceModel::computeReducedStiffnessMatrix(const double *q,double *reduced_K/*Matrix<double> &reduced_K*/) const { double total_time=0.0,else_time=0.0,other_count_time=0.0,assemble_k=0.0,F_time=0.0,whole_time=0.0; static int count=1; // ++count; Matrix<double> K((int)r_,(int)r_); // PerformanceCounter counter1; // counter1.StartCounter(); // std::cout<<cubica_num_<<"\n"; for(int cubica_idx=0;cubica_idx<cubica_num_;++cubica_idx) { // PerformanceCounter counter21; // counter21.StartCounter(); // // PerformanceCounter counter33; // counter33.StartCounter(); int ele=cubica_elements_[cubica_idx]; Matrix<double> subU=tetSubBasis(cubica_idx);//12*r // counter33.StopCounter(); // else_time+=counter33.GetElapsedTime(); Matrix<double> ele_K(12,12); // PerformanceCounter counter2; // counter2.StartCounter(); Mat3d F=computeF(cubica_idx,q); // counter2.StopCounter(); // F_time+=counter2.GetElapsedTime(); // PerformanceCounter counter3; // counter3.StartCounter(); Mat3d trans_DmInv=trans(computeDmInv(ele)); // counter3.StopCounter(); // std::cout<<"computeK-counter3:"<<counter3.GetElapsedTime()<<"\n"; // PerformanceCounter counter4; // counter4.StartCounter(); for(int i=0;i<4;++i) { std::vector<Mat3d> g_derivative(3);//computes dg/dx_j^0,dg/dx_j^1,dg/dx_j^2 g_derivative.clear(); for(int j=0;j<3;++j) { g_derivative[j].set(0.0); g_derivative[j]=computeP_gradient(ele,F,i,j)*trans_DmInv; } for(int j=0;j<4;++j) { // Mat3d f_derivative(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0); for(int row=0;row<3;++row) { for(int col=0;col<3;++col) { if(j==3) { ele_K(3*i+row,3*j+col)=(-1.0)*(g_derivative[row][col][0]+g_derivative[row][col][1]+g_derivative[row][col][2]); } else ele_K(3*i+row,3*j+col)=g_derivative[row][col][j]; } } } } // counter4.StopCounter(); // assemble_k+=counter4.GetElapsedTime(); // // counter21.StopCounter(); // other_count_time+=counter21.GetElapsedTime(); // PerformanceCounter counter5; // counter5.StartCounter(); // MultiplyMatrix() // modal_matrix_ = new ModalMatrix(volumetric_mesh_->getNumVertices(),r_,U); // modal_matrix_->ProjectMatrix(r_,ele_K,) Matrix<double> temp=subU.MultiplyT(ele_K); // for(int i=0;i<r_;++i) // { // for(int j=0;j<12;++j) // std::cout<<temp(i,j)<<","; // std::cout<<"\n"; // } // std::cout<<"\n"; // std::cout<<"\n"; // if(cubica_idx==0) // getchar(); Matrix<double> temp1=temp*subU; K+=(cubica_weights_[cubica_idx]*temp1); // counter5.StopCounter(); // total_time+=counter5.GetElapsedTime(); } for(int i=0;i<r_;++i) for(int j=0;j<r_;++j) reduced_K[i*r_+j]=K(i,j); // reduced_K(i,j)=K(i,j); // counter1.StopCounter(); // whole_time+=counter1.GetElapsedTime(); // std::cout<<"computeK--F:"<<F_time/count<<"\n"; // std::cout<<"computeK--assemble K:"<<assemble_k/count<<"\n"; // std::cout<<"computeK--else time:"<<else_time/count<<"\n"; // std::cout<<"computeK--other time:"<<other_count_time/count<<"\n"; // // std::cout<<"computeK--multi matrix:"<<total_time/count<<"\n"; // std::cout<<"computeK--for all cubica elements:"<<whole_time/count<<"\n"; }