|
1 | 1 | /*
|
2 | 2 | * this test verifies that the convert process properly clones the underlying
|
3 |
| - * fields and numberings |
| 3 | + * fields |
4 | 4 | */
|
| 5 | +#include <PCU.h> |
5 | 6 | #include <apf.h>
|
| 7 | +#include <apfConvert.h> |
| 8 | +#include <apfMDS.h> |
| 9 | +#include <apfMesh2.h> |
6 | 10 | #include <gmi_null.h>
|
7 |
| -#include <PCU.h> |
8 | 11 | #include <pcu_util.h>
|
9 |
| -#include <apfMesh2.h> |
10 |
| -#include <apfMDS.h> |
11 |
| -#include <apfConvert.h> |
12 |
| - |
13 |
| -apf::Mesh2* createEmptyMesh() { |
| 12 | +#include <cassert> |
| 13 | +apf::Mesh2* createEmptyMesh() |
| 14 | +{ |
14 | 15 | gmi_model* mdl = gmi_load(".null");
|
15 | 16 | return apf::makeEmptyMdsMesh(mdl, 3, false);
|
16 | 17 | }
|
17 |
| -apf::Mesh2* createMesh() { |
| 18 | +apf::Mesh2* createMesh() |
| 19 | +{ |
18 | 20 | apf::Mesh2* m = createEmptyMesh();
|
19 | 21 | apf::MeshEntity* verts[2];
|
20 |
| - verts[0] = m->createVertex(NULL, apf::Vector3(0,0,0), apf::Vector3(0,0,0)); |
21 |
| - verts[1] = m->createVertex(NULL, apf::Vector3(1,0,0), apf::Vector3(1,0,0)); |
| 22 | + verts[0] = |
| 23 | + m->createVertex(NULL, apf::Vector3(0, 0, 0), apf::Vector3(0, 0, 0)); |
| 24 | + verts[1] = |
| 25 | + m->createVertex(NULL, apf::Vector3(1, 0, 0), apf::Vector3(1, 0, 0)); |
22 | 26 | m->createEntity(apf::Mesh::Type::EDGE, NULL, verts);
|
23 | 27 | return m;
|
24 | 28 | }
|
| 29 | +class twox : public apf::Function { |
| 30 | + private: |
| 31 | + apf::Field* x; |
25 | 32 |
|
26 |
| -int main(int argc, char* argv[]) { |
27 |
| - MPI_Init(&argc,&argv); |
| 33 | + public: |
| 34 | + virtual void eval(apf::MeshEntity* e, double* result) |
| 35 | + { |
| 36 | + result[0] = 2 * apf::getScalar(x, e, 0); |
| 37 | + } |
| 38 | + twox(apf::Field* x) : x(x) {} |
| 39 | +}; |
| 40 | +int main(int argc, char* argv[]) |
| 41 | +{ |
| 42 | + MPI_Init(&argc, &argv); |
28 | 43 | PCU_Comm_Init();
|
29 | 44 | gmi_register_null();
|
30 | 45 | apf::Mesh* m1 = createMesh();
|
31 | 46 | apf::Mesh2* m2 = createEmptyMesh();
|
32 | 47 | // create field on m1
|
33 | 48 | apf::Field* f = apf::createLagrangeField(m1, "field1", apf::SCALAR, 1);
|
| 49 | + apf::Function* func = new twox(f); |
| 50 | + apf::Field* uf = |
| 51 | + apf::createUserField(m1, "ufield1", apf::SCALAR, apf::getShape(f), func); |
34 | 52 | // loop over all vertices in mesh and set values
|
35 | 53 | int count = 1;
|
36 | 54 | apf::MeshIterator* it = m1->begin(0);
|
37 |
| - while(apf::MeshEntity * vert = m1->iterate(it)) { |
| 55 | + while (apf::MeshEntity* vert = m1->iterate(it)) { |
38 | 56 | apf::setScalar(f, vert, 0, count++);
|
39 | 57 | }
|
40 | 58 | m1->end(it);
|
| 59 | + // verify the user field works on mesh 1 |
| 60 | + it = m1->begin(0); |
| 61 | + while (apf::MeshEntity* vert = m1->iterate(it)) { |
| 62 | + double val = apf::getScalar(f, vert, 0); |
| 63 | + double uval = apf::getScalar(uf, vert, 0); |
| 64 | + if (!(std::abs(uval - 2 * double(val)) < 1E-15)) return 1; |
| 65 | + } |
| 66 | + m1->end(it); |
41 | 67 | // copy m1 to m2
|
42 |
| - apf::convert(m1, m2); |
43 |
| - // read values from m2 and verify that they match values on m1 |
44 |
| - // here we assume that the nodes are ordered in the same way for |
45 |
| - // both meshes |
| 68 | + apf::convert(m1, m2); |
46 | 69 | apf::Field* f2 = m2->findField("field1");
|
| 70 | + apf::Field* uf2 = m2->findField("ufield1"); |
| 71 | + // update the user field to reference the field in mesh 2 |
| 72 | + apf::updateUserField(uf2, new twox(f2)); |
47 | 73 | count = 1;
|
48 | 74 | it = m2->begin(0);
|
49 |
| - while(apf::MeshEntity * vert = m2->iterate(it) ) { |
| 75 | + while (apf::MeshEntity* vert = m2->iterate(it)) { |
50 | 76 | double val = apf::getScalar(f2, vert, 0);
|
51 |
| - if(!(std::abs(val-count) < 1E-15)) |
52 |
| - return 1; |
| 77 | + double uval = apf::getScalar(uf2, vert, 0); |
| 78 | + assert(std::abs(val - count) < 1E-15); |
| 79 | + assert(std::abs(uval - 2 * double(count)) < 1E-15); |
| 80 | + apf::setScalar(f2, vert, 0, 18.0); |
| 81 | + // make sure that the function updated properly |
| 82 | + uval = apf::getScalar(uf2, vert, 0); |
| 83 | + assert(std::abs(uval - 36.0) < 1E-15); |
53 | 84 | ++count;
|
54 | 85 | }
|
55 | 86 | m2->end(it);
|
56 |
| - |
57 |
| - |
| 87 | + delete func; |
58 | 88 | m1->destroyNative();
|
59 | 89 | m2->destroyNative();
|
60 | 90 | apf::destroyMesh(m1);
|
|
0 commit comments