Actual source code: ao.c

petsc-3.3-p7 2013-05-11
  2: /*  
  3:    Defines the abstract operations on AO (application orderings) 
  4: */
  5: #include <../src/dm/ao/aoimpl.h>      /*I "petscao.h" I*/

  7: /* Logging support */
  8: PetscClassId  AO_CLASSID;
  9: PetscLogEvent  AO_PetscToApplication, AO_ApplicationToPetsc;

 13: /*@C
 14:    AOView - Displays an application ordering.

 16:    Collective on AO and PetscViewer

 18:    Input Parameters:
 19: +  ao - the application ordering context
 20: -  viewer - viewer used for display

 22:    Level: intermediate

 24:     Options Database Key:
 25: .   -ao_view - calls AOView() at end of AOCreate()

 27:    Note:
 28:    The available visualization contexts include
 29: +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
 30: -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
 31:          output where only the first processor opens
 32:          the file.  All other processors send their 
 33:          data to the first processor to print. 

 35:    The user can open an alternative visualization context with
 36:    PetscViewerASCIIOpen() - output to a specified file.

 38: .keywords: application ordering

 40: .seealso: PetscViewerASCIIOpen()
 41: @*/
 42: PetscErrorCode  AOView(AO ao,PetscViewer viewer)
 43: {

 48:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(((PetscObject)ao)->comm);
 50:   (*ao->ops->view)(ao,viewer);
 51:   return(0);
 52: }

 56: /*@C
 57:    AODestroy - Destroys an application ordering.

 59:    Collective on AO

 61:    Input Parameters:
 62: .  ao - the application ordering context

 64:    Level: beginner

 66: .keywords: destroy, application ordering

 68: .seealso: AOCreate()
 69: @*/
 70: PetscErrorCode  AODestroy(AO *ao)
 71: {

 75:   if (!*ao) return(0);
 77:   if (--((PetscObject)(*ao))->refct > 0) {*ao = 0; return(0);}
 78:   /* if memory was published with AMS then destroy it */
 79:   PetscObjectDepublish((*ao));
 80:   /* destroy the internal part */
 81:   if ((*ao)->ops->destroy) {
 82:     (*(*ao)->ops->destroy)(*ao);
 83:   }
 84:   PetscHeaderDestroy(ao);
 85:   return(0);
 86: }


 89: #include <../src/vec/is/impls/general/general.h>
 90: /* ---------------------------------------------------------------------*/
 93: /*@
 94:    AOPetscToApplicationIS - Maps an index set in the PETSc ordering to 
 95:    the application-defined ordering.

 97:    Collective on AO and IS

 99:    Input Parameters:
100: +  ao - the application ordering context
101: -  is - the index set; this is replaced with its mapped values

103:    Output Parameter:
104: .  is - the mapped index set

106:    Level: intermediate

108:    Notes:
109:    The index set cannot be of type stride or block
110:    
111:    Any integers in ia[] that are negative are left unchanged. This 
112:          allows one to convert, for example, neighbor lists that use negative
113:          entries to indicate nonexistent neighbors due to boundary conditions
114:          etc.

116: .keywords: application ordering, mapping

118: .seealso: AOCreateBasic(), AOView(),AOApplicationToPetsc(),
119:           AOApplicationToPetscIS(),AOPetscToApplication()
120: @*/
121: PetscErrorCode  AOPetscToApplicationIS(AO ao,IS is)
122: {
124:   PetscInt       n;
125:   PetscInt       *ia;

130:   ISToGeneral(is);
131:   /* we cheat because we know the is is general and that we can change the indices */
132:   ISGetIndices(is,(const PetscInt**)&ia);
133:   ISGetLocalSize(is,&n);
134:   (*ao->ops->petsctoapplication)(ao,n,ia);
135:   ISRestoreIndices(is,(const PetscInt**)&ia);
136:   return(0);
137: }

