Subversion Repositories slepc-dev

Rev

Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2757 jroman 1
/*
2
   Basic PS routines
3
 
4
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5
   SLEPc - Scalable Library for Eigenvalue Problem Computations
6
   Copyright (c) 2002-2011, Universitat Politecnica de Valencia, Spain
7
 
8
   This file is part of SLEPc.
9
 
10
   SLEPc is free software: you can redistribute it and/or modify it under  the
11
   terms of version 3 of the GNU Lesser General Public License as published by
12
   the Free Software Foundation.
13
 
14
   SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY
15
   WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS
16
   FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for
17
   more details.
18
 
19
   You  should have received a copy of the GNU Lesser General  Public  License
20
   along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
21
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22
*/
23
 
24
#include <slepc-private/psimpl.h>      /*I "slepcps.h" I*/
25
 
26
PetscFList       PSList = 0;
27
PetscBool        PSRegisterAllCalled = PETSC_FALSE;
28
PetscClassId     PS_CLASSID = 0;
2807 jroman 29
PetscLogEvent    PS_Solve = 0,PS_Sort = 0,PS_Vectors = 0,PS_Other = 0;
2757 jroman 30
static PetscBool PSPackageInitialized = PETSC_FALSE;
2777 jroman 31
const char       *PSMatName[PS_NUM_MAT] = {"A","B","C","T","Q","X","Y","U","VT","W"};
2757 jroman 32
 
33
#undef __FUNCT__  
34
#define __FUNCT__ "PSFinalizePackage"
35
/*@C
36
   PSFinalizePackage - This function destroys everything in the Slepc interface
37
   to the PS package. It is called from SlepcFinalize().
38
 
39
   Level: developer
40
 
41
.seealso: SlepcFinalize()
42
@*/
43
PetscErrorCode PSFinalizePackage(void)
44
{
45
  PetscFunctionBegin;
46
  PSPackageInitialized = PETSC_FALSE;
47
  PSList               = 0;
48
  PSRegisterAllCalled  = PETSC_FALSE;
49
  PetscFunctionReturn(0);
50
}
51
 
52
#undef __FUNCT__  
53
#define __FUNCT__ "PSInitializePackage"
54
/*@C
55
  PSInitializePackage - This function initializes everything in the PS package.
56
  It is called from PetscDLLibraryRegister() when using dynamic libraries, and
57
  on the first call to PSCreate() when using static libraries.
58
 
59
  Input Parameter:
60
  path - The dynamic library path, or PETSC_NULL
61
 
62
  Level: developer
63
 
64
.seealso: SlepcInitialize()
65
@*/
66
PetscErrorCode PSInitializePackage(const char *path)
67
{
68
  char             logList[256];
69
  char             *className;
70
  PetscBool        opt;
71
  PetscErrorCode   ierr;
72
 
73
  PetscFunctionBegin;
74
  if (PSPackageInitialized) PetscFunctionReturn(0);
75
  PSPackageInitialized = PETSC_TRUE;
76
  /* Register Classes */
77
  ierr = PetscClassIdRegister("Projected system",&PS_CLASSID);CHKERRQ(ierr);
78
  /* Register Constructors */
79
  ierr = PSRegisterAll(path);CHKERRQ(ierr);
80
  /* Register Events */
81
  ierr = PetscLogEventRegister("PSSolve",PS_CLASSID,&PS_Solve);CHKERRQ(ierr);
82
  ierr = PetscLogEventRegister("PSSort",PS_CLASSID,&PS_Sort);CHKERRQ(ierr);
2807 jroman 83
  ierr = PetscLogEventRegister("PSVectors",PS_CLASSID,&PS_Vectors);CHKERRQ(ierr);
2771 jroman 84
  ierr = PetscLogEventRegister("PSOther",PS_CLASSID,&PS_Other);CHKERRQ(ierr);
2757 jroman 85
  /* Process info exclusions */
86
  ierr = PetscOptionsGetString(PETSC_NULL,"-info_exclude",logList,256,&opt);CHKERRQ(ierr);
87
  if (opt) {
88
    ierr = PetscStrstr(logList,"ps",&className);CHKERRQ(ierr);
89
    if (className) {
90
      ierr = PetscInfoDeactivateClass(PS_CLASSID);CHKERRQ(ierr);
91
    }
92
  }
93
  /* Process summary exclusions */
94
  ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary_exclude",logList,256,&opt);CHKERRQ(ierr);
95
  if (opt) {
96
    ierr = PetscStrstr(logList,"ps",&className);CHKERRQ(ierr);
97
    if (className) {
98
      ierr = PetscLogEventDeactivateClass(PS_CLASSID);CHKERRQ(ierr);
99
    }
100
  }
101
  ierr = PetscRegisterFinalize(PSFinalizePackage);CHKERRQ(ierr);
102
  PetscFunctionReturn(0);
103
}
104
 
