-
Notifications
You must be signed in to change notification settings - Fork 0
/
ac_particles.c
5671 lines (5064 loc) · 198 KB
/
ac_particles.c
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************ *
* Goma - Multiphysics finite element software *
* Sandia National Laboratories *
* *
* Copyright (c) 2014 Sandia Corporation. *
* *
* Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, *
* the U.S. Government retains certain rights in this software. *
* *
* This software is distributed under the GNU General Public License. *
\************************************************************************/
/* ac_particles -- calculations involving discrete particles.
*/
/*
* $Id: ac_particles.c,v 5.6 2009-04-24 23:42:32 hkmoffa Exp $
*/
/* Standard include files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* GOMA include files */
#define _AC_PARTICLES_C
#include "goma.h"
/* Global variables extern declared in ac_particles.h. */
int Particle_Dynamics; /* global toggle indicating particles are present. */
enum Particle_Model_t Particle_Model; /* What flavor of particle<->continuum stuff... */
dbl Particle_Model_Data[MAX_PARTICLE_MODEL_DATA_VALUES]; /* Real values for this model. */
int Particle_Number; /* number of discrete particles. */
particle_filename_s Particle_Restart_Filename; /* restart filename */
int Particle_Output_Stride; /* How often to output particle information. */
dbl Particle_Output_Time_Step; /* Output every these units. */
int Particle_Max_Time_Steps; /* max number of particle time steps if steady solution. */
enum Particle_Output_Format_t Particle_Output_Format; /* What kind of output file? */
int Particle_Full_Output_Stride; /* > 0 => full output every that many steps. */
particle_filename_s Particle_Full_Output_Filename; /* where to put them. */
int Particle_Number_Sample_Types; /* How many datasets to output? */
int *Particle_Number_Samples_Existing; /* How many are tagged for each sample type?*/
int *Particle_Number_Samples; /* How many particles to output for dataset #n? */
int *Particle_Number_Output_Variables; /* How many output vars for each sample. */
particle_variable_s **Particle_Output_Variables; /* List of variable indices to output for dataset #n */
particle_filename_s *Particle_Filename_Template; /* Template of where to put the data... */
dbl Particle_Density; /* Density of particle in problem units */
dbl Particle_Radius; /* Radius of particle in problem units. */
dbl Particle_Ratio; /* Real/computational particle ratio. */
int Particle_Show_Debug_Info; /* Show particle debug info. */
enum Particle_Domain_t Particle_Creation_Domain;
enum Particle_Domain_t Particle_Move_Domain;
particle_filename_s Particle_Creation_Domain_Filename;
particle_s Particle_Creation_Domain_Name;
particle_filename_s Particle_Move_Domain_Filename;
particle_s Particle_Move_Domain_Name;
dbl Particle_Creation_Domain_Reals[MAX_DOMAIN_REAL_VALUES];
dbl Particle_Move_Domain_Reals[MAX_DOMAIN_REAL_VALUES];
dbl xi_boundary_tolerances[3] = { XI_BOUNDARY_TOLERANCE0, XI_BOUNDARY_TOLERANCE1, XI_BOUNDARY_TOLERANCE2 };
#ifdef USE_CGM
FaceHandle *Particle_Creation_Domain_FaceHdl;
FaceHandle *Particle_Move_Domain_FaceHdl;
VolumeHandle *Particle_Creation_Domain_VolumeHdl;
VolumeHandle *Particle_Move_Domain_VolumeHdl;
int (* cgm_dist_func)(char const *, double *, double *, double *);
#endif
int Particle_Number_PBCs; /* Number of particle-related sideset BC's. */
PBC_t *PBCs; /* Particle boundary condition structures. */
/* Global variables that reside entirely within this file. */
static particle_t *particles_to_do, *particles_to_send;
static particle_t **element_particle_list_head;
static particle_t **backup_element_particle_list_head;
static int num_particles;
static int last_element_loaded;
static int velo_interp;
static int last_elements_nodes_loaded;
static int last_elements_fv_loaded;
static dbl last_fvs_xi_loaded[DIM];
static my_fv_old_t *my_fv_old;
static int max_newton_iterations, max_particle_iterations;
static dbl total_accum_ust, output_accum_ust, particle_accum_ust;
#ifdef PARALLEL
static int total_num_particle_ghosts;
static dbl communication_accum_ust;
#endif
static int nodes_per_element;
static int sides_per_element;
static dbl **node_coord;
static const Exo_DB * static_exo;
static dbl * static_x;
static dbl * static_x_old;
static dbl * static_xdot;
static dbl * static_xdot_old;
static dbl * static_resid_vector;
element_particle_info_t *element_particle_info;
static dbl my_volume;
static dbl *el_volume;
static FILE **pa_fp;
static FILE *pa_full_fp;
static int pdim; /* particle dimension (coordinate, velocities, etc.) */
static int mdim; /* mesh dimension */
static int Num_Impermeable_PBCs; /* Number of IMPERMEABLE particle boundary conditions. */
/* Function Prototypes */
static void find_exit_wound(const int, particle_t *, const dbl [3], const dbl [3], const dbl [3], const int, const int);
static dbl fill_element_volumes(const int, const int);
static int position_particle_uniformly(const int, particle_t *);
static char * construct_filename(const char *);
static void get_boundary_xi_newton(const dbl * const, const int, dbl *, const int, const dbl);
static particle_t * obtain_particle_space(const int);
static particle_t * create_a_particle(particle_t *, const int);
static int rejection_sample_a_particle(void);
static void initialize_and_create_new_particle(particle_t *, const int);
static void create_element_particle_info_maps(void);
static void handle_surface_interaction(particle_t *, dbl *, dbl *, int );
static void generate_source_particles(const dbl, const dbl, const dbl);
static void zero_a_particle(particle_t *);
static void initialize_surface_interactions(void);
static void load_element_information(const int);
static void load_element_node_coordinates(const int);
static void fill_hex_side_indices(const int, int[4]);
static void select_random_side_location(const int , const int, dbl *, dbl *);
static dbl get_side_area(const int, const int);
static void load_field_variables_at_xi(const int, dbl * const);
static int get_element_xi_newton(const int, const dbl * const, dbl *);
static dbl get_element_minimum_side_length(const int);
static void output_TECPLOT_zone_info(const dbl, const int, const int);
static void output_a_particle(particle_t * const, const dbl, const dbl, const int, const int, const int);
static void store_particle_data(particle_t * const);
static dbl compute_particle_dt(particle_t * const, const dbl);
static int move_particle(particle_t * const, dbl *, const dbl, const dbl);
static dbl my_distance(const dbl *, const dbl *, const int);
static void enforce_impermeable_PBCs(particle_t * const, dbl *, const dbl);
static dbl drand_standard_normal(void);
static dbl drand_truncated_normal(const dbl, const dbl);
static void dump_fcn(const char * const, const int, const int, particle_t *, ...);
static void fill_my_fv_old(void);
static void advance_a_particle(particle_t *, const dbl, const dbl, const int);
static void remove_from_element_particle_list(particle_t *, const int);
static void add_to_send_list(particle_t *);
static void add_to_do_list(particle_t *);
static void couple_to_continuum(void);
static void load_restart_file(void);
static void test_map_integrity(void);
/* Initialize the particles' positions and angular distributions.
* Currently, only a uniform distribution is available. This was a
* pain in the ass to do...
*
* Unfortunately, this code assumes e_begin = 0... Oh well.
*/
void
initialize_particles(const Exo_DB * exo,
dbl * const x,
dbl * const x_old,
dbl * const xdot,
dbl * const xdot_old,
dbl * const resid_vector)
{
dbl r, total_volume = 0.0;
int rejection, rejection_count, creation_count;
int i, j, el_index_begin, el_index_end, num_els, elem_id;
char time_of_day[80];
particle_t p;
#ifdef PARALLEL
int proc_index, left_over;
int *number_to_create = 0, *local_number_created = 0, *number_created = 0;
dbl *proc_volume_local = 0, *proc_volume = 0;
#endif
#ifdef USE_CGM
FILE *acis_fp;
BodyHandle * bodyHdl;
#endif
DPRINTF(stdout, "\nParticle initialization:\n"); fflush(stdout);
if((Particle_Model == SWIMMER_EXPLICIT || Particle_Model == SWIMMER_IMPLICIT) &&
pd_glob[0]->CoordinateSystem != CARTESIAN)
EH(-1, "You probably need to fix something first...");
/* Allocate one of these. I like the -> references instead of the
* . references... */
my_fv_old = (my_fv_old_t *)malloc(sizeof(my_fv_old_t));
/* Set these at the two entries into particle stuff so I don't have
* to pass them around EVERYWHERE. */
static_exo = exo;
static_x = x;
static_x_old = x_old;
static_xdot = xdot;
static_xdot_old = xdot_old;
static_resid_vector = resid_vector;
/* Get the appropriate dimension for the coordinate, velocity loops... */
pdim = pd_glob[0]->Num_Dim;
if(pd_glob[0]->CoordinateSystem == SWIRLING ||
pd_glob[0]->CoordinateSystem == PROJECTED_CARTESIAN)
pdim = pdim + 1;
/* Get mesh dimension */
mdim = static_exo->num_dim;
if(pd_glob[0]->e[R_MESH1])
EH(-1, "Cannot couple particles and deformable mesh, yet.");
if(mdim == 2)
{
if(static_exo->eb_elem_itype[0] != BIQUAD_QUAD &&
static_exo->eb_elem_itype[0] != BILINEAR_QUAD)
EH(-1, "Can only have 9-node and 4-node quadrilateral elements in 2D.");
sides_per_element = 4;
}
else /* static_exo->num_dim == mdim == 3 */
{
if(static_exo->eb_elem_itype[0] != TRIQUAD_HEX &&
static_exo->eb_elem_itype[0] != TRILINEAR_HEX)
EH(-1, "Can only handly 27-node and 8-node hex elements in 3D.");
sides_per_element = 6;
}
nodes_per_element = elem_info(NNODES, static_exo->eb_elem_itype[0]);
node_coord = (dbl **)malloc(nodes_per_element * sizeof(dbl *));
for(i = 0; i < nodes_per_element; i++)
node_coord[i] = (dbl *)malloc(DIM * sizeof(dbl));
element_particle_list_head = (particle_t **)malloc(static_exo->num_elems * sizeof(particle_t *));
backup_element_particle_list_head = (particle_t **)malloc(static_exo->num_elems * sizeof(particle_t *));
particles_to_send = NULL;
DPRINTF(stdout, " elements: "); fflush(stdout);
/* We will always allocate this element-sized space even though we
* might not always use it. There are too many use cases (some
* delayed) that complicate this... */
element_particle_info = (element_particle_info_t *)malloc(static_exo->num_elems * sizeof(element_particle_info_t));
for(i = 0; i < static_exo->num_elems; i++)
{
element_particle_info[i].PBC_side_id = (int *)malloc(sides_per_element * sizeof(int));
#ifdef PARALLEL
element_particle_info[i].owner_local_element_id = (int *)calloc((unsigned)sides_per_element, sizeof(int));
element_particle_info[i].num_ghost_target_elems = 0; /* number of times each particle needs to be ghosted. */
element_particle_info[i].ghost_proc = NULL; /* what proc do they ghost to? */
element_particle_info[i].ghost_local_elem_id = NULL; /* what's the local element id there? */
#endif
for(j = 0; j < sides_per_element; j++)
{
element_particle_info[i].PBC_side_id[j] = -1;
#ifdef PARALLEL
element_particle_info[i].owner_local_element_id[j] = -1;
#endif
}
if(Particle_Model == SWIMMER_IMPLICIT || Particle_Model == SWIMMER_EXPLICIT)
element_particle_info[i].source_term = (dbl *)calloc(MDE, sizeof(dbl));
else
element_particle_info[i].source_term = NULL;
element_particle_info[i].list_PBC_IMPERMEABLE = NULL;
element_particle_list_head[i] = NULL;
backup_element_particle_list_head[i] = NULL;
}
get_time(time_of_day);
DPRINTF(stdout, "done at %s\n", time_of_day); fflush(stdout);
DPRINTF(stdout, " output: "); fflush(stdout);
/* Open the output file for full output/restart if we've selected one. */
if(Particle_Full_Output_Stride)
{
if(!(pa_full_fp = fopen(construct_filename(Particle_Full_Output_Filename), "w")))
EH(-1, "Could not open Particle_Full_Output_Filename for zeroing.");
DPRINTF(stdout, "%s, ", Particle_Full_Output_Filename); fflush(stdout);
}
else
pa_full_fp = NULL;
if(Particle_Number_Sample_Types)
{
pa_fp = (FILE **)array_alloc(1, Particle_Number_Sample_Types, sizeof(FILE *));
for(i = 0; i < Particle_Number_Sample_Types; i++)
{
if(!(pa_fp[i] = fopen(construct_filename(Particle_Filename_Template[i]), "w")))
EH(-1, "Could not open a particle file for zeroing.");
DPRINTF(stdout, "%s, ", Particle_Filename_Template[i]); fflush(stdout);
Particle_Number_Samples_Existing[i] = 0;
if(Particle_Output_Format == TECPLOT)
{
fprintf(pa_fp[i], "TITLE = \"Goma Particles\"\n");
#ifdef PARALLEL
if(pdim == 2)
fprintf(pa_fp[i], "VARIABLES = \"X\", \"Y\", \"PROCID\", \"TIME\"");
else
fprintf(pa_fp[i], "VARIABLES = \"X\", \"Y\", \"Z\", \"PROCID\", \"TIME\"");
#else
if(pdim == 2)
fprintf(pa_fp[i], "VARIABLES = \"X\", \"Y\", \"TIME\"");
else
fprintf(pa_fp[i], "VARIABLES = \"X\", \"Y\", \"Z\", \"TIME\"");
#endif
for(j = 0; j < Particle_Number_Output_Variables[i]; j++)
fprintf(pa_fp[i], ", \"%s\"", Particle_Output_Variables[i][j]);
fprintf(pa_fp[i], "\n");
}
}
}
else
pa_fp = NULL;
if(Particle_Output_Format == TECPLOT &&
Particle_Number > 0)
output_TECPLOT_zone_info(0.0, 0, 1);
get_time(time_of_day);
DPRINTF(stdout, "done at %s\n", time_of_day); fflush(stdout);
last_element_loaded = -1;
last_elements_nodes_loaded = -1;
last_elements_fv_loaded = -1;
for(i = 0; i < DIM; i++)
last_fvs_xi_loaded[i] = -1.0e+10;
num_particles = 0;
DPRINTF(stdout, " volumes: "); fflush(stdout);
/* Position initial particles. */
if(Particle_Number > 0)
{
el_index_begin = static_exo->eb_ptr[0];
el_index_end = static_exo->eb_ptr[static_exo->num_elem_blocks];
num_els = el_index_end - el_index_begin;
el_volume = (dbl *)malloc(num_els * sizeof(dbl));
/* First get total element "volume" */
total_volume = my_volume = fill_element_volumes(el_index_begin, el_index_end);
#ifdef PARALLEL
proc_volume = (dbl *)calloc((unsigned)Num_Proc, sizeof(dbl));
proc_volume_local = (dbl *)calloc((unsigned)Num_Proc, sizeof(dbl));
proc_volume_local[ProcID] = my_volume;
MPI_Reduce(proc_volume_local, proc_volume, Num_Proc, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
/* Get total volume. */
total_volume = 0.0;
for(i = 0; i < Num_Proc; i++)
total_volume += proc_volume[i];
/*
DPRINTF(stderr, "Total volume = %g\n", total_volume); fflush(stderr);
*/
#endif
}
get_time(time_of_day);
DPRINTF(stdout, "done at %s\n", time_of_day); fflush(stdout);
DPRINTF(stdout, " domains: "); fflush(stdout);
if(Particle_Number > 0)
{
/* This is really the only check we can make unless I want to
* implement some pretty serious probabilistic sampling and
* in/out comparisons for ACIS objects. */
if(Particle_Creation_Domain == BRICK && Particle_Move_Domain == BRICK)
{
for(i = 0; i < 3; i++)
if(Particle_Move_Domain_Reals[2*i] > Particle_Creation_Domain_Reals[2*i] ||
Particle_Move_Domain_Reals[2*i+1] < Particle_Creation_Domain_Reals[2*i+1])
EH(-1, "Mismatch in BRICK bounds for Creation/Move.");
}
#ifdef USE_CGM
if(Particle_Creation_Domain == ACIS_OBJECT)
{
if(read_ACIS_file(Particle_Creation_Domain_Filename, bodyHdl) == -1)
EH(-1, "Error reading ACIS file.");
/* When getting the signed distance function, I'm assuming
* FACE's are supplied for 2D problems and VOLUME's are
* supplied for 3D problems. */
if(mdim == 2)
{
if(cgm_get_face_by_name((char const *)Particle_Creation_Domain_Name, &Particle_Creation_Domain_FaceHdl))
{
fprintf(stderr, "Could not find FACE named %s\n", Particle_Creation_Domain_Name);
EH(-1, "Could not find FACE named Particle_Creation_Domain_Name.");
}
cgm_dist_func = cgm_named_face_get_closest_boundary_keep;
}
else
{
if(cgm_get_volume_by_name((char const *)Particle_Creation_Domain_Name, &Particle_Creation_Domain_VolumeHdl))
{
fprintf(stderr, "Could not find VOLUME named %s\n", Particle_Creation_Domain_Name);
EH(-1, "Could not find VOLUME named Particle_Creation_Domain_Name.");
}
cgm_dist_func = cgm_named_volume_get_closest_boundary_keep;
}
DPRINTF(stdout, "creation=%s, ", Particle_Creation_Domain_Name); fflush(stdout);
}
#endif
}
#ifdef USE_CGM
if(Particle_Move_Domain == ACIS_OBJECT)
{
if(Particle_Creation_Domain == ACIS_OBJECT && Particle_Number > 0)
{
if(strcmp(Particle_Creation_Domain_Filename, Particle_Move_Domain_Filename))
if(read_ACIS_file(Particle_Move_Domain_Filename, bodyHdl) == -1)
EH(-1, "Error reading ACIS file.");
}
else
{
if(read_ACIS_file(Particle_Move_Domain_Filename, bodyHdl) == -1)
EH(-1, "Error reading ACIS file.");
}
/* When getting the signed distance function, I'm assuming
* FACE's are supplied for 2D problems and VOLUME's are
* supplied for 3D problems. If they get reassigned here
* it's not a problem... */
if(mdim == 2)
{
if(cgm_get_face_by_name((char const *)Particle_Move_Domain_Name, &Particle_Move_Domain_FaceHdl))
{
fprintf(stderr, "Could not find FACE named %s\n", Particle_Move_Domain_Name);
EH(-1, "Could not find FACE named Particle_Move_Domain_Name.");
}
cgm_dist_func = cgm_named_face_get_closest_boundary_keep;
}
else
{
if(cgm_get_volume_by_name((char const *)Particle_Move_Domain_Name, &Particle_Move_Domain_VolumeHdl))
{
fprintf(stderr, "Could not find VOLUME named %s\n", Particle_Move_Domain_Name);
EH(-1, "Could not find VOLUME named Particle_Creation_Domain_Name.");
}
cgm_dist_func = cgm_named_volume_get_closest_boundary_keep;
}
DPRINTF(stdout, "move=%s, ", Particle_Move_Domain_Name); fflush(stdout);
}
#endif
get_time(time_of_day);
DPRINTF(stdout, "done at %s\n", time_of_day); fflush(stdout);
DPRINTF(stdout, " particles: "); fflush(stdout);
/* If Particle_Number == -1, then we're performing the "particle
* mass test". One particle is placed on each element, and the
* total particle mass is interpolated by shape functions. The
* global value should sum to be, of course, (# particles) x (# real
* particles per computational particle) x (mass per real particle).
* Note that in this situation # particles = # elements. */
if(Particle_Number == -1)
{
el_index_begin = static_exo->eb_ptr[0];
el_index_end = static_exo->eb_ptr[static_exo->num_elem_blocks];
num_els = el_index_end - el_index_begin;
for(elem_id = el_index_begin; elem_id < el_index_end; elem_id++)
{
#ifdef PARALLEL
if(DPI_ptr->elem_owner[elem_id] != ProcID)
continue;
#endif
/* Place the particles at the arithmetic mean of the node
* coordinates. This will place the particle in the middle
* of the element as long as the element is non-concave. */
load_element_node_coordinates(elem_id);
zero_a_particle(&p);
for(i = 0; i < mdim; i++)
{
for(j = 0; j < nodes_per_element; j++)
p.x[i] += node_coord[j][i];
p.x[i] /= (dbl)nodes_per_element;
}
initialize_and_create_new_particle(&p, elem_id);
}
}
if(Particle_Number > 0)
{
rejection_count = 0;
creation_count = 0;
#ifdef PARALLEL
DPRINTF(stdout, "\n"); fflush(stdout);
/* Processor zero controls the global random sampling procedure.
* This is pretty trivial in single-processor. */
number_to_create = (int *)calloc((unsigned)Num_Proc, sizeof(int));
number_created = (int *)calloc((unsigned)Num_Proc, sizeof(int));
local_number_created = (int *)calloc((unsigned)Num_Proc, sizeof(int));
if(ProcID == 0)
{
while(creation_count < Particle_Number)
{
DPRINTF(stdout, " %d=", Particle_Number - creation_count); fflush(stdout);
/* First figure out how much each processor should generate, rounded down. */
left_over = Particle_Number - creation_count;
for(proc_index = 0; proc_index < Num_Proc; proc_index++)
{
number_to_create[proc_index] = (int)floor(proc_volume[proc_index]/total_volume * (Particle_Number - creation_count));
left_over -= number_to_create[proc_index];
}
/* Now we need to assign the leftovers. */
for(i = 0; i < left_over; i++)
{
/* This random number determines which processor we're going
* to try to create a particle on. */
r = drand48() * total_volume;
proc_index = 0;
while(proc_index < Num_Proc &&
r > proc_volume[proc_index])
{
r -= proc_volume[proc_index];
proc_index++;
}
if(proc_index == Num_Proc)
EH(-1, "proc_index == Num_Proc");
number_to_create[proc_index]++;
}
for(i = 0; i < Num_Proc - 1; i++)
DPRINTF(stdout, "%d+", number_to_create[i]);
DPRINTF(stdout, "%d", number_to_create[Num_Proc - 1]); fflush(stdout);
/* Now broadcast the vector containing how many particles each processor should create. */
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(number_to_create, Num_Proc, MPI_INT, 0, MPI_COMM_WORLD);
/* Create our particles while everyone else is creating theirs. */
rejection = 0;
for(i = 0; i < number_to_create[0]; i++)
rejection += rejection_sample_a_particle();
/* Now receive how many particles were created by everyone. */
memset(local_number_created, 0, Num_Proc * sizeof(int));
local_number_created[0] = number_to_create[0] - rejection;
MPI_Barrier(MPI_COMM_WORLD);
MPI_Reduce(local_number_created, number_created, Num_Proc, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
for(i = 0; i < Num_Proc; i++)
{
creation_count += number_created[i];
rejection_count += number_to_create[i] - number_created[i];
}
DPRINTF(stdout, " -> ");
for(i = 0; i < Num_Proc - 1; i++)
DPRINTF(stdout, "%d+", number_created[i]);
DPRINTF(stdout, "%d", number_created[Num_Proc - 1]); fflush(stderr);
DPRINTF(stdout, ", total C/R=%d/%d\n", creation_count, rejection_count);
}
number_to_create[0] = -1;
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(number_to_create, Num_Proc, MPI_INT, 0, MPI_COMM_WORLD);
}
else
{
while(number_to_create[0] != -1)
{
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(number_to_create, Num_Proc, MPI_INT, 0, MPI_COMM_WORLD);
if(number_to_create[0] == -1)
continue;
rejection = 0;
for(i = 0; i < number_to_create[ProcID]; i++)
rejection += rejection_sample_a_particle();
memset(local_number_created, 0, Num_Proc * sizeof(int));
local_number_created[ProcID] = number_to_create[ProcID] - rejection;
MPI_Barrier(MPI_COMM_WORLD);
MPI_Reduce(local_number_created, number_created, Num_Proc, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
}
}
DPRINTF(stdout, " total R = %d, ", rejection_count); fflush(stdout);
#else
/* Single processor version. */
while(creation_count < Particle_Number)
{
rejection = rejection_sample_a_particle();
rejection_count += rejection;
creation_count += (1 - rejection);
}
DPRINTF(stdout, "total R=%d, ", creation_count); fflush(stdout);
#endif
}
get_time(time_of_day);
DPRINTF(stdout, "done at %s\n", time_of_day); fflush(stdout);
DPRINTF(stdout, " restart: "); fflush(stdout);
if(strncmp(Particle_Restart_Filename, "<not active>", 12))
{
load_restart_file();
DPRINTF(stdout, "%s, ", Particle_Restart_Filename);
}
get_time(time_of_day);
DPRINTF(stdout, "done at %s\n", time_of_day); fflush(stdout);
DPRINTF(stdout, " surfaces: "); fflush(stdout);
initialize_surface_interactions();
get_time(time_of_day);
DPRINTF(stdout, "done at %s\n", time_of_day); fflush(stdout);
DPRINTF(stdout, " maps: "); fflush(stdout);
create_element_particle_info_maps();
get_time(time_of_day);
DPRINTF(stdout, "done at %s\n", time_of_day); fflush(stdout);
DPRINTF(stdout, " coupling: "); fflush(stdout);
couple_to_continuum();
#ifdef PARALLEL
DPRINTF(stdout, "%d ghosts, ", total_num_particle_ghosts);
#endif
get_time(time_of_day);
DPRINTF(stdout, "done at %s\n", time_of_day); fflush(stdout);
#ifdef PARALLEL
if(Particle_Number > 0)
{
free(proc_volume);
free(proc_volume_local);
free(number_to_create);
free(number_created);
free(local_number_created);
}
#endif
}
/* This routine should be called to find out what side a particle
* intersected as it left the problem domain. It handles cases where
* the particle intersected exactly one extended side on its way out,
* as well as the case of the particle intersecting two extended sides
* on its way out. In either case, it computes the point of
* intersection on the boundary by using Newton's method.
*
* All calls to get_boundary_xi_newton also modify fv->x, etc.
*
* Upon entering, p is a pointer to the particle in question, and
* old_el_id is the element it was in before it exited the domain.
*
* This is where the final particle location is computed, especially
* with respect to particle boundary conditions. */
static void
find_exit_wound(const int elem_id,
particle_t * p,
const dbl x_start[DIM],
const dbl x_end[DIM],
const dbl xi[DIM],
const int stack_count,
const int tolerance_level)
{
dbl xi_tmp[DIM], x_intersect[DIM], first_xi_tmp[DIM], second_xi_tmp[DIM] = {0.0, 0.0, 0.0};
dbl coeff[6];
/* dbl len; */
int exit_id1, exit_id2, exit_id3, PBC_id;
int bdry_crossed[3], num_crossed;
int i;
/* initialize first_xi_tmp */
for (i = 0; i < DIM; i++) {
first_xi_tmp[i] = 0;
}
if(stack_count == 100)
dump1(EXIT, p, "Hit 100 recursive calls to find_exit_wound(), something must be wrong...");
exit_id1 = exit_id2 = exit_id3 = -1;
/* Not necessary except for debugging. */
load_element_node_coordinates(elem_id);
if(mdim == 2)
{
/* For 2D, these are always 0.0. */
xi_tmp[2] = x_intersect[2] = 0.0;
memcpy(xi_tmp, xi, 2 * sizeof(dbl));
/* Write the physical coordinate line segment as a line, ax + by + c = 0 */
coeff[0] = x_start[1] - x_end[1];
coeff[1] = x_end[0] - x_start[0];
coeff[2] = -(coeff[0] * x_start[0] + coeff[1] * x_start[1]);
bdry_crossed[0] = (fabs(xi[0]) > 1.0) ? 1 : 0;
bdry_crossed[1] = (fabs(xi[1]) > 1.0) ? 1 : 0;
bdry_crossed[2] = 0;
num_crossed = bdry_crossed[0] + bdry_crossed[1];
if(num_crossed == 2)
{
/* Find the two intersecting sides (0-based). */
exit_id1 = (xi[0] < -1.0) ? 3 : 1;
exit_id2 = (xi[1] < -1.0) ? 0 : 2;
/* First, check the intersection corresponding to exit_id1. */
xi_tmp[0] = (exit_id1 == 3 ? -1.0 : 1.0);
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 0, 1.0e-12);
if(fabs(xi_tmp[1]) > 1.0) /* Not this intersection! Do exit_id2... */
{
xi_tmp[0] = xi[0]; /* reset initial guess for Newton's method. */
xi_tmp[1] = (exit_id2 == 0 ? -1.0 : 1.0);
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 1, 1.0e-12);
if(fabs(xi_tmp[0]) > 1.0)
dump20(EXIT, p, " x_start = (%g,%g,%g), x_end = (%g,%g,%g)\n bdry_crossed = (%d,%d,%d), exit_ids = (%d,%d,%d)\n xi = (%g,%g,%g), xi_tmp = (%g,%g,%g)\n stack_count = %d\nOh no! I couldn't find any good intersections! Bye-bye!\n",
x_start[0], x_start[1], x_start[2], x_end[0], x_end[1], x_end[2],
bdry_crossed[0], bdry_crossed[1], bdry_crossed[2], exit_id1, exit_id2, exit_id3,
xi[0], xi[1], xi[2], xi_tmp[0], xi_tmp[1], xi_tmp[2], stack_count);
exit_id1 = exit_id2; /* so exit_side has the right value. */
}
}
else
{
/* At this point only one of xi_tmp[*] is < -1.0 or > 1.0. */
if(bdry_crossed[0])
{
if(xi[0] > 1.0) /* to side 1 (0-based) */
{
xi_tmp[0] = 1.0;
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 0, 1.0e-12);
exit_id1 = 1;
}
else /* xi[0] < -1.0, to side 3 (0-based) */
{
xi_tmp[0] = -1.0;
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 0, 1.0e-12);
exit_id1 = 3;
}
}
else /* fabs(xi[1]) > 1.0 */
{
if(xi[1] > 1.0) /* to side 2 (0-based) */
{
xi_tmp[1] = 1.0;
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 1, 1.0e-12);
exit_id1 = 2;
}
else /* xi[1] < -1.0, to side 0 (0-based) */
{
xi_tmp[1] = -1.0;
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 1, 1.0e-12);
exit_id1 = 0;
}
}
}
}
else
{ /* 3D crossings */
/* Write the physical coordinate line segment as a line, x_start + t * (x_end - x_start) = 0 */
for(i = 0; i < 3; i++)
{
coeff[i] = x_start[i];
coeff[i+3] = x_end[i] - x_start[i];
}
/* len = nnorm(3, &(coeff[3])); */
/* We know len > 0.0 b/c we crossed a boundary to get into this
* routine; hence, we must have moved. */
/*
for(i = 3; i < 6; i++)
coeff[i] /= len;
*/
/* Initialize ... */
memcpy(xi_tmp, xi, 3 * sizeof(dbl));
/* Calculate which xi[0,1,2] faces we crossed */
num_crossed = 0;
for(i = 0; i < 3; i++)
{
bdry_crossed[i] = (fabs(xi[i]) > 1.0) ? 1 : 0;
num_crossed += bdry_crossed[i];
}
/*
fprintf(stderr, "Crossed %d boundaries: %d %d %d\n", num_crossed,
bdry_crossed[0], bdry_crossed[1], bdry_crossed[2]);
fprintf(stderr, " xi = (%g,%g,%g)\n", xi[0], xi[1], xi[2]);
dump_fcn(__FILE__, __LINE__, NO_EXIT, p, "");
*/
if(num_crossed == 3)
{
/* We are in one of the eight "triple" crossings. */
/* Find the three intersecting faces (0-based). */
exit_id1 = (xi[0] < -1.0) ? 3 : 1;
exit_id2 = (xi[1] < -1.0) ? 0 : 2; /* possibly needs to be switched */
exit_id3 = (xi[2] < -1.0) ? 4 : 5;
/* First, check the intersection corresponding to exit_id1. */
xi_tmp[0] = (exit_id1 == 3 ? -1.0 : 1.0);
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 0, xi_boundary_tolerances[tolerance_level]);
if(fabs(xi_tmp[1]) > 1.0 || fabs(xi_tmp[2]) > 1.0) /* Not this intersection! Do exit_id2... */
{
xi_tmp[0] = xi[0]; /* reset initial guess for Newton's method. */
xi_tmp[2] = xi[2];
xi_tmp[1] = (exit_id2 == 0 ? -1.0 : 1.0);
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 1, xi_boundary_tolerances[tolerance_level]);
if(fabs(xi_tmp[0]) > 1.0 || fabs(xi_tmp[2]) > 1.0) /* Ok,third one's a charm, right? */
{
xi_tmp[0] = xi[0]; /* reset initial guess for Newton's method. */
xi_tmp[1] = xi[1];
xi_tmp[2] = (exit_id3 == 4 ? -1.0 : 1.0);
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 2, xi_boundary_tolerances[tolerance_level]);
if(fabs(xi_tmp[0]) > 1.0 || fabs(xi_tmp[1]) > 1.0)
{
if(tolerance_level < 2)
{
if(Num_Proc > 1)
fprintf(stderr, "WARNING: Proc%d: Going to tolerance level %d in find_exit_wound().\n",
ProcID, tolerance_level + 1);
else
fprintf(stderr, "WARNING: Going to tolerance level %d in find_exit_wound().\n",
tolerance_level + 1);
fprintf(stderr, "first xi_tmp = (%g,%g,%g)\n", first_xi_tmp[0], first_xi_tmp[1], first_xi_tmp[2]);
fprintf(stderr, "second xi_tmp = (%g,%g,%g)\n", second_xi_tmp[0], second_xi_tmp[1], second_xi_tmp[2]);
dump20(NO_EXIT, p, " x_start = (%g,%g,%g), x_end = (%g,%g,%g)\n bdry_crossed = (%d,%d,%d), exit_ids = (%d,%d,%d)\n xi = (%g,%g,%g), xi_tmp = (%g,%g,%g)\n stack_count = %d\nOh no! I couldn't find any good intersections! Trying again...\n",
x_start[0], x_start[1], x_start[2], x_end[0], x_end[1], x_end[2],
bdry_crossed[0], bdry_crossed[1], bdry_crossed[2], exit_id1, exit_id2, exit_id3,
xi[0], xi[1], xi[2], xi_tmp[0], xi_tmp[1], xi_tmp[2], stack_count);
find_exit_wound(elem_id, p, x_start, x_end, xi, stack_count+1, tolerance_level+1);
return;
}
else
{
fprintf(stderr, "first xi_tmp = (%g,%g,%g)\n", first_xi_tmp[0], first_xi_tmp[1], first_xi_tmp[2]);
fprintf(stderr, "second xi_tmp = (%g,%g,%g)\n", second_xi_tmp[0], second_xi_tmp[1], second_xi_tmp[2]);
dump20(EXIT, p, " x_start = (%g,%g,%g), x_end = (%g,%g,%g)\n bdry_crossed = (%d,%d,%d), exit_ids = (%d,%d,%d)\n xi = (%g,%g,%g), xi_tmp = (%g,%g,%g)\n stack_count = %d\nOh no! I couldn't find any good intersections! Bye-bye!\n",
x_start[0], x_start[1], x_start[2], x_end[0], x_end[1], x_end[2],
bdry_crossed[0], bdry_crossed[1], bdry_crossed[2], exit_id1, exit_id2, exit_id3,
xi[0], xi[1], xi[2], xi_tmp[0], xi_tmp[1], xi_tmp[2], stack_count);
}
}
exit_id1 = exit_id3; /* exit_id1 should have the intersecting face. */
}
else
exit_id1 = exit_id2; /* exit_id1 should have the final intersecting face. */
}
}
else if(num_crossed == 2)
{
if(!bdry_crossed[0]) /* crossed xi[1] and xi[2] */
{
exit_id1 = (xi[1] < -1.0) ? 0 : 2;
exit_id2 = (xi[2] < -1.0) ? 4 : 5;
/* Check exit_id1 first. */
xi_tmp[1] = (exit_id1 == 0) ? -1.0 : 1.0;
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 1, xi_boundary_tolerances[tolerance_level]);
/*
fprintf(stderr, "xi_tmp = (%g,%g,%g)\n", xi_tmp[0], xi_tmp[1], xi_tmp[2]);
*/
if(fabs(xi_tmp[0]) > 1.0 || fabs(xi_tmp[2]) > 1.0) /* Not this intersction! Do exit_id2... */
{
memcpy(first_xi_tmp, xi_tmp, DIM*sizeof(dbl));
xi_tmp[1] = xi[1]; /* reset initial guess for Newton's method. */
xi_tmp[2] = (exit_id2 == 4) ? -1.0 : 1.0;
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 2, xi_boundary_tolerances[tolerance_level]);
if(fabs(xi_tmp[0]) > 1.0 || fabs(xi_tmp[1]) > 1.0)
{
if(tolerance_level < 2)
{
if(Num_Proc > 1)
fprintf(stderr, "WARNING: Proc%d: Going to tolerance level %d in find_exit_wound().\n",
ProcID, tolerance_level + 1);
else
fprintf(stderr, "WARNING: Going to tolerance level %d in find_exit_wound().\n",
tolerance_level + 1);
fprintf(stderr, "first xi_tmp = (%g,%g,%g)\n", first_xi_tmp[0], first_xi_tmp[1], first_xi_tmp[2]);
dump20(NO_EXIT, p, " x_start = (%g,%g,%g), x_end = (%g,%g,%g)\n bdry_crossed = (%d,%d,%d), exit_ids = (%d,%d,%d)\n xi = (%g,%g,%g), xi_tmp = (%g,%g,%g)\n stack_count = %d\nOh no! I couldn't find any good intersections! Trying again...\n",
x_start[0], x_start[1], x_start[2], x_end[0], x_end[1], x_end[2],
bdry_crossed[0], bdry_crossed[1], bdry_crossed[2], exit_id1, exit_id2, exit_id3,
xi[0], xi[1], xi[2], xi_tmp[0], xi_tmp[1], xi_tmp[2], stack_count);
find_exit_wound(elem_id, p, x_start, x_end, xi, stack_count+1, tolerance_level+1);
return;
}
else
{
fprintf(stderr, "first xi_tmp = (%g,%g,%g)\n", first_xi_tmp[0], first_xi_tmp[1], first_xi_tmp[2]);
dump20(EXIT, p, " x_start = (%g,%g,%g), x_end = (%g,%g,%g)\n bdry_crossed = (%d,%d,%d), exit_ids = (%d,%d,%d)\n xi = (%g,%g,%g), xi_tmp = (%g,%g,%g)\n stack_count = %d\nOh no! I couldn't find any good intersections! Bye-bye!\n",
x_start[0], x_start[1], x_start[2], x_end[0], x_end[1], x_end[2],
bdry_crossed[0], bdry_crossed[1], bdry_crossed[2], exit_id1, exit_id2, exit_id3,
xi[0], xi[1], xi[2], xi_tmp[0], xi_tmp[1], xi_tmp[2], stack_count);
}
}
else
exit_id1 = exit_id2; /* exit_id1 should have the intersecting face. */
}
}
else if(!bdry_crossed[1]) /* crossed xi[0] and xi[2] */
{
exit_id1 = (xi[0] < -1.0) ? 3 : 1;
exit_id2 = (xi[2] < -1.0) ? 4 : 5;
/* Check exit_id1 first. */
xi_tmp[0] = (exit_id1 == 3) ? -1.0 : 1.0;
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 0, xi_boundary_tolerances[tolerance_level]);
if(fabs(xi_tmp[1]) > 1.0 || fabs(xi_tmp[2]) > 1.0) /* Not this intersction! Do exit_id2... */
{
memcpy(first_xi_tmp, xi_tmp, DIM*sizeof(dbl));
xi_tmp[0] = xi[0]; /* reset initial guess for Newton's method. */
xi_tmp[2] = (exit_id2 == 4) ? -1.0 : 1.0;
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 2, xi_boundary_tolerances[tolerance_level]);
if(fabs(xi_tmp[0]) > 1.0 || fabs(xi_tmp[1]) > 1.0)
{
if(tolerance_level < 2)
{
if(Num_Proc > 1)
fprintf(stderr, "WARNING: Proc%d: Going to tolerance level %d in find_exit_wound().\n",
ProcID, tolerance_level + 1);
else
fprintf(stderr, "WARNING: Going to tolerance level %d in find_exit_wound().\n",
tolerance_level + 1);
fprintf(stderr, "first xi_tmp = (%g,%g,%g)\n", first_xi_tmp[0], first_xi_tmp[1], first_xi_tmp[2]);
dump20(NO_EXIT, p, " x_start = (%g,%g,%g), x_end = (%g,%g,%g)\n bdry_crossed = (%d,%d,%d), exit_ids = (%d,%d,%d)\n xi = (%g,%g,%g), xi_tmp = (%g,%g,%g)\n stack_count = %d\nOh no! I couldn't find any good intersections! Trying again...\n",
x_start[0], x_start[1], x_start[2], x_end[0], x_end[1], x_end[2],
bdry_crossed[0], bdry_crossed[1], bdry_crossed[2], exit_id1, exit_id2, exit_id3,
xi[0], xi[1], xi[2], xi_tmp[0], xi_tmp[1], xi_tmp[2], stack_count);
find_exit_wound(elem_id, p, x_start, x_end, xi, stack_count+1, tolerance_level+1);
return;
}
else
{
fprintf(stderr, "first xi_tmp = (%g,%g,%g)\n", first_xi_tmp[0], first_xi_tmp[1], first_xi_tmp[2]);
dump20(EXIT, p, " x_start = (%g,%g,%g), x_end = (%g,%g,%g)\n bdry_crossed = (%d,%d,%d), exit_ids = (%d,%d,%d)\n xi = (%g,%g,%g), xi_tmp = (%g,%g,%g)\n stack_count = %d\nOh no! I couldn't find any good intersections! Bye-bye!\n",
x_start[0], x_start[1], x_start[2], x_end[0], x_end[1], x_end[2],
bdry_crossed[0], bdry_crossed[1], bdry_crossed[2], exit_id1, exit_id2, exit_id3,
xi[0], xi[1], xi[2], xi_tmp[0], xi_tmp[1], xi_tmp[2], stack_count);
}
}
else
exit_id1 = exit_id2; /* exit_id1 should have the intersecting face. */
}
}
else /* crossed xi[0] and xi[1] */
{
exit_id1 = (xi[0] < -1.0) ? 3 : 1;
exit_id2 = (xi[1] < -1.0) ? 0 : 2;
/* Check exit_id1 first. */
xi_tmp[0] = (exit_id1 == 3) ? -1.0 : 1.0;
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 0, xi_boundary_tolerances[tolerance_level]);
if(fabs(xi_tmp[1]) > 1.0 || fabs(xi_tmp[2]) > 1.0) /* Not this intersction! Do exit_id2... */
{
memcpy(first_xi_tmp, xi_tmp, DIM*sizeof(dbl));
xi_tmp[0] = xi[0]; /* reset initial guess for Newton's method. */
xi_tmp[1] = (exit_id2 == 0) ? -1.0 : 1.0;
get_boundary_xi_newton(coeff, elem_id, xi_tmp, 1, xi_boundary_tolerances[tolerance_level]);
if(fabs(xi_tmp[0]) > 1.0 || fabs(xi_tmp[2]) > 1.0)
{
if(tolerance_level < 2)
{