Skip to content

Instantly share code, notes, and snippets.

@YetAnotherMinion
Created August 27, 2015 21:09
Show Gist options
  • Select an option

  • Save YetAnotherMinion/5d09e143d695336f83d5 to your computer and use it in GitHub Desktop.

Select an option

Save YetAnotherMinion/5d09e143d695336f83d5 to your computer and use it in GitHub Desktop.
class MyUserFunction : public apf::Function
{
public:
MyUserFunction(apf::Field* f, apf::Mesh* m) : node_field(f), mesh(m) {}
void eval(apf::MeshEntity* e, double *result) {
if(NULL != node_field) {
apf::MeshElement* me = apf::createMeshElement(this->mesh, e);
apf::Element* f_elm = apf::createElement(this->node_field, me);
/*find the location of the single node on entity*/
apf::Vector3 tmp_vec;
apf::getVector(this->node_field, e, 0, tmp_vec);
apf::Vector3 x_vec;
apf::mapLocalToGlobal(me, tmp_vec, x_vec);
*result = (2*x_vec[0] + 5*x_vec[1]);
} else {
std::cout << "failed to set field" << std::endl;
*result = -1.0;
}
}
protected:
apf::Field* node_field;
apf::Mesh* mesh;
};
void test(){
apf::Mesh2* m = NULL;
this->mesh_builder->buildBatmanElementMesh(m);
apf::MeshIterator* it;
apf::MeshEntity* e;
it = m->begin(2);
e = m->iterate(it);
m->end(it);
apf::Field* node_f = apf::createField(m, "nodeField", apf::SCALAR, m->getShape());
// apf::zeroField(node_f);
MyUserFunction test_fnc(node_f, m);
apf::Field* test_f = apf::createUserField(m, "testingField", apf::SCALAR, m->getShape(), &test_fnc);
apf::MeshElement* me = apf::createMeshElement(m, e);
apf::Element* f_elm = apf::createElement(test_f, me);
/*pick a random spot inside the element coordinates*/
apf::Vector3 sample_points[8] =
{apf::Vector3(-0.1241, 0.4889, 0.0),
apf::Vector3( 0.4897, 0.0347, 0.0),
apf::Vector3( 0.4090, 0.7269, 0.0),
apf::Vector3( 0.4172, -0.3034, 0.0),
apf::Vector3( 0.6715, 0.2939, 0.0),
apf::Vector3(-0.2075, -0.7873, 0.0),
apf::Vector3( 0.7172, 0.8884, 0.0),
apf::Vector3( 0.6302, -0.1471, 0.0)};
for(uint32_t ii = 0; ii < 8; ++ii) {
apf::Vector3 tmp_grad(0.0, 0.0, 0.0);
apf::getGrad(f_elm, sample_points[ii], tmp_grad);
EXPECT_FLOAT_EQ(2.0, tmp_grad[0]);
EXPECT_FLOAT_EQ(5.0, tmp_grad[1]);
}
apf::writeVtkFiles("batman_elm", m);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment