bool ParametricCurveTests::testGradientWrapper() { bool success = true; // create an artificial function whose gradient is "interesting" and known FunctionPtr t1 = Function::xn(1); FunctionPtr t2 = Function::yn(1); FunctionPtr xt = t1 + t1 * t2; FunctionPtr yt = t2 + 2 * t1 * t2; FunctionPtr xt_dt1 = 1 + t2; FunctionPtr xt_dt2 = t1; FunctionPtr yt_dt1 = 2 * t2; FunctionPtr yt_dt2 = 1 + 2 * t1; FunctionPtr ft = Function::vectorize(xt, yt); FunctionPtr ft_dt1 = Function::vectorize(xt_dt1, yt_dt1); FunctionPtr ft_dt2 = Function::vectorize(xt_dt2, yt_dt2); FunctionPtr ft_gradt = Function::vectorize(ft_dt1, ft_dt2); // first test: confirm that on a parametric quad, the wrapped function agrees with the naked one int cubatureDegree = 5; BasisCachePtr parametricQuadCache = BasisCache::parametricQuadCache(cubatureDegree); FunctionPtr fx_gradx = ParametricCurve::parametricGradientWrapper(ft_gradt, true); double tol = 1e-14; if (! ft_gradt->equals(fx_gradx, parametricQuadCache)) { success = false; cout << "on a parametric quad, the wrapped gradient doesn't agree with the naked one"; reportFunctionValueDifferences(ft_gradt, fx_gradx, parametricQuadCache, tol); } if (! ft_gradt->equals(ft->grad(), parametricQuadCache)) { success = false; cout << "on a parametric quad, manual gradient disagrees with automatic one (error in test construction, likely)."; reportFunctionValueDifferences(ft_gradt, ft->grad(), parametricQuadCache, tol); } // on the quad domain defined by (0,0), (1,0), (2,3), (0,1), // some algebra shows that for x and y as functions of the parametric // coordinates, we have // x = t1 + t1 * t2 // y = t2 + 2 * t1 * t2 // which gives the result that our original function f(t1,t2) = (t1 + t1 * t2, t2 + 2 * t1 * t2) = (x, y) FunctionPtr x = Function::xn(1); // understood in physical space FunctionPtr y = Function::yn(1); FunctionPtr f1_xy = x; FunctionPtr f2_xy = y; FunctionPtr f_xy = Function::vectorize(f1_xy, f2_xy); // set up the quad domain FieldContainer<double> physicalCellNodes(1,4,2); // (C,P,D) physicalCellNodes(0,0,0) = 0; physicalCellNodes(0,0,1) = 0; physicalCellNodes(0,1,0) = 1; physicalCellNodes(0,1,1) = 0; physicalCellNodes(0,2,0) = 2; physicalCellNodes(0,2,1) = 3; physicalCellNodes(0,3,0) = 0; physicalCellNodes(0,3,1) = 1; // physical space BasisCache: shards::CellTopology quad_4(shards::getCellTopologyData<shards::Quadrilateral<4> >() ); BasisCachePtr basisCache = Teuchos::rcp( new BasisCache(physicalCellNodes, quad_4, cubatureDegree)); // as a preliminary test, check that the Jacobian values and inverse values agree with our expectations // we expect the Jacobian to be: // [ 1 + t2 t1 ] // 1/2 * [ ] // [ 2 * t2 1 + t2 ] // where (t1,t2) are parametric coordinates and the 1/2 comes from the transformation from reference // to parametric space int numCells = 1; int numPoints = basisCache->getRefCellPoints().dimension(0); int spaceDim = 2; FieldContainer<double> jacobianExpected(numCells,numPoints,spaceDim,spaceDim); FieldContainer<double> jacobianInvExpected(numCells,numPoints,spaceDim,spaceDim); // also check that the function we've chosen has the expected values // by first computing its gradient in parametric space and then dividing by 2 to account // for the transformation from reference to parametric space FieldContainer<double> fgrad_based_jacobian(numCells,numPoints,spaceDim,spaceDim); ft_gradt->values(fgrad_based_jacobian, parametricQuadCache); FieldContainer<double> parametricPoints = basisCache->computeParametricPoints(); for (int ptIndex=0; ptIndex<numPoints; ptIndex++) { double t1 = parametricPoints(0,ptIndex,0); double t2 = parametricPoints(0,ptIndex,1); jacobianExpected(0,ptIndex,0,0) = 0.5 * (1 + t2); jacobianExpected(0,ptIndex,0,1) = 0.5 * (t1); jacobianExpected(0,ptIndex,1,0) = 0.5 * (2 * t2); jacobianExpected(0,ptIndex,1,1) = 0.5 * (1 + 2 * t1); jacobianInvExpected(0,ptIndex,0,0) = (2.0 / (1 + 2 * t1 + t2) ) * (1 + 2 * t1); jacobianInvExpected(0,ptIndex,0,1) = (2.0 / (1 + 2 * t1 + t2) ) * (- t1); jacobianInvExpected(0,ptIndex,1,0) = (2.0 / (1 + 2 * t1 + t2) ) * (- 2 * t2); jacobianInvExpected(0,ptIndex,1,1) = (2.0 / (1 + 2 * t1 + t2) ) * (1 + t2); fgrad_based_jacobian(0,ptIndex,0,0) /= 2.0; fgrad_based_jacobian(0,ptIndex,0,1) /= 2.0; fgrad_based_jacobian(0,ptIndex,1,0) /= 2.0; fgrad_based_jacobian(0,ptIndex,1,1) /= 2.0; } FieldContainer<double> jacobian = basisCache->getJacobian(); FieldContainer<double> jacobianInv = basisCache->getJacobianInv(); double maxDiff = 0; if (! fcsAgree(jacobianExpected, jacobian, tol, maxDiff)) { success = false; cout << "Jacobian expected does not match actual.\n"; reportFunctionValueDifferences(parametricPoints, jacobian, jacobianExpected, tol); } if (! fcsAgree(jacobianInvExpected, jacobianInv, tol, maxDiff)) { success = false; cout << "Jacobian inverse expected does not match actual.\n"; reportFunctionValueDifferences(parametricPoints, jacobianInv, jacobianInvExpected, tol); } if (! fcsAgree(fgrad_based_jacobian, jacobianExpected, tol, maxDiff)) { success = false; cout << "Jacobian from fgrad does not agree with the transformation jacobian (problem with test?).\n"; reportFunctionValueDifferences(parametricPoints, fgrad_based_jacobian, jacobianExpected, tol); } // test that the gradient values agree if (! fx_gradx->equals(f_xy->grad(), basisCache)) { success = false; cout << "wrapped gradient does not agree with analytically transformed function.\n"; reportFunctionValueDifferences(fx_gradx, f_xy->grad(), basisCache, tol); } // finally, although this isn't really the right place for this, it is convenient here // to test the TFI for the "mesh" we were concerned with above. int H1Order = 5; BFPtr bf = VGPStokesFormulation(1.0).bf(); physicalCellNodes.resize(4,2); int horizontalElements = 1, verticalElements = 1; MeshPtr mesh = MeshFactory::quadMesh(bf, H1Order, physicalCellNodes); int cellID = 0; vector< ParametricCurvePtr > edges = mesh->parametricEdgesForCell(cellID); ParametricSurfacePtr tfi = ParametricSurface::transfiniteInterpolant(edges); double v2[2]; edges[2]->value(0, v2[0], v2[1]); // cout << "v2 = (" << v2[0] << ", " << v2[1] << ")\n"; if ( ! tfi->equals(f_xy, basisCache) ) { success = false; cout << "TFI does not agree with analytically constructed transformation function.\n"; reportFunctionValueDifferences(tfi, f_xy, basisCache, tol); } if ( ! tfi->grad()->equals(f_xy->grad(), basisCache) ) { success = false; cout << "TFI does not agree with analytically constructed transformation function.\n"; reportFunctionValueDifferences(tfi->grad(), f_xy->grad(), basisCache, tol); } return success; }
bool FunctionTests::testJacobianOrdering() { bool success = true; FunctionPtr y = Function::yn(1); FunctionPtr f = Function::vectorize(y, Function::zero()); // test 1: Jacobian ordering is f_i,j int spaceDim = 2; int cellID = 0; BasisCachePtr basisCache = BasisCache::basisCacheForCell(_spectralConfusionMesh, cellID); FieldContainer<double> physicalPoints = basisCache->getPhysicalCubaturePoints(); int numCells = physicalPoints.dimension(0); int numPoints = physicalPoints.dimension(1); FieldContainer<double> expectedValues(numCells, numPoints, spaceDim, spaceDim); for (int cellIndex=0; cellIndex<numCells; cellIndex++) { for (int ptIndex=0; ptIndex<numPoints; ptIndex++) { expectedValues(cellIndex,ptIndex,0,0) = 0; expectedValues(cellIndex,ptIndex,0,1) = 1; expectedValues(cellIndex,ptIndex,1,0) = 0; expectedValues(cellIndex,ptIndex,1,1) = 0; } } FieldContainer<double> values(numCells, numPoints, spaceDim, spaceDim); f->grad(spaceDim)->values(values, basisCache); double maxDiff = 0; double tol = 1e-14; if (! fcsAgree(expectedValues, values, tol, maxDiff)) { cout << "expectedValues does not match values in testJacobianOrdering().\n"; reportFunctionValueDifferences(physicalPoints, expectedValues, values, tol); success = false; } // test 2: ordering of VectorizedBasis agrees // (actually implemented where it belongs, in Vectorized_BasisTestSuite) // test 3: ordering of CellTools::getJacobian FieldContainer<double> nodes(1,4,2); nodes(0,0,0) = 1; nodes(0,0,1) = -2; nodes(0,1,0) = 1; nodes(0,1,1) = 2; nodes(0,2,0) = -1; nodes(0,2,1) = 2; nodes(0,3,0) = -1; nodes(0,3,1) = -2; shards::CellTopology quad_4(shards::getCellTopologyData<shards::Quadrilateral<4> >() ); int cubDegree = 4; BasisCachePtr rotatedCache = Teuchos::rcp( new BasisCache(nodes, quad_4, cubDegree) ); physicalPoints = rotatedCache->getPhysicalCubaturePoints(); numCells = physicalPoints.dimension(0); numPoints = physicalPoints.dimension(1); FieldContainer<double> expectedJacobian(numCells,numPoints,spaceDim,spaceDim); for (int cellIndex=0; cellIndex<numCells; cellIndex++) { for (int ptIndex=0; ptIndex<numPoints; ptIndex++) { expectedJacobian(cellIndex,ptIndex,0,0) = 0; expectedJacobian(cellIndex,ptIndex,0,1) = -1; expectedJacobian(cellIndex,ptIndex,1,0) = 2; expectedJacobian(cellIndex,ptIndex,1,1) = 0; } } FieldContainer<double> jacobianValues = rotatedCache->getJacobian(); maxDiff = 0; if (! fcsAgree(expectedJacobian, jacobianValues, tol, maxDiff)) { cout << "expectedJacobian does not match jacobianValues in testJacobianOrdering().\n"; reportFunctionValueDifferences(physicalPoints, expectedJacobian, jacobianValues, tol); success = false; } return success; }