1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) 2022-2023
// Author: Tommy Breslein (github.com/tbreslein)
// License: MIT

//! Exports useful macros for public use

/// Expands to a type alias `P` for the type of physics you are using, and a constant `E` that
/// represents the number of equations in that system.
/// This macro is used to make sure that E is always set correctly for your type of physics, while
/// also giving you a useful type alias to make your code look more generic than it actually is.
///
/// NOTE: You need to have set the constant S, which sets how many cells the mesh will have, before
/// calling this macro, otherwise it will not compile!
///
/// # Arguments
///
/// Only accepts either of:
///
/// * `Euler1DAdiabatic`
/// * `Euler1DIsot`
///
/// # Examples
///
/// ```
/// use corries::prelude::*;
/// use std::any::TypeId;
///
/// // Set up adiabatic 1d Euler physics
/// const S: usize = 100;
/// set_Physics_and_E!(Euler1DAdiabatic);
/// assert_eq!(TypeId::of::<P>(), TypeId::of::<Euler1DAdiabatic<S>>());
/// assert_eq!(E, 3);
/// ```
///
/// ```
/// use corries::prelude::*;
/// use std::any::TypeId;
///
/// // Set up isothermal 1d Euler physics
/// const S: usize = 100;
/// set_Physics_and_E!(Euler1DIsot);
/// assert_eq!(TypeId::of::<P>(), TypeId::of::<Euler1DIsot<S>>());
/// assert_eq!(E, 2);
/// ```
#[macro_export]
macro_rules! set_Physics_and_E {
    (Euler1DAdiabatic) => {
        type P = Euler1DAdiabatic<S>;
        const E: usize = P::NUM_EQ;
    };
    (Euler1DIsot) => {
        type P = Euler1DIsot<S>;
        const E: usize = P::NUM_EQ;
    };
}

/// Macro to check that each double in a list is finite.
///
/// # Examples
///
/// ```
/// use color_eyre::{Result, eyre::ensure};
/// use corries::check_finite_double;
///
/// fn get_finite() -> Result<()> {
///     check_finite_double!(1.0_f64);
///     Ok(())
/// }
/// fn get_inf() -> Result<()> {
///     check_finite_double!(2.2_f64, f64::INFINITY, 1.0_f64);
///     Ok(())
/// }
///
/// fn main() {
///     let succeeds = get_finite();
///     let errors = get_inf();
///     assert!(succeeds.is_ok());
///     assert!(errors.is_err());
/// }
/// ```
#[macro_export]
macro_rules! check_finite_double {
    ($($s:ident.$x:ident),*) => {
       $(ensure!(
            $s.$x.is_finite(),
            "{0} turned non-finite! Got: {0} = {1}",
            stringify!($x),
            $s.$x
        );)*
    };
    ($($x:expr),*) => {
        $(
            ensure!(
                $x.is_finite(),
                "{0} turned non-finite! Got: {0} = {1}",
                stringify!($x),
                $x);
         )*
    };
}

/// Macro to check that each `Array` in a list is finite.
///
/// # Examples
///
/// ```
/// use color_eyre::{Result, eyre::ensure};
/// use corries::check_finite_arrayd;
/// use ndarray::Array1;
///
/// fn get_finites() -> Result<()> {
///     check_finite_arrayd!(Array1::<f64>::zeros(5));
///     Ok(())
/// }
/// fn get_infs() -> Result<()> {
///     check_finite_arrayd!(Array1::<f64>::ones(3), Array1::<f64>::from_elem(5, f64::INFINITY));
///     Ok(())
/// }
///
/// fn main() {
///     let succeeds = get_finites();
///     let errors = get_infs();
///     assert!(succeeds.is_ok());
///     assert!(errors.is_err());
/// }
/// ```
#[macro_export]
macro_rules! check_finite_arrayd {
    ($($s:ident.$x:ident),*) => {
       $(ensure!(
            $s.$x.fold(true, |acc, y| acc && y.is_finite()),
            "{0} turned non-finite! Got: {0} = {1}",
            stringify!($x),
            $s.$x
        );)*
    };
    ($($x:expr),*) => {
       $(ensure!(
            $x.fold(true, |acc, y| acc && y.is_finite()),
            "{0} turned non-finite! Got: {0} = {1}",
            stringify!($x),
            $x
        );)*
    };
}

/// Macro to check that each double in a list is finite.
///
/// # Examples
///
/// ```
/// use color_eyre::{Result, eyre::ensure};
/// use corries::check_positive_double;
///
/// fn get_positive() -> Result<()> {
///     check_positive_double!(1.0_f64);
///     Ok(())
/// }
/// fn get_zero() -> Result<()> {
///     check_positive_double!(2.2_f64, 0.0_f64, 1.0_f64);
///     Ok(())
/// }
///
/// fn main() {
///     let succeeds = get_positive();
///     let errors = get_zero();
///     assert!(succeeds.is_ok());
///     assert!(errors.is_err());
/// }
/// ```
#[macro_export]
macro_rules! check_positive_double {
    ($($s:ident.$x:ident),*) => {
       $(ensure!(
            $s.$x > 0.0,
            "{0} is non-positive! Got: {0} = {1}",
            stringify!($x),
            $s.$x
        );)*
    };
    ($($x:expr),*) => {
        $(
            ensure!(
                $x > 0.0,
                "{0} is non-positive! Got: {0} = {1}",
                stringify!($x),
                $x);
         )*
    };
}

/// Macro to check that each `Array` has only positive non-zero elements.
///
/// # Examples
///
/// ```
/// use color_eyre::{Result, eyre::ensure};
/// use corries::check_positive_arrayd;
/// use ndarray::Array1;
///
/// fn get_ones() -> Result<()> {
///     check_positive_arrayd!(Array1::<f64>::ones(5));
///     Ok(())
/// }
/// fn get_zeros() -> Result<()> {
///     check_positive_arrayd!(Array1::<f64>::ones(2), Array1::<f64>::zeros(2));
///     Ok(())
/// }
///
/// fn main() {
///     let succeeds = get_ones();
///     let errors = get_zeros();
///     assert!(succeeds.is_ok());
///     assert!(errors.is_err());
/// }
/// ```
#[macro_export]
macro_rules! check_positive_arrayd {
    ($($s:ident.$x:ident),*) => {
       $(ensure!(
            $s.$x.fold(true, |acc, y| acc && y > &0.0),
            "{0} turned non-positive! Got: {0} = {1}",
            stringify!($x),
            $s.$x
        );)*
    };
    ($($x:expr),*) => {
       $(ensure!(
            $x.fold(true, |acc, y| acc && y > &0.0),
            "{0} turned non-positive! Got: {0} = {1}",
            stringify!($x),
            $x
        );)*
    };
}