bool solvetestFixedSize(void) { Size s(n, n); for (unsigned i = 0; i < 100; ++i) if (!solvetest(rMatrix(s), rVec(s(0)))) return false; return true; }
void Foam::cfdemCloudIB::calcVelocityCorrection ( volScalarField& p, volVectorField& U, volScalarField& phiIB, volScalarField& voidfraction ) { label cellI=0; vector uParticle(0,0,0); vector rVec(0,0,0); vector velRot(0,0,0); vector angVel(0,0,0); for(int index=0; index< numberOfParticles(); index++) { //if(regionM().inRegion()[index][0]) //{ for(int subCell=0;subCell<voidFractionM().cellsPerParticle()[index][0];subCell++) { //Info << "subCell=" << subCell << endl; cellI = cellIDs()[index][subCell]; if (cellI >= 0) { // calc particle velocity for(int i=0;i<3;i++) rVec[i]=U.mesh().C()[cellI][i]-position(index)[i]; for(int i=0;i<3;i++) angVel[i]=angularVelocities()[index][i]; velRot=angVel^rVec; for(int i=0;i<3;i++) uParticle[i] = velocities()[index][i]+velRot[i]; // impose field velocity U[cellI]=(1-voidfractions_[index][subCell])*uParticle+voidfractions_[index][subCell]*U[cellI]; } } //} } // make field divergence free - set reference value in case it is needed fvScalarMatrix phiIBEqn ( fvm::laplacian(phiIB) == fvc::div(U) + fvc::ddt(voidfraction) ); if(phiIB.needReference()) { phiIBEqn.setReference(pRefCell_, pRefValue_); } phiIBEqn.solve(); U=U-fvc::grad(phiIB); U.correctBoundaryConditions(); // correct the pressure as well p=p+phiIB/U.mesh().time().deltaT(); // do we have to account for rho here? p.correctBoundaryConditions(); }
vector<vector<int> > generate(int numRows) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int> > output; for (int r = 0; r < numRows; r++) { vector<int> rVec(r+1, 1); for (int c = 1; c <= r - 1; c++) rVec[c] = output[r-1][c-1] + output[r-1][c]; output.push_back(rVec); } return output; }
RGBAPixel GeometricObject::rayTrace(PointLight& lightSrc, Point3D& pt, Ray& viewRay, vector<GeometricObject*>& shapes){ RGBAPixel color = material.color; if(texture != NULL) color = *mapToTexture(pt); Vector3D dir(lightSrc.o,pt); dir.normalize(); Point3D tempPt(pt+dir.reflect()*0.01); Ray backtraceRay(tempPt,dir.reflect(), "shadow"); //from hit point on shape to light source double tHitLight = lightSrc.o.distance(pt); bool shadow = false; Point3D trash(0,0,0); for(int i = 0; i < shapes.size(); i++){ if(shapes[i]->isLightSrc) continue; double tHitAnotherShape = shapes[i]->hit(backtraceRay,trash); if(tHitAnotherShape < tHitLight && tHitAnotherShape > 0){ shadow = true; break; } } //pt's intersection with viewRay and geometry Vector3D n(this->getNormal(pt)); n.normalize(); double r = 0.0; double g = 0.0; double b = 0.0; //Ambient Lighting r += 0.04 * 30; g += 0.04 * 30; b += 0.04 * 30; RGBAPixel temp(r*color.red, g*color.green ,b*color.blue); if(shadow){ return temp; } //Diffuse Lighting Vector3D reflectRay = (dir.reflect()).hat(); double product = reflectRay.dot(n); if (product > 0){ r += color.red*product * material.kd; g += color.green*product * material.kd; b += color.blue*product * material.kd; } //Specular Lighting double epsilon = 10.0; Vector3D rVec(dir - (n*dir.dot(n)*2.0)); double spec = rVec.dot(viewRay.d.reflect()); double rw0e; if(spec > 0) rw0e = pow(spec,epsilon); else rw0e = 0; if(product > 0){ r += color.red*rw0e * material.ks * 0.5; g += color.green*rw0e * material.ks * 0.5; b += color.blue*rw0e * material.ks * 0.5; } r = r*material.directScale; g = g*material.directScale; b = b*material.directScale; //Reflections double minTime = 100000.0; double tHitAnotherShape = 0.0; Vector3D recViewDir(viewRay.d - (2*viewRay.d*n)*n); //direction of mirror reflection recViewDir.normalize(); Ray recViewRay(pt,recViewDir, "view"); recViewRay.recurseLevel = viewRay.recurseLevel+1; Point3D recPt; RGBAPixel recColor; //Mirror Reflection if(material.reflectProperty == "mirror" && viewRay.recurseLevel < 3){ GeometricObject* nextShape = NULL; for(int k = 0; k < shapes.size(); k++){ if(shapes[k] == this) continue; tHitAnotherShape = shapes[k]->hit(recViewRay,recPt); if(tHitAnotherShape > 0.0 && tHitAnotherShape < minTime){ nextShape = shapes[k]; minTime = tHitAnotherShape; } } if(nextShape != NULL){ recColor = nextShape->rayTrace(lightSrc, recPt, recViewRay, shapes); r += (recColor.red); //* (1-material.directScale); g += (recColor.green);// * (1-material.directScale); b += (recColor.blue);// * (1-material.directScale); } } if(material.reflectProperty == "glossy" && viewRay.recurseLevel < 3){ double tempR = 0.0; double tempG = 0.0; double tempB = 0.0; Vector3D axisA = Vector3D(1,0,0).cross(recViewDir).hat() * 1; Vector3D axisB = axisA.cross(recViewDir).hat() * 1; Point3D tempPt = pt + recViewDir - 0.5*axisA - 0.5*axisB; Rectangle rect(tempPt, axisA, axisB); vector<Point3D> samplepts = rect.generatePoints(100); for(int i = 0; i < samplepts.size(); i++){ Vector3D indirectDir(pt,samplepts[i]); Ray indirectRay(pt,indirectDir); indirectRay.recurseLevel = viewRay.recurseLevel + 1; GeometricObject* nextShape = NULL; double minTime = 100000.0; double tHitAnotherShape = 0.0; for(int k = 0; k < shapes.size(); k++){ if(shapes[k] == this) continue; tHitAnotherShape = shapes[k]->hit(indirectRay,recPt); if(tHitAnotherShape > 0.0 && tHitAnotherShape < minTime){ nextShape = shapes[k]; minTime = tHitAnotherShape; } if(nextShape != NULL && nextShape->material.transparency == 0){ recColor = nextShape->rayTrace(lightSrc, recPt, recViewRay, shapes); tempR += (recColor.red); tempG += (recColor.green); tempB += (recColor.blue); } } } r += tempR / samplepts.size(); g += tempG / samplepts.size(); b += tempB / samplepts.size(); } if(material.transparency > 0 && viewRay.recurseLevel == 0){ double tempR = 255; double tempG = 255; double tempB = 255; Vector3D invNormal = n.reflect(); Ray inverseRay(pt,invNormal); inverseRay.recurseLevel = viewRay.recurseLevel + 1; GeometricObject* nextShape = NULL; double minTime = 100000.0; double tHitAnotherShape = 0.0; for(int k = 0; k < shapes.size(); k++){ if(shapes[k] == this) continue; tHitAnotherShape = shapes[k]->hit(inverseRay,recPt); if(tHitAnotherShape > 0.0 && tHitAnotherShape < minTime){ nextShape = shapes[k]; minTime = tHitAnotherShape; } if(nextShape != NULL && nextShape != this){ recColor = nextShape->rayTrace(lightSrc, recPt, inverseRay, shapes); tempR = (recColor.red); tempG = (recColor.green); tempB = (recColor.blue); } } r = tempR * material.transparency + (r*(1-material.transparency)); g = tempG * material.transparency + (g*(1-material.transparency)); b = tempB * material.transparency + (b*(1-material.transparency)); } //cap off maximum color values r =std::min((int)r,255); g =std::min((int)g,255); b =std::min((int)b,255); temp(r,g,b); return temp; }
//------------------------------------------------------ //------------------------------------------------------ //------------------------------------------------------ void debug_hyst(const QUESO::FullEnvironment& env) { unsigned int numFloors = 4; unsigned int numTimeSteps = 401; std::vector<double> accel(numTimeSteps,0.); FILE *inp; inp = fopen("an.txt","r"); unsigned int numObservations = 0; double tmpA; while (fscanf(inp,"%lf",&tmpA) != EOF) { UQ_FATAL_TEST_MACRO((numObservations >= accel.size()), env.fullRank(), "debug_hyst()", "input file has too many lines"); accel[numObservations] = tmpA; numObservations++; } UQ_FATAL_TEST_MACRO((numObservations != accel.size()), env.fullRank(), "debug_hyst()", "input file has a smaller number of observations than expected"); QUESO::VectorSpace<> floorSpace(env, "floor_", numFloors, NULL); QUESO::GslVector kVec(floorSpace.zeroVector()); kVec[0] = 2.20e+7; kVec[1] = 2.00e+7; kVec[2] = 1.70e+7; kVec[3] = 1.45e+7; QUESO::GslVector rVec(floorSpace.zeroVector()); rVec[0] = 0.1; rVec[1] = 0.1; rVec[2] = 0.1; rVec[3] = 0.1; QUESO::GslVector uVec(floorSpace.zeroVector()); uVec[0] = 0.008; uVec[1] = 0.008; uVec[2] = 0.007; uVec[3] = 0.007; double rho = 7.959e-1 ;//0.1976; double gamma = 2.500e-3 ; //0.0038; std::vector<double> t(numTimeSteps,0.); QUESO::SequenceOfVectors<> u (floorSpace,numTimeSteps,""); // absolute displacement QUESO::SequenceOfVectors<> ud (floorSpace,numTimeSteps,""); // velocity QUESO::SequenceOfVectors<> udd (floorSpace,numTimeSteps,""); // acceleration QUESO::SequenceOfVectors<> resfor(floorSpace,numTimeSteps,""); // restoring force QUESO::SequenceOfVectors<> ru (floorSpace,numTimeSteps,""); // relative displacement u.setPositionValues (0,floorSpace.zeroVector()); ud.setPositionValues (0,floorSpace.zeroVector()); udd.setPositionValues (0,floorSpace.zeroVector()); resfor.setPositionValues(0,floorSpace.zeroVector()); ru.setPositionValues (0,floorSpace.zeroVector()); QUESO::GslVector massVec(floorSpace.zeroVector()); massVec.cwSet(2.0e+4); hystereticModel(env, massVec, kVec, rVec, uVec, rho, gamma, accel, t, // output u, ud, udd, resfor, ru); std::set<unsigned int> auxSet; auxSet.insert(0); // Writing some data to the file 'outputData/cpp_output.m' std::ofstream myFile; myFile.open ("outputData/cpp_output.m"); // Write 't_cpp' myFile << "t_cpp = zeros(" << 1 << "," << numTimeSteps << ");\n" << "t_cpp = ["; for (unsigned int j = 0; j < numTimeSteps; ++j) { myFile << t[j] << " "; } myFile << "];" << std::endl; // Write 'a_cpp' myFile << "a_cpp = zeros(" << 1 << "," << numTimeSteps << ");\n" << "a_cpp = ["; for (unsigned int j = 0; j < numTimeSteps; ++j) { myFile << accel[j] << " "; } myFile << "];" << std::endl; QUESO::GslVector auxVec(floorSpace.zeroVector()); // Write 'u_cpp' myFile << "u_cpp = zeros(" << numFloors << "," << numTimeSteps << ");\n" << "u_cpp = ["; for (unsigned int i = 0; i < numFloors; ++i) { for (unsigned int j = 0; j < numTimeSteps; ++j) { u.getPositionValues(j,auxVec); myFile << auxVec[i] << " "; } myFile << std::endl; } myFile << "];" << std::endl; // Write 'ud_cpp' myFile << "ud_cpp = zeros(" << numFloors << "," << numTimeSteps << ");\n" << "ud_cpp = ["; for (unsigned int i = 0; i < numFloors; ++i) { for (unsigned int j = 0; j < numTimeSteps; ++j) { ud.getPositionValues(j,auxVec); myFile << auxVec[i] << " "; } myFile << std::endl; } myFile << "];" << std::endl; // Write 'udd_cpp' myFile << "udd_cpp = zeros(" << numFloors << "," << numTimeSteps << ");\n" << "udd_cpp = ["; for (unsigned int i = 0; i < numFloors; ++i) { for (unsigned int j = 0; j < numTimeSteps; ++j) { udd.getPositionValues(j,auxVec); myFile << auxVec[i] << " "; } myFile << std::endl; } myFile << "];" << std::endl; // Write 'resfor_cpp' myFile << "resfor_cpp = zeros(" << numFloors << "," << numTimeSteps << ");\n" << "resfor_cpp = ["; for (unsigned int i = 0; i < numFloors; ++i) { for (unsigned int j = 0; j < numTimeSteps; ++j) { resfor.getPositionValues(j,auxVec); myFile << auxVec[i] << " "; } myFile << std::endl; } myFile << "];" << std::endl; // Write 'ru_cpp' myFile << "ru_cpp = zeros(" << numFloors << "," << numTimeSteps << ");\n" << "ru_cpp = ["; for (unsigned int i = 0; i < numFloors; ++i) { for (unsigned int j = 0; j < numTimeSteps; ++j) { ru.getPositionValues(j,auxVec); myFile << auxVec[i] << " "; } myFile << std::endl; } myFile << "];" << std::endl; myFile.close(); return; }
RGBAPixel Sphere::castDirectionalLight(DirectionalLight& lightSrc, Point3D& pt, Ray& viewRay, vector<GeometricObject*>& shapes){ Point3D tempPt(pt+lightSrc.dir.reflect()*0.01); Ray backtraceRay(tempPt,lightSrc.dir.reflect(),"shadow"); double tHitLight = std::numeric_limits<double>::max(); bool shadow = false; Point3D trash(0,0,0); for(int i = 0; i < shapes.size(); i++){ double tHitAnotherShape = shapes[i]->hit(backtraceRay,trash); if(tHitAnotherShape < tHitLight && tHitAnotherShape > 0){ shadow = true; break; } } //pt's intersection with viewRay and sphere Vector3D normal(c, pt); //normal from center to hitPoint normal.normalize(); double r = 0.0; double g = 0.0; double b = 0.0; //Ambient Lighting r += 0.08 * 30; g += 0.08 * 30; b += 0.08 * 30; RGBAPixel temp(r*material.color.red, r*material.color.green ,r*material.color.blue); if(shadow){ return temp; } //Diffuse Lighting Vector3D reflectRay = (lightSrc.dir.reflect()).hat(); double product = reflectRay.dot(normal); if (product <= 0){ temp(r,g,b); return temp; } r += material.color.red*product * material.kd; g += material.color.green*product * material.kd; b += material.color.blue*product * material.kd; //Specular Lighting double epsilon = 10.0; Vector3D rVec(lightSrc.dir - normal*lightSrc.dir.dot(normal)*2.0); double rw0e = pow(rVec.dot(viewRay.d.reflect()),epsilon); r += material.color.red*rw0e * material.ks; g += material.color.green*rw0e * material.ks; b += material.color.blue*rw0e * material.ks; //cap off maximum color values r =std::min((int)r,255); g =std::min((int)g,255); b =std::min((int)b,255); temp(r,g,b); return temp; }
SGridder::SGridder(SGridderConfig cfg) { // ** Input scale = Param<double>("scale"); grid_size = Param<int>("grid_size"); vis = ImageParam(type_of<double>(), 2, "vis"); // GCF: Array of OxOxSxS complex numbers. We "fuse" two dimensions // as Halide only supports up to 4 dimensions. gcf_fused = ImageParam(type_of<double>(), 4, "gcf"); // ** Output // Grid starts out undefined so we can update the output buffer F(uvg); uvg(cmplx, x, y) = undef<double>(); // Get grid limits. This limits the uv pixel coordinates we accept // for the top-left corner of the GCF. Expr min_u = uvg.output_buffer().min(1); Expr max_u = uvg.output_buffer().min(1) + uvg.output_buffer().extent(1) - cfg.gcfSize - 1; Expr min_v = uvg.output_buffer().min(2); Expr max_v = uvg.output_buffer().min(2) + uvg.output_buffer().extent(2) - cfg.gcfSize - 1; // ** Helpers // Coordinate preprocessing Func Q(uvs); F(uv), F(overc); uvs(uvdim, t) = vis(uvdim, t) * scale; overc(uvdim, t) = clamp(cast<int>(round(OVER * (uvs(uvdim, t) - floor(uvs(uvdim, t))))), 0, OVER-1); uv(uvdim, t) = cast<int>(floor(uvs(uvdim, t)) + grid_size / 2 - cfg.gcfSize / 2); // Visibilities to ignore due to being out of bounds F(inBound); inBound(t) = uv(_U, t) >= min_u && uv(_U, t) <= max_u && uv(_V, t) >= min_v && uv(_V, t) <= max_v; // GCF lookup for a given visibility Func Q(gcf); Var suppx("suppx"), suppy("suppy"), overx("overx"), overy("overy"); gcf(suppx, suppy, t) = Complex(gcf_fused(_REAL, suppx, suppy, overc(_U, t) + OVER * overc(_V, t)), gcf_fused(_IMAG, suppx, suppy, overc(_U, t) + OVER * overc(_V, t))); // ** Definition // Reduction domain. Note that we iterate over time steps before // switching the GCF row in order to increase locality (Romein). typedef std::pair<Expr, Expr> rType; rType cRange = {0, _CPLX_FIELDS} , gRange = {0, cfg.gcfSize} , vRange = {0, cfg.steps} , blRange = {0, vis.height() / cfg.steps} ; std::vector<rType> rVec(5); rVec[cfg.cpos] = cRange; rVec[cfg.xpos] = gRange; rVec[cfg.ypos] = gRange; rVec[cfg.vpos] = vRange; rVec[cfg.blpos] = blRange; RDom red(rVec); rcmplx = red[cfg.cpos] , rgcfx = red[cfg.xpos] , rgcfy = red[cfg.ypos] , rstep = red[cfg.vpos] , rbl = red[cfg.blpos] ; Expr rvis = vis.top() + cfg.steps * rbl + rstep; // Get visibility as complex number Complex visC(vis(_R, rvis), vis(_I, rvis)); // Update grid uvg(rcmplx, rgcfx + clamp(uv(_U, rvis), min_u, max_u), rgcfy + clamp(uv(_V, rvis), min_v, max_v)) += select(inBound(rvis), (visC * Complex(gcf(rgcfx, rgcfy, rvis))).unpack(rcmplx), undef<double>()); if (cfg.dim & (1 << _VIS0)) vis.set_min(0,0).set_stride(0,1).set_extent(0,_VIS_FIELDS); if (cfg.dim & (1 << _VIS1)) vis.set_stride(1,_VIS_FIELDS); if (cfg.dim & (1 << _GCF0)) gcf_fused.set_min(0,0).set_stride(0,1).set_extent(0,_CPLX_FIELDS); if (cfg.dim & (1 << _GCF1)) gcf_fused.set_min(1,0).set_stride(1,_CPLX_FIELDS).set_extent(1,cfg.gcfSize); if (cfg.dim & (1 << _GCF2)) gcf_fused.set_min(2,0).set_stride(2,_CPLX_FIELDS*cfg.gcfSize).set_extent(2,cfg.gcfSize); if (cfg.dim & (1 << _GCF3)) gcf_fused.set_min(3,0).set_stride(3,_CPLX_FIELDS*cfg.gcfSize*cfg.gcfSize).set_extent(3,OVER*OVER); if (cfg.dim & (1 << _UVG0)) uvg.output_buffer().set_stride(0,1).set_extent(0,_CPLX_FIELDS); if (cfg.dim & (1 << _UVG1)) uvg.output_buffer().set_stride(1,_CPLX_FIELDS); // Compute UV & oversampling coordinates per visibility overc.compute_at(uvg, rstep).vectorize(uvdim); uv.compute_at(uvg, rstep).vectorize(uvdim); inBound.compute_at(uvg, rstep); RVar rgcfxc("rgcfxc"); switch(cfg.upd) { case _UPD_NONE: break; case _UPD_VECT: uvg.update() .allow_race_conditions() .vectorize(rcmplx); break; case _UPD_FUSE: uvg.update() .allow_race_conditions() .fuse(rgcfx, rcmplx, rgcfxc) .vectorize(rgcfxc, cfg.vector); break; case _UPD_FUSE_UNROLL: uvg.update() .allow_race_conditions() .fuse(rgcfx, rcmplx, rgcfxc) .vectorize(rgcfxc, cfg.vector) .unroll(rgcfxc, cfg.gcfSize * 2 / cfg.vector); break; case _UPD_UNROLL: uvg.update() .unroll(rcmplx); break; } }
/// Return normalized random vector Vector rnVec(unsigned len) { return normalize(rVec(len)); }