141: /*@
142:    AOApplicationToPetscIS - Maps an index set in the application-defined
143:    ordering to the PETSc ordering.

145:    Collective on AO and IS

147:    Input Parameters:
148: +  ao - the application ordering context
149: -  is - the index set; this is replaced with its mapped values

151:    Output Parameter:
152: .  is - the mapped index set

154:    Level: beginner

156:    Note:
157:    The index set cannot be of type stride or block
158:    
159:    Any integers in ia[] that are negative are left unchanged. This 
160:    allows one to convert, for example, neighbor lists that use negative
161:    entries to indicate nonexistent neighbors due to boundary conditions, etc.

163: .keywords: application ordering, mapping

165: .seealso: AOCreateBasic(), AOView(), AOPetscToApplication(),
166:           AOPetscToApplicationIS(), AOApplicationToPetsc()
167: @*/
168: PetscErrorCode  AOApplicationToPetscIS(AO ao,IS is)
169: {
171:   PetscInt       n,*ia;

176:   ISToGeneral(is);
177:   /* we cheat because we know the is is general and that we can change the indices */
178:   ISGetIndices(is,(const PetscInt**)&ia);
179:   ISGetLocalSize(is,&n);
180:   (*ao->ops->applicationtopetsc)(ao,n,ia);
181:   ISRestoreIndices(is,(const PetscInt**)&ia);
182:   return(0);
183: }

187: /*@
188:    AOPetscToApplication - Maps a set of integers in the PETSc ordering to 
189:    the application-defined ordering.

191:    Collective on AO

193:    Input Parameters:
194: +  ao - the application ordering context
195: .  n - the number of integers
196: -  ia - the integers; these are replaced with their mapped value

198:    Output Parameter:
199: .   ia - the mapped integers

201:    Level: beginner

203:    Note:
204:    Any integers in ia[] that are negative are left unchanged. This 
205:    allows one to convert, for example, neighbor lists that use negative
206:    entries to indicate nonexistent neighbors due to boundary conditions, etc.

208: .keywords: application ordering, mapping

210: .seealso: AOCreateBasic(), AOView(),AOApplicationToPetsc(),
211:           AOPetscToApplicationIS(), AOApplicationToPetsc()
212: @*/
213: PetscErrorCode  AOPetscToApplication(AO ao,PetscInt n,PetscInt ia[])
214: {

220:   (*ao->ops->petsctoapplication)(ao,n,ia);
221:   return(0);
222: }

226: /*@
227:    AOApplicationToPetsc - Maps a set of integers in the application-defined
228:    ordering to the PETSc ordering.

230:    Collective on AO

232:    Input Parameters:
233: +  ao - the application ordering context
234: .  n - the number of integers
235: -  ia - the integers; these are replaced with their mapped value

237:    Output Parameter:
238: .   ia - the mapped integers

240:    Level: beginner

242:    Note:
243:    Any integers in ia[] that are negative are left unchanged. This 
244:    allows one to convert, for example, neighbor lists that use negative
245:    entries to indicate nonexistent neighbors due to boundary conditions, etc.

247: .keywords: application ordering, mapping

249: .seealso: AOCreateBasic(), AOView(), AOPetscToApplication(),
250:           AOPetscToApplicationIS(), AOApplicationToPetsc()
251: @*/
252: PetscErrorCode  AOApplicationToPetsc(AO ao,PetscInt n,PetscInt ia[])
253: {

259:   (*ao->ops->applicationtopetsc)(ao,n,ia);
260:   return(0);
261: }