105
#undef __FUNCT__  
106
#define __FUNCT__ "PSCreate"
107
/*@C
108
   PSCreate - Creates a PS context.
109
 
110
   Collective on MPI_Comm
111
 
112
   Input Parameter:
113
.  comm - MPI communicator
114
 
115
   Output Parameter:
116
.  newps - location to put the PS context
117
 
118
   Level: beginner
119
 
120
   Note:
121
   PS objects are not intended for normal users but only for
122
   advanced user that for instance implement their own solvers.
123
 
124
.seealso: PSDestroy(), PS
125
@*/
126
PetscErrorCode PSCreate(MPI_Comm comm,PS *newps)
127
{
128
  PS             ps;
129
  PetscInt       i;
130
  PetscErrorCode ierr;
131
 
132
  PetscFunctionBegin;
133
  PetscValidPointer(newps,2);
134
  ierr = PetscHeaderCreate(ps,_p_PS,struct _PSOps,PS_CLASSID,-1,"PS","Projected System","PS",comm,PSDestroy,PSView);CHKERRQ(ierr);
2794 jroman 135
  *newps      = ps;
136
  ps->state   = PS_STATE_RAW;
137
  ps->method  = 0;
138
  ps->compact = PETSC_FALSE;
2815 jroman 139
  ps->refined = PETSC_FALSE;
2794 jroman 140
  ps->ld      = 0;
141
  ps->l       = 0;
142
  ps->n       = 0;
143
  ps->k       = 0;
2757 jroman 144
  for (i=0;i<PS_NUM_MAT;i++) {
2758 jroman 145
    ps->mat[i]  = PETSC_NULL;
2757 jroman 146
    ps->rmat[i] = PETSC_NULL;
147
  }
2770 jroman 148
  ps->work   = PETSC_NULL;
149
  ps->rwork  = PETSC_NULL;
150
  ps->iwork  = PETSC_NULL;
151
  ps->lwork  = 0;
152
  ps->lrwork = 0;
153
  ps->liwork = 0;
2757 jroman 154
  PetscFunctionReturn(0);
155
}
156
 
157
#undef __FUNCT__  
158
#define __FUNCT__ "PSSetOptionsPrefix"
159
/*@C
160
   PSSetOptionsPrefix - Sets the prefix used for searching for all
161
   PS options in the database.
162
 
163
   Logically Collective on PS
164
 
165
   Input Parameters:
166
+  ps - the projected system context
167
-  prefix - the prefix string to prepend to all PS option requests
168
 
169
   Notes:
170
   A hyphen (-) must NOT be given at the beginning of the prefix name.
171
   The first character of all runtime options is AUTOMATICALLY the
172
   hyphen.
173
 
174
   Level: advanced
175
 
176
.seealso: PSAppendOptionsPrefix()
177
@*/
178
PetscErrorCode PSSetOptionsPrefix(PS ps,const char *prefix)
179
{
180
  PetscErrorCode ierr;
181
 
182
  PetscFunctionBegin;
183
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
184
  ierr = PetscObjectSetOptionsPrefix((PetscObject)ps,prefix);CHKERRQ(ierr);
185
  PetscFunctionReturn(0);
186
}
187
 
188
#undef __FUNCT__  
189
#define __FUNCT__ "PSAppendOptionsPrefix"
190
/*@C
191
   PSAppendOptionsPrefix - Appends to the prefix used for searching for all
192
   PS options in the database.
193
 
194
   Logically Collective on PS
195
 
196
   Input Parameters:
197
+  ps - the projected system context
198
-  prefix - the prefix string to prepend to all PS option requests
199
 
200
   Notes:
201
   A hyphen (-) must NOT be given at the beginning of the prefix name.
202
   The first character of all runtime options is AUTOMATICALLY the hyphen.
203
 
204
   Level: advanced
205
 
206
.seealso: PSSetOptionsPrefix()
207
@*/
208
PetscErrorCode PSAppendOptionsPrefix(PS ps,const char *prefix)
209
{
210
  PetscErrorCode ierr;
211
 
212
  PetscFunctionBegin;
213
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
214
  ierr = PetscObjectAppendOptionsPrefix((PetscObject)ps,prefix);CHKERRQ(ierr);
215
  PetscFunctionReturn(0);
216
}
217
 
218
#undef __FUNCT__
219
#define __FUNCT__ "PSGetOptionsPrefix"
220
/*@C
221
   PSGetOptionsPrefix - Gets the prefix used for searching for all
222
   PS options in the database.
223
 
224
   Not Collective
225
 
226
   Input Parameters:
227
.  ps - the projected system context
228
 
229
   Output Parameters:
230
.  prefix - pointer to the prefix string used is returned
231
 
232
   Notes: On the fortran side, the user should pass in a string 'prefix' of
233
   sufficient length to hold the prefix.
234
 
235
   Level: advanced
236
 
237
.seealso: PSSetOptionsPrefix(), PSAppendOptionsPrefix()
238
@*/
239
PetscErrorCode PSGetOptionsPrefix(PS ps,const char *prefix[])
240
{
241
  PetscErrorCode ierr;
242
 
243
  PetscFunctionBegin;
244
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
245
  PetscValidPointer(prefix,2);
246
  ierr = PetscObjectGetOptionsPrefix((PetscObject)ps,prefix);CHKERRQ(ierr);
247
  PetscFunctionReturn(0);
248
}
249
 
