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
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 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
129
130
131
132
133
134
135
136
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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
302
303
304
305
306
307
308
309
310
311
#include <iostream>
#include "moab/Interface.hpp"
#ifndef IS_BUILDING_MB
#define IS_BUILDING_MB
#endif
#include "TestUtil.hpp"
#include "Internals.hpp"
#include "moab/Core.hpp"

#include "DagMC.hpp"

using namespace moab;

using moab::DagMC;

DagMC* DAG;

#define CHKERR( A )                                                                                        \
    do                                                                                                     \
    {                                                                                                      \
        if( MB_SUCCESS != ( A ) )                                                                          \
        {                                                                                                  \
            std::cerr << "Failure (error code " << ( A ) << ") at " __FILE__ ":" << __LINE__ << std::endl; \
            return A;                                                                                      \
        }                                                                                                  \
    } while( false )

const std::string input_file = TestDir + "unittest/test_geom.h5m";

void dagmc_load_file()
{
    ErrorCode rval = DAG->load_file( input_file.c_str() );  // open the Dag file
    CHECK_ERR( rval );
}

void dagmc_load_file_dagmc()
{
    /* 1 - Test with external moab, load file in DAGMC*/
    // make new moab core
    Core* mbi = new moab::Core();
    // make new dagmc into that moab
    DagMC* dagmc = new moab::DagMC( mbi );

    ErrorCode rval;
    // load a file
    rval = dagmc->load_file( input_file.c_str() );CHECK_ERR( rval );

    // delete dagmc
    delete dagmc;
    delete mbi;
}

void dagmc_load_file_dagmc_via_moab()
{
    /* 2 - Test with external moab, load file in MOAB*/
    // load the file into moab rather than dagmc
    ErrorCode rval;

    moab::Core* mbi = new moab::Core();
    rval            = mbi->load_file( input_file.c_str() );CHECK_ERR( rval );
    moab::DagMC* dagmc = new moab::DagMC( mbi );
    rval               = dagmc->load_existing_contents();CHECK_ERR( rval );

    // delete dagmc;
    delete dagmc;
    delete mbi;
}

void dagmc_load_file_dagmc_internal()
{
    /* 3 - Test with internal moab, load file in DAG*/
    // make new dagmc into that moab
    ErrorCode rval;

    moab::DagMC* dagmc = new moab::DagMC();
    // load a file
    rval = dagmc->load_file( input_file.c_str() );CHECK_ERR( rval );
    delete dagmc;
}

void dagmc_load_file_dagmc_build_obb()
{
    /* 1 - Test with external moab, load file in DAGMC*/
    // make new moab core
    ErrorCode rval;

    moab::Core* mbi = new moab::Core();
    // make new dagmc into that moab
    DagMC* dagmc = new moab::DagMC( mbi );

    // load a file
    rval = dagmc->load_file( input_file.c_str() );CHECK_ERR( rval );
    rval = dagmc->init_OBBTree();CHECK_ERR( rval );
    // delete dagmc
    delete dagmc;
    delete mbi;
}

void dagmc_load_file_dagmc_via_moab_build_obb()
{
    /* 2 - Test with external moab, load file in MOAB*/
    // load the file into moab rather than dagmc
    ErrorCode rval;

    moab::Core* mbi = new moab::Core();
    rval            = mbi->load_file( input_file.c_str() );CHECK_ERR( rval );
    moab::DagMC* dagmc = new moab::DagMC( mbi );
    rval               = dagmc->load_existing_contents();CHECK_ERR( rval );
    rval = dagmc->init_OBBTree();CHECK_ERR( rval );

    // delete dagmc;
    delete dagmc;
    delete mbi;
}

void dagmc_load_file_dagmc_internal_build_obb()
{
    /* 3 - Test with internal moab, load file in DAG*/
    // make new dagmc into that moab
    ErrorCode rval;

    moab::DagMC* dagmc = new moab::DagMC();
    // load a file
    rval = dagmc->load_file( input_file.c_str() );CHECK_ERR( rval );
    rval = dagmc->init_OBBTree();CHECK_ERR( rval );
    delete dagmc;
}

void dagmc_test_obb_retreval()
{
    // make new dagmc
    std::cout << "test_obb_retreval" << std::endl;

    DagMC* dagmc = new moab::DagMC();

    ErrorCode rval;
    // load a file
    rval = dagmc->load_file( input_file.c_str() );CHECK_ERR( rval );
    rval = dagmc->init_OBBTree();CHECK_ERR( rval );

    // write the file
    rval = dagmc->write_mesh( "fcad", 4 );<--- rval is assigned

    // now remove the dagmc instance a
    delete dagmc;

    dagmc = new moab::DagMC();
    rval  = dagmc->load_file( "fcad" );CHECK_ERR( rval );<--- rval is overwritten
    rval = dagmc->init_OBBTree();CHECK_ERR( rval );

    // delete the fcad file
    remove( "fcad" );
    delete dagmc;
}

void dagmc_build_obb()
{
    ErrorCode rval = DAG->init_OBBTree();CHECK_ERR( rval );
}

void dagmc_num_vols()
{
    int expect_num_vols = 2;
    int num_vols        = DAG->num_entities( 3 );
    CHECK_EQUAL( expect_num_vols, num_vols );
}

void dagmc_entity_handle()<--- The function 'dagmc_entity_handle' is never used.
{
    /*
    int num_vols = DAG->num_entities(3);
    EntityHandle vol_h;
    for (int i = 0; i < num_vols; i++)
      vol_h = DAG->entity_by_index(3, i);
    */
    // EntityHandle expect_vol_h = 12682136550675316765;
    // CHECK_EQUAL(expect_vol_h, vol_h);
}

