// DPOSV uses Cholesky factorization A=U^T*U, A=L*L^T // to compute the solution to a real system of linear // equations A*X=B, where A is a square, (N,N) symmetric // positive definite matrix and X and B are (N,NRHS). //--------------------------------------------------------- void umSOLVE_CH(const DMat& mat, const DVec& b, DVec& x) //--------------------------------------------------------- { // check args assert(mat.is_square()); // symmetric assert(b.size() >= mat.num_rows()); // is b consistent? assert(b.size() <= x.size()); // can x store solution? DMat A(mat); // work with copy of input x = b; // allocate solution vector int rows=A.num_rows(), LDA=A.num_rows(), cols=A.num_cols(); int LDB=b.size(), NRHS=1, info=0; if (rows<1) {umWARNING("umSOLVE_CH()", "system is empty"); return;} // Solve the system. POSV('U', rows, NRHS, A.data(), LDA, x.data(), LDB, info); if (info < 0) { x = 0.0; umERROR("umSOLVE_CH(A,b, x)", "Error in input argument (%d)\nNo solution computed.", -info); } else if (info > 0) { x = 0.0; umERROR("umSOLVE_CH(A,b, x)", "\nINFO = %d. The leading minor of order %d of A" "\nis not positive definite, so the factorization" "\ncould not be completed. No solution computed.", info, info); } }
// compute eigensystem of a real symmetric matrix //--------------------------------------------------------- void eig_sym(const DMat& A, DVec& ev, DMat& Q, bool bDoEVecs) //--------------------------------------------------------- { if (!A.is_square()) { umERROR("eig_sym(A)", "matrix is not square."); } int N = A.num_rows(); int LDA=N, LDVL=N, LDVR=N, ldwork=10*N, info=0; DVec work(ldwork, 0.0, OBJ_temp, "work_TMP"); Q = A; // Calculate eigenvectors in Q (optional) ev.resize(N); // Calculate eigenvalues in ev char jobV = bDoEVecs ? 'V' : 'N'; SYEV (jobV,'U', N, Q.data(), LDA, ev.data(), work.data(), ldwork, info); if (info < 0) { umERROR("eig_sym(A, Re,Im)", "Error in input argument (%d)\nNo solution computed.", -info); } else if (info > 0) { umLOG(1, "eig_sym(A, W): ...\n" "\nthe algorithm failed to converge;" "\n%d off-diagonal elements of an intermediate" "\ntridiagonal form did not converge to zero.\n", info); } }
//--------------------------------------------------------- void eig(const DMat& A, DVec& Re, DMat& VL, DMat& VR, bool bL, bool bR) //--------------------------------------------------------- { // Compute eigensystem of a real general matrix // Currently NOT returning imaginary components static DMat B; if (!A.is_square()) { umERROR("eig(A)", "matrix is not square."); } int N = A.num_rows(); int LDA=N, LDVL=N, LDVR=N, ldwork=10*N, info=0; Re.resize(N); // store REAL components of eigenvalues in Re VL.resize(N,N); // storage for LEFT eigenvectors VR.resize(N,N); // storage for RIGHT eigenvectors DVec Im(N); // NOT returning imaginary components DVec work(ldwork, 0.0); // Work on a copy of A B = A; char jobL = bL ? 'V' : 'N'; // calc LEFT eigenvectors? char jobR = bR ? 'V' : 'N'; // calc RIGHT eigenvectors? GEEV (jobL,jobR, N, B.data(), LDA, Re.data(), Im.data(), VL.data(), LDVL, VR.data(), LDVR, work.data(), ldwork, info); if (info < 0) { umERROR("eig(A, Re,Im)", "Error in input argument (%d)\nNo solution computed.", -info); } else if (info > 0) { umLOG(1, "eig(A, Re,Im): ...\n" "\nThe QR algorithm failed to compute all the" "\neigenvalues, and no eigenvectors have been" "\ncomputed; elements %d+1:N of WR and WI contain" "\neigenvalues which have converged.\n", info); } #if (0) // Return (Re,Imag) parts of eigenvalues as columns of Ev Ev.resize(N,2); Ev.set_col(1, Re); Ev.set_col(2, Im); #endif #ifdef _DEBUG //##################################################### // Check for imaginary components in eigenvalues //##################################################### double im_max = Im.max_val_abs(); if (im_max > 1e-6) { umERROR("eig(A)", "imaginary components in eigenvalues."); } //##################################################### #endif }
// access ////////////////////////////////////////////////////////////////////// void TabFunction::setData(const DVec &x, const DVec &y) { if (x.size() != y.size()) { LATAN_ERROR(Size, "tabulated function x/y data size mismatch"); } FOR_VEC(x, i) { value_[x(i)] = y(i); }
int main(int argc, char *argv[]) { // preprocess data NodeFiles node_files; std::vector<Block> col_blocks; // std::vector<string> in_files; AssignData(&node_files, &col_blocks); // PreprocessData(node_files, col_blocks, &in_files); std::vector<string> in_files = node_files[FLAGS_my_row_rank]; int nf = in_files.size(); Block col = col_blocks[FLAGS_my_col_rank]; RSpMat<size_t> tmp; RSpMat<uint32_t> *adjs = new RSpMat<uint32_t>[nf]; for (int i = 0; i < nf; ++i) { // load data tmp.Load(in_files[i]); tmp.VSlice(col, adjs+i); } // do actual computing // w = alpha * X * w + (1-alpha)*1/n // TODO CHECK_EQ(nf, 1); CHECK(adjs[0].square()); int f = 0; uint32_t* index = adjs[f].index(); size_t* offset = adjs[f].offset(); double penalty = (1 - FLAGS_alpha) / (double) adjs[f].rows(); DVec w = DVec::Ones(col.size()) * penalty; DVec u(adjs[f].rows()); for (int it = 0; it < 40; ++it) { for (size_t i = 0; i < adjs[f].rows(); ++i) { double v = 0; double degree = offset[i+1] - offset[i]; for (uint32_t j = offset[i]; j < offset[i+1]; ++j) { v += w[index[j]] / degree; } u[i] = v*FLAGS_alpha + penalty; } LL << "iter " << it << " err " << (u-w).norm() / w.norm() << " 1-norm " << w.cwiseAbs().sum(); w = u; } return 0; }
//--------------------------------------------------------- void CurvedINS2D::INScylinderBC2D ( const DVec& xin, // [in] const DVec& yin, // [in] const DVec& nxi, // [in] const DVec& nyi, // [in] const IVec& MAPI, // [in] const IVec& MAPO, // [in] const IVec& MAPW, // [in] const IVec& MAPC, // [in] double ti, // [in] double nu, // [in] DVec& BCUX, // [out] DVec& BCUY, // [out] DVec& BCPR, // [out] DVec& BCDUNDT // [out] ) //--------------------------------------------------------- { // function [bcUx, bcUy, bcPR, bcdUndt] = INScylinderBC2D(x, y, nx, ny, mapI, mapO, mapW, mapC, time, nu) // Purpose: evaluate boundary conditions for channel bounded cylinder flow with walls at y=+/- .15 // TEST CASE: from // V. John "Reference values for drag and lift of a two-dimensional time dependent flow around a cylinder", // Int. J. Numer. Meth. Fluids 44, 777 - 788, 2004 DVec yI("yI"); int len = xin.size(); BCUX.resize(len); BCUY.resize(len); // resize result arrays BCPR.resize(len); BCDUNDT.resize(len); // and set to zero // inflow #ifdef _MSC_VER yI = yin(MAPI); yI += 0.20; #else int Ni=MAPI.size(); yI.resize(Ni); for(int n=1;n<=Ni;++n) {yI(n)=yin(MAPI(n))+0.2;} #endif BCUX(MAPI) = SQ(1.0/0.41)*6.0 * yI.dm(0.41 - yI); BCUY(MAPI) = 0.0; BCDUNDT(MAPI) = -SQ(1.0/0.41)*6.0 * yI.dm(0.41 - yI); // wall BCUX(MAPW) = 0.0; BCUY(MAPW) = 0.0; // cylinder BCUX(MAPC) = 0.0; BCUY(MAPC) = 0.0; // outflow BCUX(MAPO) = 0.0; BCUY(MAPO) = 0.0; BCDUNDT(MAPO) = 0.0; }
//--------------------------------------------------------- DVec& lu_solve(DMat& LU, const DVec& b) //--------------------------------------------------------- { // Solve a linear system using lu-factored square matrix. DVec *x = new DVec("x", OBJ_temp); try { LU.solve_LU(b, (*x), false, false); } catch(...) { x->Free(); } return (*x); }
DVec OCCFace::inertia() { DVec ret; GProp_GProps prop; BRepGProp::SurfaceProperties(this->getShape(), prop); gp_Mat mat = prop.MatrixOfInertia(); ret.push_back(mat(1,1)); // Ixx ret.push_back(mat(2,2)); // Iyy ret.push_back(mat(3,3)); // Izz ret.push_back(mat(1,2)); // Ixy ret.push_back(mat(1,3)); // Ixz ret.push_back(mat(2,3)); // Iyz return ret; }
// load {R,S} output nodes void OutputSampleNodes2D(int sample_N, DVec &R, DVec &S) { int Npts = OutputSampleNpts2D(sample_N); R.resize(Npts); S.resize(Npts); double denom = (double)(sample_N); int sampleNq = sample_N+1; for (int sk=0, i=0; i<sampleNq; ++i) { for (int j=0; j<sampleNq-i; ++j, ++sk) { R[sk] = -1. + (2.*j)/denom; S[sk] = -1. + (2.*i)/denom; } } }
//--------------------------------------------------------- void GradSimplex3DP ( const DVec& a, // [in] const DVec& b, // [in] const DVec& c, // [in] int id, // [in] int jd, // [in] int kd, // [in] DVec& V3Dr, // [out] DVec& V3Ds, // [out] DVec& V3Dt // [out] ) //--------------------------------------------------------- { // function [V3Dr, V3Ds, V3Dt] = GradSimplex3DP(a,b,c,id,jd,kd) // Purpose: Return the derivatives of the modal basis (id,jd,kd) // on the 3D simplex at (a,b,c) DVec fa, dfa, gb, dgb, hc, dhc, tmp; fa = JacobiP(a,0,0,id); dfa = GradJacobiP(a,0,0,id); gb = JacobiP(b,2*id+1,0,jd); dgb = GradJacobiP(b,2*id+1,0,jd); hc = JacobiP(c,2*(id+jd)+2,0,kd); dhc = GradJacobiP(c,2*(id+jd)+2,0,kd); // r-derivative V3Dr = dfa.dm(gb.dm(hc)); if (id>0) { V3Dr *= pow(0.5*(1.0-b), double(id-1)); } if (id+jd>0) { V3Dr *= pow(0.5*(1.0-c), double(id+jd-1)); } // s-derivative V3Ds = V3Dr.dm(0.5*(1.0+a)); tmp = dgb.dm(pow(0.5*(1.0-b), double(id))); if (id>0) { tmp -= (0.5*id) * gb.dm(pow(0.5*(1.0-b), (id-1.0))); } if (id+jd>0) { tmp *= pow(0.5*(1.0-c),(id+jd-1.0)); } tmp = fa.dm(tmp.dm(hc)); V3Ds += tmp; // t-derivative V3Dt = V3Dr.dm(0.5*(1.0+a)) + tmp.dm(0.5*(1.0+b)); tmp = dhc.dm(pow(0.5*(1.0-c), double(id+jd))); if (id+jd>0) { tmp -= (0.5*(id+jd)) * hc.dm(pow(0.5*(1.0-c), (id+jd-1.0))); } tmp = fa.dm(gb.dm(tmp)); tmp *= pow(0.5*(1.0-b), double(id)); V3Dt += tmp; // normalize double fac = pow(2.0, (2.0*id + jd+1.5)); V3Dr *= fac; V3Ds *= fac; V3Dt *= fac; }
//--------------------------------------------------------- void xyztorst ( const DVec& X, // [in] const DVec& Y, // [in] const DVec& Z, // [in] DVec& r, // [out] DVec& s, // [out] DVec& t // [out] ) //--------------------------------------------------------- { // function [r,s,t] = xyztorst(x, y, z) // Purpose : Transfer from (x,y,z) in equilateral tetrahedron // to (r,s,t) coordinates in standard tetrahedron double sqrt3=sqrt(3.0), sqrt6=sqrt(6.0); int Nc=X.size(); DVec v1(3),v2(3),v3(3),v4(3); DMat tmat1(3,Nc), A(3,3), rhs; v1(1)=(-1.0); v1(2)=(-1.0/sqrt3); v1(3)=(-1.0/sqrt6); v2(1)=( 1.0); v2(2)=(-1.0/sqrt3); v2(3)=(-1.0/sqrt6); v3(1)=( 0.0); v3(2)=( 2.0/sqrt3); v3(3)=(-1.0/sqrt6); v4(1)=( 0.0); v4(2)=( 0.0 ); v4(3)=( 3.0/sqrt6); // back out right tet nodes tmat1.set_row(1,X); tmat1.set_row(2,Y); tmat1.set_row(3,Z); rhs = tmat1 - 0.5*outer(v2+v3+v4-v1, ones(Nc)); A.set_col(1,0.5*(v2-v1)); A.set_col(2,0.5*(v3-v1)); A.set_col(3,0.5*(v4-v1)); DMat RST = A|rhs; r=RST.get_row(1); s=RST.get_row(2); t=RST.get_row(3); }
//================================================================== bool ParamsFindP( ParamList ¶ms, const SymbolList &globalSymbols, DVec<Float3> &out_vectorP, int fromIdx ) { bool gotP = false; for (size_t i=fromIdx; i < params.size(); i += 2) { DASSERT( params[i].type == Param::STR ); const Symbol* pSymbol = globalSymbols.FindSymbol( params[i] ); if ( pSymbol && pSymbol->IsName( "P" ) ) { DASSTHROW( (i+1) < params.size(), "Invalid number of arguments" ); const FltVec &fltVec = params[ i+1 ].NumVec(); DASSTHROW( (fltVec.size() % 3) == 0, "Invalid number of arguments" ); out_vectorP.resize( fltVec.size() / 3 ); for (size_t iv=0, id=0; iv < fltVec.size(); iv += 3) out_vectorP[id++] = Float3( &fltVec[ iv ] ); return true; } } return false; }
//--------------------------------------------------------- void Maxwell2D::SetIC() //--------------------------------------------------------- { // Set initial conditions for simulation mmode = 1.0; nmode = 1.0; //Ez = sin(mmode*pi*x) .* sin(nmode*pi*y); DVec tsinx = apply(sin, (mmode*pi*x)), tsiny = apply(sin, (nmode*pi*y)); Ezinit = tsinx.dm(tsiny); Ez = Ezinit; Hx = 0.0; Hy = 0.0; }
//================================================================== void MemFile::InitExclusiveOwenership( DVec<U8> &fromData ) { mDataSize = fromData.size(); mOwnData.get_ownership( fromData ); mpData = &mOwnData[0]; mReadPos = 0; mIsReadOnly = true; }
//--------------------------------------------------------- void Poly3D::AddPoint(const DVec& point) //--------------------------------------------------------- { if (!HavePoint(point)) { // append this point m_xyz.append_col(3, (double*)point.data()); ++m_N; } }
//--------------------------------------------------------- DVec& chol_solve(const DMat& ch, const DVec& b) //--------------------------------------------------------- { // Solves a linear system using Cholesky-factored // symmetric positive-definite matrix, A = U^T U. if (FACT_CHOL != ch.get_factmode()) {umERROR("chol_solve(ch,b)", "matrix is not factored.");} int M=ch.num_rows(), lda=ch.num_rows(); int nrhs=1, ldb=b.size(); assert(ldb == M); char uplo = 'U'; int info=0; double* ch_data = const_cast<double*>(ch.data()); // copy RHS into x, then overwrite x with solution DVec* x = new DVec(b, OBJ_temp); POTRS (uplo, M, nrhs, ch_data, lda, x->data(), ldb, info); if (info) { umERROR("chol_solve(ch,b)", "dpotrs reports: info = %d", info); } return (*x); }
BasicStats::BasicStats(DVec &raw) { this->n = raw.size(); double fcount = raw.size(); DVec data(raw.begin(), raw.end()); std::sort(data.begin(), data.end()); this->min = data.front(); this->max = data.back(); this->median = data.at(data.size() / 2); double s = 0; for (decimal_t v : data) s += v; this->mean = s / fcount; s = 0; for (decimal_t v : data) s += ::pow(v - this->mean, 2); this->stdev = ::sqrt(s / fcount); }
// Computes an SVD factorization of a real MxN matrix. // Returns the vector of singular values. // Also, factors U, VT, where A = U * D * VT. //--------------------------------------------------------- DVec& svd ( const DMat& mat, // [in] DMat& U, // [out: left singular vectors] DMat& VT, // [out: right singular vectors] char ju, // [in: want U?] char jvt // [in: want VT?] ) //--------------------------------------------------------- { // Work with a copy of the input matrix. DMat A(mat, OBJ_temp, "svd.TMP"); // A(MxN) int m=A.num_rows(), n=A.num_cols(); int mmn=A.min_mn(), xmn=A.max_mn(); // resize parameters U.resize (m,m, true, 0.0); VT.resize(n,n, true, 0.0); DVec* s = new DVec(mmn, 0.0, OBJ_temp, "s.TMP"); char jobu = ju; char jobvt = jvt; int info = 0; // NBN: ACML does not use the work vector. int lwork = 2 * std::max(3*mmn+xmn, 5*mmn); DVec work(lwork, 0.0, OBJ_temp, "work.TMP"); GESVD (jobu, jobvt, m, n, A.data(), m, s->data(), U.data(), m, VT.data(), n, work.data(), lwork, info); if (info < 0) { (*s) = 0.0; umERROR("SVD", "Error in input argument (%d)\nNo solution computed.", -info); } else if (info > 0) { (*s) = 0.0; umLOG(1, "DBDSQR did not converge." "\n%d superdiagonals of an intermediate bidiagonal" "\nform B did not converge to zero.\n", info); } return (*s); }
//--------------------------------------------------------- bool isInf(const DVec& V) //--------------------------------------------------------- { for (int i=1; i<=V.size(); ++i) { double Vi = V(i); if (isinf(Vi)) { return true; // v has a non-finite element } } return false; // all elements are finite }
//--------------------------------------------------------- void MaxwellCurved2D::SetIC() //--------------------------------------------------------- { #if (0) // NBN: to compare with base version // Maxwell2D::SetIC(); // return; #endif // Set initial conditions for simulation // First 6 modes of eigenmodes with 6 azimuthal periods m_alpha.resize(6); m_alpha(1) = 9.936109524217684; m_alpha(2) = 13.589290170541217; m_alpha(3) = 17.003819667816014; m_alpha(4) = 20.320789213566506; m_alpha(5) = 23.586084435581391; m_alpha(6) = 26.820151983411403; // this configuration has an analytic solution // Note: analytic sol. depends on m_Ezinit: m_bHasAnalyticSol = true; // choose radial mode alpha0 = m_alpha(2); m_theta = atan2(y,x); m_rad = sqrt(sqr(x) + sqr(y)); //Ez = besselj(6, alpha0*rad).*cos(6*theta); DVec tbsslj = besselj(6, alpha0 * m_rad); DVec tcosth = apply(cos, (6.0 * m_theta)); Ezinit = tbsslj.dm(tcosth); Ez = Ezinit; Hx = 0.0; Hy = 0.0; }
//--------------------------------------------------------- bool Poly3D::HavePoint(const DVec& point) //--------------------------------------------------------- { const double* p1 = point.data(); double tol = 1e-6, normi=0.0; DVec pnti,tv; for (int i=1; i<=m_N; ++i) { const double* p2 = m_xyz.pCol(i); normi = sqrt( SQ(p1[0]-p2[0]) + SQ(p1[1]-p2[1]) + SQ(p1[2]-p2[2]) ); if (normi < tol) { return true; } } return false; }
//--------------------------------------------------------- void VertexAngles ( const DVec& x1, const DVec& x2, const DVec& x3, const DVec& y1, const DVec& y2, const DVec& y3, DVec& a1, DVec& a2, DVec& a3 ) //--------------------------------------------------------- { //------------------------------------------ // Expand definitions from ElmTools //------------------------------------------ // a1 = acos ( -a23() / sqrt(a22()*a33()) ); // a2 = acos ( -a13() / sqrt(a11()*a33()) ); // a3 = acos ( -a12() / sqrt(a11()*a22()) ); DVec g2x=(y3-y1), g2y=(x1-x3), g3x=(y1-y2), g3y=(x2-x1); DVec det = g3y*g2x - g3x*g2y; DVec d = 1.0/det; g2x *= d; g2y *= d; g3x *= d; g3y *= d; DVec g1x = - g2x - g3x; DVec g1y = - g2y - g3y; a1 = acos( -(g2x*g3x + g2y*g3y) / sqrt((sqr(g2x)+sqr(g2y)) * (sqr(g3x)+sqr(g3y)) )); a2 = acos( -(g1x*g3x + g1y*g3y) / sqrt((sqr(g1x)+sqr(g1y)) * (sqr(g3x)+sqr(g3y)) )); a3 = acos( -(g1x*g2x + g1y*g2y) / sqrt((sqr(g1x)+sqr(g1y)) * (sqr(g2x)+sqr(g2y)) )); #if (0) // check that the angles in each element sum to 180 int Ni = x1.size(); double sum=0.0; umMSG(1, "\nChecking sum of angles in %d element\n", Ni); for (int i=1; i<=Ni; ++i) { sum = fabs(a1(i)) + fabs(a2(i)) + fabs(a3(i)); if ( fabs(sum-M_PI) > 1e-15) { umMSG(1, "element %4d: %12.5e\n", i, fabs(sum-M_PI)); } } #endif }
//================================================================== bool GetServersList( int argc, char **argv, DVec<RRL::NET::Server> &list ) { list.clear(); for (int i=1; i < argc; ++i) { if ( 0 == strcasecmp( "-server", argv[i] ) ) { if ( (i+1) >= argc ) { printf( "Missing server definition.\n" ); return false; } RRL::NET::Server &servEntry = Dgrow( list ); char *pContext = NULL; char *pToken = strtok_r( argv[i+1], ":", &pContext ); if ( pToken ) { servEntry.mAddressName = pToken; if ( pToken = strtok_r( NULL, ":", &pContext ) ) { servEntry.mPortToCall = atoi( pToken ); if ( servEntry.mPortToCall <= 0 && servEntry.mPortToCall >= 65536 ) { printf( "Invalid port range.\n" ); return false; } } } else servEntry.mAddressName = argv[i+1]; i += 1; } } return true; }
// DGESV computes the solution to a real system of linear // equations, A*x = b, where A is an N-by-N matrix, and // x and b are N-by-1 vectors. The LU decomposition // with partial pivoting and row interchanges is used to // factor A as A = P*L*U, where P is a permutation matrix, // L is unit lower triangular, and U is upper triangular. // The system is solved using this factored form of A. //--------------------------------------------------------- void umSOLVE(const DMat& mat, const DVec& b, DVec& x) //--------------------------------------------------------- { // Work with copies of input arrays. DMat A(mat); x = b; int NRHS = 1; int LDA = A.num_rows(); int rows = A.num_rows(); int cols = A.num_cols(); int info = 0; if (rows != cols) { umERROR("umSOLVE(DMat, DVec)", "Matrix A (%d,%d) is not square.\n" "For a Least-Squares solution, see umSOLVE_LS(A,B).", rows, cols); } if (rows < 1) { umLOG(1, "Empty system passed into umSOLVE().\n"); return; } IVec ipiv(rows, 0); GESV (rows, NRHS, A.data(), LDA, ipiv.data(), x.data(), rows, info); if (info < 0) { x = 0.0; umERROR("umSOLVE(DMat&, DVec&)", "Error in input argument (%d)\nNo solution computed.", -info); } else if (info > 0) { x = 0.0; umERROR("umSOLVE(DMat&, DVec&)", "\nINFO = %d. U(%d,%d) was exactly zero." "\nThe factorization has been completed, but the factor U is " "\nexactly singular, so the solution could not be computed.", info, info, info); } }
//--------------------------------------------------------- void CurvedINS2D::INSLiftDrag2D(double ra) //--------------------------------------------------------- { // function [Cd, Cl, dP, sw, stri] = INSLiftDrag2D(Ux, Uy, PR, ra, nu, time, tstep, Nsteps) // Purpose: compute coefficients of lift, drag and pressure drop at cylinder static FILE* fid; static DVec sw1, sw2; static int Nc=0, stri1=0, stri2=0; if (1 == tstep) { char buf[50]; sprintf(buf, "liftdraghistory%d.dat", N); fid = fopen(buf, "w"); fprintf(fid, "timeCdCldP = [...\n"); // Sample location and weights for pressure drop // Note: the VolkerCylinder test assumes the 2 // sample points (-ra, 0), (ra, 0), are vertices // located on the internal cylinder boundary Sample2D(-ra, 0.0, sw1, stri1); Sample2D( ra, 0.0, sw2, stri2); Nc = mapC.size()/Nfp; } bool bDo=false; if (1==tstep || tstep==Nsteps || !umMOD(tstep,10)) { bDo=true; } else if (time > 3.90 && time < 3.96) { bDo=true; } // catch C_d (3.9362) else if (time > 5.65 && time < 5.73) { bDo=true; } // catch C_l (5.6925) else if (time > 7.999) { bDo=true; } // catch dP (8.0) if (!bDo) return; DVec PRC, nxC, nyC, wv, tv; DMat dUxdx,dUxdy, dUydx,dUydy, MM1D, V1D; DMat dUxdxC,dUxdyC, dUydxC,dUydyC, hforce, vforce, sJC; double dP=0.0, Cd=0.0, Cl=0.0; dP = sw1.inner(PR(All,stri1)) - sw2.inner(PR(All,stri2)); // compute derivatives Grad2D(Ux, dUxdx,dUxdy); dUxdxC=dUxdx(vmapC); dUxdyC=dUxdy(vmapC); Grad2D(Uy, dUydx,dUydy); dUydxC=dUydx(vmapC); dUydyC=dUydy(vmapC); PRC=PR(vmapC); nxC=nx(mapC); nyC=ny(mapC); sJC=sJ(mapC); hforce = -PRC.dm(nxC) + nu*( nxC.dm( 2.0 *dUxdxC) + nyC.dm(dUydxC+dUxdyC) ); vforce = -PRC.dm(nyC) + nu*( nxC.dm(dUydxC+dUxdyC) + nyC.dm( 2.0 *dUydyC) ); hforce.reshape(Nfp, Nc); vforce.reshape(Nfp, Nc); sJC .reshape(Nfp, Nc); // compute weights for integrating (1,hforce) and (1,vforce) V1D = Vandermonde1D(N, r(Fmask(All,1))); MM1D = trans(inv(V1D)) / V1D; wv = MM1D.col_sums(); tv = wv*(sJC.dm(hforce)); Cd=tv.sum(); // Compute drag coefficient tv = wv*(sJC.dm(vforce)); Cl=tv.sum(); // Compute lift coefficient // Output answers for plotting fprintf(fid, "%15.14f %15.14f %15.14f %15.14f;...\n", time, Cd/ra, Cl/ra, dP); fflush(fid); // LOG report if (1==tstep || tstep==Nsteps || !umMOD(tstep,Nreport)) { umLOG(1, "%7d %6.3lf %9.5lf %10.6lf %9.5lf\n", tstep, time, Cd/ra, Cl/ra, dP); } if (tstep==Nsteps) { fprintf(fid, "];\n"); fclose(fid); fid=NULL; } }
//--------------------------------------------------------- void CurvedINS2D::KovasznayBC2D ( const DVec& xin, // [in] const DVec& yin, // [in] const DVec& nxi, // [in] const DVec& nyi, // [in] const IVec& MAPI, // [in] const IVec& MAPO, // [in] const IVec& MAPW, // [in] const IVec& MAPC, // [in] double ti, // [in] double nu, // [in] DVec& BCUX, // [out] DVec& BCUY, // [out] DVec& BCPR, // [out] DVec& BCDUNDT // [out] ) //--------------------------------------------------------- { // function [bcUx, bcUy, bcPR, bcdUndt] = KovasznayBC2D(x, y, nx, ny, MAPI, MAPO, MAPW, MAPC, time, nu) // Purpose: evaluate boundary conditions for Kovasznay flow static DVec xI("xI"), yI("yI"), xO("xO"), yO("yO"); int len = xin.size(); BCUX.resize(len); BCUY.resize(len); // resize result arrays BCPR.resize(len); BCDUNDT.resize(len); // and set to zero double lam = (0.5/nu) - sqrt( (0.25/SQ(nu)) + 4.0*SQ(pi)); // inflow #ifdef _MSC_VER xI = xin(MAPI); yI = yin(MAPI); #else int Ni=MAPI.size(), n=0; xI.resize(Ni); yI.resize(Ni); for (n=1;n<=Ni;++n) {xI(n)=xin(MAPI(n));} for (n=1;n<=Ni;++n) {yI(n)=yin(MAPI(n));} #endif DVec elamXI = exp(lam*xI), twopiyI = 2.0*pi*yI; BCUX(MAPI) = 1.0 - elamXI.dm(cos(twopiyI)); BCUY(MAPI) = (0.5*lam/pi) * elamXI.dm(sin(twopiyI)); // outflow #ifdef _MSC_VER xO = xin(MAPO); yO = yin(MAPO); #else int No=MAPO.size(); xO.resize(No); yO.resize(No); for (n=1;n<=No;++n) {xO(n)=xin(MAPO(n));} for (n=1;n<=No;++n) {yO(n)=yin(MAPO(n));} #endif DVec elamXO = exp(lam*xO), twopiyO = 2.0*pi*yO; BCPR (MAPO) = 0.5*(1.0-exp(2.0*lam*xO)); //BCDUNDT(MAPO) = -lam*elamXO.dm(cos(twopiyO)); if (0) { // THIS WORKED BCUX(MAPO) = 1.0 - elamXO.dm(cos(twopiyO)); BCUY(MAPO) = (0.5*lam/pi) * elamXO.dm(sin(twopiyO)); } else { // Neumann data for each velocity BCUX(MAPO) = -lam* elamXO.dm(cos(twopiyO)); BCUY(MAPO) = lam*(0.5*lam/pi) * elamXO.dm(sin(twopiyO)); } }
//--------------------------------------------------------- void umPOLISH(DVec& V, double eps) //--------------------------------------------------------- { // round elements close to certain values int N = V.size(); double *p = V.data(); for (int i=0; i<N; ++i) { if (fabs(p[i]) < eps) { p[i] = 0.0; } else { if (p[i] > 0.0) { // check for proximity to certain positive values if (fabs (p[i] - 0.10) < eps) { p[i] = 0.10; } else if (fabs (p[i] - 0.20) < eps) { p[i] = 0.20; } else if (fabs (p[i] - 0.25) < eps) { p[i] = 0.25; } else if (fabs (p[i] - 0.50) < eps) { p[i] = 0.50; } else if (fabs (p[i] - 0.75) < eps) { p[i] = 0.75; } else if (fabs (p[i] - 0.80) < eps) { p[i] = 0.80; } else if (fabs (p[i] - 0.90) < eps) { p[i] = 0.90; } else if (fabs (p[i] - 1.00) < eps) { p[i] = 1.00; } else if (fabs (p[i] - 2.00) < eps) { p[i] = 2.00; } else if (fabs (p[i] - 4.00) < eps) { p[i] = 4.00; } else if (fabs (p[i] - 4.50) < eps) { p[i] = 4.50; } else if (fabs (p[i] - 5.00) < eps) { p[i] = 5.00; } else if (fabs (p[i] - M_PI ) < eps) { p[i] = M_PI ; } else if (fabs (p[i] - M_PI_2) < eps) { p[i] = M_PI_2; } else if (fabs (p[i] - M_PI_4) < eps) { p[i] = M_PI_4; } else if (fabs (p[i] - M_E ) < eps) { p[i] = M_E ; } } else { // check for proximity to certain negative values if (fabs (p[i] + 0.10) < eps) { p[i] = -0.10; } else if (fabs (p[i] + 0.20) < eps) { p[i] = -0.20; } else if (fabs (p[i] + 0.25) < eps) { p[i] = -0.25; } else if (fabs (p[i] + 0.50) < eps) { p[i] = -0.50; } else if (fabs (p[i] + 0.75) < eps) { p[i] = -0.75; } else if (fabs (p[i] + 0.80) < eps) { p[i] = -0.80; } else if (fabs (p[i] + 0.90) < eps) { p[i] = -0.90; } else if (fabs (p[i] + 1.00) < eps) { p[i] = -1.00; } else if (fabs (p[i] + 2.00) < eps) { p[i] = -2.00; } else if (fabs (p[i] + 4.00) < eps) { p[i] = -4.00; } else if (fabs (p[i] + 4.50) < eps) { p[i] = -4.50; } else if (fabs (p[i] + 5.00) < eps) { p[i] = -5.00; } else if (fabs (p[i] + M_PI ) < eps) { p[i] = -M_PI ; } else if (fabs (p[i] + M_PI_2) < eps) { p[i] = -M_PI_2; } else if (fabs (p[i] + M_PI_4) < eps) { p[i] = -M_PI_4; } else if (fabs (p[i] + M_E ) < eps) { p[i] = -M_E ; } } } } }
//================================================================== bool FileManagerAndroid::GrabFile(const char* pFileName, DVec<U8> &out_data, const char* pMode) { DFUNCTION(); DLOG("File: %s", pFileName); #if defined(ANDROID) bool prefs = isPrefsMode( pMode ); if (prefs) { // Read from the prefs storage DLOG("Grabbing file (prefs): %s", pFileName); char *contents = ::DoReadPrefs(pFileName); if (0 == contents) { DLOG("Failed to open the file"); return false; } // Decode into binary size_t binLength = 0; void *binData = FromBase64(contents, &binLength); free(contents); if (binData == 0) { DPRINT("!! File '%s' was not base64 format. Rejecting.", pFileName); return false; } // Copy into output array and free original // TODO: Could be sped up by pre-calculating length and not // copying. out_data.resize(binLength); memcpy(&out_data[0], binData, binLength); free(binData); DLOG("copied %d bytes", binLength); return true; } ::FileLoad(pFileName); int datasize; void *data; if (!FileGetData(pFileName, &data, &datasize)) { //DASSTHROW(0, ("failed loading file '%s'", pFileName)); return false; } out_data.resize(datasize); DVERBOSE("GrabFile: memcpy-ing '%s', %d bytes", pFileName, datasize); memcpy(&out_data[0], data, datasize); FileRemove(pFileName); DVERBOSE("GrabFile: released"); #endif return true; }
// function call /////////////////////////////////////////////////////////////// double DoubleModel::operator()(const DVec &arg, const DVec &par) const { checkSize(arg.size(), par.size()); return (*this)(arg.data(), par.data()); }
int OCCEdge::createNURBS(OCCVertex *start, OCCVertex *end, std::vector<OCCStruct3d> points, DVec knots, DVec weights, IVec mult) { try { Standard_Boolean periodic = false; int vertices = 0; if (start != NULL && end != NULL) { vertices = 2; periodic = true; } int nbControlPoints = points.size() + vertices; TColgp_Array1OfPnt ctrlPoints(1, nbControlPoints); TColStd_Array1OfReal _knots(1, knots.size()); TColStd_Array1OfReal _weights(1, weights.size()); TColStd_Array1OfInteger _mult(1, mult.size()); for (unsigned i = 0; i < knots.size(); i++) { _knots.SetValue(i+1, knots[i]); } for (unsigned i = 0; i < weights.size(); i++) { _weights.SetValue(i+1, weights[i]); } int totKnots = 0; for (unsigned i = 0; i < mult.size(); i++) { _mult.SetValue(i+1, mult[i]); totKnots += mult[i]; } const int degree = totKnots - nbControlPoints - 1; int index = 1; if (!periodic) { ctrlPoints.SetValue(index++, gp_Pnt(start->X(), start->Y(), start->Z())); } for (unsigned i = 0; i < points.size(); i++) { gp_Pnt aP(points[i].x,points[i].y,points[i].z); ctrlPoints.SetValue(index++, aP); } if (!periodic) { ctrlPoints.SetValue(index++, gp_Pnt(end->X(), end->Y(), end->Z())); } Handle(Geom_BSplineCurve) NURBS = new Geom_BSplineCurve (ctrlPoints, _weights, _knots, _mult, degree, periodic); if (!periodic) { this->setShape(BRepBuilderAPI_MakeEdge(NURBS, start->vertex, end->vertex)); } else { this->setShape(BRepBuilderAPI_MakeEdge(NURBS)); } } catch(Standard_Failure &err) { Handle_Standard_Failure e = Standard_Failure::Caught(); const Standard_CString msg = e->GetMessageString(); if (msg != NULL && strlen(msg) > 1) { setErrorMessage(msg); } else { setErrorMessage("Failed to create nurbs"); } return 1; } return 0; }