1 #if HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 /** @file
6 * operations on sections of 2D arrays
7 */
8
9 #include "global.h" /* used only to define datatypes */
10 #include "drap.h"
11
12 #define PARIO_MAX(a,b) (((a) >= (b)) ? (a) : (b))
13 #define PARIO_MIN(a,b) (((a) <= (b)) ? (a) : (b))
14
15
16 /**
17 * check if two patches are conforming (dimensions are divisible)
18 */
dai_patches_conforming(Integer * ailo,Integer * aihi,Integer * ajlo,Integer * ajhi,Integer * bilo,Integer * bihi,Integer * bjlo,Integer * bjhi)19 logical dai_patches_conforming(
20 Integer* ailo, Integer* aihi, Integer* ajlo, Integer* ajhi,
21 Integer* bilo, Integer* bihi, Integer* bjlo, Integer* bjhi)
22 {
23 Integer mismatch;
24 Integer adim1, bdim1, adim2, bdim2;
25 adim1 = *aihi - *ailo +1;
26 adim2 = *ajhi - *ajlo +1;
27 bdim1 = *bihi - *bilo +1;
28 bdim2 = *bjhi - *bjlo +1;
29 mismatch = (adim1<bdim1) ? bdim1%adim1 : adim1%bdim1;
30 mismatch += (adim2<bdim2) ? bdim2%adim2 : adim2%bdim2;
31 if(mismatch)return(FALSE);
32 else return(TRUE);
33 }
34
35
36 /**
37 * check if patches are identical
38 */
dai_comp_patch(Integer * ilo,Integer * ihi,Integer * jlo,Integer * jhi,Integer * ilop,Integer * ihip,Integer * jlop,Integer * jhip)39 logical dai_comp_patch(
40 Integer* ilo, Integer* ihi, Integer* jlo, Integer* jhi,
41 Integer* ilop, Integer* ihip, Integer* jlop, Integer* jhip)
42 {
43 if(*ihip != *ihi || *ilop != *ilo || *jhip != *jhi || *jlop != *jlo)
44 return(FALSE);
45 else return(TRUE);
46 }
47
48
49 /**
50 * check if patches have a nontrivial intersection
51 * if yes, find the intersection and update [ilop:ihip, jlop:jhip]
52 */
dai_patch_intersect(Integer ilo,Integer ihi,Integer jlo,Integer jhi,Integer * ilop,Integer * ihip,Integer * jlop,Integer * jhip)53 logical dai_patch_intersect(
54 Integer ilo, Integer ihi, Integer jlo, Integer jhi,
55 Integer* ilop, Integer* ihip, Integer* jlop, Integer* jhip)
56 {
57 /* check consistency of patch coordinates */
58 if( ihi < ilo || jhi < jlo) return FALSE; /* inconsistent */
59 if( *ihip < *ilop || *jhip < *jlop) return FALSE; /* inconsistent */
60
61 /* find the intersection and update (ilop: ihip, jlop: jhip) */
62 if( ihi < *ilop || *ihip < ilo) return FALSE; /* don't intersect */
63 if( jhi < *jlop || *jhip < jlo) return FALSE; /* don't intersect */
64 *ilop = PARIO_MAX(ilo,*ilop);
65 *ihip = PARIO_MIN(ihi,*ihip);
66 *jlop = PARIO_MAX(jlo,*jlop);
67 *jhip = PARIO_MIN(jhi,*jhip);
68
69 return(TRUE);
70 }
71
72
73 /**
74 * check if sections have a nontrivial intersection
75 * if yes, find the intersection and update [ilop:ihip, jlop:jhip]
76 * section format
77 */
dai_section_intersect(section_t sref,section_t * sadj)78 logical dai_section_intersect(section_t sref, section_t *sadj)
79 {
80 Integer ndim = sref.ndim;
81 Integer i;
82 logical isconsistent = TRUE;
83 /* check that patches have same dimension */
84 if (ndim != sadj->ndim) isconsistent = FALSE;
85 /* check consistency of patch coordinates */
86 if (isconsistent) {
87 for (i=0; i<ndim; i++) {
88 if (sref.hi[i] < sref.lo[i]) isconsistent = FALSE;
89 if (sadj->hi[i] < sadj->lo[i]) isconsistent = FALSE;
90 }
91 }
92 /* check to see if there is an intersection */
93 if (isconsistent) {
94 for (i=0; i<ndim; i++) {
95 if (sref.hi[i] < sadj->lo[i]) isconsistent = FALSE;
96 if (sadj->hi[i] < sref.lo[i]) isconsistent = FALSE;
97 }
98 }
99 /* if there is an intersection then return it in sadj */
100 if (isconsistent) {
101 for (i=0; i<ndim; i++) {
102 sadj->lo[i] = PARIO_MAX(sref.lo[i],sadj->lo[i]);
103 sadj->hi[i] = PARIO_MIN(sref.hi[i],sadj->hi[i]);
104 }
105 }
106 return (isconsistent);
107 }
108