test_Mesh_to_FEM.cpp 10.2 KB
Newer Older
JasonPries's avatar
JasonPries committed
1 2
#include "test_Library_Integration.hpp"

3
std::string SAVE_DIR = "./test/output/Integration/";
JasonPries's avatar
JasonPries committed
4

5
TEST(Library_Integration, Full_Circle_Uniform_Current_Density) {
6 7
    // Create Sketch
    Sketch sk;
JasonPries's avatar
JasonPries committed
8

9 10
    auto v0 = sk.new_element<Vertex>(0.0, 0.0);
    auto v1 = sk.new_element<Vertex>(1.0, 0.0);
11 12
    auto v2 = sk.new_element<Vertex>(1.0 * std::cos(M_PI * 2.0 / 3.0), 1.0 * std::sin(M_PI * 2.0 / 3.0));
    auto v3 = sk.new_element<Vertex>(1.0 * std::cos(-M_PI * 2.0 / 3.0), 1.0 * std::sin(-M_PI * 2.0 / 3.0));
JasonPries's avatar
JasonPries committed
13

14 15 16
    auto c0 = sk.new_element<CircularArc>(v1, v2, v0, 1.0);
    auto c1 = sk.new_element<CircularArc>(v2, v3, v0, 1.0);
    auto c2 = sk.new_element<CircularArc>(v3, v1, v0, 1.0);
JasonPries's avatar
JasonPries committed
17

18 19 20 21
    auto f0 = sk.new_element<Fixation>(v0);
    auto f1 = sk.new_element<Fixation>(v1);
    auto f2 = sk.new_element<Fixation>(v2);
    auto f3 = sk.new_element<Fixation>(v3);
JasonPries's avatar
JasonPries committed
22

23 24
    double_t tol = sk.solve();
    EXPECT_LE(tol, FLT_EPSILON);
JasonPries's avatar
JasonPries committed
25

26 27
    bool result = sk.build();
    EXPECT_TRUE(result);
JasonPries's avatar
JasonPries committed
28

29
    EXPECT_EQ(sk.size_contours(), 1);
JasonPries's avatar
JasonPries committed
30

31
    sk.save_as<SaveMethod::Rasterize>(SAVE_DIR, std::string("circle_sketch"));
JasonPries's avatar
JasonPries committed
32

33
    // Create Refineable Mesh
JasonPries's avatar
JasonPries committed
34 35 36 37 38 39 40 41 42
    Mesh me{sk};
    me.create();

    me.MinimumElementSize = 0.01;
    me.MaximumElementSize = 0.1;
    me.MinimumElementQuality = 0.5;

    me.refine();

43
    me.save_as(SAVE_DIR, std::string("circle_mesh"));
JasonPries's avatar
JasonPries committed
44

45 46 47
    // Convert to FiniteElementMesh
    FiniteElementMesh<2, 1> fem{me};

48 49 50
    for (std::shared_ptr<Boundary<2>> const &b : fem.boundaries()) {
        double_t a0{-2 * M_PI};
        for (size_t i : b->nodes()) {
51 52 53 54 55 56
            XY const &p = fem.node(i);

            // Test radius of boundary
            EXPECT_NEAR(1.0, std::hypot(p.x(), p.y()), FLT_EPSILON);

            // Test ordering of boundary nodes
57
            double_t a1{std::atan2(p.y(), p.x())};
58
            if (a1 < 0 || (a1 == 0 && a0 > M_PI)) {
59 60
                a1 += 2 * M_PI;
            }
61

62 63 64 65 66 67 68 69
            EXPECT_TRUE(a1 > a0);
            a0 = a1;
        }
    }

    // Create magnetostatic physics
    Magnetostatic<2, 1, 1, FieldVariable::A> msph{fem};

70
    msph.add_current_density([](double t) { return 1.0 / (2.0 * M_PI * 1e-7); }, {fem.region(0)});
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    msph.add_magnetic_insulation({fem.boundary(0), fem.boundary(1), fem.boundary(2)});

    msph.build_matrices();

    msph.apply_conditions();

    // Initialize matrices
    auto J = msph.init_unknown_matrix();
    auto v = msph.init_unknown_vector();
    auto r = msph.init_unknown_vector();
    auto f = msph.init_unknown_vector();
    auto Fx = msph.init_element_array();
    auto Fy = msph.init_element_array();
    auto dFxdx = msph.init_element_array();
    auto dFydy = msph.init_element_array();
    auto dFxdy = msph.init_element_array();

    //
    // Set time
    msph.time(0.0);
    msph.calculate_forcing(f);
JasonPries's avatar
JasonPries committed
93

94 95 96
    // Linearize
    v.setZero();
    msph.linearize(J, v, r, f, Fx, Fy, dFxdx, dFydy, dFxdy);
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    // Factor
    Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> ldlt;
    ldlt.compute(J);
    ASSERT_EQ(ldlt.info(), Eigen::Success);

    // Solve
    v -= ldlt.solve(r);

    // Test
    msph.linearize(J, v, r, f, Fx, Fy, dFxdx, dFydy, dFxdy);

    for (size_t i = 0; i != r.size(); ++i) {
        EXPECT_NEAR(r(i), 0.0, FLT_EPSILON);
    }

    // Test flux-density values
    Eigen::ArrayXd Bx = msph.derivatives().dy(0).transpose() * v;
    Eigen::ArrayXd By = -msph.derivatives().dx(0).transpose() * v;

    Eigen::ArrayXd Bmag(By.size());
    Eigen::ArrayXd Bang(By.size());

    for (size_t i = 0; i != By.size(); ++i) {
        Bmag(i) = hypot(Bx(i), By(i));
        Bang(i) = atan2(By(i), Bx(i)) * 180.0 / M_PI;
    }

    std::vector<std::vector<XY>> qp = fem.quadrature_points<1>();

    for (size_t i = 0; i != qp.size(); ++i) {
        double_t r = std::hypot(qp[i][0].x(), qp[i][0].y());

130
        EXPECT_NEAR(Bmag(i), r, 1.0 * 0.05);
131 132 133 134

        double_t a = std::atan2(qp[i][0].y(), qp[i][0].x()) * 180 / M_PI + 90.0;

        if (a > 180.0) { a -= 360.0; }
135
        if (a - Bang(i) > 180.0) { a -= 360.0; }
136
        if (a - Bang(i) < -180.0) { a += 360.0; }
137
        EXPECT_NEAR(Bang(i), a, 360 * 0.05);
138
    }
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
}

