Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
PetscSetupUtils.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "PetscSetupUtils.hpp"
37
38#include <cassert>
39#include <cstdlib>
40#include <cstring>
41#include <iostream>
42#include <petsc.h>
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#include "PetscAndChasteCitations.hpp"
54
55#ifdef TEST_FOR_FPE
56#include <fenv.h>
57#include <signal.h>
58
59void FpeSignalToAbort(int sig_num, siginfo_t* info, void* context)
60{
61 if (info->si_code == FPE_FLTDIV)
62 {
63 std::cerr << "SIGFPE: floating point exception was divide by zero.\n";
64 }
65 else if (info->si_code == FPE_FLTINV)
66 {
67 std::cerr << "SIGFPE: floating point exception was an invalid operation (like 0.0/0.0).\n";
68 }
69 else
70 {
71 std::cerr << "SIGFPE: unexpected error code.\n";
72 }
73}
74#endif
75
77{
78 // The CommandLineArguments instance is filled in by the cxxtest test suite runner.
80 PETSCEXCEPT(PetscInitialize(p_args->p_argc, p_args->p_argv, CHASTE_PETSC_NULLPTR, CHASTE_PETSC_NULLPTR));
81 // Work around what seems to be an Intel compiler bug/quirk that makes the cache stale,
82 // by using an explicit reset to ensure all code is aware we're running in parallel.
84}
85
87{
89
90 if ((PETSC_VERSION_MAJOR < 3) || (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 5)) // PETSc 3.4 or earlier
91 {
92 // Add some PETSc citations if we're not using the built-in citation mechanisms
93 Citations::Register(PetscCitation1, &PetscCite1);
94 Citations::Register(PetscCitation2, &PetscCite2);
95 }
96 // Add main Chaste citation
97 Citations::Register(ChasteCitation, &ChasteCite);
98
99 // Check that the working directory is correct, or many tests will fail
100 std::string cwd = GetCurrentWorkingDirectory() + "/";
101 if (strcmp(cwd.c_str(), ChasteSourceRootDir()) != 0)
102 {
103 // Change directory
104 std::cout << std::endl
105 << "Changing directory from '" << cwd << "' to '" << ChasteSourceRootDir() << "'." << std::endl;
107 std::cout << "CWD now: " << GetCurrentWorkingDirectory() << std::endl;
108 }
109
110#ifdef TEST_FOR_FPE
111 // Give all PETSc enabled tests the ability to trap for divide-by-zero
112 feenableexcept(FE_DIVBYZERO | FE_INVALID);
113 // Catch all SIGFPE signals and convert them to exceptions (before PETSc gets to them)
114 struct sigaction sa;
115 sa.sa_sigaction = FpeSignalToAbort;
116 sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
117 sa.sa_restorer = 0;
118 sigaction(SIGFPE, &sa, NULL);
119#endif
120}
121
123{
124 // This does nothing if we are on a new PETSc, just allows Chaste to print citations instead in this case
126
127 PETSCEXCEPT(PetscFinalize());
128}
129
const char * ChasteSourceRootDir()
#define EXPECT0(cmd, arg)
#define CHASTE_PETSC_NULLPTR
A macro to define PETSc null pointer based on the PETSc version.
static void Register(const char *pCitation, PetscBool *pSet)
Definition Citations.cpp:43
static void Print()
Definition Citations.cpp:71
static CommandLineArguments * Instance()
static void ResetStatusCache()
static void InitialisePetsc()
static void CommonFinalize()
static void CommonSetup()
static void ResetCache()