Skip to content

Commit bb649d8

Browse files
mlee03mlee03
andauthored
data_override_2d (#15)
* first version of pointer to array subroutines * maybe working c_diag_manager * add 2d test * add with-yaml to fms build * add untracked file * add TODO comments to tests * combine 2d and 3d test * data_override_init * fix merge error * make optional arguments null pointers * woking version before merge * add scalar * fix comment in test_data_override_ongrid * commit files that were not commited previously? * add untracked files * add array_t_pointer * add other flavors * change test from int to double * override 2d in the works * working test * add include * fix missed cleanups in test * fix unit test with abs * lower tolerance * ci is failing --------- Co-authored-by: mlee03 <Mikyung.Lee@lscamd50-d.gfdl.noaa.gov>
1 parent 5e3a032 commit bb649d8

11 files changed

Lines changed: 362 additions & 28 deletions

c_data_override/Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ noinst_LTLIBRARIES = lib_c_data_override.la
3232
# Each convenience library depends on its source.
3333
lib_c_data_override_la_SOURCES = c_data_override.F90 \
3434
include/c_data_override_0d.fh \
35-
include/c_data_override_0d.inc
35+
include/c_data_override_0d.inc \
36+
include/c_data_override_2d.fh \
37+
include/c_data_override_2d.inc
3638

3739
c_data_override_mod.mod : c_data_override.F90
3840

c_data_override/c_data_override.F90

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module c_data_override_mod
55
use FMS, only: fms_string_utils_c2f_string, fms_string_utils_f2c_string
66
use FMS, only: fms_time_manager_set_time, fms_time_manager_set_date, FmsTime_type
77

8-
use c_fms_mod, only : cFMS_get_domain_from_id, NAME_LENGTH, MESSAGE_LENGTH
8+
use c_fms_mod, only: cFMS_get_domain_from_id, NAME_LENGTH, MESSAGE_LENGTH
9+
use c_fms_utils_mod, only: cFMS_array_to_pointer
910

1011
use iso_c_binding
1112
implicit none
@@ -14,6 +15,8 @@ module c_data_override_mod
1415

1516
public :: cFMS_data_override_0d_cfloat
1617
public :: cFMS_data_override_0d_cdouble
18+
public :: cFMS_data_override_2d_cfloat
19+
public :: cFMS_data_override_2d_cdouble
1720
public :: cFMS_data_override_init
1821
public :: cFMS_data_override_set_time
1922

@@ -94,5 +97,6 @@ subroutine cFMS_data_override_set_time(year, month, day, hour, minute, second, t
9497
end subroutine cFMS_data_override_set_time
9598

9699
#include "c_data_override_0d.fh"
100+
#include "c_data_override_2d.fh"
97101

98102
end module c_data_override_mod

c_data_override/c_data_override.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ extern void cFMS_data_override_0d_cfloat(char *gridname, char *fieldname_code, f
1212
extern void cFMS_data_override_0d_cdouble(char *gridname, char *fieldname_code, float *data_out, bool *override,
1313
int *data_index);
1414

15+
extern void cFMS_data_override_2d_cfloat(char *gridname, char *fieldname, int *data_shape, float *data,
16+
bool *override, int *is, int *ie, int *js, int *je);
17+
18+
extern void cFMS_data_override_2d_cdouble(char *gridname, char *fieldname, int *data_shape, double *data,
19+
bool *override, int *is, int *ie, int *js, int *je);
20+
1521
extern void cFMS_data_override_init(int *atm_domain_id, int *ocn_domain_id, int *ice_domain_id, int *land_domain_id,
1622
int *land_domainUG_id, int *mode);
1723

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#undef CFMS_DATA_OVERRIDE_2D_
2+
#undef CFMS_DATA_OVERRIDE_2D_BINDC_
3+
#undef CFMS_DATA_OVERRIDE_2D_TYPE_
4+
#define CFMS_DATA_OVERRIDE_2D_ cFMS_data_override_2d_cfloat
5+
#define CFMS_DATA_OVERRIDE_2D_BINDC_ "cFMS_data_override_2d_cfloat"
6+
#define CFMS_DATA_OVERRIDE_2D_TYPE_ real(c_float)
7+
#include "c_data_override_2d.inc"
8+
9+
#undef CFMS_DATA_OVERRIDE_2D_
10+
#undef CFMS_DATA_OVERRIDE_2D_BINDC_
11+
#undef CFMS_DATA_OVERRIDE_2D_TYPE_
12+
#define CFMS_DATA_OVERRIDE_2D_ cFMS_data_override_2d_cdouble
13+
#define CFMS_DATA_OVERRIDE_2D_BINDC_ "cFMS_data_override_2d_cdouble"
14+
#define CFMS_DATA_OVERRIDE_2D_TYPE_ real(c_double)
15+
#include "c_data_override_2d.inc"
16+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
subroutine CFMS_DATA_OVERRIDE_2D_(gridname, fieldname, data_shape, data, override, is, ie, js, je) &
2+
bind(C, name=CFMS_DATA_OVERRIDE_2D_BINDC_)
3+
4+
implicit none
5+
character(c_char), intent(in) :: gridname(NAME_LENGTH)
6+
character(c_char), intent(in) :: fieldname(NAME_LENGTH)
7+
integer, intent(in) :: data_shape(2)
8+
type(c_ptr), value,intent(in) :: data
9+
logical(c_bool), intent(out), optional :: override
10+
integer, intent(in), optional :: is
11+
integer, intent(in), optional :: ie
12+
integer, intent(in), optional :: js
13+
integer, intent(in), optional :: je
14+
15+
character(len=NAME_LENGTH-1) :: gridname_f
16+
character(len=NAME_LENGTH-1) :: fieldname_f
17+
logical :: override_f
18+
19+
CFMS_DATA_OVERRIDE_2D_TYPE_, allocatable :: data_f(:,:)
20+
integer :: i, j
21+
22+
gridname_f = fms_string_utils_c2f_string(gridname)
23+
fieldname_f = fms_string_utils_c2f_string(fieldname)
24+
25+
allocate(data_f(data_shape(1),data_shape(2)))
26+
27+
call fms_data_override(gridname = gridname_f, &
28+
fieldname = fieldname_f, &
29+
data_2D = data_f, &
30+
time = data_override_time, &
31+
override = override_f, &
32+
is_in = is, &
33+
js_in = js, &
34+
ie_in = ie, &
35+
je_in = je)
36+
37+
call cfms_array_to_pointer(data_f, data_shape, data)
38+
deallocate(data_f)
39+
40+
if(present(override)) override = logical(override, c_bool)
41+
42+
end subroutine CFMS_DATA_OVERRIDE_2D_

test_cfms/c_data_override/Makefile.am

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ AM_CPPFLAGS = -I. -I$(MODDIR) -I${top_builddir}/c_data_override \
2525
# Link to the FMS library.
2626
LDADD = ${top_builddir}/libcFMS/libcFMS.la
2727

28-
check_PROGRAMS = test_data_override
28+
check_PROGRAMS = test_data_override_scalar \
29+
test_data_override_2d
2930

30-
TESTS = test_data_override.sh
31+
TESTS = test_data_override_scalar.sh \
32+
test_data_override_2d.sh
3133

32-
test_data_override_SOURCES = ../c_fms/c_mpp_domains_helper.c test_data_override.c
34+
test_data_override_scalar_SOURCES = ../c_fms/c_mpp_domains_helper.c test_data_override_scalar.c
35+
test_data_override_2d_SOURCES = ../c_fms/c_mpp_domains_helper.c test_data_override_2d.c
36+
37+
test_data_override_ongrid : test_data_override_ongrid.F90
38+
$(FC) $(FCFLAGS) $(LDFLAGS) -I./include test_data_override_ongrid.F90 -o test_data_override_ongrid
3339

3440
test_data_override_ongrid : test_data_override_ongrid.F90
3541
$(FC) $(FCFLAGS) $(LDFLAGS) -I./include test_data_override_ongrid.F90 -o test_data_override_ongrid
@@ -39,7 +45,7 @@ SH_LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \
3945
$(abs_top_srcdir)/test_cfms/tap-driver.sh
4046

4147
# Include these files with the distribution.
42-
EXTRA_DIST = test_data_override.sh
48+
EXTRA_DIST = test_data_override_scalar.sh test_data_override_2d.sh
4349

4450
# Clean up
4551
CLEANFILES = *.nml* *.out *.dpi *.spi *.dyn *.spl *_table* input* *trs *.nc*
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <c_fms.h>
4+
#include <c_data_override.h>
5+
#include <c_mpp_domains_helper.h>
6+
7+
#define NX 360
8+
#define NY 180
9+
#define TOLERANCE 1e-10
10+
#define TEST_NTIMES 11
11+
12+
#define ABS(val,answ) (val<answ ? answ-val : val-answ)
13+
14+
int main()
15+
{
16+
17+
int ndomain = 1;
18+
int nnest_domain = 0;
19+
int domain_id = 0;
20+
int ehalo = 2;
21+
int whalo = 2;
22+
int shalo = 2;
23+
int nhalo = 2;
24+
int calendar_type = JULIAN;
25+
int is, ie, js, je, xsize, ysize;
26+
27+
cFMS_init(NULL, NULL, &ndomain, &nnest_domain, &calendar_type);
28+
29+
// define domain
30+
{
31+
int global_indices[4] = {0, NX-1, 0, NY-1};
32+
33+
cDomainStruct domain;
34+
cFMS_null_cdomain(&domain);
35+
36+
domain.layout = (int *)malloc(2*sizeof(int));
37+
domain.layout[0] = 2;
38+
domain.layout[1] = 2;
39+
40+
domain.domain_id = &domain_id;
41+
domain.global_indices = global_indices;
42+
domain.ehalo = &ehalo;
43+
domain.whalo = &whalo;
44+
domain.shalo = &shalo;
45+
domain.nhalo = &nhalo;
46+
47+
cFMS_define_domains_easy(domain);
48+
}
49+
50+
//get_compute_domain
51+
{
52+
int *xmax_size = NULL;
53+
int *ymax_size = NULL;
54+
bool *x_is_global = NULL;
55+
bool *y_is_global = NULL;
56+
int *tile_count = NULL;
57+
int *position = NULL;
58+
59+
cFMS_get_compute_domain(&domain_id, &is, &ie, &js, &je, &xsize, xmax_size, &ysize, ymax_size,
60+
x_is_global, y_is_global, tile_count, position, NULL, NULL);
61+
}
62+
63+
//data override init
64+
{
65+
int *atm_domain_id = NULL;
66+
int *ice_domain_id = NULL;
67+
int *land_domain_id = NULL;
68+
int *land_domainUG_id = NULL;
69+
cFMS_data_override_init(atm_domain_id, &domain_id, ice_domain_id, land_domain_id, land_domainUG_id, NULL);
70+
}
71+
72+
// data override 2d
73+
{
74+
char gridname[NAME_LENGTH] = "OCN";
75+
char fieldname[NAME_LENGTH] = "runoff_decreasing";
76+
int data_shape[2];
77+
double *data = NULL;
78+
bool override = false;
79+
80+
int year = 1;
81+
int month = 1;
82+
int day = 4;
83+
int hour = 0;
84+
int minute = 0;
85+
int second = 0;
86+
87+
data_shape[0] = xsize;
88+
data_shape[1] = ysize;
89+
data = (double *)malloc(xsize*ysize*sizeof(double));
90+
91+
cFMS_data_override_set_time(&year, &month, &day, &hour, &minute, &second, NULL, NULL);
92+
cFMS_data_override_2d_cdouble(gridname, fieldname, data_shape, data, &override, NULL, NULL, NULL, NULL);
93+
94+
for(int ij=0; ij<xsize*ysize; ij++) {
95+
if( ABS(data[ij],100.03) > TOLERANCE ) {
96+
printf("index %d data=%lf answer=%lf, diff=%lf\n", ij, data[ij], 100.03, ABS(data[ij],100.03));
97+
cFMS_error(FATAL, "FAILURE IN 2D DATA_OVERRIDE");
98+
exit(EXIT_FAILURE);
99+
}
100+
}
101+
}
102+
103+
cFMS_end();
104+
return EXIT_SUCCESS;
105+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/sh
2+
#***********************************************************************
3+
#* GNU Lesser General Public License
4+
#*
5+
#* This file is part of the GFDL Flexible Modeling System (FMS).
6+
#*
7+
#* FMS is free software: you can redistribute it and/or modify it under
8+
#* the terms of the GNU Lesser General Public License as published by
9+
#* the Free Software Foundation, either version 3 of the License, or (at
10+
#* your option) any later version.
11+
#*
12+
#* FMS is distributed in the hope that it will be useful, but WITHOUT
13+
#* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
#* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
#* for more details.
16+
#*
17+
#* You should have received a copy of the GNU Lesser General Public
18+
#* License along with FMS. If not, see <http://www.gnu.org/licenses/>.
19+
#***********************************************************************
20+
# This is part of the GFDL FMS package. This is a shell script to
21+
# execute tests in the test_fms/coupler directory.
22+
23+
# Set common test settings.
24+
. ../test-lib.sh
25+
26+
if [ -f "input.nml" ] ; then rm -f input.nml ; fi
27+
28+
make test_data_override_ongrid
29+
30+
if [ -d INPUT ] ; then rm -rf INPUT; fi
31+
mkdir INPUT
32+
33+
#generate input for bilinear 2d
34+
cat <<EOF > input.nml
35+
&test_data_override_ongrid_nml
36+
test_case=2
37+
write_only=.True.
38+
/
39+
&data_override_nml
40+
use_data_table_yaml = .True.
41+
/
42+
EOF
43+
44+
cat <<_EOF > data_table.yaml
45+
data_table:
46+
- grid_name: OCN
47+
fieldname_in_model: runoff_increasing
48+
override_file:
49+
- fieldname_in_file: runoff
50+
file_name: ./INPUT/bilinear_increasing.nc
51+
interp_method: bilinear
52+
factor: 1.0
53+
- grid_name: OCN
54+
fieldname_in_model: runoff_decreasing
55+
override_file:
56+
- fieldname_in_file: runoff
57+
file_name: ./INPUT/bilinear_decreasing.nc
58+
interp_method: bilinear
59+
factor: 1.0
60+
_EOF
61+
62+
./test_data_override_ongrid
63+
64+
test_expect_success "c_data_override_2d" 'mpirun -n 4 ./test_data_override_2d'
65+
test_done
66+
67+
rm -rf INPUT test_data_override_ongrid

0 commit comments

Comments
 (0)