Skip to content

Commit 6c953f7

Browse files
committed
Repplaced all exit() calls with exception throws
1 parent c7a618a commit 6c953f7

16 files changed

+127
-202
lines changed

deps/eigen

Submodule eigen updated from 285da30 to 2fae4d7

src/afm_necking_fraction.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "remove_overlap.h"
2525
#include "io_common.h"
2626
#include "random_engine.h"
27+
#include "exception.h"
2728

2829
// Alias for aggregate mechanics model
2930
using aggregate_model_t = aggregate<Eigen::Vector3d, double>;
@@ -63,19 +64,15 @@ bool is_monomer_hindered(long monomer, std::vector<Eigen::Vector3d> const & xs,
6364

6465
// Entry point
6566
int main(int argc, const char ** argv) {
66-
if (argc < 2) {
67-
std::cerr << "Path to the input file must be provided as an argument" << std::endl;
68-
exit(EXIT_FAILURE);
69-
}
67+
if (argc < 2)
68+
throw DemException("Path to the input file must be provided as an argument");
7069

7170
auto parameter_store = load_parameters(argv[1]);
7271

7372
print_header(parameter_store, "afm_necking_fraction");
7473

75-
if (parameter_store.simulation_type != "afm_necking_fraction") {
76-
std::cerr << "Parameter file simulation type must be `afm_necking_fraction`" << std::endl;
77-
exit(EXIT_FAILURE);
78-
}
74+
if (parameter_store.simulation_type != "afm_necking_fraction")
75+
throw DemException("Parameter file simulation type must be `afm_necking_fraction`");
7976

8077
/* General simulation parameters */
8178
// Integration time step
@@ -212,14 +209,10 @@ int main(int argc, const char ** argv) {
212209
bool has_necks_path = has_path_parameter(parameter_store, "necks_path");
213210
bool has_break_n_necks = has_integer_parameter(parameter_store, "break_n_necks");
214211

215-
if (has_frac_necks && has_necks_path) {
216-
std::cerr << "Parameters `frac_necks` and `necks_path` are mutually exclusive, but both were provided" << std::endl;
217-
exit(EXIT_FAILURE);
218-
}
219-
if (has_break_n_necks && !has_necks_path) {
220-
std::cerr << "Parameter `break_n_necks` can only be used in combination with `necks_path`" << std::endl;
221-
exit(EXIT_FAILURE);
222-
}
212+
if (has_frac_necks && has_necks_path)
213+
throw DemException("Parameters `frac_necks` and `necks_path` are mutually exclusive, but both were provided");
214+
if (has_break_n_necks && !has_necks_path)
215+
throw DemException("Parameter `break_n_necks` can only be used in combination with `necks_path`");
223216

224217
// Substrate vertices
225218
const std::tuple<Eigen::Vector3d, Eigen::Vector3d, Eigen::Vector3d, Eigen::Vector3d> substrate_vertices {
@@ -249,10 +242,8 @@ int main(int argc, const char ** argv) {
249242

250243
x0 = load_aggregate(parameter_store);
251244

252-
if (x0.size() == 0) {
253-
std::cerr << "Loaded an empty aggregate" << std::endl;
254-
exit(EXIT_FAILURE);
255-
}
245+
if (x0.empty())
246+
throw DemException("Loaded an empty aggregate");
256247
std::cout << "Loaded an aggregate of size " << x0.size() << std::endl;
257248

258249
remove_overlap(x0, r_part, d_crit, n_overlap_iter);
@@ -360,8 +351,7 @@ int main(int argc, const char ** argv) {
360351
}
361352

362353
} else {
363-
std::cerr << "Either `frac_necks` or `necks_path` must be provided for this simulation type" << std::endl;
364-
exit(EXIT_FAILURE);
354+
throw DemException("Either `frac_necks` or `necks_path` must be provided for this simulation type");
365355
}
366356

367357
state_printer_t state_printer(system.get_x(), system.get_v(), system.get_theta(), system.get_omega(), mass, inertia, n_dumps);

src/afm_tip.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <libgran/surface_force/triangular_facet.h>
1616
#include <libgran/granular_system/granular_system.h>
1717

18+
#include "exception.h"
19+
1820
template <typename field_value_t, typename real_t>
1921
struct afm_tip {
2022

@@ -106,10 +108,8 @@ struct afm_tip {
106108

107109
std::ofstream ofs(ss.str());
108110

109-
if (!ofs.good()) {
110-
std::cerr << "Unable to create a facet file" << std::endl;
111-
exit(EXIT_FAILURE);
112-
}
111+
if (!ofs.good())
112+
throw DemException("Unable to create a facet file");
113113

114114
ofs << "solid facet\n";
115115

src/aggregate.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,6 @@ struct aggregate {
102102
}
103103

104104
private:
105-
106-
/*void load_mackowski_aggregate(std::string const & path) {
107-
std::ifstream ifs(path);
108-
109-
if (!ifs.good()) {
110-
std::cerr << "Unable to read the aggregate file: " << path << std::endl;
111-
exit(EXIT_FAILURE);
112-
}
113-
114-
std::string line;
115-
116-
while (getline(ifs, line)) {
117-
if (!line.empty()) {
118-
std::istringstream oss(line);
119-
double _, x, y, z;
120-
oss >> _ >> x >> y >> z;
121-
x0.emplace_back(x * r_part, y * r_part, z * r_part);
122-
}
123-
}
124-
}*/
125-
126105
contact_model_t contact_model;
127106
sinter_model_t sinter_model;
128107
hamaker_model_t hamaker_model;

src/aggregate_deposition.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "energy.h"
2020
#include "remove_overlap.h"
2121
#include "io_common.h"
22+
#include "exception.h"
2223

2324
using aggregate_model_t = aggregate<Eigen::Vector3d, double>;
2425
using rect_substrate_model_t = rect_substrate<Eigen::Vector3d, double>;
@@ -49,19 +50,15 @@ void center_in_the_xy_plane(std::vector<Eigen::Vector3d> & x) {
4950
}
5051

5152
int main(int argc, const char ** argv) {
52-
if (argc < 2) {
53-
std::cerr << "Path to the input file must be provided as an argument" << std::endl;
54-
exit(EXIT_FAILURE);
55-
}
53+
if (argc < 2)
54+
throw DemException("Path to the input file must be provided as an argument");
5655

5756
auto parameter_store = load_parameters(argv[1]);
5857

5958
print_header(parameter_store, "aggregate_deposition");
6059

61-
if (parameter_store.simulation_type != "deposition") {
62-
std::cerr << "Parameter file simulation type must be `deposition`" << std::endl;
63-
exit(EXIT_FAILURE);
64-
}
60+
if (parameter_store.simulation_type != "deposition")
61+
throw DemException("Parameter file simulation type must be `deposition`");
6562

6663
// General simulation parameters
6764
const double dt = get_real_parameter(parameter_store, "dt");
@@ -148,10 +145,8 @@ int main(int argc, const char ** argv) {
148145

149146
x0 = load_aggregate(parameter_store);
150147

151-
if (x0.size() == 0) {
152-
std::cerr << "Loaded an empty aggregate" << std::endl;
153-
exit(EXIT_FAILURE);
154-
}
148+
if (x0.empty())
149+
throw DemException("Loaded an empty aggregate");
155150
std::cout << "Loaded an aggregate of size " << x0.size() << std::endl;
156151

157152
remove_overlap(x0, r_part, d_crit, n_overlap_iter);

src/aggregation.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "io_common.h"
3030
#include "parameter_loader.h"
3131
#include "random_engine.h"
32+
#include "exception.h"
3233

3334
using contact_force_model_t = contact_force_functor<Eigen::Vector3d, double>;
3435
using hamaker_force_model_t = hamaker_functor<Eigen::Vector3d, double>;
@@ -137,19 +138,15 @@ void populate_node_indices(AggregateGraph & graph, std::vector<GraphEdge> const
137138
}
138139

139140
int main(int argc, char ** argv) {
140-
if (argc < 2) {
141-
std::cerr << "Path to the input file must be provided as an argument" << std::endl;
142-
exit(EXIT_FAILURE);
143-
}
141+
if (argc < 2)
142+
throw DemException("Path to the input file must be provided as an argument");
144143

145144
auto parameter_store = load_parameters(argv[1]);
146145

147146
print_header(parameter_store, "aggregation");
148147

149-
if (parameter_store.simulation_type != "aggregation") {
150-
std::cerr << "Parameter file simulation type must be `aggregation`" << std::endl;
151-
exit(EXIT_FAILURE);
152-
}
148+
if (parameter_store.simulation_type != "aggregation")
149+
throw DemException("Parameter file simulation type must be `aggregation`");
153150

154151
// General simulation parameters
155152
const double dt = get_real_parameter(parameter_store, "dt");

src/anchored_restructuring.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "parameter_loader.h"
3030
#include "random_engine.h"
3131
#include "rect_substrate.h"
32+
#include "exception.h"
3233

3334
using aggregate_model_t = aggregate<Eigen::Vector3d, double>;
3435
using coating_model_t = binary_coating_functor<Eigen::Vector3d, double>;
@@ -41,19 +42,15 @@ using granular_system_t = granular_system_neighbor_list<Eigen::Vector3d, double,
4142
rotational_step_handler, binary_force_container_t, unary_force_container_t>;
4243

4344
int main(int argc, const char ** argv) {
44-
if (argc < 2) {
45-
std::cerr << "Path to the input file must be provided as an argument" << std::endl;
46-
exit(EXIT_FAILURE);
47-
}
45+
if (argc < 2)
46+
throw DemException("Path to the input file must be provided as an argument");
4847

4948
auto parameter_store = load_parameters(argv[1]);
5049

5150
print_header(parameter_store, "anchored_restructuring");
5251

53-
if (parameter_store.simulation_type != "anchored_restructuring") {
54-
std::cerr << "Parameter file simulation type must be `anchored_restructuring`" << std::endl;
55-
exit(EXIT_FAILURE);
56-
}
52+
if (parameter_store.simulation_type != "anchored_restructuring")
53+
throw DemException("Parameter file simulation type must be `anchored_restructuring`");
5754

5855
// General simulation parameters
5956
const double dt = get_real_parameter(parameter_store, "dt");
@@ -146,10 +143,8 @@ int main(int argc, const char ** argv) {
146143
x0 = load_aggregate(parameter_store);
147144
remove_overlap(x0, r_part, d_crit, n_overlap_iter);
148145

149-
if (x0.size() == 0) {
150-
std::cerr << "Loaded an empty aggregate" << std::endl;
151-
exit(EXIT_FAILURE);
152-
}
146+
if (x0.empty())
147+
throw DemException("Loaded an empty aggregate");
153148
std::cout << "Loaded an aggregate of size " << x0.size() << std::endl;
154149

155150
// Fill the remaining buffers with zeros

src/exception.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Created by Egor on 8/11/2024.
3+
//
4+
5+
#ifndef SOOT_AFM_EXCEPTION_H
6+
#define SOOT_AFM_EXCEPTION_H
7+
8+
#include <string>
9+
#include <exception>
10+
11+
class DemException : public std::exception {
12+
public:
13+
explicit DemException(std::string const & message) : message{message} {}
14+
15+
const char * what() const noexcept override {
16+
return message.c_str();
17+
}
18+
19+
private:
20+
const std::string message;
21+
};
22+
23+
#endif //SOOT_AFM_EXCEPTION_H

src/io_common.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "energy.h"
99
#include "aggregate_stats.h"
1010
#include "git.h"
11+
#include "exception.h"
1112

1213
#include "io_common.h"
1314

@@ -45,8 +46,7 @@ std::vector<Eigen::Vector3d> load_aggregate(parameter_store_t const & parameter_
4546
} else if (aggregate_type == "vtk") {
4647
x0 = load_vtk_aggregate(aggregate_path, r_part);
4748
} else {
48-
std::cerr << "Unrecognized aggregate type: `" << aggregate_type << "`" << std::endl;
49-
exit(EXIT_FAILURE);
49+
throw DemException("Unrecognized aggregate type: `" + aggregate_type + "`");
5050
}
5151

5252
return x0;

0 commit comments

Comments
 (0)