xref: /petsc/src/ts/characteristic/impls/da/slda.c (revision bebe2cf65d55febe21a5af8db2bd2e168caaa2e7)
1 #include <../src/ts/characteristic/impls/da/slda.h>       /*I  "petsccharacteristic.h"  I*/
2 #include <petscdmda.h>
3 #include <petscviewer.h>
4 
5 #undef __FUNCT__
6 #define __FUNCT__ "CharacteristicView_DA"
7 PetscErrorCode CharacteristicView_DA(Characteristic c, PetscViewer viewer)
8 {
9   Characteristic_DA *da = (Characteristic_DA*) c->data;
10   PetscBool         iascii, isstring;
11   PetscErrorCode    ierr;
12 
13   PetscFunctionBegin;
14   /* Pull out field names from DM */
15   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
16   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERSTRING, &isstring);CHKERRQ(ierr);
17   if (iascii) {
18     ierr = PetscViewerASCIIPrintf(viewer,"  DMDA: dummy=%D\n", da->dummy);CHKERRQ(ierr);
19   } else if (isstring) {
20     ierr = PetscViewerStringSPrintf(viewer,"dummy %D", da->dummy);CHKERRQ(ierr);
21   }
22   PetscFunctionReturn(0);
23 }
24 
25 #undef __FUNCT__
26 #define __FUNCT__ "CharacteristicDestroy_DA"
27 PetscErrorCode CharacteristicDestroy_DA(Characteristic c)
28 {
29   Characteristic_DA *da = (Characteristic_DA*) c->data;
30   PetscErrorCode    ierr;
31 
32   PetscFunctionBegin;
33   ierr = PetscFree(da);CHKERRQ(ierr);
34   PetscFunctionReturn(0);
35 }
36 
37 #undef __FUNCT__
38 #define __FUNCT__ "CharacteristicSetUp_DA"
39 PetscErrorCode CharacteristicSetUp_DA(Characteristic c)
40 {
41   PetscMPIInt    blockLen[2];
42   MPI_Aint       indices[2];
43   MPI_Datatype   oldtypes[2];
44   PetscInt       dim, numValues;
45   PetscErrorCode ierr;
46 
47   ierr = DMDAGetInfo(c->velocityDA, &dim, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0);CHKERRQ(ierr);
48   if (c->structured) c->numIds = dim;
49   else c->numIds = 3;
50   if (c->numFieldComp > MAX_COMPONENTS) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "The maximum number of fields allowed is %d, you have %d. You can recompile after increasing MAX_COMPONENTS.", MAX_COMPONENTS, c->numFieldComp);
51   numValues = 4 + MAX_COMPONENTS;
52 
53   /* Create new MPI datatype for communication of characteristic point structs */
54   blockLen[0] = 1+c->numIds; indices[0] = 0;                              oldtypes[0] = MPIU_INT;
55   blockLen[1] = numValues;   indices[1] = (1+c->numIds)*sizeof(PetscInt); oldtypes[1] = MPIU_SCALAR;
56   ierr = MPI_Type_create_struct(2, blockLen, indices, oldtypes, &c->itemType);CHKERRQ(ierr);
57   ierr = MPI_Type_commit(&c->itemType);CHKERRQ(ierr);
58 
59   /* Initialize the local queue for char foot values */
60   ierr = VecGetLocalSize(c->velocity, &c->queueMax);CHKERRQ(ierr);
61   ierr = PetscMalloc1(c->queueMax, &c->queue);CHKERRQ(ierr);
62   c->queueSize = 0;
63 
64   /* Allocate communication structures */
65   if (c->numNeighbors <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Invalid number of neighbors %d. Call CharactersiticSetNeighbors() before setup.", c->numNeighbors);
66   ierr = PetscMalloc1(c->numNeighbors, &c->needCount);CHKERRQ(ierr);
67   ierr = PetscMalloc1(c->numNeighbors, &c->localOffsets);CHKERRQ(ierr);
68   ierr = PetscMalloc1(c->numNeighbors, &c->fillCount);CHKERRQ(ierr);
69   ierr = PetscMalloc1(c->numNeighbors, &c->remoteOffsets);CHKERRQ(ierr);
70   ierr = PetscMalloc1(c->numNeighbors-1, &c->request);CHKERRQ(ierr);
71   ierr = PetscMalloc1(c->numNeighbors-1,  &c->status);CHKERRQ(ierr);
72   PetscFunctionReturn(0);
73 }
74 
75 #undef __FUNCT__
76 #define __FUNCT__ "CharacteristicCreate_DA"
77 PETSC_EXTERN PetscErrorCode CharacteristicCreate_DA(Characteristic c)
78 {
79   Characteristic_DA *da;
80   PetscErrorCode    ierr;
81 
82   PetscFunctionBegin;
83   ierr    = PetscNew(&da);CHKERRQ(ierr);
84   ierr    = PetscMemzero(da, sizeof(Characteristic_DA));CHKERRQ(ierr);
85   ierr    = PetscLogObjectMemory((PetscObject)c, sizeof(Characteristic_DA));CHKERRQ(ierr);
86   c->data = (void*) da;
87 
88   c->ops->setup   = CharacteristicSetUp_DA;
89   c->ops->destroy = CharacteristicDestroy_DA;
90   c->ops->view    = CharacteristicView_DA;
91 
92   da->dummy = 0;
93   PetscFunctionReturn(0);
94 }
95 
96 #undef __FUNCT__
97 #define __FUNCT__ "DMDAMapCoordsToPeriodicDomain"
98 /* -----------------------------------------------------------------------------
99    Checks for periodicity of a DM and Maps points outside of a domain back onto the domain
100    using appropriate periodicity. At the moment assumes only a 2-D DMDA.
101    ----------------------------------------------------------------------------------------*/
102 PetscErrorCode DMDAMapCoordsToPeriodicDomain(DM da, PetscScalar *x, PetscScalar *y)
103 {
104   DMBoundaryType bx, by;
105   PetscInt       dim, gx, gy;
106   PetscErrorCode ierr;
107 
108   PetscFunctionBegin;
109   ierr = DMDAGetInfo(da, &dim, &gx, &gy, 0, 0, 0, 0, 0, 0, &bx, &by, 0, 0);
110 
111   if (bx == DM_BOUNDARY_PERIODIC) {
112       while (*x >= (PetscScalar)gx) *x -= (PetscScalar)gx;
113       while (*x < 0.0)              *x += (PetscScalar)gx;
114     }
115     if (by == DM_BOUNDARY_PERIODIC) {
116       while (*y >= (PetscScalar)gy) *y -= (PetscScalar)gy;
117       while (*y < 0.0)              *y += (PetscScalar)gy;
118     }
119   PetscFunctionReturn(ierr);
120 }
121