YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_core.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
12try:
13 from mpi4py import MPI # calls MPI_Init with some special treatments
14 comm = MPI.COMM_WORLD
15 rank = comm.rank
16except:
17 comm = None
18 rank = 0
19
20from yac.core import (BasicGrid,
21 InterpField,
22 InterpolationStack,
23 compute_weights,
24 version,
25 yac_location,
26 yac_weight_file_on_existing,
27 lonlat2xyz,
28 get_io_ranks,
29 )
30import numpy as np
31import sys
32import asyncio
33import logging
34
35logging.basicConfig(level=logging.WARNING, format=f'%(name)s rank={rank} - %(levelname)s - %(message)s')
36logging.getLogger("yac.core").setLevel(logging.INFO)
37
38logging.getLogger("yac.core").info(f"YAC core version: {version()}")
39
40grid_dir = sys.argv[1]
41
42reg_grid = BasicGrid.reg_2d_new("reg_grid",
43 np.linspace(-2, 2, 20),
44 np.linspace(-1.5, 1.5, 20))
45reg_grid.set_core_mask(yac_location.YAC_LOC_CORNER, np.ones(400, dtype=int))
46
47# assert that set_global_index raises an exception if indices are out of bounds
48try:
49 reg_grid.set_global_index(yac_location.YAC_LOC_CORNER, range(10**99, 10**99+400))
50 raise RuntimeError("set_global_index did not throw an exception for very large indices.")
51except AssertionError:
52 pass
53
54reg_grid.set_global_index(yac_location.YAC_LOC_CORNER, range(400))
55reg_grid.to_file("reg_grid.nc", comm)
56
57coords = reg_grid.add_coordinates(yac_location.YAC_LOC_CORNER)
58
59assert reg_grid.check_coordinates(is_fatal=True) == False
60
61src_field = InterpField(coords)
62
63icon_grid, icon_cell_field = BasicGrid.read_icon(grid_dir+"icon_grid_R02B01.nc",
64 "icon", comm=comm)
65
66interp_stack = InterpolationStack.from_list([("nnn", {"n": 3, "max_search_distance": 2.}),
67 ("fixed", {"value": 42.0})])
68
69weights = compute_weights(interp_stack,
70 src_field,
71 icon_cell_field)
72
73weights.write_to_file("weights.nc", on_existing=yac_weight_file_on_existing.YAC_WEIGHT_FILE_OVERWRITE)
74
75interpolate = weights.get_interpolation(collection_selection=[1, 4, 2])
76
77src_size = src_field.basic_grid.get_data_size(yac_location.YAC_LOC_CORNER)
78src = np.stack([i*np.ones(src_size, dtype=np.float64) for i in range(5)], axis=0)
79
80tgt = interpolate(src)
81assert np.all(tgt[0,...] == 1.0), f"Wrong result: {tgt=}"
82assert np.all(tgt[1,...] == 4.0), f"Wrong result: {tgt=}"
83assert np.all(tgt[2,...] == 2.0), f"Wrong result: {tgt=}"
84
85local_is_io, io_ranks = get_io_ranks()
86assert local_is_io == True
87assert len(io_ranks) == 1
88assert io_ranks[0] == 0
89
90async def _main():
91 tgt = await interpolate.execute_async(src)
92 assert np.all(tgt[0,...] == 1.0), f"Wrong result: {tgt=}"
93 assert np.all(tgt[1,...] == 4.0), f"Wrong result: {tgt=}"
94 assert np.all(tgt[2,...] == 2.0), f"Wrong result: {tgt=}"
95
96asyncio.run(_main())
int info