265: /*@
266:   AOPetscToApplicationPermuteInt - Permutes an array of blocks of integers
267:   in the PETSc ordering to the application-defined ordering.

269:   Collective on AO

271:   Input Parameters:
272: + ao    - The application ordering context
273: . block - The block size
274: - array - The integer array

276:   Output Parameter:
277: . array - The permuted array

279:   Note: The length of the array should be block*N, where N is length
280:   provided to the AOCreate*() method that created the AO.

282:   The permutation takes array[i_pet] --> array[i_app], where i_app is
283:   the index of 'i' in the application ordering and i_pet is the index
284:   of 'i' in the petsc ordering.

286:   Level: beginner

288: .keywords: application ordering, mapping
289: .seealso: AOCreateBasic(), AOView(), AOApplicationToPetsc(), AOPetscToApplicationIS()
290: @*/
291: PetscErrorCode  AOPetscToApplicationPermuteInt(AO ao, PetscInt block, PetscInt array[])
292: {

298:   (*ao->ops->petsctoapplicationpermuteint)(ao, block, array);
299:   return(0);
300: }

304: /*@
305:   AOApplicationToPetscPermuteInt - Permutes an array of blocks of integers
306:   in the application-defined ordering to the PETSc ordering.

308:   Collective on AO

310:   Input Parameters:
311: + ao    - The application ordering context
312: . block - The block size
313: - array - The integer array

315:   Output Parameter:
316: . array - The permuted array

318:   Note: The length of the array should be block*N, where N is length
319:   provided to the AOCreate*() method that created the AO.

321:   The permutation takes array[i_app] --> array[i_pet], where i_app is
322:   the index of 'i' in the application ordering and i_pet is the index
323:   of 'i' in the petsc ordering.

325:   Level: beginner

327: .keywords: application ordering, mapping

329: .seealso: AOCreateBasic(), AOView(), AOPetscToApplicationIS(), AOApplicationToPetsc()
330: @*/
331: PetscErrorCode  AOApplicationToPetscPermuteInt(AO ao, PetscInt block, PetscInt array[])
332: {

338:   (*ao->ops->applicationtopetscpermuteint)(ao, block, array);
339:   return(0);
340: }

344: /*@
345:   AOPetscToApplicationPermuteReal - Permutes an array of blocks of reals
346:   in the PETSc ordering to the application-defined ordering.

348:   Collective on AO

350:   Input Parameters:
351: + ao    - The application ordering context
352: . block - The block size
353: - array - The integer array

355:   Output Parameter:
356: . array - The permuted array

358:   Note: The length of the array should be block*N, where N is length
359:   provided to the AOCreate*() method that created the AO.

361:   The permutation takes array[i_pet] --> array[i_app], where i_app is
362:   the index of 'i' in the application ordering and i_pet is the index
363:   of 'i' in the petsc ordering.

365:   Level: beginner

367: .keywords: application ordering, mapping

369: .seealso: AOCreateBasic(), AOView(), AOApplicationToPetsc(), AOPetscToApplicationIS()
370: @*/
371: PetscErrorCode  AOPetscToApplicationPermuteReal(AO ao, PetscInt block, PetscReal array[])
372: {

378:   (*ao->ops->petsctoapplicationpermutereal)(ao, block, array);
379:   return(0);
380: }

384: /*@
385:   AOApplicationToPetscPermuteReal - Permutes an array of blocks of reals
386:   in the application-defined ordering to the PETSc ordering.

388:   Collective on AO

390:   Input Parameters:
391: + ao    - The application ordering context
392: . block - The block size
393: - array - The integer array

395:   Output Parameter:
396: . array - The permuted array

398:   Note: The length of the array should be block*N, where N is length
399:   provided to the AOCreate*() method that created the AO.

401:   The permutation takes array[i_app] --> array[i_pet], where i_app is
402:   the index of 'i' in the application ordering and i_pet is the index
403:   of 'i' in the petsc ordering.

405:   Level: beginner

407: .keywords: application ordering, mapping

409: .seealso: AOCreateBasic(), AOView(),AOApplicationToPetsc(), AOPetscToApplicationIS()
410: @*/
411: PetscErrorCode  AOApplicationToPetscPermuteReal(AO ao, PetscInt block, PetscReal array[])
412: {

418:   (*ao->ops->applicationtopetscpermutereal)(ao, block, array);
419:   return(0);
420: }

