DumpEMFieldsTest.cpp 11.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 *  Copyright (c) 2014, Chris Rogers
 *  All rights reserved.
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *  3. Neither the name of STFC nor the names of its contributors may be used to
 *     endorse or promote products derived from this software without specific
 *     prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */
#include <fstream>
#include <iostream>

#include "gtest/gtest.h"

#include "opal_src/Utilities/MockComponent.h"

34 35 36
#include "BasicActions/DumpEMFields.h"

#include "AbstractObjects/OpalData.h"
37
#include "Attributes/Attributes.h"
38
#include "Physics/Physics.h"
39
#include "Utilities/OpalException.h"
40
#include "Utilities/Util.h"
41

42 43
#include "opal_test_utilities/SilenceTest.h"

44 45 46 47 48 49
namespace DumpEMFieldsTest {

void setOneAttribute(DumpEMFields* dump, std::string name, double value) {
    Attributes::setReal(*dump->findAttribute(name), value);
}

50
void setAttributesCart(DumpEMFields* dump,
51 52 53 54
                   double x0, double dx, double nx,
                   double y0, double dy, double ny,
                   double z0, double dz, double nz,
                   double t0, double dt, double nt,
55
                   std::string filename, bool defaultCoords = true) {
56 57 58 59 60 61 62 63 64 65 66 67 68
    setOneAttribute(dump, "X_START", x0);
    setOneAttribute(dump, "DX", dx);
    setOneAttribute(dump, "X_STEPS", nx);
    setOneAttribute(dump, "Y_START", y0);
    setOneAttribute(dump, "DY", dy);
    setOneAttribute(dump, "Y_STEPS", ny);
    setOneAttribute(dump, "Z_START", z0);
    setOneAttribute(dump, "DZ", dz);
    setOneAttribute(dump, "Z_STEPS", nz);
    setOneAttribute(dump, "T_START", t0);
    setOneAttribute(dump, "DT", dt);
    setOneAttribute(dump, "T_STEPS", nt);
    Attributes::setString(*dump->findAttribute("FILE_NAME"), filename);
69
    if (!defaultCoords) {
70
        Attributes::setUpperCaseString(*dump->findAttribute("COORDINATE_SYSTEM"), "cARtesiAN");
71
    }
72 73
}

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
void setAttributesCyl(DumpEMFields* dump,
                   double x0, double dx, double nx,
                   double y0, double dy, double ny,
                   double z0, double dz, double nz,
                   double t0, double dt, double nt,
                   std::string filename) {
    setOneAttribute(dump, "R_START", x0);
    setOneAttribute(dump, "DR", dx);
    setOneAttribute(dump, "R_STEPS", nx);
    setOneAttribute(dump, "PHI_START", y0);
    setOneAttribute(dump, "DPHI", dy);
    setOneAttribute(dump, "PHI_STEPS", ny);
    setOneAttribute(dump, "Z_START", z0);
    setOneAttribute(dump, "DZ", dz);
    setOneAttribute(dump, "Z_STEPS", nz);
    setOneAttribute(dump, "T_START", t0);
    setOneAttribute(dump, "DT", dt);
    setOneAttribute(dump, "T_STEPS", nt);
    Attributes::setString(*dump->findAttribute("FILE_NAME"), filename);
93
    Attributes::setUpperCaseString(*dump->findAttribute("COORDINATE_SYSTEM"), "cYLindriCAL");
94
}
95
TEST(DumpEMFieldsTest, ConstructorDestructor) {
96 97
    OpalTestUtilities::SilenceTest silencer;

98 99 100 101 102
    // neither in the set and grid is null
    DumpEMFields* dump1 = new DumpEMFields();
    delete dump1;
    // grid is not null and it is in the set
    DumpEMFields* dump2 = new DumpEMFields();
103
    setAttributesCart(dump2, 1., 1., 1.,   1., 1., 1.,   1., 1., 1.,   1., 1., 1., "/dev/null");
104 105 106 107 108 109 110 111 112 113 114 115 116 117
    dump2->execute();
    delete dump2;
}

void execute_throws(DumpEMFields* dump, std::string reason) {
    try {
        dump->execute();
        EXPECT_TRUE(false) << reason;
    } catch (OpalException& exc) {
        // pass;
    }
}

TEST(DumpEMFieldsTest, executeTest) {
118 119
    OpalTestUtilities::SilenceTest silencer;

120 121 122
    // dump the fields
    DumpEMFields dump1;
    execute_throws(&dump1, "should throw due to nsteps < 1");
123
    setAttributesCart(&dump1, 1., 1., 1.,   1., 1., 1.,   1., 1., 1.,   1., 1., 1., "/dev/null", true);
124
    dump1.execute();  // should be okay (normal)
125
    setAttributesCart(&dump1, 1., 1., 1.,   1., 1., 1.,   1., 1., 1.,   1., 1., 1., "/dev/null", false);
126
    dump1.execute();  // should be okay (normal)
127
    setAttributesCart(&dump1, -1., -1., 1.,   -1., -1., 1.,   -1., -1., 1.,   1., 1., 1., "/dev/null");
128
    dump1.execute();  // should be okay (-ve step is okay)
129
    setAttributesCart(&dump1, -1., -1., 0.,   -1., -1., 1.,   -1., -1., 1.,   1., 1., 1., "/dev/null");
130
    execute_throws(&dump1, "should throw due to nsteps x < 1");
131
    setAttributesCart(&dump1, -1., -1., 1.,   -1., -1., 0.,   -1., -1., 1.,   1., 1., 1., "/dev/null");
132
    execute_throws(&dump1, "should throw due to nsteps y < 1");
133
    setAttributesCart(&dump1, -1., -1., 1.,   -1., -1., 1.,   -1., -1., 0.,   1., 1., 1., "/dev/null");
134
    execute_throws(&dump1, "should throw due to nsteps z < 1");
135
    setAttributesCart(&dump1, -1., -1., 1.,   -1., -1., 1.,   -1., -1., 1.,   1., 1., 0., "/dev/null");
136
    execute_throws(&dump1, "should throw due to nsteps t < 1");
137
    setAttributesCart(&dump1, -1., -1., 1.,   -1., -1., 1.,   -1., -1., 1.5,   1., 1., 1., "/dev/null");
138 139 140 141
    execute_throws(&dump1, "should throw due to nsteps not integer");
}

void clear_files() {
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

    std::string fname1 = Util::combineFilePath({
        OpalData::getInstance()->getAuxiliaryOutputDirectory(), "test1"
    });
    std::string fname2 = Util::combineFilePath({
        OpalData::getInstance()->getAuxiliaryOutputDirectory(), "test2"
    });
    std::string fname3 = Util::combineFilePath({
        OpalData::getInstance()->getAuxiliaryOutputDirectory(), "test3"
    });
    std::string fname4 = Util::combineFilePath({
        OpalData::getInstance()->getAuxiliaryOutputDirectory(), "test4"
    });
    std::string fnameCyl = Util::combineFilePath({
        OpalData::getInstance()->getAuxiliaryOutputDirectory(), "testCyl"
    });

159
    size_t n_str_array = 0;
160
    std::string str_array[5] = {fname1, fname2, fname3, fname4, fnameCyl};
161
    for (size_t i = 0; i < n_str_array; ++i) {
162 163
        if (std::fopen(str_array[i].c_str(), "r") != NULL) {
            std::remove(str_array[i].c_str());
164 165 166 167
        }
    }
}

168
TEST(DumpEMFieldsTest, writeFieldsCartTest) {
169 170
    OpalTestUtilities::SilenceTest silencer;

171 172 173 174 175 176 177 178 179 180 181 182 183
    std::string fname1 = Util::combineFilePath({
        OpalData::getInstance()->getAuxiliaryOutputDirectory(), "test1"
    });
    std::string fname2 = Util::combineFilePath({
        OpalData::getInstance()->getAuxiliaryOutputDirectory(), "test2"
    });
    std::string fname3 = Util::combineFilePath({
        OpalData::getInstance()->getAuxiliaryOutputDirectory(), "test3"
    });
    std::string fname4 = Util::combineFilePath({
        OpalData::getInstance()->getAuxiliaryOutputDirectory(), "test4"
    });

184 185
    clear_files();
    DumpEMFields dump1;
186
    setAttributesCart(&dump1, 1., 1., 1.,   1., 1., 1.,   1., 1., 1.,   1., 1., 1., "test1");
187 188
    dump1.execute();
    DumpEMFields dump2;
189
    setAttributesCart(&dump2, 1., 1., 1.,   1., 1., 1.,   1., 1., 1.,   1., 1., 1., "test2");
190 191
    dump2.execute();
    DumpEMFields dump3;
192
    setAttributesCart(&dump3, 1., 1., 1.,   1., 1., 1.,   1., 1., 1.,   1., 1., 1., "test3");
193 194
    // note we don't execute dump3; so it should not be written
    DumpEMFields dump4;
195
    setAttributesCart(&dump4, 0.1, 0.1, 3.,   -0.1, 0.2, 2.,   0.2, 0.3, 2.,   1., 1., 2., "test4");
196 197 198 199 200 201 202
    dump4.execute();
    MockComponent comp;
    try {
        DumpEMFields::writeFields(&comp);
    } catch (OpalException& exc) {
        EXPECT_TRUE(false) << "Threw OpalException on writefields: " << exc.what() << std::endl;;
    }
203
    std::ifstream fin1(fname1);
204
    EXPECT_TRUE(fin1.good());
205
    std::ifstream fin2(fname2);
206
    EXPECT_TRUE(fin2.good());
207 208 209
    std::ifstream fin3(fname3);
    EXPECT_FALSE(fin3.good()); // does not exist
    std::ifstream fin4(fname4);
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    EXPECT_TRUE(fin4.good());
    int n_lines;
    fin4 >> n_lines;
    EXPECT_EQ(n_lines, 24);
    std::string test_line;
    for (size_t i = 0; i < 12; ++i) {
        std::getline(fin4, test_line);
    }
    std::vector<double> line(10, 0.);
    double tol = 1e-9;
    for (size_t line_index = 0; line_index < 24; ++line_index) {
        for (size_t i = 0; i < 10; ++i) {
            fin4 >> line[i];
        }
        if (line_index == 0) {
            EXPECT_NEAR(line[0], 0.1, tol);
            EXPECT_NEAR(line[1], -0.1, tol);
            EXPECT_NEAR(line[2], 0.2, tol);
            EXPECT_NEAR(line[3], 1., tol);
        }
        if (line[1] < 0.) {
            EXPECT_NEAR(line[4], line[0], tol);
            EXPECT_NEAR(line[5], line[1], tol);
            EXPECT_NEAR(line[6], line[2], tol);
        } else {
            EXPECT_NEAR(line[4], 0., tol);
            EXPECT_NEAR(line[5], 0., tol);
            EXPECT_NEAR(line[6], 0., tol);
        }
        EXPECT_NEAR(line[7], -line[4], tol);
        EXPECT_NEAR(line[8], -line[5], tol);
        EXPECT_NEAR(line[9], -line[6], tol);
    }
243
    // clear_files();
244 245
}

246 247
TEST(DumpEMFieldsTest, writeFieldsCylTest) {
    OpalTestUtilities::SilenceTest silencer;
248 249 250 251 252

    std::string fnameCyl = Util::combineFilePath({
        OpalData::getInstance()->getAuxiliaryOutputDirectory(), "testCyl"
    });

253 254
    clear_files();
    DumpEMFields dump;
255
    setAttributesCyl(&dump, 0.1, 0.1, 3.,   90.*Physics::deg2rad, 45.*Physics::deg2rad, 16,   0.2, 0.3, 2.,   1., 1., 2., "testCyl");
256 257 258 259 260 261 262 263
    dump.execute();
    // depending on execution order, this might write cartesian tests as well... never mind
    MockComponent comp;
    try {
        DumpEMFields::writeFields(&comp);
    } catch (OpalException& exc) {
        EXPECT_TRUE(false) << "Threw OpalException on writefields: " << exc.what() << std::endl;;
    }
264
    std::ifstream fin(fnameCyl);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    EXPECT_TRUE(fin.good());
    int n_lines;
    fin >> n_lines;
    EXPECT_EQ(n_lines, 192);
    std::string test_line;
    for (size_t i = 0; i < 12; ++i) {
        std::getline(fin, test_line);
    }
    std::vector<double> line(10, 0.);
    double tol = 1e-9;
    for (size_t line_index = 0; line_index < 24; ++line_index) {
        for (size_t i = 0; i < 10; ++i) {
            fin >> line[i];
        }
        if (line_index == 0) {
            EXPECT_NEAR(line[0], 0.1, tol);
            EXPECT_NEAR(line[1], 90, tol);
            EXPECT_NEAR(line[2], 0.2, tol);
            EXPECT_NEAR(line[3], 1., tol);
        }
        while (line[1] > 360.) {
            line[1] -= 360.;
        }
        if (line[1] < 90. || line[1] > 270.) {
            EXPECT_NEAR(line[4]*line[4]+line[5]*line[5], line[0]*line[0], tol);
            EXPECT_NEAR(line[6], line[2], tol);
        } else {
            EXPECT_NEAR(line[4], 0., tol);
            EXPECT_NEAR(line[5], 0., tol);
            EXPECT_NEAR(line[6], 0., tol);
        }
        EXPECT_NEAR(line[7], -line[4], tol);
        EXPECT_NEAR(line[8], -line[5], tol);
        EXPECT_NEAR(line[9], -line[6], tol);
    }
    clear_files();

ext-rogers_c's avatar
ext-rogers_c committed
302
    // EXPECT_TRUE(false) << "Do DumpEMFields cylindrical documentation!";
303
}
304

305
}