xref: /petsc/src/dm/impls/da/dadestroy.c (revision e5a36eccef3d6b83a2c625c30d0dfd5adb4001f2)
1 
2 /*
3   Code for manipulating distributed regular arrays in parallel.
4 */
5 
6 #include <petsc/private/dmdaimpl.h>    /*I   "petscdmda.h"   I*/
7 
8 /*
9    DMDestroy_Private - handles the work vectors created by DMGetGlobalVector() and DMGetLocalVector()
10 
11 */
12 PetscErrorCode  DMDestroy_Private(DM dm,PetscBool  *done)
13 {
14   PetscErrorCode ierr;
15   PetscErrorCode i,cnt = 0;
16 
17   PetscFunctionBegin;
18   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19   *done = PETSC_FALSE;
20 
21   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
22     if (dm->localin[i])  cnt++;
23     if (dm->globalin[i]) cnt++;
24   }
25 
26   if (--((PetscObject)dm)->refct - cnt > 0) PetscFunctionReturn(0);
27 
28   /*
29          Need this test because the dm references the vectors that
30      reference the dm, so destroying the dm calls destroy on the
31      vectors that cause another destroy on the dm
32   */
33   if (((PetscObject)dm)->refct < 0) PetscFunctionReturn(0);
34   ((PetscObject)dm)->refct = 0;
35 
36   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
37     if (dm->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
38     ierr = VecDestroy(&dm->localin[i]);CHKERRQ(ierr);
39     if (dm->globalout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a global vector obtained with DMGetGlobalVector()");
40     ierr = VecDestroy(&dm->globalin[i]);CHKERRQ(ierr);
41   }
42   ierr = ISLocalToGlobalMappingDestroy(&dm->ltogmap);CHKERRQ(ierr);
43 
44   *done = PETSC_TRUE;
45   PetscFunctionReturn(0);
46 }
47 
48 PetscErrorCode  DMDestroy_DA(DM da)
49 {
50   PetscErrorCode ierr;
51   PetscErrorCode i;
52   DM_DA          *dd = (DM_DA*)da->data;
53 
54   PetscFunctionBegin;
55   /* destroy the external/common part */
56   for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
57     ierr = PetscFree(dd->startghostedout[i]);CHKERRQ(ierr);
58     ierr = PetscFree(dd->startghostedin[i]);CHKERRQ(ierr);
59     ierr = PetscFree(dd->startout[i]);CHKERRQ(ierr);
60     ierr = PetscFree(dd->startin[i]);CHKERRQ(ierr);
61   }
62 
63   ierr = VecScatterDestroy(&dd->gtol);CHKERRQ(ierr);
64   ierr = VecScatterDestroy(&dd->ltol);CHKERRQ(ierr);
65   ierr = VecDestroy(&dd->natural);CHKERRQ(ierr);
66   ierr = VecScatterDestroy(&dd->gton);CHKERRQ(ierr);
67   ierr = AODestroy(&dd->ao);CHKERRQ(ierr);
68   ierr = PetscFree(dd->aotype);CHKERRQ(ierr);
69 
70   ierr = PetscFree(dd->lx);CHKERRQ(ierr);
71   ierr = PetscFree(dd->ly);CHKERRQ(ierr);
72   ierr = PetscFree(dd->lz);CHKERRQ(ierr);
73 
74   ierr = PetscFree(dd->refine_x_hier);CHKERRQ(ierr);
75   ierr = PetscFree(dd->refine_y_hier);CHKERRQ(ierr);
76   ierr = PetscFree(dd->refine_z_hier);CHKERRQ(ierr);
77 
78   if (dd->fieldname) {
79     for (i=0; i<dd->w; i++) {
80       ierr = PetscFree(dd->fieldname[i]);CHKERRQ(ierr);
81     }
82     ierr = PetscFree(dd->fieldname);CHKERRQ(ierr);
83   }
84   if (dd->coordinatename) {
85     for (i=0; i<da->dim; i++) {
86       ierr = PetscFree(dd->coordinatename[i]);CHKERRQ(ierr);
87     }
88     ierr = PetscFree(dd->coordinatename);CHKERRQ(ierr);
89   }
90   ierr = ISColoringDestroy(&dd->localcoloring);CHKERRQ(ierr);
91   ierr = ISColoringDestroy(&dd->ghostedcoloring);CHKERRQ(ierr);
92 
93   ierr = PetscFree(dd->neighbors);CHKERRQ(ierr);
94   ierr = PetscFree(dd->dfill);CHKERRQ(ierr);
95   ierr = PetscFree(dd->ofill);CHKERRQ(ierr);
96   ierr = PetscFree(dd->ofillcols);CHKERRQ(ierr);
97   ierr = PetscFree(dd->e);CHKERRQ(ierr);
98   ierr = ISDestroy(&dd->ecorners);CHKERRQ(ierr);
99 
100   ierr = PetscObjectComposeFunction((PetscObject)da,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
101 
102   /* ierr = PetscSectionDestroy(&dd->defaultGlobalSection);CHKERRQ(ierr); */
103   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
104   ierr = PetscFree(dd);CHKERRQ(ierr);
105   PetscFunctionReturn(0);
106 }
107