Skip to content

Commit 8064ee7

Browse files
committed
work in progress on AeroState init
1 parent a41e620 commit 8064ee7

File tree

7 files changed

+74
-16
lines changed

7 files changed

+74
-16
lines changed

examples/condense.ipynb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
"[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/open-atmos/PyPartMC.git/main?urlpath=lab/tree/examples/condense.ipynb)"
1010
]
1111
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 12,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"# This file is a part of PyPartMC licensed under the GNU General Public License v3\n",
19+
"# Copyright (C) 2022 University of Illinois Urbana-Champaign\n",
20+
"# Based on (GPL v2 code): https://github.com/compdyn/partmc/tree/master/test/condense\n",
21+
"# Authors:\n",
22+
"# - https://github.com/compdyn/partmc/graphs/contributors\n",
23+
"# - https://github.com/open-atmos/PyPartMC/graphs/contributors"
24+
]
25+
},
1226
{
1327
"cell_type": "code",
1428
"execution_count": 1,
@@ -96,7 +110,7 @@
96110
"outputs": [],
97111
"source": [
98112
"N_PART = 44\n",
99-
"aero_state = ppmc.AeroState(N_PART)"
113+
"aero_state = ppmc.AeroState(N_PART, aero_data)"
100114
]
101115
},
102116
{

src/aero_state.F90

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,33 @@ subroutine f_aero_state_dtor(ptr_c) bind(C)
2727
deallocate(ptr_f)
2828
end subroutine
2929

30-
subroutine f_aero_state_set_n_part_ideal(ptr_c, n_part) bind(C)
30+
subroutine f_aero_state_init(ptr_c, n_part, aero_data_ptr_c) bind(C)
3131
type(aero_state_t), pointer :: ptr_f => null()
32-
type(c_ptr), intent(in) :: ptr_c
32+
type(aero_data_t), pointer :: aero_data_ptr_f => null()
33+
type(c_ptr), intent(in) :: ptr_c, aero_data_ptr_c
3334
real(c_double), intent(in) :: n_part
3435

3536
call c_f_pointer(ptr_c, ptr_f)
36-
print*, n_part
37-
! TODO
38-
!call aero_state_set_n_part_ideal(ptr_f, n_part)
37+
call c_f_pointer(aero_data_ptr_c, aero_data_ptr_f)
38+
39+
call aero_state_zero(ptr_f)
40+
call aero_state_set_weight(ptr_f, aero_data_ptr_f, AERO_STATE_WEIGHT_FLAT)
41+
call aero_state_set_n_part_ideal(ptr_f, n_part)
42+
!call aero_state_add_aero_dist_sample(aero_state, aero_data_ptr_f, &
43+
! aero_dist_init, 1d0, 0d0, &
44+
! .false. & ! TODO run_part_opt%allow_doubling, &
45+
! .false. & ! TODO run_part_opt%allow_halving)
46+
!)
47+
48+
end subroutine
49+
50+
subroutine f_aero_state_len(ptr_c, len) bind(C)
51+
type(aero_state_t), pointer :: ptr_f => null()
52+
type(c_ptr), intent(in) :: ptr_c
53+
integer(c_int), intent(out) :: len
54+
55+
call c_f_pointer(ptr_c, ptr_f)
56+
len = aero_state_n_part(ptr_f)
3957
end subroutine
4058

4159
end module

src/aero_state.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,30 @@
77
#pragma once
88

99
#include "pmc_resource.hpp"
10+
#include "aero_data.hpp"
1011

1112
extern "C" void f_aero_state_ctor(void *ptr) noexcept;
1213
extern "C" void f_aero_state_dtor(void *ptr) noexcept;
13-
extern "C" void f_aero_state_set_n_part_ideal(const void *ptr, const double *n_part) noexcept;
14-
14+
extern "C" void f_aero_state_init(
15+
const void *ptr,
16+
const double *n_part,
17+
const void *aero_dataptr
18+
) noexcept;
19+
extern "C" void f_aero_state_len(const void *ptr, const int *len) noexcept;
1520

1621
struct AeroState {
1722
PMCResource ptr;
1823

19-
AeroState(const double &n_part) :
24+
AeroState(const double &n_part, const AeroData &aero_data) :
2025
ptr(f_aero_state_ctor, f_aero_state_dtor)
2126
{
22-
f_aero_state_set_n_part_ideal(ptr.f_arg(), &n_part);
27+
f_aero_state_init(ptr.f_arg(), &n_part, aero_data.ptr.f_arg());
28+
}
29+
30+
static std::size_t __len__(const AeroState &self) {
31+
int len;
32+
f_aero_state_len(&self.ptr, &len);
33+
return len;
2334
}
2435
};
2536

src/pypartmc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ PYBIND11_MODULE(PyPartMC, m) {
7979
is typically cleared each time we output data to disk.
8080
)pbdoc"
8181
)
82-
.def(py::init<double>())
82+
.def(py::init<const double, const AeroData&>())
83+
.def("__len__", AeroState::__len__)
8384
;
8485

8586
py::class_<GasData>(m, "GasData",

tests/test_aero_state.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,36 @@
44
# Author: Sylwester Arabas #
55
####################################################################################################
66

7+
import pytest
78
import PyPartMC as ppmc
89

10+
from .test_aero_data import AERO_DATA_CTOR_ARG_MINIMAL
911
AERO_STATE_CTOR_ARG_MINIMAL = 44
1012

1113

1214
class TestAeroState:
1315
@staticmethod
1416
def test_ctor():
1517
# arrange
18+
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
1619

1720
# act
18-
sut = ppmc.AeroState(AERO_STATE_CTOR_ARG_MINIMAL)
21+
sut = ppmc.AeroState(AERO_STATE_CTOR_ARG_MINIMAL, aero_data)
1922

2023
# assert
2124
assert sut is not None
2225

2326
@staticmethod
24-
def test_todo():
25-
pass # TODO
27+
@pytest.mark.xfail(strict=True) # TODO
28+
@pytest.mark.parametrize("n_part", (1, 44, 666))
29+
def test_len(n_part):
30+
# arrange
31+
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
32+
sut = ppmc.AeroState(n_part, aero_data)
33+
34+
# act
35+
size = len(sut)
36+
37+
# assert
38+
assert size == n_part
39+

tests/test_condense.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_equilib_particles():
1818
# arrange
1919
env_state = ppmc.EnvState(ENV_STATE_CTOR_ARG_MINIMAL)
2020
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
21-
aero_state = ppmc.AeroState(AERO_STATE_CTOR_ARG_MINIMAL)
21+
aero_state = ppmc.AeroState(AERO_STATE_CTOR_ARG_MINIMAL, aero_data)
2222

2323
# act
2424
# ppmc.condense_equilib_particles(env_state, aero_data, aero_state)

tests/test_run_part.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_args():
1919
# arrange
2020
env_state = ppmc.EnvState(ENV_STATE_CTOR_ARG_MINIMAL)
2121
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
22-
aero_state = ppmc.AeroState(AERO_STATE_CTOR_ARG_MINIMAL)
22+
aero_state = ppmc.AeroState(AERO_STATE_CTOR_ARG_MINIMAL, aero_data)
2323
gas_data = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL)
2424
gas_state = ppmc.GasState()
2525
scenario = ppmc.Scenario(gas_data, aero_data, SCENARIO_CTOR_ARG_MINIMAL)

0 commit comments

Comments
 (0)