250
#undef __FUNCT__  
251
#define __FUNCT__ "PSSetType"
252
/*@C
253
   PSSetType - Selects the type for the PS object.
254
 
255
   Logically Collective on PS
256
 
257
   Input Parameter:
2793 jroman 258
+  ps   - the projected system context
2757 jroman 259
-  type - a known type
260
 
261
   Level: advanced
262
 
263
.seealso: PSGetType()
264
@*/
265
PetscErrorCode PSSetType(PS ps,const PSType type)
266
{
267
  PetscErrorCode ierr,(*r)(PS);
268
  PetscBool      match;
269
 
270
  PetscFunctionBegin;
271
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
272
  PetscValidCharPointer(type,2);
273
 
2823 jroman 274
  ierr = PetscObjectTypeCompare((PetscObject)ps,type,&match);CHKERRQ(ierr);
2757 jroman 275
  if (match) PetscFunctionReturn(0);
276
 
277
  ierr =  PetscFListFind(PSList,((PetscObject)ps)->comm,type,PETSC_TRUE,(void (**)(void))&r);CHKERRQ(ierr);
278
  if (!r) SETERRQ1(((PetscObject)ps)->comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PS type %s",type);
279
 
280
  ierr = PetscMemzero(ps->ops,sizeof(struct _PSOps));CHKERRQ(ierr);
281
 
282
  ierr = PetscObjectChangeTypeName((PetscObject)ps,type);CHKERRQ(ierr);
283
  ierr = (*r)(ps);CHKERRQ(ierr);
284
  PetscFunctionReturn(0);
285
}
286
 
287
#undef __FUNCT__  
288
#define __FUNCT__ "PSGetType"
289
/*@C
290
   PSGetType - Gets the PS type name (as a string) from the PS context.
291
 
292
   Not Collective
293
 
294
   Input Parameter:
295
.  ps - the projected system context
296
 
297
   Output Parameter:
298
.  name - name of the projected system
299
 
300
   Level: advanced
301
 
302
.seealso: PSSetType()
303
@*/
304
PetscErrorCode PSGetType(PS ps,const PSType *type)
305
{
306
  PetscFunctionBegin;
307
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
308
  PetscValidPointer(type,2);
309
  *type = ((PetscObject)ps)->type_name;
310
  PetscFunctionReturn(0);
311
}
312
 
313
#undef __FUNCT__  
2793 jroman 314
#define __FUNCT__ "PSSetMethod"
2794 jroman 315
/*@
2793 jroman 316
   PSSetMethod - Selects the method to be used to solve the problem.
317
 
318
   Logically Collective on PS
319
 
320
   Input Parameter:
321
+  ps   - the projected system context
322
-  meth - an index indentifying the method
323
 
324
   Level: advanced
325
 
326
.seealso: PSGetMethod()
327
@*/
328
PetscErrorCode PSSetMethod(PS ps,PetscInt meth)
329
{
330
  PetscFunctionBegin;
331
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
332
  PetscValidLogicalCollectiveInt(ps,meth,2);
333
  if (meth<0) SETERRQ(((PetscObject)ps)->comm,PETSC_ERR_ARG_OUTOFRANGE,"The method must be a non-negative integer");
2832 jroman 334
  if (meth>PS_MAX_SOLVE) SETERRQ(((PetscObject)ps)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Too large value for the method");
2793 jroman 335
  ps->method = meth;
336
  PetscFunctionReturn(0);
337
}
338
 
339
#undef __FUNCT__  
340
#define __FUNCT__ "PSGetMethod"
2794 jroman 341
/*@
2793 jroman 342
   PSGetMethod - Gets the method currently used in the PS.
343
 
344
   Not Collective
345
 
346
   Input Parameter:
347
.  ps - the projected system context
348
 
349
   Output Parameter:
350
.  meth - identifier of the method
351
 
352
   Level: advanced
353
 
354
.seealso: PSSetMethod()
355
@*/
356
PetscErrorCode PSGetMethod(PS ps,PetscInt *meth)
357
{
358
  PetscFunctionBegin;
359
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
360
  PetscValidPointer(meth,2);
361
  *meth = ps->method;
362
  PetscFunctionReturn(0);
363
}
364
 
365
#undef __FUNCT__  
2794 jroman 366
#define __FUNCT__ "PSSetCompact"
367
/*@
368
   PSSetCompact - Switch to compact storage of matrices.
369
 
370
   Logically Collective on PS
371
 
372
   Input Parameter:
373
+  ps   - the projected system context
374
-  comp - a boolean flag
375
 
376
   Notes:
377
   Compact storage is used in some PS types such as PSHEP when the matrix
378
   is tridiagonal. This flag can be used to indicate whether the user
379
   provides the matrix entries via the compact form (the tridiagonal PS_MAT_T)
380
   or the non-compact one (PS_MAT_A).
381
 
382
   The default is PETSC_FALSE.
383
 
384
   Level: advanced
385
 
386
.seealso: PSGetCompact()
387
@*/
388
PetscErrorCode PSSetCompact(PS ps,PetscBool comp)
389
{
390
  PetscFunctionBegin;
391
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
392
  PetscValidLogicalCollectiveBool(ps,comp,2);
393
  ps->compact = comp;
394
  PetscFunctionReturn(0);
395
}
396
 
397
#undef __FUNCT__  
398
#define __FUNCT__ "PSGetCompact"
399
/*@
400
   PSGetCompact - Gets the compact storage flag.
401
 
402
   Not Collective
403
 
404
   Input Parameter:
405
.  ps - the projected system context
406
 
407
   Output Parameter:
408
.  comp - the flag
409
 
410
   Level: advanced
411
 
412
.seealso: PSSetCompact()
413
@*/
414
PetscErrorCode PSGetCompact(PS ps,PetscBool *comp)
415
{
416
  PetscFunctionBegin;
417
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
418
  PetscValidPointer(comp,2);
419
  *comp = ps->compact;
420
  PetscFunctionReturn(0);
421
}
422
 
423
#undef __FUNCT__  
2815 jroman 424
#define __FUNCT__ "PSSetRefined"
425
/*@
426
   PSSetRefined - Sets a flag to indicate that refined vectors must be
427
   computed.
428
 
429
   Logically Collective on PS
430
 
431
   Input Parameter:
432
+  ps  - the projected system context
433
-  ref - a boolean flag
434
 
435
   Notes:
436
   Normally the vectors returned in PS_MAT_X are eigenvectors of the
437
   projected matrix. With this flag activated, PSVectors() will return
438
   the right singular vector of the smallest singular value of matrix
439
   \tilde{A}-theta*I, where \tilde{A} is the extended (n+1)xn matrix
440
   and theta is the Ritz value. This is used in the refined Ritz
441
   approximation.
442
 
443
   The default is PETSC_FALSE.
444
 
445
   Level: advanced
446
 
447
.seealso: PSVectors(), PSGetRefined()
448
@*/
449
PetscErrorCode PSSetRefined(PS ps,PetscBool ref)
450
{
451
  PetscFunctionBegin;
452
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
453
  PetscValidLogicalCollectiveBool(ps,ref,2);
454
  ps->refined = ref;
455
  PetscFunctionReturn(0);
456
}
457
 
458
#undef __FUNCT__  
459
#define __FUNCT__ "PSGetRefined"
460
/*@
461
   PSGetRefined - Gets the refined vectors flag.
462
 
463
   Not Collective
464
 
465
   Input Parameter:
466
.  ps - the projected system context
467
 
468
   Output Parameter:
469
.  comp - the flag
470
 
471
   Level: advanced
472
 
473
.seealso: PSSetRefined()
474
@*/
475
PetscErrorCode PSGetRefined(PS ps,PetscBool *ref)
476
{
477
  PetscFunctionBegin;
478
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
479
  PetscValidPointer(ref,2);
480
  *ref = ps->refined;
481
  PetscFunctionReturn(0);
482
}
483
 
484
#undef __FUNCT__  
2757 jroman 485
#define __FUNCT__ "PSSetFromOptions"
486
/*@
487
   PSSetFromOptions - Sets PS options from the options database.
488
 
489
   Collective on PS
490
 
491
   Input Parameters:
492
.  ps - the projected system context
493
 
494
   Notes:  
495
   To see all options, run your program with the -help option.
496
 
497
   Level: beginner
498
@*/
499
PetscErrorCode PSSetFromOptions(PS ps)
500
{
501
  PetscErrorCode ierr;
2795 jroman 502
  PetscInt       meth;
2757 jroman 503
 
504
  PetscFunctionBegin;
505
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
506
  if (!PSRegisterAllCalled) { ierr = PSRegisterAll(PETSC_NULL);CHKERRQ(ierr); }
507
  /* Set default type (we do not allow changing it with -ps_type) */
508
  if (!((PetscObject)ps)->type_name) {
2758 jroman 509
    ierr = PSSetType(ps,PSNHEP);CHKERRQ(ierr);
2757 jroman 510
  }
511
  ierr = PetscOptionsBegin(((PetscObject)ps)->comm,((PetscObject)ps)->prefix,"Projecte System (PS) Options","PS");CHKERRQ(ierr);
2795 jroman 512
    meth = 0;
513
    ierr = PetscOptionsInt("-ps_method","Method to be used for the projected system","PSSetMethod",ps->method,&meth,PETSC_NULL);CHKERRQ(ierr);
514
    ierr = PSSetMethod(ps,meth);CHKERRQ(ierr);
2757 jroman 515
    ierr = PetscObjectProcessOptionsHandlers((PetscObject)ps);CHKERRQ(ierr);
516
  ierr = PetscOptionsEnd();CHKERRQ(ierr);
517
  PetscFunctionReturn(0);
518
}
519
 
520
#undef __FUNCT__  
521
#define __FUNCT__ "PSView"
522
/*@C
523
   PSView - Prints the PS data structure.
524
 
525
   Collective on PS
526
 
527
   Input Parameters:
528
+  ps - the projected system context
529
-  viewer - optional visualization context
530
 
531
   Note:
532
   The available visualization contexts include
533
+     PETSC_VIEWER_STDOUT_SELF - standard output (default)
534
-     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
535
         output where only the first processor opens
536
         the file.  All other processors send their
537
         data to the first processor to print.
538
 
539
   The user can open an alternative visualization context with
540
   PetscViewerASCIIOpen() - output to a specified file.
541
 
542
   Level: beginner
543
 
544
.seealso: PetscViewerASCIIOpen()
545
@*/
546
PetscErrorCode PSView(PS ps,PetscViewer viewer)
547
{
2777 jroman 548
  PetscBool         isascii;
549
  const char        *state;
550
  PetscViewerFormat format;
551
  PetscErrorCode    ierr;
2757 jroman 552
 
553
  PetscFunctionBegin;
554
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
555
  if (!viewer) viewer = PETSC_VIEWER_STDOUT_(((PetscObject)ps)->comm);
556
  PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
557
  PetscCheckSameComm(ps,1,viewer,2);
2823 jroman 558
  ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr);
2757 jroman 559
  if (isascii) {
2777 jroman 560
    ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
2757 jroman 561
    ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ps,viewer,"PS Object");CHKERRQ(ierr);
2777 jroman 562
    if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
563
      switch (ps->state) {
564
        case PS_STATE_RAW:          state = "raw"; break;
565
        case PS_STATE_INTERMEDIATE: state = "intermediate"; break;
566
        case PS_STATE_CONDENSED:    state = "condensed"; break;
567
        case PS_STATE_SORTED:       state = "sorted"; break;
568
        default: SETERRQ(((PetscObject)ps)->comm,1,"Wrong value of ps->state");
569
      }
570
      ierr = PetscViewerASCIIPrintf(viewer,"  current state: %s\n",state);CHKERRQ(ierr);
571
      ierr = PetscViewerASCIIPrintf(viewer,"  dimensions: ld=%d, n=%d, l=%d, k=%d\n",ps->ld,ps->n,ps->l,ps->k);CHKERRQ(ierr);
572
    }
573
    if (ps->ops->view) {
574
      ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
575
      ierr = (*ps->ops->view)(ps,viewer);CHKERRQ(ierr);
576
      ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
577
    }
2762 jroman 578
  } else SETERRQ1(((PetscObject)ps)->comm,1,"Viewer type %s not supported for PS",((PetscObject)viewer)->type_name);