void dagmc_point_in()
{
    int result         = 0;
    int expect_result  = 1;
    int vol_idx        = 1;
    double xyz[3]      = { 0.0, 0.0, 0.0 };
    EntityHandle vol_h = DAG->entity_by_index( 3, vol_idx );
    ErrorCode rval     = DAG->point_in_volume( vol_h, xyz, result );CHECK_ERR( rval );
    CHECK_EQUAL( expect_result, result );
}

void dagmc_test_obb_retreval_rayfire()
{
    // make new dagmc
    std::cout << "test_obb_retreval and ray_fire" << std::endl;

    DagMC* dagmc = new moab::DagMC();

    ErrorCode rval;
    // load a file
    rval = dagmc->load_file( input_file.c_str() );CHECK_ERR( rval );
    rval = dagmc->init_OBBTree();CHECK_ERR( rval );

    // write the file
    rval = dagmc->write_mesh( "fcad", 4 );<--- rval is assigned

    // now remove the dagmc instance a
    delete dagmc;

    // now create new DAGMC
    dagmc = new moab::DagMC();
    rval  = dagmc->load_file( "fcad" );CHECK_ERR( rval );<--- rval is overwritten
    rval = dagmc->init_OBBTree();CHECK_ERR( rval );

    // delete the fcad file
    remove( "fcad" );

    // now perform full ray fire
    double eps  = 1.e-6;
    int vol_idx = 1;
    // note model is cube of side 10, centred at 0,0,0, so ray fire along
    // any unit direction should be exactly 5.0
    double xyz[3] = { 0.0, 0.0, 0.0 };
    double dir[3] = { 0.0, 0.0, 1.0 };
    EntityHandle next_surf;
    double next_surf_dist;
    double expect_next_surf_dist = 5.0;
    EntityHandle vol_h           = DAG->entity_by_index( 3, vol_idx );

    rval = DAG->ray_fire( vol_h, xyz, dir, next_surf, next_surf_dist );CHECK_ERR( rval );
    CHECK_REAL_EQUAL( expect_next_surf_dist, next_surf_dist, eps );
    delete dagmc;
}

void dagmc_rayfire()
{
    const double eps = 1e-6;  // epsilon for test, faceting tol?

    int vol_idx = 1;
    // note model is cube of side 10, centred at 0,0,0, so ray fire along
    // any unit direction should be exactly 5.0
    double xyz[3] = { 0.0, 0.0, 0.0 };
    double dir[3] = { 0.0, 0.0, 1.0 };
    EntityHandle next_surf;
    double next_surf_dist;
    double expect_next_surf_dist = 5.0;
    EntityHandle vol_h           = DAG->entity_by_index( 3, vol_idx );

    ErrorCode rval = DAG->ray_fire( vol_h, xyz, dir, next_surf, next_surf_dist );CHECK_ERR( rval );
    CHECK_REAL_EQUAL( expect_next_surf_dist, next_surf_dist, eps );
}

void dagmc_closest_to()
{
    const double eps = 1e-6;  // epsilon for test, faceting tol?

    int vol_idx = 1;
    // note model is cube of side 10, centred at 0,0,0, so ray fire along
    // any unit direction should be exactly 5.0
    double xyz[3] = { -6.0, 0.0, 0.0 };
    double distance;  // distance from point to nearest surface
    double expect_distance = 1.0;
    EntityHandle vol_h     = DAG->entity_by_index( 3, vol_idx );

    ErrorCode rval = DAG->closest_to_location( vol_h, xyz, distance );CHECK_ERR( rval );
    // distance should be 1.0 cm
    CHECK_REAL_EQUAL( expect_distance, distance, eps );
}

void dagmc_test_boundary()
{
    int vol_idx         = 1;
    EntityHandle vol_h  = DAG->entity_by_index( 3, vol_idx );
    int surf_idx        = 1;
    EntityHandle surf_h = DAG->entity_by_index( 2, surf_idx );

    double xyz[3] = { 0.0, 0.0, 5.0 };
    double dir[3] = { 0.0, 0.0, 1.0 };
    int result;
    int expect_result = 0;

    ErrorCode rval = DAG->test_volume_boundary( vol_h, surf_h, xyz, dir, result );CHECK_ERR( rval );
    // check ray leaving volume
    CHECK_EQUAL( expect_result, result );
}

int main( int /* argc */, char** /* argv */ )
{
    int result = 0;

    DAG = new moab::DagMC();

    result += RUN_TEST( dagmc_load_file );                           // test ray fire
    result += RUN_TEST( dagmc_build_obb );                           // build the obb
    result += RUN_TEST( dagmc_num_vols );                            // make sure the num of vols correct
    result += RUN_TEST( dagmc_load_file_dagmc );                     //
    result += RUN_TEST( dagmc_load_file_dagmc_via_moab );            //
    result += RUN_TEST( dagmc_load_file_dagmc_internal );            //
    result += RUN_TEST( dagmc_load_file_dagmc_build_obb );           //
    result += RUN_TEST( dagmc_load_file_dagmc_via_moab_build_obb );  //
    result += RUN_TEST( dagmc_load_file_dagmc_internal_build_obb );  //
    result += RUN_TEST( dagmc_test_obb_retreval );                   // check that we are retreving loaded obbs
    result += RUN_TEST( dagmc_test_obb_retreval_rayfire );           // check that we can ray fire on loaded obbs
    result += RUN_TEST( dagmc_point_in );                            // check entity by point
    result += RUN_TEST( dagmc_rayfire );                             // ensure ray fire distance is correct
    result += RUN_TEST( dagmc_closest_to );                          // check the distance to surface nearest point
    result += RUN_TEST( dagmc_test_boundary );                       // check particle entering leaving

    delete DAG;

    return result;
}