YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_coroutines.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
13import numpy as np
14import asyncio
15
16yac.def_calendar(yac.Calendar.PROLEPTIC_GREGORIAN)
17
18yac_instance = yac.YAC()
19
20yac_instance.def_datetime("2020-01-01T00:00:00", "2020-01-02T00:00:00")
21
22comp1, comp2 = yac_instance.def_comps(["comp1", "comp2"])
23
24grid1 = yac.Reg2dGrid("grid1", [-1, 0, 1], [-1, 0, 1])
25points1 = grid1.def_points(yac.Location.CELL, [-0.5, 0.5], [-0.5, 0.5])
26
27grid2 = yac.Reg2dGrid("grid2", [-1, 0, 1], [-1, 0, 1])
28points2 = grid2.def_points(yac.Location.CELL, [-0.5, 0.5], [-0.5, 0.5])
29
30field1 = yac.Field.create("field1", comp1, points1, 1, "1", yac.TimeUnit.HOUR)
31field2 = yac.Field.create("field2", comp2, points2, 1, "1", yac.TimeUnit.HOUR)
32field3 = yac.Field.create("field3", comp2, points2, 1, "2", yac.TimeUnit.HOUR)
33
35interp.add_nnn(yac.NNNReductionType.AVG, 1, 0.0, 1.0)
36
37yac_instance.def_couple(
38 "comp1",
39 "grid1",
40 "field1",
41 "comp2",
42 "grid2",
43 "field2",
44 "60",
45 yac.TimeUnit.MINUTE,
46 yac.Reduction.TIME_NONE,
47 interp,
48)
49
50yac_instance.def_couple(
51 "comp1",
52 "grid1",
53 "field1",
54 "comp2",
55 "grid2",
56 "field3",
57 "120",
58 yac.TimeUnit.MINUTE,
59 yac.Reduction.TIME_NONE,
60 interp,
61)
62
63yac_instance.enddef()
64
65
66async def recv_loop(field, delay=False):
67 while True:
68 buf, info = await field.get_coro()
69 print(f"recv (field_id={field.field_id}):", buf, info)
70 if delay:
71 await asyncio.sleep(0.01)
72 if info == yac.Action.GET_FOR_RESTART:
73 break
74
75
76async def send_loop(field):
77 while True:
78 info = await field.put_coro(np.random.rand(4))
79 print(f"send (field_id={field.field_id}):", info)
80 if info == yac.Action.PUT_FOR_RESTART:
81 break
82
83
84# asyncio entry point
85async def main():
86 # create task group
87 await asyncio.gather(
88 send_loop(field1), recv_loop(field2), recv_loop(field3, True)
89 )
90
91
92# entry point
93asyncio.run(main())
94print("done!")
create(cls, str field_name, Component comp, points, collection_size, str timestep, TimeUnit timeunit, masks=None)
Definition yac.pyx:1773
A stuctured 2d Grid.
Definition yac.pyx:1510
Initializies a YAC instance and provides further functionality.
Definition yac.pyx:755
recv_loop(field, delay=False)
def_calendar(Calendar calendar)
Definition yac.pyx:610