double testInterplationErr(T* Atree, int Ns, int Nf) { Atree->buildFMMTree(); int i, j, k=0; vector3 source[Ns]; // Position array for the source points vector3 field[Nf]; // Position array for the field points double q[Ns*Atree->dof->s]; // Source array for (i=0;i<Ns;i++) { for (j=0; j<Atree->dof->s; j++, k++){ q[k] = frand(0,1); } } double size = Atree->L/4; //generate random points in well seperated box. for (i=0;i<Ns;i++) { source[i].x = frand(-0.5,0.5)*size - 1.5*size; source[i].y = frand(-0.5,0.5)*size - 1.5*size; source[i].z = frand(-0.5,0.5)*size - 1.5*size; } // Randomly set field points for (i=0;i<Nf;i++) { field[i].x = frand(-0.5,0.5)*size + 0.5*size; field[i].y = frand(-0.5,0.5)*size + 0.5*size; field[i].z = frand(-0.5,0.5)*size + 0.5*size; } double *stress = new double[Nf*Atree->dof->f];// Field array (BBFMM calculation) double *stress_dir = new double[Nf*Atree->dof->f];// Field array (direct O(N^2) calculation) H2_3D_Compute<T> compute(Atree, field, source, Ns, Nf, q,1, stress); DirectCalc3D(Atree, field, Nf, source, q, 1, Ns, Atree->dof,0 ,0, stress_dir); double err = ComputeError(stress,stress_dir,Nf,Atree->dof,1); delete []stress; delete []stress_dir; cout << "Interplation error is: " << err << endl; return err; }
void Legendre::FindPolynomial(DArray &dArray) { DMatrix dMatrix; dMatrix.reserve(nn); dArray.clear(); dArray.reserve(nn); //double dbg[6] = { -.7746, .5556, 0., .8889, .7746, .5556 }; for(int i = 0; i < nn; ++i) { double rat = static_cast<double>(i + 1) / static_cast<double>(nn); dArray.push_back((i % 2) ? 1. - fabs(dArray[i - 1]) / (ul - ll) * 2. : (ll + (ul - ll) * rat)); //dArray.push_back(rat); //dArray.push_back(dbg[i]); cerr << "Initially " << i << ": " << dArray[i] << endl; dMatrix.push_back(dIntegrals); } do { Increment(dMatrix, dArray); SolveSytem(dMatrix, dArray); } while(ComputeError(dArray) > precision); ExtractPolynomial(dArray); }
int main(int argc, char *argv[]) { /**********************************************************/ /* */ /* Initializing the problem */ /* */ /**********************************************************/ double L; // Length of simulation cell (assumed to be a cube) int n; // Number of Chebyshev nodes per dimension doft dof; int Ns; // Number of sources in simulation cell int Nf; // Number of field points in simulation cell int m; int level; double eps; int use_chebyshev = 1; SetMetaData(L, n, dof, Ns, Nf, m, level, eps); vector3* source = new vector3[Ns]; // Position array for the source points vector3* field = new vector3[Nf]; // Position array for the field points double *q = new double[Ns*dof.s*m]; // Source array SetSources(field,Nf,source,Ns,q,m,&dof,L); double err; double *stress = new double[Nf*dof.f*m];// Field array (BBFMM calculation) double *stress_dir = new double[Nf*dof.f*m];// Field array (direct O(N^2) calculation) /**********************************************************/ /* */ /* Fast matrix vector product */ /* */ /**********************************************************/ /***** Pre Computation ******/ clock_t t0 = clock(); myKernel Atree(&dof,L,level, n, eps, use_chebyshev); Atree.buildFMMTree(); clock_t t1 = clock(); double tPre = t1 - t0; /***** FMM Computation *******/ t0 = clock(); H2_3D_Compute<myKernel> compute(&Atree, field, source, Ns, Nf, q,m, stress); t1 = clock(); double tFMM = t1 - t0; /***** output result to binary file ******/ // string outputfilename = "../output/stress.bin"; // write_Into_Binary_File(outputfilename, stress, m*Nf*dof.f); /**********************************************************/ /* */ /* Exact matrix vector product */ /* */ /**********************************************************/ t0 = clock(); DirectCalc3D(&Atree, field, Nf, source, q, m, Ns, &dof,0 , L, stress_dir); t1 = clock(); double tExact = t1 - t0; cout << "Pre-computation time: " << double(tPre) / double(CLOCKS_PER_SEC) << endl; cout << "FMM computing time: " << double(tFMM) / double(CLOCKS_PER_SEC) << endl; cout << "FMM total time: " << double(tFMM+tPre) / double(CLOCKS_PER_SEC) << endl; cout << "Exact computing time: " << double(tExact) / double(CLOCKS_PER_SEC) << endl; //Compute the 2-norm error err = ComputeError(stress,stress_dir,Nf,&dof,m); cout << "Relative Error: " << err << endl; /******* Clean Up *******/ delete []stress; delete []stress_dir; delete []q; delete []source; delete []field; return 0; }
//----------------------------------------------------------------------------- //----------------------------------------------------------------------------- bool CGrabController::UpdateObject( CBasePlayer *pPlayer, float flError ) { CBaseEntity *pEntity = GetAttached(); if ( !pEntity || ComputeError() > flError || pPlayer->GetGroundEntity() == pEntity || !pEntity->VPhysicsGetObject() ) { return false; } //Adrian: Oops, our object became motion disabled, let go! IPhysicsObject *pPhys = pEntity->VPhysicsGetObject(); if ( pPhys && pPhys->IsMoveable() == false ) { return false; } Vector forward, right, up; QAngle playerAngles = pPlayer->EyeAngles(); AngleVectors( playerAngles, &forward, &right, &up ); float pitch = AngleDistance(playerAngles.x,0); if( !m_bAllowObjectOverhead ) { playerAngles.x = clamp( pitch, -75, 75 ); } else { playerAngles.x = clamp( pitch, -90, 75 ); } // Now clamp a sphere of object radius at end to the player's bbox Vector radial = physcollision->CollideGetExtent( pPhys->GetCollide(), vec3_origin, pEntity->GetAbsAngles(), -forward ); Vector player2d = pPlayer->CollisionProp()->OBBMaxs(); float playerRadius = player2d.Length2D(); float radius = playerRadius + fabs(DotProduct( forward, radial )); float distance = 24 + ( radius * 2.0f ); // Add the prop's distance offset distance += m_flDistanceOffset; Vector start = pPlayer->Weapon_ShootPosition(); Vector end = start + ( forward * distance ); trace_t tr; CTraceFilterSkipTwoEntities traceFilter( pPlayer, pEntity, COLLISION_GROUP_NONE ); Ray_t ray; ray.Init( start, end ); enginetrace->TraceRay( ray, MASK_SOLID_BRUSHONLY, &traceFilter, &tr ); if ( tr.fraction < 0.5 ) { end = start + forward * (radius*0.5f); } else if ( tr.fraction <= 1.0f ) { end = start + forward * ( distance - radius ); } Vector playerMins, playerMaxs, nearest; pPlayer->CollisionProp()->WorldSpaceAABB( &playerMins, &playerMaxs ); Vector playerLine = pPlayer->CollisionProp()->WorldSpaceCenter(); CalcClosestPointOnLine( end, playerLine+Vector(0,0,playerMins.z), playerLine+Vector(0,0,playerMaxs.z), nearest, NULL ); if( !m_bAllowObjectOverhead ) { Vector delta = end - nearest; float len = VectorNormalize(delta); if ( len < radius ) { end = nearest + radius * delta; } } //Show overlays of radius if ( g_debug_physcannon.GetBool() ) { NDebugOverlay::Box( end, -Vector( 2,2,2 ), Vector(2,2,2), 0, 255, 0, true, 0 ); NDebugOverlay::Box( GetAttached()->WorldSpaceCenter(), -Vector( radius, radius, radius), Vector( radius, radius, radius ), 255, 0, 0, true, 0.0f ); } QAngle angles = TransformAnglesFromPlayerSpace( m_attachedAnglesPlayerSpace, pPlayer ); // If it has a preferred orientation, update to ensure we're still oriented correctly. Pickup_GetPreferredCarryAngles( pEntity, pPlayer, pPlayer->EntityToWorldTransform(), angles ); // We may be holding a prop that has preferred carry angles if ( m_bHasPreferredCarryAngles ) { matrix3x4_t tmp; ComputePlayerMatrix( pPlayer, tmp ); angles = TransformAnglesToWorldSpace( m_vecPreferredCarryAngles, tmp ); } matrix3x4_t attachedToWorld; Vector offset; AngleMatrix( angles, attachedToWorld ); VectorRotate( m_attachedPositionObjectSpace, attachedToWorld, offset ); SetTargetPosition( end - offset, angles ); return true; }
//Entrena la red con los patrones de entrada y los patrones de salida //en modo de obtener como respuesta los patrones de salida void RedNeuronal::TrainRed(void) { FeedForward(); ComputeError(); Backpropagate(); }
int main(int argc, char** argv) { DM da; PetscErrorCode ierr; Vec x, rhs; Mat A, jac; ierr = PetscInitialize(&argc, &argv, NULL, NULL); CHKERRQ(ierr); ierr = PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "Laplacian in 2D", ""); CHKERRQ(ierr); ierr = PetscOptionsEnd(); CHKERRQ(ierr); ierr = HpddmRegisterKSP(); CHKERRQ(ierr); MPI_Barrier(PETSC_COMM_WORLD); double time = MPI_Wtime(); ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_STAR, 10, 10, PETSC_DECIDE, PETSC_DECIDE, 1, 1, 0, 0, &da); CHKERRQ(ierr); ierr = DMSetFromOptions(da); CHKERRQ(ierr); ierr = DMSetUp(da); CHKERRQ(ierr); ierr = DMCreateGlobalVector(da, &rhs); CHKERRQ(ierr); ierr = DMCreateGlobalVector(da, &x); CHKERRQ(ierr); ierr = DMCreateMatrix(da, &A); CHKERRQ(ierr); ierr = DMCreateMatrix(da, &jac); CHKERRQ(ierr); ierr = ComputeMatrix(da, jac, A); CHKERRQ(ierr); MPI_Barrier(PETSC_COMM_WORLD); time = MPI_Wtime() - time; ierr = PetscPrintf(PETSC_COMM_WORLD, "--- Mat assembly = %f\n", time); CHKERRQ(ierr); MPI_Barrier(PETSC_COMM_WORLD); time = MPI_Wtime(); KSP ksp; ierr = KSPCreate(PETSC_COMM_WORLD, &ksp); CHKERRQ(ierr); ierr = KSPSetDM(ksp, da); CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp); CHKERRQ(ierr); ierr = KSPSetOperators(ksp, A, A); CHKERRQ(ierr); ierr = KSPSetDMActive(ksp, PETSC_FALSE); CHKERRQ(ierr); ierr = KSPSetInitialGuessNonzero(ksp, PETSC_TRUE); CHKERRQ(ierr); ierr = KSPSetUp(ksp); CHKERRQ(ierr); MPI_Barrier(PETSC_COMM_WORLD); time = MPI_Wtime() - time; ierr = PetscPrintf(PETSC_COMM_WORLD, "--- PC setup = %f\n", time); CHKERRQ(ierr); PetscScalar nus[SIZE_ARRAY_NU] = {0.1, 10.0, 0.001, 100.0}; float t_time[SIZE_ARRAY_NU]; int t_its[SIZE_ARRAY_NU]; int i, j; for (j = 0; j < 2; ++j) { { if (j == 1) { ierr = KSPSetType(ksp, "hpddm"); CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp); CHKERRQ(ierr); ierr = VecZeroEntries(x); CHKERRQ(ierr); } ierr = KSPSolve(ksp, rhs, x); CHKERRQ(ierr); if (j == 1) { const HpddmOption* const opt = HpddmOptionGet(); int previous = HpddmOptionVal(opt, "krylov_method"); if (previous == HPDDM_KRYLOV_METHOD_GCRODR || previous == HPDDM_KRYLOV_METHOD_BGCRODR) HpddmDestroyRecycling(); } } for (i = 0; i < SIZE_ARRAY_NU; ++i) { ierr = VecZeroEntries(x); CHKERRQ(ierr); ierr = ComputeRHS(da, rhs, nus[i]); CHKERRQ(ierr); MPI_Barrier(PETSC_COMM_WORLD); time = MPI_Wtime(); ierr = KSPSolve(ksp, rhs, x); CHKERRQ(ierr); MPI_Barrier(PETSC_COMM_WORLD); t_time[i] = MPI_Wtime() - time; PetscInt its; ierr = KSPGetIterationNumber(ksp, &its); CHKERRQ(ierr); t_its[i] = its; ierr = ComputeError(A, rhs, x); CHKERRQ(ierr); } for (i = 0; i < SIZE_ARRAY_NU; ++i) { ierr = PetscPrintf(PETSC_COMM_WORLD, "%d\t%d\t%f\n", i + 1, t_its[i], t_time[i]); CHKERRQ(ierr); if (i > 0) { t_its[0] += t_its[i]; t_time[0] += t_time[i]; } } if (SIZE_ARRAY_NU > 1) { ierr = PetscPrintf(PETSC_COMM_WORLD, "------------------------\n\t%d\t%f\n", t_its[0], t_time[0]); CHKERRQ(ierr); } } ierr = KSPDestroy(&ksp); CHKERRQ(ierr); ierr = VecDestroy(&x); CHKERRQ(ierr); ierr = VecDestroy(&rhs); CHKERRQ(ierr); ierr = MatDestroy(&A); CHKERRQ(ierr); ierr = MatDestroy(&jac); CHKERRQ(ierr); ierr = DMDestroy(&da); CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
int main(int argc, char *argv[]) { /**********************************************************/ /* */ /* Initializing the problem */ /* */ /**********************************************************/ double L; // Length of simulation cell (assumed to be a cube) int n; // Number of Chebyshev nodes per dimension doft dof; int Ns; // Number of sources in simulation cell int Nf; // Number of field points in simulation cell int m; int level; double eps; int use_chebyshev = 1; SetMetaData(L, n, dof, Ns, Nf, m, level, eps); vector3* source = new vector3[Ns]; // Position array for the source points vector3* field = new vector3[Nf]; // Position array for the field points double *q = new double[Ns*dof.s*m]; // Source array SetSources(field,Nf,source,Ns,q,m,&dof,L); double err; double *stress = new double[Nf*dof.f*m];// Field array (BBFMM calculation) double *stress_dir = new double[Nf*dof.f*m];// Field array (direct O(N^2) calculation)*/ /**********************************************************/ /* */ /* Fast matrix vector product */ /* */ /**********************************************************/ /***** Pre Computation ******/ clock_t t0 = clock(); kernel_Gaussian Atree(&dof,L,level, n, eps, use_chebyshev); Atree.buildFMMTree(); clock_t t1 = clock(); double tPre = t1 - t0; /***** FMM Computation *******/ t0 = clock(); H2_3D_Compute<kernel_Gaussian> compute(&Atree, field, source, Ns, Nf, q,m, stress); t1 = clock(); double tFMM = t1 - t0; /***** You can repeat this part with different source, field points and charges *****/ /*vector3* source1 = new vector3[Ns]; // Position array for the source points vector3* field1 = new vector3[Nf]; // Position array for the field points double *q1 = new double[Ns*dof.s*m]; // Source array SetSources(field1,Nf,source1,Ns,q1,m,&dof,L); double *stress1 = new double[Nf*dof.f*m];// Field array (BBFMM calculation) H2_3D_Compute<kernel_LaplacianForce> compute1(&Atree, field1, source1, Ns, Nf, q1,m, stress1);*/ /**** Test interplation error *****/ // kernel_Gaussian testTree(&dof,1.0/4 ,2, n, eps, use_chebyshev); // double errtest = testInterplationErr(&testTree, 100, 100); /***** output result to binary file ******/ // string outputfilename = "../output/stress.bin"; // write_Into_Binary_File(outputfilename, stress, m*Nf*dof.f); /**********************************************************/ /* */ /* Exact matrix vector product */ /* */ /**********************************************************/ // string inputfilename = "../output/stress_dir05gaussian.bin"; // read_Stress(inputfilename, stress_dir, Nf*dof.f*m); t0 = clock(); DirectCalc3D(&Atree, field, Nf, source, q, m, Ns, &dof,0 , L, stress_dir); t1 = clock(); double tExact = t1 - t0; string outputfilename = "../output/stress_dir06gaussian.bin"; write_Into_Binary_File(outputfilename, stress, m*Nf*dof.f); cout << "Pre-computation time: " << double(tPre) / double(CLOCKS_PER_SEC) << endl; cout << "FMM computing time: " << double(tFMM) / double(CLOCKS_PER_SEC) << endl; cout << "FMM total time: " << double(tPre+tFMM) / double(CLOCKS_PER_SEC) << endl; cout << "Exact computing time: " << double(tExact) / double(CLOCKS_PER_SEC) << endl; // Compute the 2-norm error err = ComputeError(stress,stress_dir,Nf,&dof,m); cout << "Relative Error: " << err << endl; /******* Clean Up *******/ delete []stress; delete []stress_dir; delete []source; delete []field; delete []q; //delete []stress1; return 0; }
/** Controls the angle of the X and Y axis. Only one axis will be tilted from the zero position at a time. @param xyCurPos_px The current position of the ball in (x, y) (in pixels) @return cv::Point A vector containing the desired angle for each axis */ cv::Point DualAxisController::AngleControl(cv::Point xyCurPos_px) { //NOTE: AngleControl is called once per frame this->xyCurPos_px = xyCurPos_px; ComputeError(); int xTiltAngle = outputZero; int yTiltAngle = outputZero; /* Like the single axis tilt controller, we want to tilt and wait for specified periods. However, we also only want to control a single axis at a time. */ // If we're already controlling X, keep controlling it. if (controllingX) { // If X is within the minimum position error, stop controlling it, // reset the X timers and do nothing else this frame. if (abs(error.x) <= minimumPositionError) { controllingX = false; this->tiltStartTick = 0; } else { // We're controlling X, it's outside of the minimum position error, // let's move it. xTiltAngle = GetTilt(); } } // else if we're controlling Y, keep controlling it. else if (controllingY) { // If Y is within min pos, stop controlling and reset Y timers. if (abs(error.y) <= minimumPositionError) { controllingY = false; this->tiltStartTick = 0; } else { // Move Y! yTiltAngle = GetTilt(); } } // else if we're controlling neither, see which one (if any) is above the // minimum position error, and start controlling it. else if (abs(error.x) > minimumPositionError) { controllingX = true; xTiltAngle = GetTilt(); } else if (abs(error.y) > minimumPositionError) { controllingY = true; yTiltAngle = GetTilt(); } // else, we're at our desired position within +- the minimum error xTiltAngle = NormalizeAngle(xTiltAngle); yTiltAngle = NormalizeAngle(yTiltAngle); return cv::Point(xTiltAngle, yTiltAngle); }
void mexFunction(int nlhs,mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /**********************************************************/ /* */ /* Initializing the problem */ /* */ /**********************************************************/ #define QH_OUT plhs[0] #define QHexact_OUT plhs[1] #define source_IN prhs[0] #define field_IN prhs[1] #define charge_IN prhs[2] #define nCheb_IN prhs[3] #define level_IN prhs[4] #define L_IN prhs[5] #define use_cheby_IN prhs[6] #define singular_IN prhs[7] // Check number of argument if(nrhs != 7) { mexErrMsgTxt("Wrong number of input arguments"); }else if(nlhs > 2){ mexErrMsgTxt("Too many output arguments"); } if( !IS_REAL_2D_FULL_DOUBLE(source_IN)) { mexErrMsgTxt("Third input argument is not a real 2D full double array."); } if( !IS_REAL_2D_FULL_DOUBLE(field_IN)) { mexErrMsgTxt("Third input argument is not a real 2D full double array."); } if( !IS_REAL_2D_FULL_DOUBLE(charge_IN)) { mexErrMsgTxt("Third input argument is not a real 2D full double array."); } if( !IS_REAL_SCALAR(nCheb_IN)){ mexErrMsgTxt("nChebnotes must be a real double scalar"); } if( !IS_REAL_SCALAR(level_IN)){ mexErrMsgTxt("level must be a real double scalar"); } if( !IS_REAL_SCALAR(L_IN)){ mexErrMsgTxt("L must be a real double scalar"); } if( !IS_REAL_SCALAR(use_cheby_IN)){ mexErrMsgTxt("use_cheby must be a real double scalar"); } double L = *mxGetPr(L_IN); // Length of simulation cell (assumed to be a cube) int n = *mxGetPr(nCheb_IN); // Number of Chebyshev nodes per dimension doft dof; dof.f = 1; dof.s = 1; int Ns = mxGetM(source_IN); // Number of sources in simulation cell int Nf = mxGetM(field_IN); // Number of field points in simulation cell int m = mxGetN(charge_IN); // Number of r.h.s. int level = *mxGetPr(level_IN); double eps = 1e-9; int use_chebyshev = *mxGetPr(use_cheby_IN); vector3* source = new vector3[Ns]; // Position array for the source points vector3* field = new vector3[Nf]; // Position array for the field points read_data(source_IN, field_IN, Ns, Nf, source, field); double * charges; charges = mxGetPr(charge_IN); double err; QH_OUT = mxCreateDoubleMatrix(Nf*dof.f, m, mxREAL); double* stress = mxGetPr(QH_OUT); /**********************************************************/ /* */ /* Fast matrix vector product */ /* */ /**********************************************************/ mexPrintf("\nStarting FMM computation...\n"); /***** Pre Computation ******/ clock_t t0 = clock(); kernel_Exp Atree(&dof,L,level, n, eps, use_chebyshev); //myKernel Atree(&dof,L,level, n, eps, use_chebyshev); Atree.buildFMMTree(); clock_t t1 = clock(); double tPre = t1 - t0; /***** FMM Computation *******/ t0 = clock(); //H2_3D_Compute<myKernel> compute(&Atree, field, source, Ns, Nf, charges,m, stress); H2_3D_Compute<kernel_Exp> compute(&Atree, field, source, Ns, Nf, charges,m, stress); t1 = clock(); double tFMM = t1 - t0; /**********************************************************/ /* */ /* Exact matrix vector product */ /* */ /**********************************************************/ mexPrintf("\nPre-computation time: %.4f\n",double(tPre) / double(CLOCKS_PER_SEC)); mexPrintf("FMM computing time: %.4f\n",double(tFMM) / double(CLOCKS_PER_SEC)); mexPrintf("FMM total time: %.4f\n",double(tFMM+tPre) / double(CLOCKS_PER_SEC)); if (nlhs == 2) { mexPrintf("\nStarting direct computation...\n"); t0 = clock(); QHexact_OUT = mxCreateDoubleMatrix(Nf*dof.f, m, mxREAL); double* stress_dir = mxGetPr(QHexact_OUT); DirectCalc3D(&Atree, field, Nf, source, charges, m, Ns, &dof,0 , L, stress_dir); t1 = clock(); double tExact = t1 - t0; mexPrintf("Exact computing time: %.4f\n",double(tExact) / double(CLOCKS_PER_SEC)); // Compute the 2-norm error err = ComputeError(stress,stress_dir,Nf,&dof,m); mexPrintf("Relative Error: %e\n",err); } /******* Clean Up *******/ delete []source; delete []field; return; }
int main(int argc, char** argv) { PC pc; PetscErrorCode ierr; PetscInt m, nn, M, j, k, ne = 4; PetscReal* coords; Vec x, rhs; Mat A; KSP ksp; PetscMPIInt npe, rank; PetscInitialize(&argc, &argv, NULL, NULL); ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD, &npe); CHKERRQ(ierr); ierr = PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "Linear elasticity in 3D", ""); { char nestring[256]; ierr = PetscSNPrintf(nestring, sizeof nestring, "number of elements in each direction, ne+1 must be a multiple of %D (sizes^{1/3})", (PetscInt)(PetscPowReal((PetscReal)npe, 1.0 / 3.0) + 0.5)); ierr = PetscOptionsInt("-ne", nestring, "", ne, &ne, NULL); } ierr = PetscOptionsEnd(); CHKERRQ(ierr); const HpddmOption* const opt = HpddmOptionGet(); { HpddmOptionParse(opt, argc, argv, rank == 0); if (rank) HpddmOptionRemove(opt, "verbosity"); } nn = ne + 1; M = 3 * nn * nn * nn; if (npe == 2) { if (rank == 1) m = 0; else m = nn * nn * nn; npe = 1; } else { m = nn * nn * nn / npe; if (rank == npe - 1) m = nn * nn * nn - (npe - 1) * m; } m *= 3; ierr = KSPCreate(PETSC_COMM_WORLD, &ksp); CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp); CHKERRQ(ierr); int i; { PetscInt Istart, Iend, jj, ic; const PetscInt NP = (PetscInt)(PetscPowReal((PetscReal)npe, 1.0 / 3.0) + 0.5); const PetscInt ipx = rank % NP, ipy = (rank % (NP * NP)) / NP, ipz = rank / (NP * NP); const PetscInt Ni0 = ipx * (nn / NP), Nj0 = ipy * (nn / NP), Nk0 = ipz * (nn / NP); const PetscInt Ni1 = Ni0 + (m > 0 ? (nn / NP) : 0), Nj1 = Nj0 + (nn / NP), Nk1 = Nk0 + (nn / NP); PetscInt *d_nnz, *o_nnz, osz[4] = {0, 9, 15, 19}, nbc; if (npe != NP * NP * NP) SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "npe=%d: npe^{1/3} must be integer", npe); if (nn != NP * (nn / NP)) SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "-ne %d: (ne+1)%(npe^{1/3}) must equal zero", ne); ierr = PetscMalloc1(m + 1, &d_nnz); CHKERRQ(ierr); ierr = PetscMalloc1(m + 1, &o_nnz); CHKERRQ(ierr); for (i = Ni0, ic = 0; i < Ni1; i++) { for (j = Nj0; j < Nj1; j++) { for (k = Nk0; k < Nk1; k++) { nbc = 0; if (i == Ni0 || i == Ni1 - 1) nbc++; if (j == Nj0 || j == Nj1 - 1) nbc++; if (k == Nk0 || k == Nk1 - 1) nbc++; for (jj = 0; jj < 3; jj++, ic++) { d_nnz[ic] = 3 * (27 - osz[nbc]); o_nnz[ic] = 3 * osz[nbc]; } } } } if (ic != m) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "ic %D does not equal m %D", ic, m); ierr = MatCreate(PETSC_COMM_WORLD, &A); CHKERRQ(ierr); ierr = MatSetSizes(A, m, m, M, M); CHKERRQ(ierr); ierr = MatSetBlockSize(A, 3); CHKERRQ(ierr); ierr = MatSetType(A, MATAIJ); CHKERRQ(ierr); ierr = MatSeqAIJSetPreallocation(A, 0, d_nnz); CHKERRQ(ierr); ierr = MatMPIAIJSetPreallocation(A, 0, d_nnz, 0, o_nnz); CHKERRQ(ierr); ierr = PetscFree(d_nnz); CHKERRQ(ierr); ierr = PetscFree(o_nnz); CHKERRQ(ierr); ierr = MatGetOwnershipRange(A, &Istart, &Iend); CHKERRQ(ierr); if (m != Iend - Istart) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "m %D does not equal Iend %D - Istart %D", m, Iend, Istart); ierr = VecCreate(PETSC_COMM_WORLD, &x); CHKERRQ(ierr); ierr = VecSetSizes(x, m, M); CHKERRQ(ierr); ierr = VecSetBlockSize(x, 3); CHKERRQ(ierr); ierr = VecSetFromOptions(x); CHKERRQ(ierr); ierr = VecDuplicate(x, &rhs); CHKERRQ(ierr); ierr = PetscMalloc1(m + 1, &coords); CHKERRQ(ierr); coords[m] = -99.0; PetscReal h = 1.0 / ne; for (i = Ni0, ic = 0; i < Ni1; i++) { for (j = Nj0; j < Nj1; j++) { for (k = Nk0; k < Nk1; k++, ic++) { coords[3 * ic] = h * (PetscReal)i; coords[3 * ic + 1] = h * (PetscReal)j; coords[3 * ic + 2] = h * (PetscReal)k; } } } } PetscReal s_r[SIZE_ARRAY_R] = {30, 0.1, 20, 10}; PetscReal x_r[SIZE_ARRAY_R] = {0.5, 0.4, 0.4, 0.4}; PetscReal y_r[SIZE_ARRAY_R] = {0.5, 0.5, 0.4, 0.4}; PetscReal z_r[SIZE_ARRAY_R] = {0.5, 0.45, 0.4, 0.35}; PetscReal r[SIZE_ARRAY_R] = {0.5, 0.5, 0.4, 0.4}; AssembleSystem(A, rhs, s_r[0], x_r[0], y_r[0], z_r[0], r[0], ne, npe, rank, nn, m); ierr = KSPSetOperators(ksp, A, A); CHKERRQ(ierr); MatNullSpace matnull; Vec vec_coords; PetscScalar* c; ierr = VecCreate(MPI_COMM_WORLD, &vec_coords); CHKERRQ(ierr); ierr = VecSetBlockSize(vec_coords, 3); CHKERRQ(ierr); ierr = VecSetSizes(vec_coords, m, PETSC_DECIDE); CHKERRQ(ierr); ierr = VecSetUp(vec_coords); CHKERRQ(ierr); ierr = VecGetArray(vec_coords, &c); CHKERRQ(ierr); for (i = 0; i < m; i++) c[i] = coords[i]; ierr = VecRestoreArray(vec_coords, &c); CHKERRQ(ierr); ierr = MatNullSpaceCreateRigidBody(vec_coords, &matnull); CHKERRQ(ierr); ierr = MatSetNearNullSpace(A, matnull); CHKERRQ(ierr); ierr = MatNullSpaceDestroy(&matnull); CHKERRQ(ierr); ierr = VecDestroy(&vec_coords); CHKERRQ(ierr); ierr = KSPSetInitialGuessNonzero(ksp, PETSC_TRUE); CHKERRQ(ierr); MPI_Barrier(PETSC_COMM_WORLD); double time = MPI_Wtime(); ierr = KSPSetUp(ksp); CHKERRQ(ierr); MPI_Barrier(PETSC_COMM_WORLD); time = MPI_Wtime() - time; ierr = PetscPrintf(PETSC_COMM_WORLD, "--- PC setup = %f\n", time); CHKERRQ(ierr); float t_time[SIZE_ARRAY_R]; int t_its[SIZE_ARRAY_R]; { { ierr = KSPSolve(ksp, rhs, x); CHKERRQ(ierr); ierr = KSPReset(ksp); CHKERRQ(ierr); ierr = KSPSetOperators(ksp, A, A); CHKERRQ(ierr); ierr = KSPSetInitialGuessNonzero(ksp, PETSC_TRUE); CHKERRQ(ierr); ierr = KSPSetUp(ksp); CHKERRQ(ierr); } for (i = 0; i < SIZE_ARRAY_R; ++i) { ierr = VecZeroEntries(x); CHKERRQ(ierr); MPI_Barrier(PETSC_COMM_WORLD); time = MPI_Wtime(); ierr = KSPSolve(ksp, rhs, x); CHKERRQ(ierr); MPI_Barrier(PETSC_COMM_WORLD); t_time[i] = MPI_Wtime() - time; PetscInt its; ierr = KSPGetIterationNumber(ksp, &its); CHKERRQ(ierr); t_its[i] = its; ierr = ComputeError(A, rhs, x); CHKERRQ(ierr); if (i == (SIZE_ARRAY_R - 1)) AssembleSystem(A, rhs, s_r[0], x_r[0], y_r[0], z_r[0], r[0], ne, npe, rank, nn, m); else AssembleSystem(A, rhs, s_r[i + 1], x_r[i + 1], y_r[i + 1], z_r[i + 1], r[i + 1], ne, npe, rank, nn, m); ierr = KSPSetOperators(ksp, A, A); CHKERRQ(ierr); ierr = KSPSetUp(ksp); CHKERRQ(ierr); } for (i = 0; i < SIZE_ARRAY_R; ++i) { ierr = PetscPrintf(PETSC_COMM_WORLD, "%d\t%d\t%f\n", i + 1, t_its[i], t_time[i]); CHKERRQ(ierr); if (i > 0) { t_its[0] += t_its[i]; t_time[0] += t_time[i]; } } if (SIZE_ARRAY_R > 1) { ierr = PetscPrintf(PETSC_COMM_WORLD, "------------------------\n\t%d\t%f\n", t_its[0], t_time[0]); CHKERRQ(ierr); } } { ierr = KSPGetPC(ksp, &pc); CHKERRQ(ierr); HpddmCustomOperator H; H._A = A; H._M = pc; H._mv = mv; H._precond = precond; H._b = rhs; H._x = x; int n; MatGetLocalSize(A, &n, NULL); { ierr = VecZeroEntries(x); K* pt_rhs; K* pt_x; VecGetArray(rhs, &pt_rhs); VecGetArray(x, &pt_x); int previous = HpddmOptionVal(opt, "verbosity"); if (previous > 0) HpddmOptionRemove(opt, "verbosity"); HpddmCustomOperatorSolve(&H, n, H._mv, H._precond, pt_rhs, pt_x, 1, &PETSC_COMM_WORLD); if (previous > 0) { char buffer[20]; snprintf(buffer, 20, "%d", previous); char* concat = malloc(strlen("-hpddm_verbosity ") + strlen(buffer) + 1); strcpy(concat, "-hpddm_verbosity "); strcat(concat, buffer); HpddmOptionParseString(opt, concat); free(concat); } VecRestoreArray(x, &pt_x); VecRestoreArray(rhs, &pt_rhs); previous = HpddmOptionVal(opt, "krylov_method"); if(previous == 4 || previous == 5) HpddmDestroyRecycling(); ierr = KSPReset(ksp); CHKERRQ(ierr); ierr = KSPSetOperators(ksp, A, A); CHKERRQ(ierr); ierr = KSPSetInitialGuessNonzero(ksp, PETSC_TRUE); CHKERRQ(ierr); ierr = KSPSetUp(ksp); CHKERRQ(ierr); } for (i = 0; i < SIZE_ARRAY_R; ++i) { ierr = VecZeroEntries(x); CHKERRQ(ierr); K* pt_rhs; K* pt_x; VecGetArray(rhs, &pt_rhs); VecGetArray(x, &pt_x); MPI_Barrier(PETSC_COMM_WORLD); time = MPI_Wtime(); t_its[i] = HpddmCustomOperatorSolve(&H, n, H._mv, H._precond, pt_rhs, pt_x, 1, &PETSC_COMM_WORLD); MPI_Barrier(PETSC_COMM_WORLD); t_time[i] = MPI_Wtime() - time; VecRestoreArray(x, &pt_x); VecRestoreArray(rhs, &pt_rhs); ierr = ComputeError(A, rhs, x); CHKERRQ(ierr); if (i != (SIZE_ARRAY_R - 1)) { AssembleSystem(A, rhs, s_r[i + 1], x_r[i + 1], y_r[i + 1], z_r[i + 1], r[i + 1], ne, npe, rank, nn, m); ierr = KSPSetOperators(ksp, A, A); CHKERRQ(ierr); ierr = KSPSetUp(ksp); CHKERRQ(ierr); } } for (i = 0; i < SIZE_ARRAY_R; ++i) { ierr = PetscPrintf(PETSC_COMM_WORLD, "%d\t%d\t%f\n", i + 1, t_its[i], t_time[i]); CHKERRQ(ierr); if (i > 0) { t_its[0] += t_its[i]; t_time[0] += t_time[i]; } } if (SIZE_ARRAY_R > 1) { ierr = PetscPrintf(PETSC_COMM_WORLD, "------------------------\n\t%d\t%f\n", t_its[0], t_time[0]); CHKERRQ(ierr); } } ierr = KSPDestroy(&ksp); CHKERRQ(ierr); ierr = VecDestroy(&x); CHKERRQ(ierr); ierr = VecDestroy(&rhs); CHKERRQ(ierr); ierr = MatDestroy(&A); CHKERRQ(ierr); ierr = PetscFree(coords); CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
void avtShapeletDecompositionQuery::Execute(vtkDataSet *ds, const int dom) { // clear any prev result if(decompResult) { delete decompResult; decompResult = NULL; } // make sure set is rectilinear if (ds->GetDataObjectType() != VTK_RECTILINEAR_GRID) { EXCEPTION1(VisItException, "The Shapelet Decomposition Query only operates on 2D" " rectilinear grids."); } vtkRectilinearGrid *rgrid = (vtkRectilinearGrid *) ds; int dims[3]; rgrid->GetDimensions(dims); if (dims[2] > 1) { EXCEPTION2(InvalidDimensionsException, "Shapelet Decomposition", "2-dimensional"); } // make sure this is a zonal varaible std::string var = queryAtts.GetVariables()[0]; vtkDataArray *vals = rgrid->GetCellData()->GetArray(var.c_str()); if(!vals) { EXCEPTION1(InvalidVariableException, var.c_str()); } // get width & height int width = dims[0]-1; int height = dims[1]-1; recompError = 1.0; // set error to 1.0 (max) // create basis set avtShapeletBasisSet basis_set(beta,nmax,width,height); avtShapeletDecompose decomp; avtShapeletReconstruct recomp; // do decomp decompResult = decomp.Execute(rgrid,var,&basis_set); if(decompResult) { // do recomp vtkRectilinearGrid *recomp_res = recomp.Execute(decompResult,var,&basis_set); // calc error recompError = ComputeError(rgrid,recomp_res,var); if(recompOutputFileName != "") { // save output vtkVisItUtility::WriteDataSet(recomp_res, recompOutputFileName.c_str()); } // delete recomp image recomp_res->Delete(); } }