Skip to content

Commit e94682a

Browse files
committed
Moves two switches to modelStructures.h, adds test update script
1 parent becec4d commit e94682a

File tree

4 files changed

+93
-5
lines changed

4 files changed

+93
-5
lines changed

Diff for: modelStructures.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//
2-
// This file is inteneded to hold settings for different model structure options,
2+
// This file is intended to hold settings for different model structure options,
33
// implemented as compile-time flags. The options are here (with nothing else) to
44
// improve testability.
55
//
6+
// If this file is changed, consider running tests/update_model_structures.sh to update
7+
// the corresponding unit test versions of this file.
68

79
#ifndef MODEL_STRUCTURES_H
810
#define MODEL_STRUCTURES_H
@@ -14,5 +16,10 @@
1416
// Read in and process agronomic events. SIPNET expects a file named events.in to exist, though
1517
// unit tests may use other names.
1618

19+
// have extra litter pool, in addition to soil c pool
20+
#define LITTER_POOL 0
21+
22+
// Whether we model root dynamics
23+
#define ROOTS 1
1724

1825
#endif //MODEL_STRUCTURES_H

Diff for: sipnet.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
#define SOIL_PHENOL 0 && !GDD
9797
// use soil temp. to determine leaf growth? (note: mutually exclusive with GDD)
9898

99-
#define LITTER_POOL 0
99+
// LITTER_POOL moved to modelStructures.h
100100
// have extra litter pool, in addition to soil c pool
101101

102102
#define SOIL_MULTIPOOL 0 && !LITTER_POOL
@@ -120,7 +120,7 @@
120120
#define STOICHIOMETRY 0 && MICROBES
121121
// do we utilize stoichometric considerations for the microbial pool?
122122

123-
#define ROOTS 1
123+
// ROOTS moved to modelStructures.h
124124
// do we model root dynamics?
125125

126126

Diff for: tests/sipnet/test_events/modelStructures.h

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
//
2-
// This file is inteneded to hold settings for different model structure options,
2+
// DO NOT ADD CUSTOM CODE TO THIS FILE (other than #define overrides for unit tests)
3+
// IT MAY BE OVERWRITTEN BY update_model_structures.sh
4+
//
5+
// This file is intended to hold settings for different model structure options,
36
// implemented as compile-time flags. The options are here (with nothing else) to
47
// improve testability.
58
//
9+
// If this file is changed, consider running tests/update_model_structures.sh to update
10+
// the corresponding unit test versions of this file.
611

712
#ifndef MODEL_STRUCTURES_H
813
#define MODEL_STRUCTURES_H
@@ -11,7 +16,13 @@
1116
// See also sipnet.c for other options (that should be moved here if/when testing is added)
1217

1318
#define EVENT_HANDLER 1
14-
// Read in and process agronomic events. Expects a file named <FILENAME>.event to exist.
19+
// Read in and process agronomic events. SIPNET expects a file named events.in to exist, though
20+
// unit tests may use other names.
21+
22+
// have extra litter pool, in addition to soil c pool
23+
#define LITTER_POOL 0
1524

25+
// Whether we model root dynamics
26+
#define ROOTS 1
1627

1728
#endif //MODEL_STRUCTURES_H

Diff for: tests/update_model_structures.sh

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
# Run this script from the root SIPNET directory with the command:
3+
# > tests/update_model_structures.sh
4+
5+
# Name of the file containing #define compiler switches
6+
STRUCTURES_FILE="modelStructures.h"
7+
8+
# Get the path to the current directory's STRUCTURES_FILE - that is,
9+
# the production version
10+
PROD_MODEL_STRUCTURES="./${STRUCTURES_FILE}"
11+
12+
# Check if the current directory has the base model_structures.h
13+
if [[ ! -f $PROD_MODEL_STRUCTURES ]]; then
14+
echo "Error: ${STRUCTURES_FILE} not found in the current directory."
15+
exit 1
16+
fi
17+
18+
# Define the awk script to update #define values; this will be called on each test
19+
# version of STRUCTURES_FILE
20+
read -r -d '' AWK_SCRIPT << 'EOF'
21+
BEGIN {
22+
FS = "[ \t]+"; # Set field separator to handle spaces or tabs
23+
define_regex = "^#define";
24+
skip_regex = "MODEL_STRUCTURES_H";
25+
}
26+
27+
# Process the test structures file and store the #define values in an associative array
28+
FNR == NR {
29+
if ($1 ~ define_regex && $2 !~ skip_regex) {
30+
name = $2; # The second field is the name of the #define
31+
value = $3; # The third field is the value of the #define
32+
defines[name] = value;
33+
}
34+
next;
35+
}
36+
37+
# Update the new file with the old #define values
38+
{
39+
if ($1 ~ define_regex && $2 !~ skip_regex) {
40+
name = $2;
41+
if (name in defines) {
42+
printf "#define %s %s\n", name, defines[name];
43+
next;
44+
}
45+
}
46+
# Print the line as-is if no overwrite is needed
47+
print $0;
48+
}
49+
EOF
50+
51+
# Find and process all target files in subdirectories
52+
find . -type f -name "$STRUCTURES_FILE" -mindepth 2 | while read -r file; do
53+
echo "Processing: $file"
54+
55+
# Create a backup of the original file
56+
cp "$file" "${file}.bak"
57+
58+
# Add a warning at the top, then update defines
59+
{
60+
echo "//"
61+
echo "// DO NOT ADD CUSTOM CODE TO THIS FILE (other than #define overrides for unit tests)"
62+
echo "// IT MAY BE OVERWRITTEN BY update_model_structures.sh"
63+
cat "$PROD_MODEL_STRUCTURES"
64+
} | awk "$AWK_SCRIPT" "$file" - > "${file}.tmp"
65+
66+
# Overwrite the target file with the updated content
67+
mv "${file}.tmp" "$file"
68+
69+
echo "Updated: $file"
70+
done

0 commit comments

Comments
 (0)