static inline void ComputePartial(Grid& sol, Grid& rhs, const Stencil& mat, const Index& begin, const Index& end, const vmg_float& prefactor, const vmg_float& diag_inv, const int& off) { int i,j,k; vmg_float temp; Stencil::iterator iter; for (i=begin.X(); i<end.X(); ++i) for (j=begin.Y(); j<end.Y(); ++j) { int z_begin = begin.Z() + (i + j + begin.Z() + off) % 2; #ifdef DEBUG int off_sum = MG::GetComm()->LevelSum(rhs, z_begin - begin.Z()); assert(z_begin - begin.Z() == 0 || z_begin - begin.Z() == 1); assert(off_sum == 0 || off_sum == MG::GetComm()->Size(rhs)); #endif /* DEBUG */ for (k=z_begin; k<end.Z(); k+=2) { temp = prefactor * rhs.GetVal(i,j,k); for (iter=mat.begin(); iter!=mat.end(); ++iter) temp -= iter->Val() * sol.GetVal(i+iter->Disp().X(), j+iter->Disp().Y(), k+iter->Disp().Z()); sol(i,j,k) = temp * diag_inv; } } }
/* * Primordial root object creation and initialization function. A pointer * to this function is passed to the object manager constructor. */ void GraphicsInitialize (RefList* root) { Picture* pict = new Picture; FullGraphic dfault; InitPPaint(); root->Append(new RefList(pict)); dfault.FillBg(true); dfault.SetColors(pblack, pwhite); dfault.SetPattern(psolid); dfault.SetBrush(psingle); dfault.SetFont(pstdfont); Line* line = new Line (0, 0, 75, 75, &dfault); MultiLine* multiline = new MultiLine (x, y, 6, &dfault); BSpline* spline = new BSpline (x, y, 6, &dfault); Rect* rect = new Rect (0, 0, 100, 100, &dfault); FillRect* frect = new FillRect (0, 0, 100, 100, &dfault); Circle* circle = new Circle (0, 0, 50, &dfault); FillCircle* fcircle = new FillCircle (0, 0, 50, &dfault); Polygon* poly = new Polygon (x, y, 6, &dfault); FillPolygon* fpoly = new FillPolygon (x, y, 6, &dfault); ClosedBSpline* cspline = new ClosedBSpline (x, y, 6, &dfault); FillBSpline* fspline = new FillBSpline (x, y, 6, &dfault); Label* label = new Label ("Type 'q' to quit this program.", &dfault); Stencil* stencil = new Stencil(new Bitmap(iv_bits, iv_width, iv_height)); RasterRect* raster = new RasterRect(CreateRaster()); line->Translate(0, 300); multiline->Translate(100, 300); spline->Translate(250, 300); rect->Translate(100, 150); frect->Translate(100, 0); circle->Scale(1.0, 0.6); circle->Translate(0, 150); fcircle->Scale(1.0, 0.6); poly->Translate(250, 150); fpoly->Translate(250, 0); cspline->Translate(400, 150); fspline->Translate(400, 0); label->Translate(350, 175); stencil->Translate(400, 300); raster->Scale(5, 5); raster->Translate(350, 350); pict->Append(line, multiline, spline, rect); pict->Append(frect, circle, fcircle, poly); pict->Append(fpoly, cspline, fspline, label); pict->Append(stencil, raster); }
void upwinding(double dt, int nt, Mat<>& g, Mat<>& su, Mat<>& phitotx) { Mat<> surfnew(su); for (int t = 0; t < nt; ++t) { for (Stencil<double> phi = su.stencilBegin(); phi != su.stencilEnd(); ++phi) { if (g[phi.i] > 2e-2) { surfnew[phi.i] = fmin(1000.0, fmax(0.0, *(phi.c) + dt * phi.deriv(phitotx[phi.i]))); } } su = surfnew; } }
Component::Instance Component::get(const std::string& address,const std::string& version,const std::string& comparison){ Instance instance; auto iterator = instances_.find(address); if(iterator!=instances_.end()){ instance = iterator->second; } else { // Try to find a component on the filesystem... std::string path = locate(address); //...if not found clone it from Stencila hub if(path.length()==0) path = Component::clone(address); // Load the component into memory Component* component; Type type = Component::type(path); if(type==NoneType){ STENCILA_THROW(Exception,"Path does not appear to be a Stencila component.\n path: "+path); } else if(type==ComponentType){ component = new Component; } else if(type==StencilType){ Stencil* stencil = new Stencil; stencil->read(path); component = stencil; } else if(type==ThemeType){ Theme* theme = new Theme; theme->read(path); component = theme; } else { STENCILA_THROW(Exception,"Type of component at path is not currently handled by `Component::get`.\n path: "+path+"\n type: "+type_name(type)); } component->path(path); component->hold(type); instance = {type,component}; } if(version.length()>0){ if(comparison.length()==0 or comparison=="=="){ Component& component = instance.as<Component>(); component.provide(version); } else { STENCILA_THROW(Exception,"Version comparison operator not yet supported <"+comparison+">"); } } return instance; }
void StencilTablesFactory::generateControlVertStencils( int numControlVerts, Stencil & dst) { // Control vertices contribute a single index with a weight of 1.0 for (int i=0; i<numControlVerts; ++i) { *dst._size = 1; *dst._indices = i; *dst._weights = 1.0f; dst.Next(); } }
// Semantic model node generates execution nodes and strands while generating the execution model void Stencil2DMVMSetupLaunch(std::vector<EStmt*> & estmts, std::vector<Variable*> vars, std::map<std::string, Variable*> & symbolTable, std::map<std::string, Constant*> & constantTable, cl_vars_t clv, Stmt * stmt) { Array2D * vec_out = dynamic_cast<Array2D*>(vars[0]); Stencil * stencil = dynamic_cast<Stencil*>(vars[1]); Array2D * vec_in = dynamic_cast<Array2D*>(vars[2]); int nd0 = constantTable["nd20"]->get_property_int("value"); int nd1 = constantTable["nd21"]->get_property_int("value"); int nt0 = constantTable["nt20"]->get_property_int("value"); int nt1 = constantTable["nt21"]->get_property_int("value"); int ne0 = constantTable["ne20"]->get_property_int("value"); int ne1 = constantTable["ne21"]->get_property_int("value"); int dim[2]; int ndevices[2]; int nblocks[2]; int nthreads[2]; int nelements[2]; int devsize[2]; int blksize[2]; if(vec_in->properties["col_mjr"] == "True") { dim[0] = vec_in->get_property_int("dim0"); dim[1] = vec_in->get_property_int("dim1"); } else { dim[1] = vec_in->get_property_int("dim0"); dim[0] = vec_in->get_property_int("dim1"); } ndevices[0] = nd0; ndevices[1] = nd1; nblocks[0] = (((dim[0] + ne0-1)/ne0 + nt0-1) / nt0); nblocks[1] = (((dim[1] + ne1-1)/ne1 + nt1-1) / nt1); nthreads[0] = nt0; nthreads[1] = nt1; nelements[0] = ne0; nelements[1] = ne1; blksize[0] = nthreads[0] * nelements[0]; blksize[1] = nthreads[1] * nelements[1]; devsize[0] = dim[0]; devsize[1] = dim[1]; int ndiags = stencil->get_property_int("ndiags"); int nd = ndevices[0] * ndevices[1]; // Operands Array2DOperand * dstOperand = new Array2DOperand(vec_out, Array2DDevicePartition( dim, ndevices, devsize, blksize, nelements, 1)); StencilOperand * stencilOperand = new StencilOperand(stencil, StencilPartition( nd )); Array2DOperand * srcOperand = new Array2DOperand(vec_in, Array2DDevicePartition( dim, ndevices, dim)); // Strand Level * level = new Stencil2DMVMElementLevel(dstOperand, stencilOperand, srcOperand, ndiags, dim[0], dim[1]); EStmt * estmt = new EStmt("Stencil2DMVM", level, LaunchPartition(2, dim, ndevices, nblocks, nthreads, nelements, ELEMENT)); estmt->addSource(stencilOperand); estmt->addSource(srcOperand); estmt->addSink(dstOperand); estmts.push_back(estmt); }
#include <iostream> #include <boost/test/unit_test.hpp> #include <stencila/stencil.hpp> using namespace Stencila; BOOST_AUTO_TEST_SUITE(stencil_attrs_quick) BOOST_AUTO_TEST_CASE(general){ Stencil s(R"(html:// <html> <body> <main id="content"> <div id="title">Yo</div> <div id="description">blah blah blah</div> <div id="keywords">a,b,cd<div> <div class="author">Arthur Dent</div> <div class="author">Slartibartfast</div> <div id="contexts">r,py</div> <div id="theme">inter-galatic-journal/theme</div> <p class="advice">Don't panic!</p> </main> </body> </html> )"); BOOST_CHECK_EQUAL(s.title(),"Yo"); BOOST_CHECK_EQUAL(s.description(),"blah blah blah");
// This function only works for max. compact 9-point stencils NumericArray DendyInterpolation::prolongate( const NumericArray& u, const Stencil& stencil, const Index nx, const Index ny) const { const Index nxNew=2*nx; const Index nyNew=2*ny; NumericArray result((nxNew+1)*(nyNew+1)); NumericArray stencilL; Precision scale=0; Precision weight1=0; Precision weight2=0; Precision erg=0; // linear interpolation on the borders for (Index sy=0; sy<=ny; sy++) { result[2*sy*(nxNew+1)]=u[sy*(nx+1)]; result[2*sy*(nxNew+1)+2*nx]=u[sy*(nx+1)+nx]; } for (Index sx=0; sx<=nx; sx++) { result[2*sx]=u[sx]; result[2*ny*(nxNew+1)+2*sx]=u[ny*(nx+1)+sx]; } for (Index sy=0; sy<=ny-1; sy++) { result[(2*sy+1)*(nxNew+1)] = 0.5 * (result[2*sy*(nxNew+1)] + result[2*(sy+1)*(nxNew+1)]); result[(2*sy+1)*(nxNew+1)+2*nx] = 0.5 * (result[2*sy*(nxNew+1)+2*nx] + result[2*(sy+1)*(nxNew+1)+2*nx]); } for (Index sx=0; sx<=nx-1; sx++) { result[2*sx+1] = 0.5 * (result[2*sx] + result[2*sx+2]); result[2*ny*(nxNew+1)+2*sx+1] = 0.5 * (result[2*ny*(nxNew+1)+2*sx] + result[2*ny*(nxNew+1)+2*sx+2]); } //copy coarse grid points for (Index sy=1; sy<=ny-1; sy++) for (Index sx=1; sx<=nx-1; sx++) result[2*sy*(nxNew+1)+2*sx]=u[sy*(nx+1)+sx]; stencilL.resize( stencil.getLInSize(C,2,1,nxNew,nyNew, 1).size()); //interpolation of fine grid points on coarse grid lines for (Index sy=2; sy<=nyNew-2; sy+=2) for (Index sx=1; sx<=nxNew-1; sx+=2) { stencilL=stencil.getLInSize(C,sx,sy,nxNew,nyNew, 1); scale=0; scale-=stencilL[C]; scale-=stencilL[S]; scale-=stencilL[N]; weight1=0; weight1+=stencilL[W]; weight1+=stencilL[SW]; weight1+=stencilL[NW]; weight2=0; weight2+=stencilL[E]; weight2+=stencilL[SE]; weight2+=stencilL[NE]; result[sy*(nxNew+1)+sx]= ( weight1*result[sy*(nxNew+1)+sx-1] +weight2*result[sy*(nxNew+1)+sx+1] )/scale; } //interpolation of fine grid points on fine grid lines and coarse grid columns for (Index sy=1; sy<=nyNew-1; sy+=2) for (Index sx=2; sx<=nxNew-2; sx+=2) { stencilL=stencil.getLInSize(C,sx,sy,nxNew,nyNew,1); scale=0; scale-=stencilL[C]; scale-=stencilL[W]; scale-=stencilL[E]; weight1=0; weight1+=stencilL[S]; weight1+=stencilL[SW]; weight1+=stencilL[SE]; weight2=0; weight2+=stencilL[N]; weight2+=stencilL[NW]; weight2+=stencilL[NE]; result[sy*(nxNew+1)+sx]= ( weight1*result[(sy-1)*(nxNew+1)+sx] +weight2*result[(sy+1)*(nxNew+1)+sx] )/scale; } //interpolation of fine grid points on fine grid lines and fine grid columns for (Index sy=1; sy<=nyNew-1; sy+=2) for (Index sx=1; sx<=nxNew-1; sx+=2) { stencilL=stencil.getLInSize(C,sx,sy,nxNew,nyNew,1); erg=0; scale=-stencilL[0]; erg+=stencilL[W] *result[ sy *(nxNew+1)+sx-1]; erg+=stencilL[E] *result[ sy *(nxNew+1)+sx+1]; erg+=stencilL[S] *result[(sy-1)*(nxNew+1)+sx ]; erg+=stencilL[SW]*result[(sy-1)*(nxNew+1)+sx-1]; erg+=stencilL[SE]*result[(sy-1)*(nxNew+1)+sx+1]; erg+=stencilL[N] *result[(sy+1)*(nxNew+1)+sx ]; erg+=stencilL[NW]*result[(sy+1)*(nxNew+1)+sx-1]; erg+=stencilL[NE]*result[(sy+1)*(nxNew+1)+sx+1]; result[sy*(nxNew+1)+sx]=erg/scale; } return result; }
StencilTable const * StencilTableFactory::AppendLocalPointStencilTable( TopologyRefiner const &refiner, StencilTable const * baseStencilTable, StencilTable const * localPointStencilTable, bool factorize) { // factorize and append. if (baseStencilTable == NULL || localPointStencilTable == NULL || localPointStencilTable->GetNumStencils() == 0) return NULL; // baseStencilTable can be built with or without singular stencils // (single weight of 1.0f) as place-holders for coarse mesh vertices. int controlVertsIndexOffset = 0; int nBaseStencils = baseStencilTable->GetNumStencils(); int nBaseStencilsElements = (int)baseStencilTable->_indices.size(); { int nverts = refiner.GetNumVerticesTotal(); if (nBaseStencils == nverts) { // the table contain stencils for the control vertices // // <----------------- nverts ------------------> // // +---------------+----------------------------+-----------------+ // | control verts | refined verts : (max lv) | local points | // +---------------+----------------------------+-----------------+ // | base stencil table | LP stencils | // +--------------------------------------------+-----------------+ // ^ / // \_________________________/ // // controlVertsIndexOffset = 0; } else if (nBaseStencils == (nverts -refiner.GetLevel(0).GetNumVertices())) { // the table does not contain stencils for the control vertices // // <----------------- nverts ------------------> // <------ nBaseStencils -------> // +---------------+----------------------------+-----------------+ // | control verts | refined verts : (max lv) | local points | // +---------------+----------------------------+-----------------+ // | base stencil table | LP stencils | // +----------------------------+-----------------+ // ^ / // \_________________/ // <--------------> // controlVertsIndexOffset // controlVertsIndexOffset = refiner.GetLevel(0).GetNumVertices(); } else { // these are not the stencils you are looking for. assert(0); return NULL; } } // copy all local points stencils to proto stencils, and factorize if needed. int nLocalPointStencils = localPointStencilTable->GetNumStencils(); int nLocalPointStencilsElements = 0; internal::StencilBuilder builder(refiner.GetLevel(0).GetNumVertices(), /*genControlVerts*/ false, /*compactWeights*/ factorize); internal::StencilBuilder::Index origin(&builder, 0); internal::StencilBuilder::Index dst = origin; internal::StencilBuilder::Index srcIdx = origin; for (int i = 0 ; i < nLocalPointStencils; ++i) { Stencil src = localPointStencilTable->GetStencil(i); dst = origin[i]; for (int j = 0; j < src.GetSize(); ++j) { Index index = src.GetVertexIndices()[j]; float weight = src.GetWeights()[j]; if (isWeightZero(weight)) continue; if (factorize) { dst.AddWithWeight( // subtracting controlVertsIndex if the baseStencil doesn't // include control vertices (see above diagram) // since currently local point stencils are created with // absolute indices including control (level=0) vertices. baseStencilTable->GetStencil(index - controlVertsIndexOffset), weight); } else { srcIdx = origin[index + controlVertsIndexOffset]; dst.AddWithWeight(srcIdx, weight); } } nLocalPointStencilsElements += builder.GetNumVertsInStencil(i); } // create new stencil table StencilTable * result = new StencilTable; result->_numControlVertices = refiner.GetLevel(0).GetNumVertices(); result->resize(nBaseStencils + nLocalPointStencils, nBaseStencilsElements + nLocalPointStencilsElements); int* sizes = &result->_sizes[0]; Index * indices = &result->_indices[0]; float * weights = &result->_weights[0]; // put base stencils first memcpy(sizes, &baseStencilTable->_sizes[0], nBaseStencils*sizeof(int)); memcpy(indices, &baseStencilTable->_indices[0], nBaseStencilsElements*sizeof(Index)); memcpy(weights, &baseStencilTable->_weights[0], nBaseStencilsElements*sizeof(float)); sizes += nBaseStencils; indices += nBaseStencilsElements; weights += nBaseStencilsElements; // endcap stencils second for (int i = 0 ; i < nLocalPointStencils; ++i) { int size = builder.GetNumVertsInStencil(i); int idx = builder.GetStencilOffsets()[i]; for (int j = 0; j < size; ++j) { *indices++ = builder.GetStencilSources()[idx+j]; *weights++ = builder.GetStencilWeights()[idx+j]; } *sizes++ = size; } // have to re-generate offsets from scratch result->generateOffsets(); return result; }
#include <boost/test/unit_test.hpp> #include <stencila/stencil.hpp> using namespace Stencila; BOOST_AUTO_TEST_SUITE(stencil_directives_quick) BOOST_AUTO_TEST_CASE(strip){ Stencil s(R"(html:// `exec` directive elements shoul be removed completely <pre data-exec="" id="exec"></pre> Directive attributes should be removed <div data-if=""></div> <div data-switch=""> <div data-case=""></div> <div data-case=""></div> </div> Flag attributes should be removed <div data-hash=""></div> <div data-out=""></div> )"); BOOST_CHECK(s.select("pre#exec"));
void xml_show(void){ std::cout<<"-------------------XML-------------------\n" <<stencil.xml()<<"\n" <<"-----------------------------------------\n"; }