2757 jroman 579
  PetscFunctionReturn(0);
580
}
581
 
582
#undef __FUNCT__  
2758 jroman 583
#define __FUNCT__ "PSAllocate"
584
/*@
585
   PSAllocate - Allocates memory for internal storage or matrices in PS.
586
 
2807 jroman 587
   Logically Collective on PS
2758 jroman 588
 
589
   Input Parameters:
590
+  ps - the projected system context
591
-  ld - leading dimension (maximum allowed dimension for the matrices)
592
 
593
   Level: advanced
594
 
595
.seealso: PSGetLeadingDimension(), PSSetDimensions()
596
@*/
597
PetscErrorCode PSAllocate(PS ps,PetscInt ld)
598
{
599
  PetscErrorCode ierr;
600
 
601
  PetscFunctionBegin;
602
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
603
  PetscValidLogicalCollectiveInt(ps,ld,2);
604
  if (ld<1) SETERRQ(((PetscObject)ps)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Leading dimension should be at least one");
605
  ps->ld = ld;
606
  ierr = (*ps->ops->allocate)(ps,ld);CHKERRQ(ierr);
607
  PetscFunctionReturn(0);
608
}
609
 
610
#undef __FUNCT__  
611
#define __FUNCT__ "PSAllocateMat_Private"
612
PetscErrorCode PSAllocateMat_Private(PS ps,PSMatType m)
613
{
2764 jroman 614
  PetscInt       sz;
2758 jroman 615
  PetscErrorCode ierr;
616
 
617
  PetscFunctionBegin;
2775 jroman 618
  if (m==PS_MAT_T) sz = 3*ps->ld*sizeof(PetscScalar);
619
  else sz = ps->ld*ps->ld*sizeof(PetscScalar);
2770 jroman 620
  if (ps->mat[m]) { ierr = PetscFree(ps->mat[m]);CHKERRQ(ierr); }
621
  else { ierr = PetscLogObjectMemory(ps,sz);CHKERRQ(ierr); }
2758 jroman 622
  ierr = PetscMalloc(sz,&ps->mat[m]);CHKERRQ(ierr);
623
  ierr = PetscMemzero(ps->mat[m],sz);CHKERRQ(ierr);
624
  PetscFunctionReturn(0);
625
}
626
 
627
#undef __FUNCT__  
2775 jroman 628
#define __FUNCT__ "PSAllocateMatReal_Private"
629
PetscErrorCode PSAllocateMatReal_Private(PS ps,PSMatType m)
630
{
631
  PetscInt       sz;
632
  PetscErrorCode ierr;
633
 
634
  PetscFunctionBegin;
635
  if (m==PS_MAT_T) sz = 3*ps->ld*sizeof(PetscReal);
636
  else sz = ps->ld*ps->ld*sizeof(PetscReal);
637
  if (ps->rmat[m]) { ierr = PetscFree(ps->rmat[m]);CHKERRQ(ierr); }
638
  else { ierr = PetscLogObjectMemory(ps,sz);CHKERRQ(ierr); }
639
  ierr = PetscMalloc(sz,&ps->rmat[m]);CHKERRQ(ierr);
640
  ierr = PetscMemzero(ps->rmat[m],sz);CHKERRQ(ierr);
641
  PetscFunctionReturn(0);
642
}
643
 
644
#undef __FUNCT__  
2770 jroman 645
#define __FUNCT__ "PSAllocateWork_Private"
646
PetscErrorCode PSAllocateWork_Private(PS ps,PetscInt s,PetscInt r,PetscInt i)
647
{
648
  PetscErrorCode ierr;
649
 
650
  PetscFunctionBegin;
651
  if (s>ps->lwork) {
652
    ierr = PetscFree(ps->work);CHKERRQ(ierr);
653
    ierr = PetscMalloc(s*sizeof(PetscScalar),&ps->work);CHKERRQ(ierr);
654
    ierr = PetscLogObjectMemory(ps,(s-ps->lwork)*sizeof(PetscScalar));CHKERRQ(ierr);
655
    ps->lwork = s;
656
  }
657
  if (r>ps->lrwork) {
658
    ierr = PetscFree(ps->rwork);CHKERRQ(ierr);
659
    ierr = PetscMalloc(r*sizeof(PetscReal),&ps->rwork);CHKERRQ(ierr);
660
    ierr = PetscLogObjectMemory(ps,(r-ps->lrwork)*sizeof(PetscReal));CHKERRQ(ierr);
661
    ps->lrwork = r;
662
  }
663
  if (i>ps->liwork) {
664
    ierr = PetscFree(ps->iwork);CHKERRQ(ierr);
665
    ierr = PetscMalloc(i*sizeof(PetscBLASInt),&ps->iwork);CHKERRQ(ierr);
666
    ierr = PetscLogObjectMemory(ps,(i-ps->liwork)*sizeof(PetscBLASInt));CHKERRQ(ierr);
667
    ps->liwork = i;
668
  }
669
  PetscFunctionReturn(0);
670
}
671
 
672
#undef __FUNCT__  
2777 jroman 673
#define __FUNCT__ "PSViewMat_Private"
674
PetscErrorCode PSViewMat_Private(PS ps,PetscViewer viewer,PSMatType m)
675
{
2785 jroman 676
  PetscErrorCode    ierr;
677
  PetscInt          i,j;
678
  PetscScalar       *v;
2783 jroman 679
  PetscViewerFormat format;
2785 jroman 680
#if defined(PETSC_USE_COMPLEX)
681
  PetscBool         allreal = PETSC_TRUE;
682
#endif
2777 jroman 683
 
684
  PetscFunctionBegin;
2783 jroman 685
  ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
686
  if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(0);
2785 jroman 687
  ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
688
#if defined(PETSC_USE_COMPLEX)
689
  /* determine if matrix has all real values */
690
  v = ps->mat[m];
691
  for (i=0;i<ps->n;i++)
692
    for (j=0;j<ps->n;j++)
693
      if (PetscImaginaryPart(v[i+j*ps->ld])) { allreal = PETSC_FALSE; break; }
694
#endif
695
  if (format == PETSC_VIEWER_ASCII_MATLAB) {
696
    ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D\n",ps->n,ps->n);CHKERRQ(ierr);
697
    ierr = PetscViewerASCIIPrintf(viewer,"%s = [\n",PSMatName[m]);CHKERRQ(ierr);
698
  } else {
699
    ierr = PetscViewerASCIIPrintf(viewer,"Matrix %s =\n",PSMatName[m]);CHKERRQ(ierr);
700
  }
701
 
702
  for (i=0;i<ps->n;i++) {
703
    v = ps->mat[m]+i;
704
    for (j=0;j<ps->n;j++) {
705
#if defined(PETSC_USE_COMPLEX)
706
      if (allreal) {
707
        ierr = PetscViewerASCIIPrintf(viewer,"%18.16e ",PetscRealPart(*v));CHKERRQ(ierr);
708
      } else {
709
        ierr = PetscViewerASCIIPrintf(viewer,"%18.16e + %18.16ei ",PetscRealPart(*v),PetscImaginaryPart(*v));CHKERRQ(ierr);
710
      }
711
#else
712
      ierr = PetscViewerASCIIPrintf(viewer,"%18.16e ",*v);CHKERRQ(ierr);
713
#endif
714
      v += ps->ld;
715
    }
716
    ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
717
  }
718
 
719
  if (format == PETSC_VIEWER_ASCII_MATLAB) {
720
    ierr = PetscViewerASCIIPrintf(viewer,"];\n");CHKERRQ(ierr);
721
  }
722
  ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
723
  ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
2777 jroman 724
  PetscFunctionReturn(0);
725
}
726
 
727
#undef __FUNCT__  
2780 jroman 728
#define __FUNCT__ "PSSortEigenvaluesReal_Private"
2818 jroman 729
PetscErrorCode PSSortEigenvaluesReal_Private(PS ps,PetscInt l,PetscInt n,PetscReal *eig,PetscInt *perm,PetscErrorCode (*comp_func)(PetscScalar,PetscScalar,PetscScalar,PetscScalar,PetscInt*,void*),void *comp_ctx)
2780 jroman 730
{
731
  PetscErrorCode ierr;
732
  PetscScalar    re;
733
  PetscInt       i,j,result,tmp;
734
 
735
  PetscFunctionBegin;
736
  for (i=0;i<n;i++) perm[i] = i;
737
  /* insertion sort */
2818 jroman 738
  for (i=l+1;i<n;i++) {
2780 jroman 739
    re = eig[perm[i]];
740
    j = i-1;
741
    ierr = (*comp_func)(re,0.0,eig[perm[j]],0.0,&result,comp_ctx);CHKERRQ(ierr);
2818 jroman 742
    while (result<=0 && j>=l) {
2780 jroman 743
      tmp = perm[j]; perm[j] = perm[j+1]; perm[j+1] = tmp; j--;
744
      if (j>=0) {
745
        ierr = (*comp_func)(re,0.0,eig[perm[j]],0.0,&result,comp_ctx);CHKERRQ(ierr);
746
      }
747
    }
748
  }
749
  PetscFunctionReturn(0);
750
}
751
 
752
#undef __FUNCT__  
2821 jroman 753
#define __FUNCT__ "PSCopyMatrix_Private"
754
/*
755
  PSCopyMatrix_Private - Copies the trailing block of a matrix (from
756
  rows/columns l to n).
757
*/
758
PetscErrorCode PSCopyMatrix_Private(PS ps,PSMatType dst,PSMatType src)
759
{
760
  PetscErrorCode ierr;
761
  PetscInt    j,m,off,ld;
762
  PetscScalar *S,*D;
763
 
764
  PetscFunctionBegin;
765
  ld  = ps->ld;
766
  m   = ps->n-ps->l;
767
  off = ps->l+ps->l*ld;
768
  S   = ps->mat[src];
769
  D   = ps->mat[dst];
770
  for (j=0;j<m;j++) {
771
    ierr = PetscMemcpy(D+off+j*ld,S+off+j*ld,m*sizeof(PetscScalar));CHKERRQ(ierr);
772
  }
773
  PetscFunctionReturn(0);
774
}
775
 
776
#undef __FUNCT__  
2792 jroman 777
#define __FUNCT__ "PSPermuteColumns_Private"
2818 jroman 778
PetscErrorCode PSPermuteColumns_Private(PS ps,PetscInt l,PetscInt n,PSMatType m,PetscInt *perm)
2791 jroman 779
{
780
  PetscInt    i,j,k,p,ld;
781
  PetscScalar *Q,rtmp;
782
 
783
  PetscFunctionBegin;
784
  ld = ps->ld;
785
  Q  = ps->mat[PS_MAT_Q];
2818 jroman 786
  for (i=l;i<n;i++) {
2791 jroman 787
    p = perm[i];
788
    if (p != i) {
789
      j = i + 1;
790
      while (perm[j] != i) j++;
791
      perm[j] = p; perm[i] = i;
792
      /* swap eigenvectors i and j */
793
      for (k=0;k<n;k++) {
794
        rtmp = Q[k+p*ld]; Q[k+p*ld] = Q[k+i*ld]; Q[k+i*ld] = rtmp;
795
      }
796
    }
797
  }
798
  PetscFunctionReturn(0);
799
}
800
 
801
#undef __FUNCT__  
2757 jroman 802
#define __FUNCT__ "PSReset"
2758 jroman 803
/*@
2757 jroman 804
   PSReset - Resets the PS context to the initial state.
805
 
806
   Collective on PS
807
 
808
   Input Parameter:
809
.  ps - the projected system context
810
 
811
   Level: advanced
812
 
813
.seealso: PSDestroy()
814
@*/
815
PetscErrorCode PSReset(PS ps)
816
{
817
  PetscInt       i;
818
  PetscErrorCode ierr;
819
 
820
  PetscFunctionBegin;
821
  PetscValidHeaderSpecific(ps,PS_CLASSID,1);
2794 jroman 822
  ps->state   = PS_STATE_RAW;
2796 jroman 823
  ps->method  = 0;
2794 jroman 824
  ps->compact = PETSC_FALSE;
825
  ps->ld      = 0;
826
  ps->l       = 0;
827
  ps->n       = 0;
828
  ps->k       = 0;
2757 jroman 829
  for (i=0;i<PS_NUM_MAT;i++) {
830
    ierr = PetscFree(ps->mat[i]);CHKERRQ(ierr);
831
    ierr = PetscFree(ps->rmat[i]);CHKERRQ(ierr);
832
  }
833
  ierr = PetscFree(ps->work);CHKERRQ(ierr);
834
  ierr = PetscFree(ps->rwork);CHKERRQ(ierr);
2766 jroman 835
  ierr = PetscFree(ps->iwork);CHKERRQ(ierr);
2770 jroman 836
  ps->lwork  = 0;
837
  ps->lrwork = 0;
838
  ps->liwork = 0;
2757 jroman 839
  PetscFunctionReturn(0);
840
}
841
 
842
#undef __FUNCT__  
843
#define __FUNCT__ "PSDestroy"
844
/*@C
845
   PSDestroy - Destroys PS context that was created with PSCreate().
846
 
847
   Collective on PS
848
 
849
   Input Parameter:
850
.  ps - the projected system context
851
 
852
   Level: beginner
853
 
854
.seealso: PSCreate()
855
@*/
856
PetscErrorCode PSDestroy(PS *ps)
857
{
858
  PetscErrorCode ierr;
859
 
860
  PetscFunctionBegin;
861
  if (!*ps) PetscFunctionReturn(0);
862
  PetscValidHeaderSpecific(*ps,PS_CLASSID,1);
863
  if (--((PetscObject)(*ps))->refct > 0) { *ps = 0; PetscFunctionReturn(0); }
864
  ierr = PSReset(*ps);CHKERRQ(ierr);
865
  ierr = PetscHeaderDestroy(ps);CHKERRQ(ierr);
866
  PetscFunctionReturn(0);
867
}
868
 
869
#undef __FUNCT__  
870
#define __FUNCT__ "PSRegister"
871
/*@C
872
   PSRegister - See PSRegisterDynamic()
873
 
874
   Level: advanced
875
@*/
876
PetscErrorCode PSRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PS))
877
{
878
  PetscErrorCode ierr;
879
  char           fullname[PETSC_MAX_PATH_LEN];
880
 
881
  PetscFunctionBegin;
882
  ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
883
  ierr = PetscFListAdd(&PSList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
884
  PetscFunctionReturn(0);
885
}
886
 
887
#undef __FUNCT__  
888
#define __FUNCT__ "PSRegisterDestroy"
889
/*@
890
   PSRegisterDestroy - Frees the list of PS methods that were
891
   registered by PSRegisterDynamic().
892
 
893
   Not Collective
894
 
895
   Level: advanced
896
 
897
.seealso: PSRegisterDynamic(), PSRegisterAll()
898
@*/
899
PetscErrorCode PSRegisterDestroy(void)
900
{
901
  PetscErrorCode ierr;
902
 
903
  PetscFunctionBegin;
904
  ierr = PetscFListDestroy(&PSList);CHKERRQ(ierr);
905
  PSRegisterAllCalled = PETSC_FALSE;
906
  PetscFunctionReturn(0);
907
}
908
 
909
EXTERN_C_BEGIN
2783 jroman 910
extern PetscErrorCode PSCreate_HEP(PS);
2758 jroman 911
extern PetscErrorCode PSCreate_NHEP(PS);
2808 carcamgo 912
extern PetscErrorCode PSCreate_GHIEP(PS);
2757 jroman 913
EXTERN_C_END
914
 
915
#undef __FUNCT__  
916
#define __FUNCT__ "PSRegisterAll"
917
/*@C
918
   PSRegisterAll - Registers all of the projected systems in the PS package.
919
 
920
   Not Collective
921
 
922
   Input Parameter:
923
.  path - the library where the routines are to be found (optional)
924
 
925
   Level: advanced
926
@*/
927
PetscErrorCode PSRegisterAll(const char *path)
928
{
929
  PetscErrorCode ierr;
930
 
931
  PetscFunctionBegin;
932
  PSRegisterAllCalled = PETSC_TRUE;
2783 jroman 933
  ierr = PSRegisterDynamic(PSHEP,path,"PSCreate_HEP",PSCreate_HEP);CHKERRQ(ierr);
2758 jroman 934
  ierr = PSRegisterDynamic(PSNHEP,path,"PSCreate_NHEP",PSCreate_NHEP);CHKERRQ(ierr);
2808 carcamgo 935
  ierr = PSRegisterDynamic(PSGHIEP,path,"PSCreate_GHIEP",PSCreate_GHIEP);CHKERRQ(ierr);
2757 jroman 936
  PetscFunctionReturn(0);
937
}
938