OpenMD 3.2
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 following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48#include "utils/simError.h"
49
50#include <config.h>
51
52#include <cstdio>
53#include <cstdlib>
54#include <cstring>
55
56#ifdef IS_MPI
57#include <mpi.h>
58#endif
59
60int nChecks;
61
62errorStruct painCave;
63
64char checkPointMsg[MAX_SIM_ERROR_MSG_LENGTH];
65int worldRank;
66
67void initSimError(void) {
68 painCave.errMsg[0] = '\0';
69 painCave.isFatal = 0;
70 painCave.severity = OPENMD_ERROR;
71 painCave.isEventLoop = 0;
72 nChecks = 0;
73#ifdef IS_MPI
74 MPI_Comm_rank(MPI_COMM_WORLD, &worldRank);
75#else
76 worldRank = 0;
77#endif
78}
79
80int simError(void) {
81 char errorMsg[MAX_SIM_ERROR_MSG_LENGTH];
82
83#ifdef IS_MPI
84 int myError = 1;
85 int isError;
86 char nodeMsg[MAX_SIM_ERROR_MSG_LENGTH];
87#endif
88
89 strcpy(errorMsg, "OpenMD ");
90 switch (painCave.severity) {
91 case OPENMD_WARNING:
92 strcat(errorMsg, "warning");
93 break;
94 case OPENMD_INFO:
95 strcat(errorMsg, "info");
96 break;
97 default:
98 if (painCave.isFatal) { strcat(errorMsg, "FATAL "); }
99 strcat(errorMsg, "ERROR");
100 }
101
102#ifdef IS_MPI
103 if (worldRank == 0) {
104 if (painCave.isEventLoop) {
105 snprintf(nodeMsg, MAX_SIM_ERROR_MSG_LENGTH, " (reported by MPI node %d)",
106 worldRank);
107 strncat(errorMsg, nodeMsg,
108 MAX_SIM_ERROR_MSG_LENGTH - strlen(errorMsg) - 1);
109 errorMsg[MAX_SIM_ERROR_MSG_LENGTH - 1] = '\0';
110 }
111#endif
112
113 strcat(errorMsg, ":\n\t");
114 strncat(errorMsg, painCave.errMsg,
115 MAX_SIM_ERROR_MSG_LENGTH - strlen(errorMsg) - 1);
116 errorMsg[MAX_SIM_ERROR_MSG_LENGTH - 1] = '\0';
117 strcat(errorMsg, "\n");
118
119 switch (painCave.severity) {
120 case OPENMD_WARNING:
121 case OPENMD_INFO:
122 fprintf(stdout, "%s", errorMsg);
123 break;
124 default:
125 fprintf(stderr, "%s", errorMsg);
126 }
127
128#ifdef IS_MPI
129 if (painCave.isEventLoop) return 1;
130 }
131#endif
132
133 if (painCave.isFatal) {
134#ifdef IS_MPI
135 MPI_Allreduce(&myError, &isError, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);
136 MPI_Finalize();
137#endif
138 exit(0);
139 }
140 return 1;
141}
142
143void errorCheckPoint(void) {
144 int myError = 0;
145 int isError = 0;
146
147#ifdef IS_MPI
148 MPI_Allreduce(&myError, &isError, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);
149#else
150 isError = myError;
151#endif
152
153 if (isError) {
154#ifdef IS_MPI
155 MPI_Finalize();
156#endif
157 exit(0);
158 }
159
160#ifdef CHECKPOINT_VERBOSE
161 nChecks++;
162
163#ifdef IS_MPI
164 if (worldRank == 0) {
165#endif
166
167 fprintf(stderr, "Checkpoint #%d reached: %s\n", nChecks, checkPointMsg);
168#ifdef IS_MPI
169 }
170#endif
171
172#endif
173}