OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
simError.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45#include "utils/simError.h"
46
47#include <config.h>
48
49#include <cstdio>
50#include <cstdlib>
51#include <cstring>
52
53#ifdef IS_MPI
54#include <mpi.h>
55#endif
56
57int nChecks;
58
59errorStruct painCave;
60
61char checkPointMsg[MAX_SIM_ERROR_MSG_LENGTH];
62int worldRank;
63
64void initSimError(void) {
65 painCave.errMsg[0] = '\0';
66 painCave.isFatal = 0;
67 painCave.severity = OPENMD_ERROR;
68 painCave.isEventLoop = 0;
69 nChecks = 0;
70#ifdef IS_MPI
71 MPI_Comm_rank(MPI_COMM_WORLD, &worldRank);
72#else
73 worldRank = 0;
74#endif
75}
76
77int simError(void) {
78 char errorMsg[MAX_SIM_ERROR_MSG_LENGTH];
79
80#ifdef IS_MPI
81 int myError = 1;
82 int isError;
83 char nodeMsg[MAX_SIM_ERROR_MSG_LENGTH];
84#endif
85
86 strcpy(errorMsg, "OpenMD ");
87 switch (painCave.severity) {
88 case OPENMD_WARNING:
89 strcat(errorMsg, "warning");
90 break;
91 case OPENMD_INFO:
92 strcat(errorMsg, "info");
93 break;
94 default:
95 if (painCave.isFatal) { strcat(errorMsg, "FATAL "); }
96 strcat(errorMsg, "ERROR");
97 }
98
99#ifdef IS_MPI
100 if (worldRank == 0) {
101 if (painCave.isEventLoop) {
102 snprintf(nodeMsg, MAX_SIM_ERROR_MSG_LENGTH, " (reported by MPI node %d)",
103 worldRank);
104 strncat(errorMsg, nodeMsg,
105 MAX_SIM_ERROR_MSG_LENGTH - strlen(errorMsg) - 1);
106 errorMsg[MAX_SIM_ERROR_MSG_LENGTH - 1] = '\0';
107 }
108#endif
109
110 strcat(errorMsg, ":\n\t");
111 strncat(errorMsg, painCave.errMsg,
112 MAX_SIM_ERROR_MSG_LENGTH - strlen(errorMsg) - 1);
113 errorMsg[MAX_SIM_ERROR_MSG_LENGTH - 1] = '\0';
114 strcat(errorMsg, "\n");
115
116 switch (painCave.severity) {
117 case OPENMD_WARNING:
118 case OPENMD_INFO:
119 fprintf(stdout, "%s", errorMsg);
120 break;
121 default:
122 fprintf(stderr, "%s", errorMsg);
123 }
124
125#ifdef IS_MPI
126 if (painCave.isEventLoop) return 1;
127 }
128#endif
129
130 if (painCave.isFatal) {
131#ifdef IS_MPI
132 MPI_Allreduce(&myError, &isError, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);
133 MPI_Finalize();
134#endif
135 exit(0);
136 }
137 return 1;
138}
139
140void errorCheckPoint(void) {
141 int myError = 0;
142 int isError = 0;
143
144#ifdef IS_MPI
145 MPI_Allreduce(&myError, &isError, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);
146#else
147 isError = myError;
148#endif
149
150 if (isError) {
151#ifdef IS_MPI
152 MPI_Finalize();
153#endif
154 exit(0);
155 }
156
157#ifdef CHECKPOINT_VERBOSE
158 nChecks++;
159
160#ifdef IS_MPI
161 if (worldRank == 0) {
162#endif
163
164 fprintf(stderr, "Checkpoint #%d reached: %s\n", nChecks, checkPointMsg);
165#ifdef IS_MPI
166 }
167#endif
168
169#endif
170}