Skip to content

Commit b2cc9f8

Browse files
zdaq12slayoo
andauthored
adding paper listings (P & J in README, F in a folder); removing offset arg from rand_init (#247)
Co-authored-by: Sylwester Arabas <[email protected]>
1 parent 67e26ab commit b2cc9f8

File tree

17 files changed

+334
-131
lines changed

17 files changed

+334
-131
lines changed

.github/workflows/readme_julia.yml

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
name: readme_listings
2+
3+
defaults:
4+
run:
5+
shell: bash
6+
7+
on:
8+
push:
9+
branches: [ main ]
10+
pull_request:
11+
branches: [ main ]
12+
schedule:
13+
- cron: '0 13 * * 4'
14+
15+
jobs:
16+
julia:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v2
20+
with:
21+
submodules: recursive
22+
23+
- uses: actions/setup-python@v1
24+
with:
25+
python-version: 3.9
26+
27+
- uses: jwlawson/[email protected]
28+
with:
29+
cmake-version: '3.26.x'
30+
31+
- run: pip install -e .
32+
- run: pip install pytest-codeblocks pytest
33+
- run: python -c "import pytest_codeblocks; code=pytest_codeblocks.extract_from_file('README.md'); f=open('readme.jl', 'w'); f.writelines(block.code for block in code if block.syntax=='Julia'); f.close()"
34+
- run: cat -n readme.jl
35+
36+
- uses: julia-actions/setup-julia@v1
37+
- run: mkdir readme_output
38+
- run: julia readme.jl > readme_output/julia.txt
39+
- uses: actions/upload-artifact@v2
40+
with:
41+
name: readme_output
42+
path: readme_output
43+
44+
python:
45+
runs-on: ubuntu-latest
46+
steps:
47+
- uses: actions/checkout@v2
48+
with:
49+
submodules: recursive
50+
51+
- uses: actions/setup-python@v1
52+
with:
53+
python-version: 3.9
54+
55+
- uses: jwlawson/[email protected]
56+
with:
57+
cmake-version: '3.26.x'
58+
59+
- run: pip install -e .
60+
- run: pip install pytest-codeblocks pytest
61+
- run: python -c "import pytest_codeblocks; code=pytest_codeblocks.extract_from_file('README.md'); f=open('readme.py', 'w'); f.writelines(block.code for block in code if block.syntax=='Python'); f.close()"
62+
- run: cat -n readme.py
63+
64+
- run: mkdir readme_output
65+
- run: python -We readme.py > readme_output/python.txt
66+
- uses: actions/upload-artifact@v2
67+
with:
68+
name: readme_output
69+
path: readme_output
70+
71+
fortran:
72+
runs-on: ubuntu-latest
73+
steps:
74+
- uses: actions/checkout@v2
75+
with:
76+
submodules: recursive
77+
78+
- run: sudo apt-get install libnetcdff-dev
79+
- run: |
80+
cd readme_fortran
81+
mkdir build
82+
cd build
83+
PARTMC_HOME=../../gitmodules/partmc cmake ..
84+
make
85+
86+
- run: |
87+
mkdir readme_output
88+
cd readme_fortran
89+
./build/main > ../readme_output/fortran.txt
90+
91+
- uses: actions/upload-artifact@v2
92+
with:
93+
name: readme_output
94+
path: readme_output
95+
96+
assert:
97+
runs-on: ubuntu-latest
98+
needs: [julia, python, matlab-draft, fortran]
99+
steps:
100+
- uses: actions/setup-python@v2
101+
- run: pip install numpy
102+
- uses: actions/download-artifact@v2
103+
with:
104+
name: readme_output
105+
path: readme_output
106+
- run : python -c 'import numpy as np; import os; dir="readme_output/"; data=[float(np.loadtxt(dir+file)) for file in os.listdir(dir)]; print("data:", data); similar_as_first = np.array([abs(data[0]-k)/data[0] for k in data[1:]]); print("similar_as_first", similar_as_first); assert((similar_as_first < .1).all())'
107+
108+
matlab-draft:
109+
runs-on: ubuntu-latest
110+
111+
steps:
112+
- uses: actions/checkout@v2
113+
with:
114+
submodules: recursive
115+
116+
- uses: actions/setup-python@v1
117+
with:
118+
python-version: 3.8
119+
120+
- uses: jwlawson/[email protected]
121+
with:
122+
cmake-version: '3.26.x'
123+
124+
- run: |
125+
echo "CC=gcc-9" >> $GITHUB_ENV
126+
echo "CXX=g++-9" >> $GITHUB_ENV
127+
VERBOSE=1 pip install --verbose -e .
128+
129+
- run: pip install pytest-codeblocks pytest
130+
- run: python -c "import pytest_codeblocks; code=pytest_codeblocks.extract_from_file('README.md'); f=open('readme.m', 'w'); f.writelines(block.code for block in code if block.syntax=='Matlab'); f.close()"
131+
- run: cat -n readme.m
132+
133+
- uses: matlab-actions/setup-matlab@v0
134+
with:
135+
release: R2022a
136+
137+
- uses: matlab-actions/run-command@v0
138+
with:
139+
command: readme

.github/workflows/readme_matlab.yml

Lines changed: 0 additions & 44 deletions
This file was deleted.

.github/workflows/readme_python.yml

Lines changed: 0 additions & 32 deletions
This file was deleted.

README.md

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,98 @@ import PyPartMC
5858

5959
## Usage examples
6060

61-
#### example object instantiation in Python
61+
The listings below depict how the identical task of randomly sampling particles from an aerosol size distribution in PartMC can be
62+
done in three different programming languages.
63+
64+
For a Fortran equivalent of the Python and Julia programs below, see the [`readme_fortran` folder](https://github.com/open-atmos/PyPartMC/tree/main/readme_fortran).
65+
66+
#### Python
67+
68+
```Python
69+
import numpy as np
6270

63-
```python
6471
import PyPartMC as ppmc
65-
print(ppmc.__version__)
66-
gas_data = ppmc.GasData(("H2SO4", "HNO3", "HCl", "NH3", "NO", "NO2"))
72+
from PyPartMC import si
73+
74+
aero_data = ppmc.AeroData((
75+
# [density, ions in solution, molecular weight, kappa]
76+
{"OC": [1000 *si.kg/si.m**3, 0, 1e-3 *si.kg/si.mol, 0.001]},
77+
{"BC": [1800 *si.kg/si.m**3, 0, 1e-3 *si.kg/si.mol, 0]},
78+
))
79+
80+
aero_dist = ppmc.AeroDist(
81+
aero_data,
82+
[{
83+
"cooking": {
84+
"mass_frac": [{"OC": [1]}],
85+
"diam_type": "geometric",
86+
"mode_type": "log_normal",
87+
"num_conc": 3200 / si.cm**3,
88+
"geom_mean_diam": 8.64 * si.nm,
89+
"log10_geom_std_dev": 0.28,
90+
}
91+
},
92+
{
93+
"diesel": {
94+
"mass_frac": [{"OC": [0.3]}, {"BC": [0.7]}],
95+
"diam_type": "geometric",
96+
"mode_type": "log_normal",
97+
"num_conc": 2900 / si.cm**3,
98+
"geom_mean_diam": 50 * si.nm,
99+
"log10_geom_std_dev": 0.24,
100+
}
101+
}],
102+
)
103+
104+
n_part = 100
105+
aero_state = ppmc.AeroState(n_part, aero_data)
106+
aero_state.dist_sample(aero_dist)
107+
print(np.dot(aero_state.masses, aero_state.num_concs), "# kg/m3")
67108
```
68109

69-
#### example object instantiation in Julia
110+
#### Julia
70111
```Julia
71112
using Pkg
72113
Pkg.add("PyCall")
73114

74115
using PyCall
75116
ppmc = pyimport("PyPartMC")
76-
print(ppmc.__version__)
77-
gas_data = ppmc.GasData(("H2SO4", "HNO3", "HCl", "NH3", "NO", "NO2"))
117+
118+
si = ppmc["si"]
119+
120+
aero_data = ppmc.AeroData((
121+
# (density, ions in solution, molecular weight, kappa)
122+
Dict("OC"=>(1000 * si.kg/si.m^3, 0, 1e-3 * si.kg/si.mol, 0.001)),
123+
Dict("BC"=>(1800 * si.kg/si.m^3, 0, 1e-3 * si.kg/si.mol, 0))
124+
))
125+
126+
aero_dist = ppmc.AeroDist(aero_data, (
127+
Dict(
128+
"cooking" => Dict(
129+
"mass_frac" => (Dict("OC" => (1,)),),
130+
"diam_type" => "geometric",
131+
"mode_type" => "log_normal",
132+
"num_conc" => 3200 / si.cm^3,
133+
"geom_mean_diam" => 8.64 * si.nm,
134+
"log10_geom_std_dev" => .28,
135+
)
136+
),
137+
Dict(
138+
"diesel" => Dict(
139+
"mass_frac" => (Dict("OC" => (.3,)), Dict("BC" => (.7,))),
140+
"diam_type" => "geometric",
141+
"mode_type" => "log_normal",
142+
"num_conc" => 2900 / si.cm^3,
143+
"geom_mean_diam" => 50 * si.nm,
144+
"log10_geom_std_dev" => .24,
145+
)
146+
)
147+
))
148+
149+
n_part = 100
150+
aero_state = ppmc.AeroState(n_part, aero_data)
151+
aero_state.dist_sample(aero_dist)
152+
print(aero_state.masses'aero_state.num_concs,"# kg/m3")
78153
```
79154

80155
#### usage in other projects

readme_fortran/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
cmake_minimum_required(VERSION 3.17)
2+
project(main LANGUAGES Fortran)
3+
add_executable(main
4+
main.f90
5+
$ENV{PARTMC_HOME}/src/spec_file.F90
6+
$ENV{PARTMC_HOME}/src/spec_line.F90
7+
$ENV{PARTMC_HOME}/src/util.F90
8+
$ENV{PARTMC_HOME}/src/constants.F90
9+
$ENV{PARTMC_HOME}/src/aero_data.F90
10+
$ENV{PARTMC_HOME}/src/aero_mode.F90
11+
$ENV{PARTMC_HOME}/src/aero_dist.F90
12+
$ENV{PARTMC_HOME}/src/aero_state.F90
13+
$ENV{PARTMC_HOME}/src/mpi.F90
14+
$ENV{PARTMC_HOME}/src/fractal.F90
15+
$ENV{PARTMC_HOME}/src/rand.F90
16+
$ENV{PARTMC_HOME}/src/bin_grid.F90
17+
$ENV{PARTMC_HOME}/src/aero_weight.F90
18+
$ENV{PARTMC_HOME}/src/aero_weight_array.F90
19+
$ENV{PARTMC_HOME}/src/aero_info.F90
20+
$ENV{PARTMC_HOME}/src/aero_info_array.F90
21+
$ENV{PARTMC_HOME}/src/aero_binned.F90
22+
$ENV{PARTMC_HOME}/src/aero_particle.F90
23+
$ENV{PARTMC_HOME}/src/aero_particle_array.F90
24+
$ENV{PARTMC_HOME}/src/aero_sorted.F90
25+
$ENV{PARTMC_HOME}/src/netcdf.F90
26+
$ENV{PARTMC_HOME}/src/env_state.F90
27+
$ENV{PARTMC_HOME}/src/integer_varray.F90
28+
$ENV{PARTMC_HOME}/src/integer_rmap.F90
29+
$ENV{PARTMC_HOME}/src/integer_rmap2.F90
30+
)
31+
find_path (NETCDF_INCLUDES netcdf.mod
32+
HINTS NETCDF_DIR ENV NETCDF_DIR REQUIRED)
33+
34+
find_library (NETCDF_LIBRARIES NAMES netcdff
35+
HINTS NETCDF_DIR ENV NETCDF_DIR REQUIRED)
36+
37+
target_link_libraries(main netcdff)
38+
target_include_directories(main PRIVATE ${NETCDF_INCLUDES})
39+
target_link_directories(main PRIVATE ${NETCDF_LIBRARIES})

0 commit comments

Comments
 (0)