TEST(Library_Integration, Quadrter_Circle_Mirror_Copy_Uniform_Current_Density) {
    // Create Sketch
    Sketch sk;

    auto v0 = sk.new_element<Vertex>(0.0, 0.0);
    auto v1 = sk.new_element<Vertex>(1.0, 0.0);
    auto v2 = sk.new_element<Vertex>(2.0, 0.0);
    auto v3 = sk.new_element<Vertex>(M_SQRT1_2, M_SQRT1_2);
    auto v4 = sk.new_element<Vertex>(M_SQRT2, M_SQRT2);

    auto f0 = sk.new_element<Fixation>(v0);

    auto l01 = sk.new_element<LineSegment>(v0, v1);
    auto l12 = sk.new_element<LineSegment>(v1, v2);

    auto l04 = sk.new_element<LineSegment>(v0, v4, true);

    auto h01 = sk.new_element<Horizontal>(l01);
    auto h12 = sk.new_element<Horizontal>(l12);

    auto a0103 = sk.new_element<Angle>(l01, l04, 45.0);

    auto c013 = sk.new_element<CircularArc>(v1, v3, v0, 1.0);
    auto c024 = sk.new_element<CircularArc>(v2, v4, v0, 2.0);

    auto r013 = sk.new_element<Radius>(c013, 1.0);
    auto r024 = sk.new_element<Radius>(c024, 2.0);

    std::vector<std::shared_ptr<Curve const>> vec{l01, l12, c013, c024};
    auto m04 = sk.new_element<MirrorCopy>(vec, l04);

    double residual = sk.solve();
    EXPECT_LE(residual, FLT_EPSILON);

    bool result = sk.build();
    ASSERT_TRUE(result);

    sk.save_as<SaveMethod::Rasterize>(SAVE_DIR, "quarter_circle_mirror_copy_sketch");

    auto periodic_boundary = sk.select_periodic_boundary_pairs(v0, 90.0);
    {
        for (auto &bp : periodic_boundary) {
183 184 185 186
            if (bp.curve0().get() == l01.get()) {
                EXPECT_EQ(bp.curve1()->is_identical(l01, v0, 90.0), MatchOrientation::Reverse);
            } else if (bp.curve0().get() == l12.get()) {
                EXPECT_EQ(bp.curve1()->is_identical(l12, v0, 90.0), MatchOrientation::Reverse);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
            } else {
                GTEST_NONFATAL_FAILURE_("No matching boundary found");
            }
        }
    }

    auto radial_boundary = sk.select_radial_boundary(v0, 2.0);
    {
        for (auto &c : radial_boundary) {
            auto cc = std::dynamic_pointer_cast<CircularArc const>(c);
            if (cc) {
                EXPECT_NEAR(cc->radius(), 2.0, FLT_EPSILON);
                EXPECT_EQ(cc->center().get(), v0.get());
            } else {
                GTEST_NONFATAL_FAILURE_("dynamic_cast of Curve to CircularArc failed");
            }
        }
    }

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    auto v1p = sk.select_periodic_vertex(v1, v0, 90.0);
    {
        ASSERT_TRUE(v1p); // Make sure that returned vertex pointer is not null
        double_t x0 = v0->x();
        double_t y0 = v0->y();
        double_t x1 = v1->x();
        double_t y1 = v1->y();
        double_t x2 = v1p->x();
        double_t y2 = v1p->y();

        double_t dx = x1 - x0;
        double_t dy = y1 - y0;
        double_t dr = std::hypot(dx, dy);
        double_t da = std::atan2(dy, dx);

        da += M_PI / 2.0;
        x1 = x0 + dr * std::cos(da);
        y1 = y0 + dr * std::sin(da);

        EXPECT_NEAR(x1, x2, dr * FLT_EPSILON);
        EXPECT_NEAR(y1, y2, dr * FLT_EPSILON);
    }
228 229 230 231 232 233 234 235 236

    // Create Mesh
    Mesh me{sk};
    me.create();

    me.MaximumElementSize = 0.1;
    me.MinimumElementSize = 0.01;
    me.MinimumElementQuality = 0.5;

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    for (auto &pb : periodic_boundary) {
        auto bc0 = me.boundary_constraint(pb.curve0());
        if (bc0) {
            bc0->uniform_discretization(true);
        } else {
            GTEST_NONFATAL_FAILURE_("No matching boundary found.");
        }

        auto bc1 = me.boundary_constraint(pb.curve1());
        if (bc1) {
            bc1->uniform_discretization(true);
        } else {
            GTEST_NONFATAL_FAILURE_("No matching boundary found.");
        }
    }

253 254 255 256
    result = me.refine();
    ASSERT_TRUE(result);

    me.save_as(SAVE_DIR, std::string("quarter_circle_mirror_copy_mesh"));
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

    // Create FiniteElementMesh
    FiniteElementMesh<2,1> fem{me};

    for (auto & pb : periodic_boundary) {
        auto const &nodes0 = fem.boundary(pb.curve0())->nodes();
        auto const &nodes1 = fem.boundary(pb.curve1())->nodes();

        for (size_t i = 0; i != nodes0.size(); ++i) {
            auto const &n0 = fem.node(nodes0[i]);
            auto const &n1 = fem.node(nodes1[nodes0.size() - 1 - i]);

            EXPECT_NEAR(n0.y(), 0.0, FLT_EPSILON);
            EXPECT_NEAR(n1.x(), 0.0, FLT_EPSILON);
            EXPECT_NEAR(n0.x(), n1.y(), FLT_EPSILON);
        }
    }

    // Create Physics
    Magnetostatic<2, 1, 1, FieldVariable::A> msph{fem};

278 279 280
    // TODO: Need a method for selecting Contour from Sketch and selecting FiniteElementMesh Region<2> given a contour
    // TODO: Add std::shared_ptr<Contour const> to Region<2>
    //msph.add_current_density([](double t) { return 1.0 / (2.0 * M_PI * 1e-7); }, {fem.region(1)});
281

282
    msph.add_magnetic_insulation(radial_boundary);
283

284
    msph.add_periodic_boundary(periodic_boundary, false);
285

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    msph.build_matrices();

    msph.apply_conditions();

    auto J = msph.init_unknown_matrix();
    auto v = msph.init_unknown_vector();
    auto r = msph.init_unknown_vector();
    auto f = msph.init_unknown_vector();
    auto Fx = msph.init_element_array();
    auto Fy = msph.init_element_array();
    auto dFxdx = msph.init_element_array();
    auto dFydy = msph.init_element_array();
    auto dFxdy = msph.init_element_array();

    v.setZero();

    msph.calculate_forcing(f);

    msph.linearize(J, v, r, f, Fx, Fy, dFxdx, dFydy, dFxdy);

    Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> ldlt;
    ldlt.compute(J);
    ASSERT_EQ(ldlt.info(), Eigen::Success);
    v -= ldlt.solve(r);

    // Verify equation is solved
    msph.linearize(J, v, r, f, Fx, Fy, dFxdx, dFydy, dFxdy);
    for (size_t i = 0; i != r.size(); ++i) {
        EXPECT_NEAR(r(i), 0.0, FLT_EPSILON);
    }

    // Test flux-density values
    Eigen::ArrayXd Bx = msph.derivatives().dy(0).transpose() * v;
    Eigen::ArrayXd By = -msph.derivatives().dx(0).transpose() * v;

    Eigen::ArrayXd Bmag(By.size());
    Eigen::ArrayXd Bang(By.size());

    for (size_t i = 0; i != By.size(); ++i) {
        Bmag(i) = hypot(Bx(i), By(i));
        Bang(i) = atan2(By(i), Bx(i)) * 180.0 / M_PI;
    }

    std::cout << Bmag << std::endl << std::endl;

    std::cout << Bang << std::endl << std::endl;

    std::cout << f << std::endl << std::endl;
334

335
    std::cout << v << std::endl << std::endl;
336

337
    GTEST_NONFATAL_FAILURE_("Solution needs testing");
JasonPries's avatar
JasonPries committed
338
}