PetscSetupUtils.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "PetscSetupUtils.hpp"
00037 
00038 #include <petsc.h>
00039 #include <cstdlib>
00040 #include <cassert>
00041 #include <cstring>
00042 #include <iostream>
00043 
00044 #include "ChasteBuildRoot.hpp"
00045 #include "ChasteSyscalls.hpp"
00046 #include "Citations.hpp"
00047 #include "CommandLineArguments.hpp"
00048 #include "Exception.hpp"
00049 #include "GetCurrentWorkingDirectory.hpp"
00050 #include "PetscException.hpp"
00051 #include "PetscTools.hpp"
00052 
00053 #ifdef TEST_FOR_FPE
00054 #include <fenv.h>
00055 #include <signal.h>
00056 
00057 void FpeSignalToAbort(int sig_num, siginfo_t* info, void* context )
00058 {
00059        if ( info->si_code == FPE_FLTDIV)
00060        {
00061            std::cerr << "SIGFPE: floating point exception was divide by zero.\n";
00062        }
00063        else if ( info->si_code == FPE_FLTINV)
00064        {
00065            std::cerr << "SIGFPE: floating point exception was an invalid operation (like 0.0/0.0).\n";
00066        }
00067        else
00068        {
00069            std::cerr << "SIGFPE: unexpected error code.\n";
00070        }
00071 }
00072 #endif
00073 
00074 #if ( PETSC_VERSION_MAJOR<3 || PETSC_VERSION_MAJOR==3 && PETSC_VERSION_MINOR<5 )
00075 /*
00076  * If using older an older PETSc (pre-3.2) include some citations here from a more recent version.
00077  * (The following are from 3.5.2)
00078  */
00079 static PetscBool PetscCite1 = PETSC_FALSE;
00080 const char PetscCitation1[] = "@TechReport{petsc-user-ref,\n"
00081                               "  Author = {Satish Balay and Shrirang Abhyankar and Mark F. Adams and Jed Brown and Peter Brune\n"
00082                               "            and Kris Buschelman and Victor Eijkhout and William D. Gropp\n"
00083                               "            and Dinesh Kaushik and Matthew G. Knepley\n"
00084                               "            and Lois Curfman McInnes and Karl Rupp and Barry F. Smith\n"
00085                               "            and Hong Zhang},\n"
00086                               "  Title = {{PETS}c Users Manual},\n"
00087                               "  Number = {ANL-95/11 - Revision 3.5},\n"
00088                               "  Institution = {Argonne National Laboratory},\n"
00089                               "  Year = {2014}\n"
00090                               "}\n";
00091 static PetscBool PetscCite2 = PETSC_FALSE;
00092 const char PetscCitation2[] = "@InProceedings{petsc-efficient,\n"
00093                               "  Author = {Satish Balay and William D. Gropp and Lois Curfman McInnes and Barry F. Smith},\n"
00094                               "  Title = {Efficient Management of Parallelism in Object Oriented Numerical Software Libraries},\n"
00095                               "  Booktitle = {Modern Software Tools in Scientific Computing},\n"
00096                               "  Editor = {E. Arge and A. M. Bruaset and H. P. Langtangen},\n"
00097                               "  Pages = {163--202},\n"
00098                               "  Publisher = {Birkh{\\\"{a}}user Press},\n"
00099                               "  Year = {1997}\n"
00100                               "}\n";
00101 #endif
00102 
00103 /* Main Chaste citation */
00104 static PetscBool ChasteCite = PETSC_FALSE;
00105 const char ChasteCitation[] = "@article{mirams2013chaste,\n"
00106                               "  author    = {Mirams, G.R. and Arthurs, C.J. and Bernabeu, M.O. and Bordas, R. and Cooper, "
00107                               "J. and Corrias, A. and Davit, Y. and Dunn, S-J. and Fletcher, A.G. and Harvey, D.G. and "
00108                               "Marsh, M.E. and Osborne, J.M. and Pathmanathan, P. and Pitt-Francis, J. and Southern, J. "
00109                               "and Zemzemi, N. and Gavaghan, D.J.},\n"
00110                               "  title     = {Chaste: an open source C++ library for computational physiology and biology},\n"
00111                               "  journal   = {PLoS computational biology},\n"
00112                               "  volume    = {9},\n"
00113                               "  number    = {3},\n"
00114                               "  pages     = {e1002970},\n"
00115                               "  year      = {2013},\n"
00116                               "  publisher = {Public Library of Science}\n"
00117                               "}\n";
00118 
00119 void PetscSetupUtils::InitialisePetsc()
00120 {
00121     // The CommandLineArguments instance is filled in by the cxxtest test suite runner.
00122     CommandLineArguments* p_args = CommandLineArguments::Instance();
00123     PETSCEXCEPT(PetscInitialize(p_args->p_argc, p_args->p_argv, PETSC_NULL, PETSC_NULL));
00124 }
00125 
00126 
00127 void PetscSetupUtils::CommonSetup()
00128 {
00129     InitialisePetsc();
00130 
00131 #if ( PETSC_VERSION_MAJOR<3 || PETSC_VERSION_MAJOR==3 && PETSC_VERSION_MINOR<5 )
00132     // Add some PETSc citations if we're not using the built-in citation mechanisms
00133     Citations::Register(PetscCitation1, &PetscCite1);
00134     Citations::Register(PetscCitation2, &PetscCite2);
00135 #endif
00136     // Add main Chaste citation
00137     Citations::Register(ChasteCitation, &ChasteCite);
00138 
00139     // Check that the working directory is correct, or many tests will fail
00140     std::string cwd = GetCurrentWorkingDirectory() + "/";
00141     if (strcmp(cwd.c_str(), ChasteBuildRootDir()) != 0)
00142     {
00143 #define COVERAGE_IGNORE
00144         // Change directory
00145         std::cout << std::endl << "Changing directory from '" << cwd << "' to '" << ChasteBuildRootDir() << "'." << std::endl;
00146         EXPECT0(chdir, ChasteBuildRootDir());
00147         std::cout << "CWD now: " << GetCurrentWorkingDirectory() << std::endl;
00148 #undef COVERAGE_IGNORE
00149     }
00150 
00151 #ifdef TEST_FOR_FPE
00152     // Give all PETSc enabled tests the ability to trap for divide-by-zero
00153     feenableexcept(FE_DIVBYZERO | FE_INVALID );
00154     // Catch all SIGFPE signals and convert them to exceptions (before PETSc gets to them)
00155     struct sigaction sa;
00156     sa.sa_sigaction = FpeSignalToAbort;
00157     sa.sa_flags = SA_RESETHAND|SA_SIGINFO;
00158     sa.sa_restorer = 0;
00159     sigaction(SIGFPE, &sa, NULL);
00160 #endif
00161 }
00162 
00163 void PetscSetupUtils::CommonFinalize()
00164 {
00165     Citations::Print();
00166     PETSCEXCEPT(PetscFinalize());
00167 }
00168 
00169 void PetscSetupUtils::ResetStatusCache()
00170 {
00171     PetscTools::ResetCache();
00172 }

Generated by  doxygen 1.6.2