YAC 3.13.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_interface.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 yac
13from mpi4py import MPI
14import numpy as np
15import os
16
17rank = MPI.COMM_WORLD.rank
18size = MPI.COMM_WORLD.size
19assert size > 1
20
21yac.def_calendar(yac.Calendar.PROLEPTIC_GREGORIAN)
22assert yac.get_calendar() == yac.Calendar.PROLEPTIC_GREGORIAN
23print(yac.version())
24
25# class YAC
26yac_def_instance = yac.YAC(default_instance=True)
27yac_def_instance2 = yac.YAC.default_instance
28yac_from_id = yac.YAC.from_id(yac_def_instance2.instance_id)
29assert yac_def_instance.instance_id == yac_def_instance2.instance_id == yac_from_id.instance_id
30assert yac_def_instance.rank == rank
31assert yac_def_instance.size == size
32assert yac_def_instance.default_instance_id_defined == True
33
34yac_def_instance.set_config_output_file("foo.yaml",
35 yac.ConfigOutputFormat.YAML,
36 yac.ConfigOutputSyncLoc.ENDDEF)
37yac_def_instance.set_grid_output_file("foo_grid", "foo_grid.nc")
38
39del yac_def_instance, yac_def_instance2
40yac_comm = yac.YAC(comm=MPI.COMM_WORLD)
41assert size == yac_comm.size
42assert rank == yac_comm.rank
43del yac_comm
44yac_instance = yac.YAC()
45
46yac_instance.def_datetime("2020-01-01T00:00:00", "2020-01-02T00:00:00")
47
48# components
49if rank % 2 == 0:
50 comp1 = yac_instance.predef_comp("comp1")
51 comp2 = yac_instance.def_comp("comp2")
52else:
53 comp1, comp3 = yac_instance.def_comps(["comp1", "comp3"])
54
55assert yac_instance.start_datetime == "2020-01-01T00:00:00.000"
56assert yac_instance.end_datetime == "2020-01-02T00:00:00.000"
57
58assert yac_instance.get_comps_comm(["comp2", "comp3"]).size == size
59assert comp1.comp_comm.size == size
60assert comp1.size == comp1.comp_comm.size
61assert comp1.rank == comp1.comp_comm.rank
62
63
64yac_instance.def_component_metadata("comp1", "COMP_METADATA".encode())
65
66assert set(yac_instance.component_names) == {"comp1", "comp2", "comp3"}
67
68# class Grid
69x = np.array([-1., 0., 1.])
70x.setflags(write=0) # require a const memoryview in Reg2d
71grid1 = yac.Reg2dGrid("grid1", x, x)
72grid1.set_global_index(range(9), yac.Location.CORNER)
73grid1.set_global_index(np.arange(4, dtype=np.intc), yac.Location.CELL)
74print(f"{grid1.compute_grid_cell_areas()=}")
75assert grid1.nbr_cells == 4
76assert grid1.nbr_corners == 9
77assert grid1.nbr_edges == 12
78grid1.set_core_mask([True, False] * 2, yac.Location.CELL)
79points1 = grid1.def_points(yac.Location.CELL, [-0.5, 0.5], [-0.5, 0.5])
80assert points1.size == grid1.nbr_cells
81points1.set_mask([True] * 4)
82
83yac_instance.def_grid_metadata("grid1", "GRID_METADATA".encode())
84
86 "grid2",
87 [3],
88 [0.0, 1.0, 0.0],
89 [0.0, 0.0, 1.0],
90 [0, 1, 2],
91 use_ll_edges=False,
92)
93mask1 = grid2.def_mask(yac.Location.CELL, [True], name="mask1")
94points2 = grid2.def_points(yac.Location.CELL, [0.5], [0.5])
95
97 "grid3",
98 [4],
99 [0.0, 1.0, 1.0, 0.0],
100 [0.0, 0.0, 1.0, 1.0],
101 [0, 1, 2, 3],
102 use_ll_edges=True,
103)
104
105grid4 = yac.CloudGrid("grid4",
106 [0.0, 1.0, 1.0, 0.0],
107 [0.0, 0.0, 1.0, 1.0])
108
109points4 = grid4.def_points([0.0, 1.0, 1.0, 0.0],
110 [0.0, 0.0, 1.0, 1.0])
111
113 [3],
114 [0.0, 1.0, 0.0],
115 [0.0, 0.0, 1.0],
116 [0, 1, 2],
117 [[0, 1], [1, 2], [2, 0]],
118 use_ll_edges=False,
119 )
120points5 = grid5.def_points(yac.Location.CELL, [0.5], [0.5])
121
123 [4],
124 [0.0, 1.0, 1.0, 0.0],
125 [0.0, 0.0, 1.0, 1.0],
126 [0, 1, 2, 3],
127 [[0, 1], [1, 2], [2, 3], [3, 0]],
128 use_ll_edges=True,
129 )
130
131# regular 2d rotated grid
132grid7 = yac.Reg2dGrid("grid7", x, x, north_pole = [3.6, 0.7])
133points7 = grid1.def_points(yac.Location.CELL, [-0.5, 0.5], [-0.5, 0.5])
134
135# class Field
136field1 = yac.Field.create("field1", comp1, points1, 3, "1", yac.TimeUnit.MINUTE)
137yac_instance.def_field_metadata(
138 "comp1", "grid1", "field1", "FIELD_METADATA".encode()
139)
140yac_instance.enable_field_frac_mask("comp1", "grid1", "field1", 42.0)
141
143 "field2", comp1, points2, 2, "1", yac.TimeUnit.HOUR, mask1
144)
146 "field3", comp1, points1, 1, "1", yac.TimeUnit.HOUR
147)
149 "field4", comp1, points2, 1, "1", yac.TimeUnit.HOUR, mask1
150)
151
152# class InterpolationStack
154interp.add_nnn(yac.NNNReductionType.AVG, 1, 0.0, 1.0)
155yac.InterpolationStack().add_average(yac.AverageReductionType.AVG_DIST, 1)
156yac.InterpolationStack().add_conservative(
157 1, 1, 1, yac.ConservNormalizationType.FRACAREA
158)
159yac.InterpolationStack().add_rbf()
160yac.InterpolationStack().add_spmap(
161 1.0, 1.0, yac.SPMAPWeightType.AVG, yac.SPMAPScaleType.NONE)
162yac.InterpolationStack().add_hcsbb()
163# interp.add_user_file()
164yac.InterpolationStack().add_fixed(999.0)
165yac.InterpolationStack().add_check("ctor", "search key")
166yac.InterpolationStack().add_creep(42)
167
168yac_instance.def_couple(
169 "comp1",
170 "grid1",
171 "field1",
172 "comp1",
173 "grid2",
174 "field2",
175 "60",
176 yac.TimeUnit.MINUTE,
177 yac.Reduction.TIME_NONE,
178 interp,
179 src_lag=2,
180 yaxt_exchanger_name="irecv_send",
181 collection_selection=[2, 0],
182)
183del interp
184
186 "- nnn:\n n: 1"
187)
188del interp_yaml
189
191 "[{\"nnn\": {\"n\": 1}}]"
192)
193del interp_json
194
195if rank == 0:
196 with open("config.yaml", "w") as f:
197 f.write(
198 """timestep_unit: minute
199coupling:
200 - src_component: comp1
201 src_grid: grid1
202 src_lag: 0
203 tgt_component: comp1
204 tgt_grid: grid2
205 coupling_period: 60
206 time_reduction: none
207 interpolation:
208 - nnn:
209 n: 1
210 field:
211 src: field3
212 tgt: field4
213"""
214 )
215 yac_instance.read_config_yaml("config.yaml")
216 os.remove("config.yaml")
217
218yac_instance.sync_def()
219
220assert yac_instance.get_component_metadata("comp1") == "COMP_METADATA"
221assert yac_instance.get_grid_metadata("grid1") == "GRID_METADATA"
222assert (
223 yac_instance.get_field_metadata("comp1", "grid1", "field1")
224 == "FIELD_METADATA"
225)
226
227assert "grid1" in yac_instance.grid_names
228assert "grid1" in yac_instance.get_comp_grid_names("comp1")
229assert "field1" in yac_instance.get_field_names("comp1", "grid1")
230assert yac_instance.get_field_is_defined("comp1", "grid1", "field1")
231assert not yac_instance.get_field_is_defined("comp1", "grid1", "undefined_field")
232assert not yac_instance.get_field_is_defined("comp1", "undefined_grid", "field1")
233assert not yac_instance.get_field_is_defined("undefined_comp", "grid1", "field1")
234field1_id = yac_instance.get_field_id("comp1", "grid1", "field1")
235assert (field1.component_name == "comp1")
236assert (field1.grid_name == "grid1")
237assert (
238 yac_instance.get_field_timestep("comp1", "grid1", "field1")
239 == field1.timestep
240 == "PT01M"
241)
242assert (
243 yac_instance.get_field_role("comp1", "grid1", "field1")
244 == yac.ExchangeType.SOURCE
245)
246assert (yac_instance.get_field_collection_size("comp1", "grid1", "field1")
247 == field1.collection_size
248 == 3)
249assert (
250 yac_instance.get_field_frac_mask_fallback_value("comp1", "grid1", "field1")
251 == 42.0
252)
253assert yac_instance.get_field_source("comp1", "grid2", "field2") == ("comp1", "grid1", "field1")
254
255yac_instance.enddef()
256
257assert (field1.role == yac.ExchangeType.SOURCE ) # it is set only after enddef
258
259data, send_info, recv_info = yac.Field.exchange(field3, field4, np.random.rand(2, 2))
260field1.update()
exchange(cls, send_field, recv_field, send_buf, recv_buf=None, send_frac_mask=None)
Definition yac.pyx:1762
create(cls, str field_name, Component comp, points, collection_size, str timestep, TimeUnit timeunit, masks=None)
Definition yac.pyx:1570
from_string_json(cls, str interp_stack_string)
Definition yac.pyx:2054
from_string_yaml(cls, str interp_stack_string)
Definition yac.pyx:2044
A stuctured 2d Grid.
Definition yac.pyx:1332
An unstuctured 2d Grid.
Definition yac.pyx:1467
Initializies a YAC instance and provides further functionality.
Definition yac.pyx:693
from_id(cls, id)
Definition yac.pyx:752
version()
Definition yac.pyx:2227
get_calendar()
Definition yac.pyx:576
def_calendar(Calendar calendar)
Definition yac.pyx:570