test_Mesh_to_FEM.cpp 6.63 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 59 60 61 62 63 64 65 66 67 68
            if (a1 < 0) {
                a1 += 2 * M_PI;
            }
            EXPECT_TRUE(a1 > a0);
            a0 = a1;
        }
    }

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

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

    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
92

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

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
    // 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());

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

        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; }
134
        if (a - Bang(i) > 180.0) { a -= 360.0; }
135
        if (a - Bang(i) < -180.0) { a += 360.0; }
136
        EXPECT_NEAR(Bang(i), a, 360 * 0.05);
137
    }
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
}

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) {
            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);
            } 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");
            }
        }
    }

    // TODO: Select periodic verticies
    // auto sk.select_periodic_vertex(v0,v1,90.0);

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

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

    result = me.refine();
    ASSERT_TRUE(result);

    me.save_as(SAVE_DIR, std::string("quarter_circle_mirror_copy_mesh"));
JasonPries's avatar
JasonPries committed
220
}