Chaste  Release::3.4
PetscSetupUtils.cpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "PetscSetupUtils.hpp"
37 
38 #include <petsc.h>
39 #include <cstdlib>
40 #include <cassert>
41 #include <cstring>
42 #include <iostream>
43 
44 #include "ChasteBuildRoot.hpp"
45 #include "ChasteSyscalls.hpp"
46 #include "Citations.hpp"
47 #include "CommandLineArguments.hpp"
48 #include "Exception.hpp"
49 #include "GetCurrentWorkingDirectory.hpp"
50 #include "PetscException.hpp"
51 #include "PetscTools.hpp"
52 
53 #ifdef TEST_FOR_FPE
54 #include <fenv.h>
55 #include <signal.h>
56 
57 void FpeSignalToAbort(int sig_num, siginfo_t* info, void* context )
58 {
59  if ( info->si_code == FPE_FLTDIV)
60  {
61  std::cerr << "SIGFPE: floating point exception was divide by zero.\n";
62  }
63  else if ( info->si_code == FPE_FLTINV)
64  {
65  std::cerr << "SIGFPE: floating point exception was an invalid operation (like 0.0/0.0).\n";
66  }
67  else
68  {
69  std::cerr << "SIGFPE: unexpected error code.\n";
70  }
71 }
72 #endif
73 
74 #if ( PETSC_VERSION_MAJOR<3 || PETSC_VERSION_MAJOR==3 && PETSC_VERSION_MINOR<5 )
75 /*
76  * If using older an older PETSc (pre-3.2) include some citations here from a more recent version.
77  * (The following are from 3.5.2)
78  */
79 static PetscBool PetscCite1 = PETSC_FALSE;
80 const char PetscCitation1[] = "@TechReport{petsc-user-ref,\n"
81  " Author = {Satish Balay and Shrirang Abhyankar and Mark F. Adams and Jed Brown and Peter Brune\n"
82  " and Kris Buschelman and Victor Eijkhout and William D. Gropp\n"
83  " and Dinesh Kaushik and Matthew G. Knepley\n"
84  " and Lois Curfman McInnes and Karl Rupp and Barry F. Smith\n"
85  " and Hong Zhang},\n"
86  " Title = {{PETS}c Users Manual},\n"
87  " Number = {ANL-95/11 - Revision 3.5},\n"
88  " Institution = {Argonne National Laboratory},\n"
89  " Year = {2014}\n"
90  "}\n";
91 static PetscBool PetscCite2 = PETSC_FALSE;
92 const char PetscCitation2[] = "@InProceedings{petsc-efficient,\n"
93  " Author = {Satish Balay and William D. Gropp and Lois Curfman McInnes and Barry F. Smith},\n"
94  " Title = {Efficient Management of Parallelism in Object Oriented Numerical Software Libraries},\n"
95  " Booktitle = {Modern Software Tools in Scientific Computing},\n"
96  " Editor = {E. Arge and A. M. Bruaset and H. P. Langtangen},\n"
97  " Pages = {163--202},\n"
98  " Publisher = {Birkh{\\\"{a}}user Press},\n"
99  " Year = {1997}\n"
100  "}\n";
101 #endif
102 
103 /* Main Chaste citation */
104 static PetscBool ChasteCite = PETSC_FALSE;
105 const char ChasteCitation[] = "@article{mirams2013chaste,\n"
106  " author = {Mirams, G.R. and Arthurs, C.J. and Bernabeu, M.O. and Bordas, R. and Cooper, "
107  "J. and Corrias, A. and Davit, Y. and Dunn, S-J. and Fletcher, A.G. and Harvey, D.G. and "
108  "Marsh, M.E. and Osborne, J.M. and Pathmanathan, P. and Pitt-Francis, J. and Southern, J. "
109  "and Zemzemi, N. and Gavaghan, D.J.},\n"
110  " title = {Chaste: an open source C++ library for computational physiology and biology},\n"
111  " journal = {PLoS computational biology},\n"
112  " volume = {9},\n"
113  " number = {3},\n"
114  " pages = {e1002970},\n"
115  " year = {2013},\n"
116  " publisher = {Public Library of Science}\n"
117  "}\n";
118 
120 {
121  // The CommandLineArguments instance is filled in by the cxxtest test suite runner.
123  PETSCEXCEPT(PetscInitialize(p_args->p_argc, p_args->p_argv, PETSC_NULL, PETSC_NULL));
124 }
125 
126 
128 {
129  InitialisePetsc();
130 
131 #if ( PETSC_VERSION_MAJOR<3 || PETSC_VERSION_MAJOR==3 && PETSC_VERSION_MINOR<5 )
132  // Add some PETSc citations if we're not using the built-in citation mechanisms
133  Citations::Register(PetscCitation1, &PetscCite1);
134  Citations::Register(PetscCitation2, &PetscCite2);
135 #endif
136  // Add main Chaste citation
137  Citations::Register(ChasteCitation, &ChasteCite);
138 
139  // Check that the working directory is correct, or many tests will fail
140  std::string cwd = GetCurrentWorkingDirectory() + "/";
141  if (strcmp(cwd.c_str(), ChasteSourceRootDir()) != 0)
142  {
143 #define COVERAGE_IGNORE
144  // Change directory
145  std::cout << std::endl << "Changing directory from '" << cwd << "' to '" << ChasteSourceRootDir() << "'." << std::endl;
146  EXPECT0(chdir, ChasteSourceRootDir());
147  std::cout << "CWD now: " << GetCurrentWorkingDirectory() << std::endl;
148 #undef COVERAGE_IGNORE
149  }
150 
151 #ifdef TEST_FOR_FPE
152  // Give all PETSc enabled tests the ability to trap for divide-by-zero
153  feenableexcept(FE_DIVBYZERO | FE_INVALID );
154  // Catch all SIGFPE signals and convert them to exceptions (before PETSc gets to them)
155  struct sigaction sa;
156  sa.sa_sigaction = FpeSignalToAbort;
157  sa.sa_flags = SA_RESETHAND|SA_SIGINFO;
158  sa.sa_restorer = 0;
159  sigaction(SIGFPE, &sa, NULL);
160 #endif
161 }
162 
164 {
166  PETSCEXCEPT(PetscFinalize());
167 }
168 
170 {
172 }
const char * ChasteSourceRootDir()
static void CommonSetup()
static void InitialisePetsc()
#define EXPECT0(cmd, arg)
Definition: Exception.hpp:254
static void CommonFinalize()
static void ResetStatusCache()
static void Register(const char *pCitation, PetscBool *pSet)
Definition: Citations.cpp:43
static CommandLineArguments * Instance()
static void ResetCache()
Definition: PetscTools.cpp:58
static void Print()
Definition: Citations.cpp:71