piegy 2.0.5__tar.gz → 2.1.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- piegy-2.1.8/MANIFEST.in +6 -0
- {piegy-2.0.5/src/piegy.egg-info → piegy-2.1.8}/PKG-INFO +3 -3
- {piegy-2.0.5 → piegy-2.1.8}/README.md +1 -1
- {piegy-2.0.5 → piegy-2.1.8}/pyproject.toml +2 -5
- piegy-2.1.8/setup.py +85 -0
- piegy-2.1.8/src/piegy/C_core/Makefile +97 -0
- piegy-2.1.8/src/piegy/C_core/model.c +108 -0
- piegy-2.1.8/src/piegy/C_core/model.h +54 -0
- piegy-2.1.8/src/piegy/C_core/patch.c +43 -0
- piegy-2.1.8/src/piegy/C_core/patch.h +43 -0
- piegy-2.1.8/src/piegy/C_core/piegyc.h +49 -0
- piegy-2.1.8/src/piegy/C_core/piegyc.so +0 -0
- piegy-2.1.8/src/piegy/C_core/runner.c +61 -0
- piegy-2.1.8/src/piegy/C_core/sim_funcs.c +561 -0
- piegy-2.1.8/src/piegy/C_core/sim_funcs.h +547 -0
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy/__init__.py +2 -1
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy/__version__.py +4 -1
- piegy-2.1.8/src/piegy/build_info.py +12 -0
- piegy-2.1.8/src/piegy/find_C.py +17 -0
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy/simulation.py +31 -22
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy/simulation_py.py +1 -2
- {piegy-2.0.5 → piegy-2.1.8/src/piegy.egg-info}/PKG-INFO +3 -3
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy.egg-info/SOURCES.txt +12 -0
- piegy-2.0.5/MANIFEST.in +0 -2
- piegy-2.0.5/src/piegy/C_core/piegyc.so +0 -0
- {piegy-2.0.5 → piegy-2.1.8}/LICENSE.txt +0 -0
- {piegy-2.0.5 → piegy-2.1.8}/setup.cfg +0 -0
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy/analysis.py +0 -0
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy/data_tools.py +0 -0
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy/figures.py +0 -0
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy/test_var.py +0 -0
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy/tools/__init__.py +0 -0
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy/tools/figure_tools.py +0 -0
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy/tools/file_tools.py +0 -0
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy/videos.py +0 -0
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy.egg-info/dependency_links.txt +0 -0
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy.egg-info/requires.txt +0 -0
- {piegy-2.0.5 → piegy-2.1.8}/src/piegy.egg-info/top_level.txt +0 -0
piegy-2.1.8/MANIFEST.in
ADDED
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: piegy
|
3
|
-
Version: 2.
|
3
|
+
Version: 2.1.8
|
4
4
|
Summary: Payoff-Driven Stochastic Spatial Model for Evolutionary Game Theory
|
5
5
|
Author-email: Chenning Xu <cxu7@caltech.edu>
|
6
6
|
License: BSD 3-Clause License
|
@@ -44,7 +44,7 @@ Classifier: Programming Language :: Python :: 3
|
|
44
44
|
Classifier: Programming Language :: Python :: 3.10
|
45
45
|
Classifier: Programming Language :: Python :: 3.11
|
46
46
|
Classifier: Operating System :: OS Independent
|
47
|
-
Requires-Python: >=3.
|
47
|
+
Requires-Python: >=3.7
|
48
48
|
Description-Content-Type: text/markdown
|
49
49
|
License-File: LICENSE.txt
|
50
50
|
Requires-Dist: numpy
|
@@ -100,7 +100,7 @@ simulation.run(mod)
|
|
100
100
|
fig1, ax1 = plt.subplots()
|
101
101
|
figures.UV_dyna(mod, ax1)
|
102
102
|
fig2, ax2 = plt.subplots(1, 2, figsize = (12.8, 4.8))
|
103
|
-
|
103
|
+
figures.UV_heatmap(mod, ax2[0], ax2[1])
|
104
104
|
```
|
105
105
|
|
106
106
|
The figures reveal population dynamics and steady state population distribution.
|
@@ -45,7 +45,7 @@ simulation.run(mod)
|
|
45
45
|
fig1, ax1 = plt.subplots()
|
46
46
|
figures.UV_dyna(mod, ax1)
|
47
47
|
fig2, ax2 = plt.subplots(1, 2, figsize = (12.8, 4.8))
|
48
|
-
|
48
|
+
figures.UV_heatmap(mod, ax2[0], ax2[1])
|
49
49
|
```
|
50
50
|
|
51
51
|
The figures reveal population dynamics and steady state population distribution.
|
@@ -4,10 +4,10 @@ build-backend = 'setuptools.build_meta'
|
|
4
4
|
|
5
5
|
[project]
|
6
6
|
name = 'piegy'
|
7
|
-
version = '2.
|
7
|
+
version = '2.1.8'
|
8
8
|
description = 'Payoff-Driven Stochastic Spatial Model for Evolutionary Game Theory'
|
9
9
|
readme = 'README.md'
|
10
|
-
requires-python = '>=3.
|
10
|
+
requires-python = '>=3.7'
|
11
11
|
license = {file = "LICENSE.txt"}
|
12
12
|
authors = [
|
13
13
|
{name = 'Chenning Xu', email = 'cxu7@caltech.edu'}
|
@@ -42,7 +42,4 @@ package-dir = {'' = 'src'}
|
|
42
42
|
[tool.setuptools.packages.find]
|
43
43
|
where = ['src']
|
44
44
|
|
45
|
-
[tool.setuptools.package-data]
|
46
|
-
"piegy" = ["C_core/piegyc.so"]
|
47
|
-
|
48
45
|
|
piegy-2.1.8/setup.py
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
import subprocess
|
2
|
+
import sys
|
3
|
+
import os
|
4
|
+
import re
|
5
|
+
from setuptools import setup, Extension
|
6
|
+
from setuptools.command.build_ext import build_ext
|
7
|
+
from setuptools.command.build_py import build_py
|
8
|
+
import platform
|
9
|
+
import datetime
|
10
|
+
import textwrap
|
11
|
+
|
12
|
+
|
13
|
+
class BuildSharedLib(build_ext):
|
14
|
+
def run(self):
|
15
|
+
# Build your shared lib first
|
16
|
+
subprocess.check_call(['make', '-C', 'src/piegy/C_core', 'clean', 'so'])
|
17
|
+
# Then continue with normal build_ext run
|
18
|
+
super().run()
|
19
|
+
|
20
|
+
def build_extension(self, ext):
|
21
|
+
if sys.platform == 'win32':
|
22
|
+
lib_name = 'piegyc.dll'
|
23
|
+
else:
|
24
|
+
lib_name = 'piegyc.so'
|
25
|
+
so_path = os.path.abspath(f'src/piegy/C_core/{lib_name}')
|
26
|
+
target_path = self.get_ext_fullpath(ext.name)
|
27
|
+
|
28
|
+
os.makedirs(os.path.dirname(target_path), exist_ok=True)
|
29
|
+
self.copy_file(so_path, target_path)
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
class AddCompileInfo(build_py):
|
34
|
+
|
35
|
+
def get_version(self):
|
36
|
+
version_file = os.path.join('src', 'piegy', '__version__.py')
|
37
|
+
with open(version_file, 'r', encoding='utf-8') as f:
|
38
|
+
content = f.read()
|
39
|
+
# Regex to extract __version__ = '...'
|
40
|
+
version_match = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', content, re.MULTILINE)
|
41
|
+
if version_match:
|
42
|
+
return version_match.group(1)
|
43
|
+
raise RuntimeError("Unable to find version string.")
|
44
|
+
|
45
|
+
def generate_build_info(self):
|
46
|
+
version = self.get_version()
|
47
|
+
build_from = os.environ.get("BUILD_TYPE", "local machine")
|
48
|
+
build_info_content = f'''
|
49
|
+
"""
|
50
|
+
Contains build info, whether it's local built, or a pre-compiled wheel.
|
51
|
+
Auto-generated at compile time.
|
52
|
+
"""
|
53
|
+
|
54
|
+
build_info = {{
|
55
|
+
"version": "{version}",
|
56
|
+
"built from": "{build_from}",
|
57
|
+
"build date": "{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}",
|
58
|
+
"python version": "{platform.python_version()}",
|
59
|
+
"platform": "{sys.platform}"
|
60
|
+
}}
|
61
|
+
'''
|
62
|
+
return textwrap.dedent(build_info_content[1:])
|
63
|
+
|
64
|
+
|
65
|
+
def run(self):
|
66
|
+
# Write build_info.py directly before building python files
|
67
|
+
build_info_path = os.path.abspath(os.path.join('src/piegy/build_info.py'))
|
68
|
+
with open(build_info_path, 'w') as f:
|
69
|
+
f.write(self.generate_build_info())
|
70
|
+
super().run()
|
71
|
+
|
72
|
+
|
73
|
+
# Declare the extension module (empty sources)
|
74
|
+
ext_modules = [
|
75
|
+
Extension('piegy.C_core.piegyc', sources=[])
|
76
|
+
]
|
77
|
+
|
78
|
+
setup(
|
79
|
+
ext_modules=ext_modules,
|
80
|
+
cmdclass={'build_ext': BuildSharedLib,
|
81
|
+
'build_py': AddCompileInfo,},
|
82
|
+
package_data={"piegy": ["C_core/piegyc.so", "build_info.py"]},
|
83
|
+
include_package_data=True,
|
84
|
+
)
|
85
|
+
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# Compiler and flags
|
2
|
+
CC = gcc
|
3
|
+
|
4
|
+
#### Common Flags ####
|
5
|
+
|
6
|
+
## Flags for Max Speed ##
|
7
|
+
# standard
|
8
|
+
CFLAGS_COMMON = -O3 -flto -std=c99 -DNDEBUG -ffp-contract=fast -MMD -MP
|
9
|
+
LDFLAGS_COMMON = -flto
|
10
|
+
|
11
|
+
## Flags for Debugging ##
|
12
|
+
# slower, debuggable with lldb, with Address Sanitizer. Don't use in python
|
13
|
+
#CFLAGS = -g -std=c99 -Wall -Wextra -MMD -MP -fsanitize=address,undefined -fno-omit-frame-pointer
|
14
|
+
#LDFLAGS = -fsanitize=address,undefined
|
15
|
+
# slower, regular build, allow lldb
|
16
|
+
#CFLAGS = -g -std=c99 -Wall -Wextra -MMD -MP
|
17
|
+
#LDFLAGS =
|
18
|
+
|
19
|
+
|
20
|
+
#### OS specific ####
|
21
|
+
OS := $(shell uname)
|
22
|
+
|
23
|
+
ifeq ($(OS),Darwin)
|
24
|
+
# macOS flags
|
25
|
+
CFLAGS = $(CFLAGS_COMMON) -fPIC -mmacosx-version-min=11.0 -arch arm64
|
26
|
+
LDFLAGS = $(LDFLAGS_COMMON)
|
27
|
+
else ifeq ($(OS),Linux)
|
28
|
+
# Linux flags
|
29
|
+
CFLAGS = $(CFLAGS_COMMON) -fPIC
|
30
|
+
LDFLAGS = $(LDFLAGS_COMMON)
|
31
|
+
else ifeq '$(findstring ;,$(PATH))' ';'
|
32
|
+
# windows flags
|
33
|
+
CFLAGS = $(CFLAGS_COMMON) -D_WIN32_WINNT=0x0A00 -DWINVER=0x0A00
|
34
|
+
LDFLAGS = $(LDFLAGS_COMMON)
|
35
|
+
else
|
36
|
+
# Default fallback
|
37
|
+
CFLAGS = $(CFLAGS_COMMON) -fPIC
|
38
|
+
LDFLAGS = $(LDFLAGS_COMMON)
|
39
|
+
endif
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
#### Targets and Commands ####
|
44
|
+
|
45
|
+
# Source and object files
|
46
|
+
SRC = sim_funcs.c model.c patch.c
|
47
|
+
RUNNER_SRC = runner.c
|
48
|
+
OBJ = $(SRC:.c=.o)
|
49
|
+
RUNNER_OBJ = $(RUNNER_SRC:.c=.o)
|
50
|
+
RUNNER_DEPS = $(RUNNER_OBJ:.o=.d)
|
51
|
+
DEPS = $(OBJ:.o=.d) $(RUNNER_DEPS)
|
52
|
+
|
53
|
+
# Executable name
|
54
|
+
TEST = runner
|
55
|
+
ifeq '$(findstring ;,$(PATH))' ';'
|
56
|
+
# windows
|
57
|
+
SHARED_LIB = piegyc.dll
|
58
|
+
else
|
59
|
+
SHARED_LIB = piegyc.so
|
60
|
+
endif
|
61
|
+
|
62
|
+
|
63
|
+
# Default target
|
64
|
+
all: $(SHARED_LIB) $(TEST)
|
65
|
+
|
66
|
+
# test file
|
67
|
+
test: $(TEST)
|
68
|
+
|
69
|
+
# shared library
|
70
|
+
so: $(SHARED_LIB)
|
71
|
+
|
72
|
+
# Link the final binary
|
73
|
+
$(TEST): $(OBJ) $(RUNNER_OBJ)
|
74
|
+
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
|
75
|
+
|
76
|
+
# Shared library target
|
77
|
+
$(SHARED_LIB): $(OBJ)
|
78
|
+
$(CC) $(CFLAGS) $(LDFLAGS) -shared -o $@ $^
|
79
|
+
|
80
|
+
# Compile source files to object files
|
81
|
+
%.o: %.c
|
82
|
+
$(CC) $(CFLAGS) -c $< -o $@
|
83
|
+
|
84
|
+
# Clean build files
|
85
|
+
clean:
|
86
|
+
rm -f $(OBJ) $(RUNNER_OBJ) $(TEST) $(SHARED_LIB) $(DEPS)
|
87
|
+
|
88
|
+
# Rebuild target: clean then all
|
89
|
+
re: clean all
|
90
|
+
|
91
|
+
# Include auto-generated dependency files
|
92
|
+
-include $(DEPS)
|
93
|
+
|
94
|
+
.PHONY: all clean re test so
|
95
|
+
|
96
|
+
|
97
|
+
|
@@ -0,0 +1,108 @@
|
|
1
|
+
/**
|
2
|
+
* This .c includes the model struct and "member functions"
|
3
|
+
* that correponds to patch class in piegy.model module
|
4
|
+
*/
|
5
|
+
|
6
|
+
#include <stdlib.h>
|
7
|
+
#include <string.h>
|
8
|
+
#include <stdio.h>
|
9
|
+
#include <stdbool.h>
|
10
|
+
|
11
|
+
#include "model.h"
|
12
|
+
|
13
|
+
|
14
|
+
bool mod_init(model_t* mod, size_t N, size_t M,
|
15
|
+
double maxtime, double record_itv, size_t sim_time, bool boundary,
|
16
|
+
const uint32_t* I, const double* X, const double* P,
|
17
|
+
int32_t print_pct, int32_t seed) {
|
18
|
+
|
19
|
+
mod->N = N;
|
20
|
+
mod->M = M;
|
21
|
+
mod->maxtime = maxtime;
|
22
|
+
mod->record_itv = record_itv;
|
23
|
+
mod->sim_time = sim_time;
|
24
|
+
mod->boundary = boundary;
|
25
|
+
mod->print_pct = print_pct;
|
26
|
+
mod->seed = seed;
|
27
|
+
|
28
|
+
size_t NM = N * M;
|
29
|
+
|
30
|
+
// I, X, P
|
31
|
+
mod->I = (uint32_t*) malloc(sizeof(uint32_t) * NM * 2);
|
32
|
+
mod->X = (double*) malloc(sizeof(double) * NM * 4);
|
33
|
+
mod->P = (double*) malloc(sizeof(double) * NM * 6);
|
34
|
+
|
35
|
+
if (!mod->I || !mod->X || !mod->P) return false;
|
36
|
+
|
37
|
+
memcpy(mod->I, I, sizeof(uint32_t) * NM * 2);
|
38
|
+
memcpy(mod->X, X, sizeof(double) * NM * 4);
|
39
|
+
memcpy(mod->P, P, sizeof(double) * NM * 6);
|
40
|
+
|
41
|
+
// Data
|
42
|
+
mod->data_empty = true;
|
43
|
+
mod->max_record = (size_t)(mod->maxtime / mod->record_itv);
|
44
|
+
mod->arr_size = NM * mod->max_record;
|
45
|
+
mod->compress_itv = 1;
|
46
|
+
|
47
|
+
mod->U1d = (double*) calloc(mod->arr_size, sizeof(double));
|
48
|
+
mod->V1d = (double*) calloc(mod->arr_size, sizeof(double));
|
49
|
+
mod->Upi_1d = (double*) calloc(mod->arr_size, sizeof(double));
|
50
|
+
mod->Vpi_1d = (double*) calloc(mod->arr_size, sizeof(double));
|
51
|
+
|
52
|
+
if (!mod->U1d || !mod->V1d || !mod->Upi_1d || !mod->Vpi_1d) {
|
53
|
+
fprintf(stdout, "Error: allocating memory in mod_init.\n");
|
54
|
+
fflush(stdout);
|
55
|
+
exit(EXIT_FAILURE);
|
56
|
+
}
|
57
|
+
|
58
|
+
return true;
|
59
|
+
}
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
void mod_free(model_t* mod) {
|
64
|
+
if (!mod) return;
|
65
|
+
|
66
|
+
free(mod->I);
|
67
|
+
free(mod->X);
|
68
|
+
free(mod->P);
|
69
|
+
free(mod->U1d);
|
70
|
+
free(mod->V1d);
|
71
|
+
free(mod->Upi_1d);
|
72
|
+
free(mod->Vpi_1d);
|
73
|
+
mod->I = NULL;
|
74
|
+
mod->X = mod->P = mod->U1d = mod->V1d = mod->Upi_1d = mod->Vpi_1d = NULL;
|
75
|
+
|
76
|
+
free(mod);
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
void mod_free_py(model_t* mod) {
|
81
|
+
// free function for python
|
82
|
+
|
83
|
+
if (!mod) return;
|
84
|
+
if (mod->I) { free(mod->I); }
|
85
|
+
if (mod->X) { free(mod->X); }
|
86
|
+
if (mod->P) { free(mod->P); }
|
87
|
+
if (mod->U1d) { free(mod->U1d); }
|
88
|
+
if (mod->V1d) { free(mod->V1d); }
|
89
|
+
if (mod->Upi_1d) { free(mod->Upi_1d); }
|
90
|
+
if (mod->Vpi_1d) { free(mod->Vpi_1d); }
|
91
|
+
// if (mod) { free(mod); }
|
92
|
+
|
93
|
+
mod->I = NULL;
|
94
|
+
mod->X = mod->P = mod->U1d = mod->V1d = mod->Upi_1d = mod->Vpi_1d = NULL;
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
void calculate_ave(model_t* mod) {
|
99
|
+
if (mod->sim_time == 1) return;
|
100
|
+
|
101
|
+
for (size_t i = 0; i < mod->arr_size; i++) {
|
102
|
+
mod->U1d[i] /= mod->sim_time;
|
103
|
+
mod->V1d[i] /= mod->sim_time;
|
104
|
+
mod->Upi_1d[i] /= mod->sim_time;
|
105
|
+
mod->Vpi_1d[i] /= mod->sim_time;
|
106
|
+
}
|
107
|
+
}
|
108
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
/**
|
2
|
+
* This .h includes the model struct and "member functions"
|
3
|
+
* that correpond to patch class in piegy.model module
|
4
|
+
*/
|
5
|
+
#ifndef MODEL_H
|
6
|
+
#define MODEL_H
|
7
|
+
|
8
|
+
#include <stdbool.h>
|
9
|
+
#include <stdlib.h>
|
10
|
+
#include <stdint.h>
|
11
|
+
|
12
|
+
|
13
|
+
typedef struct model_t {
|
14
|
+
size_t N;
|
15
|
+
size_t M;
|
16
|
+
double maxtime;
|
17
|
+
double record_itv;
|
18
|
+
size_t sim_time;
|
19
|
+
|
20
|
+
bool boundary;
|
21
|
+
|
22
|
+
// 3D arrays flattened to 1D for C
|
23
|
+
// Sizes: N * M * 2, N * M * 4, N * M * 6
|
24
|
+
uint32_t* I;
|
25
|
+
double* X;
|
26
|
+
double* P;
|
27
|
+
|
28
|
+
int32_t print_pct;
|
29
|
+
int32_t seed; // -1 for none
|
30
|
+
|
31
|
+
// vars for data storage
|
32
|
+
bool data_empty;
|
33
|
+
size_t max_record;
|
34
|
+
size_t arr_size; // size of U, V, U_pi, V_pi, equals N * M * max_record
|
35
|
+
uint32_t compress_itv;
|
36
|
+
double* U1d;
|
37
|
+
double* V1d;
|
38
|
+
double* Upi_1d;
|
39
|
+
double* Vpi_1d;
|
40
|
+
} model_t;
|
41
|
+
|
42
|
+
|
43
|
+
bool mod_init(model_t* mod, size_t N, size_t M,
|
44
|
+
double maxtime, double record_itv, size_t sim_time, bool boundary,
|
45
|
+
const uint32_t* I, const double* X, const double* P,
|
46
|
+
int32_t print_pct, int32_t seed);
|
47
|
+
void mod_free(model_t* mod);
|
48
|
+
void mod_free_py(model_t* mod);
|
49
|
+
void calculate_ave(model_t* mod);
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
#endif // MODEL_H
|
54
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
/**
|
2
|
+
* This .c includes the patch struct and "member functions"
|
3
|
+
* that correponds to patch class in piegy.model module
|
4
|
+
*/
|
5
|
+
|
6
|
+
#include "patch.h"
|
7
|
+
|
8
|
+
void patch_init(patch_t* p, uint32_t U, uint32_t V, size_t i, size_t j) {
|
9
|
+
if (p == NULL) return;
|
10
|
+
p->i = i;
|
11
|
+
p->j = j;
|
12
|
+
|
13
|
+
p->U = U;
|
14
|
+
p->V = V;
|
15
|
+
p->U_pi = 0.0;
|
16
|
+
p->V_pi = 0.0;
|
17
|
+
|
18
|
+
for (size_t i = 0; i < 4; i++) {
|
19
|
+
p->U_weight[i] = 0.0;
|
20
|
+
p->V_weight[i] = 0.0;
|
21
|
+
p->pi_death_rates[i] = 0.0;
|
22
|
+
}
|
23
|
+
for (size_t i = 0; i < 8; i++) {
|
24
|
+
p->mig_rates[i] = 0.0;
|
25
|
+
}
|
26
|
+
p->sum_U_weight = 0.0;
|
27
|
+
p->sum_V_weight = 0.0;
|
28
|
+
p->sum_pi_death_rates = 0.0;
|
29
|
+
p->sum_mig_rates = 0.0;
|
30
|
+
}
|
31
|
+
|
32
|
+
|
33
|
+
void set_nb(patch_t* world, size_t* nb_start, size_t ij, size_t NM) {
|
34
|
+
// nb_start is the where patch ij's neighbor indices start
|
35
|
+
for (size_t k = 0; k < 4; k++) {
|
36
|
+
if (nb_start[k] != NM) {
|
37
|
+
// neighbor is valid
|
38
|
+
world[ij].nb[k] = &world[nb_start[k]];
|
39
|
+
} else {
|
40
|
+
world[ij].nb[k] = NULL;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
}
|
@@ -0,0 +1,43 @@
|
|
1
|
+
/**
|
2
|
+
* This .h includes the patch struct and "member functions"
|
3
|
+
* that correponds to patch class in piegy.model module
|
4
|
+
*/
|
5
|
+
|
6
|
+
#ifndef PATCH_H
|
7
|
+
#define PATCH_H
|
8
|
+
|
9
|
+
#include <math.h>
|
10
|
+
#include <stdio.h>
|
11
|
+
#include <stdint.h>
|
12
|
+
#include <stdbool.h>
|
13
|
+
|
14
|
+
|
15
|
+
// patch_t has size 33 * sizeof(size_t)
|
16
|
+
#define SIZEOF_PATCH_T (uint32_t) (sizeof(patch_t) / sizeof(size_t))
|
17
|
+
|
18
|
+
typedef struct patch_t {
|
19
|
+
size_t i;
|
20
|
+
size_t j;
|
21
|
+
|
22
|
+
uint32_t U;
|
23
|
+
uint32_t V;
|
24
|
+
double U_pi;
|
25
|
+
double V_pi;
|
26
|
+
|
27
|
+
struct patch_t* nb[4];
|
28
|
+
double U_weight[4]; // stores migration weight of each of the 4 neighbors
|
29
|
+
double V_weight[4];
|
30
|
+
double sum_U_weight; // sum of U_weight
|
31
|
+
double sum_V_weight;
|
32
|
+
double pi_death_rates[4];
|
33
|
+
double mig_rates[8];
|
34
|
+
double sum_pi_death_rates;
|
35
|
+
double sum_mig_rates;
|
36
|
+
} patch_t;
|
37
|
+
|
38
|
+
// in .c
|
39
|
+
void patch_init(patch_t* p, uint32_t U, uint32_t V, size_t i, size_t j);
|
40
|
+
void set_nb(patch_t* world, size_t* nb_start, size_t ij, size_t NM);
|
41
|
+
|
42
|
+
#endif // PATCH_H
|
43
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
// piegyc.h
|
2
|
+
// piegy simulation module written in c
|
3
|
+
#ifndef PIEGYC_H
|
4
|
+
#define PIEGYC_H
|
5
|
+
|
6
|
+
#include <stdbool.h>
|
7
|
+
#include <stdlib.h>
|
8
|
+
|
9
|
+
|
10
|
+
typedef struct model_t {
|
11
|
+
size_t N;
|
12
|
+
size_t M;
|
13
|
+
double maxtime;
|
14
|
+
double record_itv;
|
15
|
+
size_t sim_time;
|
16
|
+
|
17
|
+
bool boundary;
|
18
|
+
|
19
|
+
// 3D arrays flattened to 1D for C
|
20
|
+
// Sizes: N * M * 2, N * M * 4, N * M * 6
|
21
|
+
uint32_t* I;
|
22
|
+
double* X;
|
23
|
+
double* P;
|
24
|
+
|
25
|
+
int32_t print_pct;
|
26
|
+
int32_t seed; // -1 for none
|
27
|
+
|
28
|
+
// vars for data storage
|
29
|
+
bool data_empty;
|
30
|
+
size_t max_record;
|
31
|
+
size_t arr_size; // size of U, V, U_pi, V_pi, equals N * M * max_record
|
32
|
+
uint32_t compress_itv;
|
33
|
+
double* U1d;
|
34
|
+
double* V;
|
35
|
+
double* U_pi;
|
36
|
+
double* V_pi;
|
37
|
+
} model_t;
|
38
|
+
|
39
|
+
bool mod_init(model_t* mod, size_t N, size_t M,
|
40
|
+
double maxtime, double record_itv, size_t sim_time, bool boundary,
|
41
|
+
const uint32_t* I, const double* X, const double* P,
|
42
|
+
int32_t print_pct, int32_t seed);
|
43
|
+
void mod_free_py(model_t* mod);
|
44
|
+
|
45
|
+
void run(model_t* mod, char* message, size_t msg_len);
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
#endif // PIEGYC_H
|
Binary file
|
@@ -0,0 +1,61 @@
|
|
1
|
+
#include <stdbool.h>
|
2
|
+
#include <stdlib.h>
|
3
|
+
#include <stdint.h>
|
4
|
+
#include <stdio.h>
|
5
|
+
|
6
|
+
#include "model.h"
|
7
|
+
#include "patch.h"
|
8
|
+
#include "sim_funcs.h"
|
9
|
+
|
10
|
+
|
11
|
+
int main() {
|
12
|
+
size_t N = 1;
|
13
|
+
size_t M = 100;
|
14
|
+
double maxtime = 1000;
|
15
|
+
double record_itv = 0.1;
|
16
|
+
size_t sim_time = 3;
|
17
|
+
bool boundary = true;
|
18
|
+
uint32_t I_single[2] = {200, 100};
|
19
|
+
double X_single[4] = {-1, 4, 0, 2};
|
20
|
+
double P_single[6] = {0.5, 0.5, 1, 1, 0.001, 0.001};
|
21
|
+
int32_t print_pct = 1;
|
22
|
+
int32_t seed = 36; // -1 for None
|
23
|
+
|
24
|
+
uint32_t I[N * M * 2];
|
25
|
+
double X[N * M * 4];
|
26
|
+
double P[N * M * 6];
|
27
|
+
|
28
|
+
//printf("sizeof(patch_t) = %zu bytes\n", sizeof(patch_t));
|
29
|
+
size_t ij = 0;
|
30
|
+
for (size_t i = 0; i < N; i++) {
|
31
|
+
for (size_t j = 0; j < M; j++) {
|
32
|
+
I[ij * 2] = I_single[0];
|
33
|
+
I[ij * 2 + 1] = I_single[1];
|
34
|
+
|
35
|
+
for (size_t k = 0; k < 4; k++) {
|
36
|
+
X[ij * 4 + k] = X_single[k];
|
37
|
+
}
|
38
|
+
for (size_t k = 0; k < 6; k++) {
|
39
|
+
P[ij * 6 + k] = P_single[k];
|
40
|
+
}
|
41
|
+
++ij;
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
model_t* mod = malloc(sizeof(model_t));
|
46
|
+
mod_init(mod, N, M, maxtime, record_itv, sim_time, boundary, I, X, P, print_pct, seed);
|
47
|
+
|
48
|
+
char message[20] = ""; // writable buffer with enough space
|
49
|
+
run(mod, message, 20);
|
50
|
+
|
51
|
+
/*for (size_t i = 0; i < mod->max_record; i++) {
|
52
|
+
fprintf(stdout, "%f ", mod->U1d[i]);
|
53
|
+
}
|
54
|
+
fprintf(stdout, "\n");
|
55
|
+
for (size_t i = 0; i < mod->max_record; i++) {
|
56
|
+
fprintf(stdout, "%f ", mod->Upi_1d[i]);
|
57
|
+
}*/
|
58
|
+
mod_free(mod);
|
59
|
+
mod = NULL;
|
60
|
+
}
|
61
|
+
|