YAC 3.12.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_raw_exchange.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3# Copyright (c) 2024 The YAC Authors
4#
5# SPDX-License-Identifier: BSD-3-Clause
6
7
11
12import numpy as np
13from scipy.sparse import csr_matrix
14
15from yac import (
16 YAC,
17 def_calendar,
18 Calendar,
19 Reg2dGrid,
20 Location,
21 TimeUnit,
22 Field,
23 InterpolationStack,
24 ConservNormalizationType,
25 Reduction,
26)
27
28def_calendar(Calendar.PROLEPTIC_GREGORIAN)
29
30yac = YAC()
31
32yac.def_datetime("2020-01-01T00:00:00", "2020-01-02T00:00:00")
33
34comp1, comp2 = yac.def_comps(["comp1", "comp2"])
35
36grid1 = Reg2dGrid("grid1", [-1, 0, 1], [-1, 0, 1])
37grid1.set_core_mask([1, 1, 0, 1], Location.CELL)
38points1 = grid1.def_points(Location.CELL, [-0.5, 0.5], [-0.5, 0.5])
39
40grid2 = Reg2dGrid("grid2", [-1, -1/3, 1/3, 1], [-1, -1/3, 1/3, 1])
41points2 = grid2.def_points(Location.CELL, [-2/3, 0.0, 2/3], [-2/3, 0.0, 2/3])
42
43field1 = Field.create("field1", comp1, points1, 1, "1", TimeUnit.HOUR)
44field2 = Field.create("field2", comp2, points2, 1, "1", TimeUnit.HOUR)
45field3 = Field.create("field3", comp2, points2, 1, "1", TimeUnit.HOUR)
46field4 = Field.create("field4", comp2, points2, 1, "1", TimeUnit.HOUR)
47
49interp.add_conservative(1, 0, 1, ConservNormalizationType.DESTAREA)
50interp.add_fixed(42.0)
51
52couple_kwargs = {"src_comp": "comp1",
53 "src_grid": "grid1",
54 "src_field": "field1",
55 "tgt_comp": "comp2",
56 "tgt_grid": "grid2",
57 "coupling_timestep": "60",
58 "timeunit": TimeUnit.MINUTE,
59 "time_reduction": Reduction.TIME_NONE,
60 "interp_stack": interp}
61
62yac.def_couple(
63 **couple_kwargs,
64 tgt_field="field2",
65 use_raw_exchange=True
66)
67
68yac.def_couple(
69 **couple_kwargs,
70 tgt_field="field3"
71)
72
73yac.def_couple(
74 **couple_kwargs,
75 tgt_field="field4",
76 use_raw_exchange=True
77)
78
79yac.enddef()
80
81raw_data = field2.get_raw_interp_weights_data()
82indptr = np.insert(np.cumsum(raw_data.num_src_per_tgt), 0, 0)
83
84raw_data_csr = field4.get_raw_interp_weights_data_csr()
85W = csr_matrix((raw_data_csr.weights, raw_data_csr.src_idx, raw_data_csr.src_indptr))
86
87buf = np.empty(shape=(1, raw_data.src_field_buffer_sizes[0]), dtype=np.float64)
88
89i = 0.
90while field2.datetime < yac.end_datetime:
91 field1.put(np.arange(i, i + 4., dtype=np.float64))
92
93 buf[:] = 0.0
94 field2.get_raw(buf)
95 f2_buf = np.empty(shape=(1, field2.size), dtype=np.float64)
96 f2_buf[0, raw_data.tgt_idx_fixed] = raw_data.fixed_values[0]
97 for tgt_idx, start_idx, end_idx in zip(raw_data.wgt_tgt_idx, indptr[:-1], indptr[1:]):
98 f2_buf[0, tgt_idx] = raw_data.weights[start_idx:end_idx]@buf[0, raw_data.src_idx[start_idx: end_idx]]
99
100 f3_buf, info = field3.get()
101 assert np.allclose(f2_buf, f3_buf)
102
103 field4.get_raw(buf)
104 f4_buf = W@buf[0, :]
105 f4_buf[raw_data_csr.tgt_idx_fixed] = raw_data_csr.fixed_values[0]
106 assert np.allclose(f2_buf[0, :], f4_buf)
107
108 i += 1
A stuctured 2d Grid.
Definition yac.pyx:1332
Initializies a YAC instance and provides further functionality.
Definition yac.pyx:693