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
use {
    crate::parameters::Parameters,
    anyhow::Result,
    std::{
        fs::File,
        io::{Read, Write},
    },
};

pub fn swto3d(parameters: &Parameters) -> Result<()> {
    let ng = parameters.numerical.grid_resolution;
    let nz = parameters.numerical.vertical_layers;

    let split = {
        let mut f = File::open(parameters.environment.output_directory.join("sw_init.r8"))?;
        let mut sw = Vec::new();
        f.read_to_end(&mut sw)?;

        sw.chunks((ng * ng + 1) * 8)
            .map(|xs| xs[8..].to_vec())
            .collect::<Vec<Vec<u8>>>()
    };

    let mut qq_file = File::create(parameters.environment.output_directory.join("qq_init.r8"))?;
    let mut dd_file = File::create(parameters.environment.output_directory.join("dd_init.r8"))?;
    let mut gg_file = File::create(parameters.environment.output_directory.join("gg_init.r8"))?;

    qq_file.write_all(&[0u8; 8])?;
    dd_file.write_all(&[0u8; 8])?;
    gg_file.write_all(&[0u8; 8])?;

    for _ in 0..=nz {
        qq_file.write_all(&split[0])?;
        dd_file.write_all(&split[1])?;
        gg_file.write_all(&split[2])?;
    }

    Ok(())
}

#[cfg(test)]
mod test {
    use {super::*, tempdir::TempDir};

    #[test]
    fn _18_2_qq() {
        let tempdir = TempDir::new("shallow-water").unwrap();

        let mut sw = File::create(tempdir.path().join("sw_init.r8")).unwrap();
        sw.write_all(include_bytes!("testdata/swto3d/18_2_sw_init.r8"))
            .unwrap();

        let mut params = Parameters::default();
        params.numerical.grid_resolution = 18;
        params.numerical.vertical_layers = 2;
        params.environment.output_directory = tempdir.path().to_owned();

        swto3d(&params).unwrap();

        for (i, byte) in File::open(params.environment.output_directory.join("qq_init.r8"))
            .unwrap()
            .bytes()
            .enumerate()
        {
            assert_eq!(
                include_bytes!("testdata/swto3d/18_2_qq_init.r8")[i],
                byte.unwrap()
            );
        }
    }

    #[test]
    fn _18_2_dd() {
        let tempdir = TempDir::new("shallow-water").unwrap();

        let mut sw = File::create(tempdir.path().join("sw_init.r8")).unwrap();
        sw.write_all(include_bytes!("testdata/swto3d/18_2_sw_init.r8"))
            .unwrap();

        let mut params = Parameters::default();
        params.numerical.grid_resolution = 18;
        params.numerical.vertical_layers = 2;
        params.environment.output_directory = tempdir.path().to_owned();

        swto3d(&params).unwrap();

        for (i, byte) in File::open(params.environment.output_directory.join("dd_init.r8"))
            .unwrap()
            .bytes()
            .enumerate()
        {
            assert_eq!(
                include_bytes!("testdata/swto3d/18_2_dd_init.r8")[i],
                byte.unwrap()
            );
        }
    }

    #[test]
    fn _18_2_gg() {
        let tempdir = TempDir::new("shallow-water").unwrap();

        let mut sw = File::create(tempdir.path().join("sw_init.r8")).unwrap();
        sw.write_all(include_bytes!("testdata/swto3d/18_2_sw_init.r8"))
            .unwrap();

        let mut params = Parameters::default();
        params.numerical.grid_resolution = 18;
        params.numerical.vertical_layers = 2;
        params.environment.output_directory = tempdir.path().to_owned();

        swto3d(&params).unwrap();

        for (i, byte) in File::open(params.environment.output_directory.join("gg_init.r8"))
            .unwrap()
            .bytes()
            .enumerate()
        {
            assert_eq!(
                include_bytes!("testdata/swto3d/18_2_gg_init.r8")[i],
                byte.unwrap()
            );
        }
    }

    #[test]
    fn _128_32() {
        let tempdir = TempDir::new("shallow-water").unwrap();

        let mut sw = File::create(tempdir.path().join("sw_init.r8")).unwrap();
        sw.write_all(include_bytes!("testdata/swto3d/128_32_sw_init.r8"))
            .unwrap();

        let mut params = Parameters::default();
        params.numerical.grid_resolution = 128;
        params.numerical.vertical_layers = 32;
        params.environment.output_directory = tempdir.path().to_owned();

        swto3d(&params).unwrap();

        for (i, byte) in File::open(params.environment.output_directory.join("qq_init.r8"))
            .unwrap()
            .bytes()
            .enumerate()
        {
            assert_eq!(
                include_bytes!("testdata/swto3d/128_32_qq_init.r8")[i],
                byte.unwrap()
            );
        }

        for (i, byte) in File::open(params.environment.output_directory.join("dd_init.r8"))
            .unwrap()
            .bytes()
            .enumerate()
        {
            assert_eq!(
                include_bytes!("testdata/swto3d/128_32_dd_init.r8")[i],
                byte.unwrap()
            );
        }

        for (i, byte) in File::open(params.environment.output_directory.join("gg_init.r8"))
            .unwrap()
            .bytes()
            .enumerate()
        {
            assert_eq!(
                include_bytes!("testdata/swto3d/128_32_gg_init.r8")[i],
                byte.unwrap()
            );
        }
    }
}