424: /*@C
425:     AOSetFromOptions - Sets AO options from the options database.

427:    Collective on AO

429:    Input Parameter:
430: .  ao - the application ordering

432:    Level: beginner

434: .keywords: AO, options, database

436: .seealso: AOCreate(), AOSetType(), AODestroy(), AOPetscToApplication(), AOApplicationToPetsc()
437: @*/
438: PetscErrorCode AOSetFromOptions(AO ao)
439: {
441:   char           type[256];
442:   const char     *def=AOBASIC;
443:   PetscBool      flg;


448:   PetscObjectOptionsBegin((PetscObject)ao);
449:     PetscOptionsList("-ao_type","AO type","AOSetType",AOList,def,type,256,&flg);
450:     if (flg) {
451:       AOSetType(ao,type);
452:     } else if (!((PetscObject)ao)->type_name){
453:       AOSetType(ao,def);
454:     }

456:     /* not used here, but called so will go into help messaage */
457:     PetscOptionsName("-ao_view","Print detailed information on AO used","AOView",0);
458: 
459:   PetscOptionsEnd();
460:   return(0);
461: }

465: /*@C
466:    AOSetIS - Sets the IS associated with the application ordering.

468:    Collective on MPI_Comm

470:    Input Parameters:
471: +  ao - the application ordering
472: .  isapp -  index set that defines an ordering
473: -  ispetsc - index set that defines another ordering (may be PETSC_NULL to use the
474:              natural ordering)

476:    Notes: 
477:    The index sets isapp and ispetsc are used only for creation of ao.

479:    Level: beginner

481: .keywords: AO, create

483: .seealso: AOCreate(), AODestroy(), AOPetscToApplication(), AOApplicationToPetsc()
484: @*/
485: PetscErrorCode AOSetIS(AO ao,IS isapp,IS ispetsc)
486: {

490:   if (ispetsc){
491:     PetscInt       napp,npetsc;
492:     ISGetLocalSize(isapp,&napp);
493:     ISGetLocalSize(ispetsc,&npetsc);
494:     if (napp != npetsc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"napp %D != npetsc %d. Local IS lengths must match",napp,npetsc);
495:   }
496:   ao->isapp   = isapp;
497:   ao->ispetsc = ispetsc;
498:   return(0);
499: }

503: /*@C
504:    AOCreate - Creates an application ordering.

506:    Collective on MPI_Comm

508:    Input Parameters:
509: .  comm - MPI communicator that is to share AO

511:    Output Parameter:
512: .  ao - the new application ordering

514:    Options Database Key:
515: +   -ao_type <aotype> - create ao with particular format
516: -   -ao_view - call AOView() at the conclusion of AOCreate()

518:    Level: beginner

520: .keywords: AO, create

522: .seealso: AOSetIS(), AODestroy(), AOPetscToApplication(), AOApplicationToPetsc()
523: @*/
524: PetscErrorCode  AOCreate(MPI_Comm comm,AO *ao)
525: {
527:   AO             aonew;
528:   PetscBool      opt;

532:   *ao = PETSC_NULL;
533: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
534:   AOInitializePackage(PETSC_NULL);
535: #endif

537:   PetscHeaderCreate(aonew,_p_AO,struct _AOOps,AO_CLASSID,-1,"AO","Application Ordering","AO",comm,AODestroy,AOView);
538:   PetscMemzero(aonew->ops, sizeof(struct _AOOps));
539:   *ao = aonew;

541:   opt = PETSC_FALSE;
542:   PetscOptionsGetBool(PETSC_NULL, "-ao_view", &opt,PETSC_NULL);
543:   if (opt) {
544:     AOView(aonew, PETSC_VIEWER_STDOUT_WORLD);
545:   }
546:   return(0);
547: }