ICON Community Interface 0.4.0
Loading...
Searching...
No Matches
yac_example.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3from yac import (
4 YAC,
5 Reg2dGrid,
6 Location,
7 Field,
8 TimeUnit,
9 Reduction,
10 InterpolationStack,
11 NNNReductionType,
12 UnstructuredGrid,
13 version as yac_version,
14)
15from packaging.version import Version
16import comin
17import numpy as np
18import matplotlib.pyplot as plt
19
21assert glob.yac_instance_id != -1, "The host-model is not configured with yac"
22
23yac = YAC.from_id(glob.yac_instance_id)
24source_comp = yac.predef_comp("comin_example_source")
25
27connectivity = (np.asarray(domain.cells.vertex_blk) - 1) * glob.nproma + (
28 np.asarray(domain.cells.vertex_idx) - 1
29)
30icon_grid = UnstructuredGrid(
31 "comin_icon_grid",
32 np.ones(domain.cells.ncells) * 3,
33 np.array(np.ravel(np.transpose(domain.verts.vlon))[: domain.verts.nverts]),
34 np.array(np.ravel(np.transpose(domain.verts.vlat))[: domain.verts.nverts]),
35 np.ravel(np.swapaxes(connectivity, 0, 1))[: 3 * domain.cells.ncells],
36)
37icon_cell_centers = icon_grid.def_points(
38 Location.CELL,
39 np.ravel(domain.cells.clon)[: domain.cells.ncells],
40 np.ravel(domain.cells.clat)[: domain.cells.ncells],
41)
42
43rank = comin.parallel_get_host_mpi_rank()
44if rank == 0:
45 target_comp = yac.predef_comp("comin_example_target")
46
47 vlon = np.linspace(-np.pi, np.pi, 360)
48 vlat = np.linspace(np.pi / 2, -np.pi / 2, 180)
49 grid = Reg2dGrid("comin_example_grid", vlon, vlat, cyclic=(True, False))
50 cell_centers = grid.def_points(
51 Location.CELL,
52 vlon + 0.5 * (vlon[1] - vlon[0]),
53 vlat[:-1] + 0.5 * (vlat[1] - vlat[0]),
54 )
55
56
57@comin.register_callback(comin.EP_SECONDARY_CONSTRUCTOR)
58def sec_ctr():
59 global comin_pres_sfc
60 comin_pres_sfc = comin.var_get(
61 [comin.EP_ATM_WRITE_OUTPUT_BEFORE], ("pres_sfc", 1), comin.COMIN_FLAG_READ
62 )
63
64
65@comin.register_callback(comin.EP_ATM_YAC_DEFCOMP_AFTER)
67 global yac_pres_sfc_target, yac_pres_sfc_source
68 yac_pres_sfc_source = Field.create(
69 "pres_sfc",
70 source_comp,
71 icon_cell_centers,
72 1,
73 str(int(comin.descrdata_get_timesteplength(1))),
74 TimeUnit.SECOND,
75 )
76 if rank == 0:
77 yac_pres_sfc_target = Field.create(
78 "pres_sfc",
79 target_comp,
80 cell_centers,
81 1,
82 str(int(comin.descrdata_get_timesteplength(1))),
83 TimeUnit.SECOND,
84 )
85 nnn = InterpolationStack()
86 kwargs = {}
87 if Version(yac_version()) > Version("3.3.0"):
88 kwargs["max_search_distance"] = 0.0
89 nnn.add_nnn(NNNReductionType.AVG, n=1, scale=1.0, **kwargs)
90 yac.def_couple(
91 "comin_example_source",
92 "comin_icon_grid",
93 "pres_sfc",
94 "comin_example_target",
95 "comin_example_grid",
96 "pres_sfc",
97 str(int(comin.descrdata_get_timesteplength(1))),
98 TimeUnit.SECOND,
99 Reduction.TIME_NONE,
100 nnn,
101 )
102
103
104@comin.register_callback(comin.EP_ATM_TIMELOOP_END)
106 yac_pres_sfc_source.put(np.ravel(comin_pres_sfc)[: domain.cells.ncells])
107 if rank == 0:
108 data, info = yac_pres_sfc_target.get()
109 plt.imshow(np.reshape(data, [179, 360]))
110 print(f"pres_sfc_{comin.current_get_datetime()}.png")
111 plt.savefig(f"pres_sfc_{comin.current_get_datetime()}.png")
var_get(context, var_descriptor, flag)
get variable object, arguments: [entry point], (name string, domain id), access flag)
Definition comin.py:107
descrdata_get_domain(jg)
returns descriptive data for a given domain, arguments: jg
Definition comin.py:167
descrdata_get_global()
returns global descriptive data object
Definition comin.py:172