Skip to content

Commit a6aa0d6

Browse files
committed
update gas state functions
1 parent 7966a37 commit a6aa0d6

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

src/gas_data.F90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ subroutine f_gas_data_spec_by_name(ptr_c, value, name_data, name_size) &
5454
integer(c_int), intent(in) :: name_size
5555
integer(c_int) :: value
5656

57-
character(len=name_size), allocatable:: name
57+
character(len=name_size) :: name
5858

5959
call c_f_pointer(ptr_c, ptr_f)
6060
name = name_data(1:name_size)

src/gas_state.F90

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,16 @@ subroutine f_gas_state_to_json(ptr_c) bind(C)
8080
call gas_state_output_netcdf(ptr_f, ncid, gas_data)
8181
deallocate(gas_data) ! TODO #122
8282
end subroutine
83+
84+
subroutine f_gas_state_set_size(ptr_c, gas_data_ptr_c) bind(C)
85+
type(c_ptr), intent(inout) :: ptr_c
86+
type(c_ptr), intent(in) :: gas_data_ptr_c
87+
type(gas_state_t), pointer :: ptr_f => null()
88+
type(gas_data_t), pointer :: gas_data_ptr_f => null()
89+
90+
call c_f_pointer(ptr_c, ptr_f)
91+
call c_f_pointer(gas_data_ptr_c, gas_data_ptr_f)
92+
call gas_state_set_size(ptr_f, gas_data_n_spec(gas_data_ptr_f))
93+
94+
end subroutine
8395
end module

src/gas_state.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "nlohmann/json.hpp"
1111
#include "gimmicks.hpp"
1212
#include "pmc_resource.hpp"
13+
#include "gas_data.hpp"
1314

1415
extern "C" void f_gas_state_ctor(void *ptr) noexcept;
1516
extern "C" void f_gas_state_dtor(void *ptr) noexcept;
@@ -18,6 +19,7 @@ extern "C" void f_gas_state_get_item(const void *ptr, const int *idx, double *va
1819
extern "C" void f_gas_state_len(const void *ptr, int *len) noexcept;
1920
extern "C" void f_gas_state_to_json(const void *ptr) noexcept;
2021
extern "C" void f_gas_state_from_json(const void *ptr) noexcept;
22+
extern "C" void f_gas_state_set_size(const void *ptr, const void *gasdata_ptr) noexcept;
2123

2224
struct GasState {
2325
PMCResource ptr;
@@ -63,4 +65,27 @@ struct GasState {
6365
f_gas_state_len(&self.ptr, &len);
6466
return len;
6567
}
68+
69+
static double mix_rat(const GasState &self, const GasData &gasData,
70+
const std::string &name) {
71+
int value;
72+
const int name_size = name.size();
73+
74+
f_gas_data_spec_by_name(&gasData.ptr, &value, name.c_str(), &name_size);
75+
if (value==0) throw std::runtime_error("Element not found.");
76+
return get_item(self, value-1);
77+
}
78+
79+
static void set_size(GasState &self, const GasData &GasData) {
80+
f_gas_state_set_size(&self.ptr, &GasData.ptr);
81+
}
82+
83+
static std::valarray<double> mix_rats(const GasState &self) {
84+
int len;
85+
f_gas_state_len(&self.ptr, &len);
86+
std::valarray<double> data(len);
87+
88+
89+
return data;
90+
}
6691
};

src/pypartmc.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ PYBIND11_MODULE(_PyPartMC, m) {
166166
.def("__len__", GasState::__len__)
167167
.def("__str__", GasState::__str__,
168168
"returns a string with JSON representation of the object")
169+
.def("set_size", GasState::set_size,
170+
"sets the GasState to the size of GasData")
171+
.def("mix_rat", GasState::mix_rat,
172+
"returns the mixing ratio of a gas species")
173+
.def_property_readonly("mix_rats", GasState::mix_rats,
174+
"returns array of mixing ratios")
169175
;
170176

171177
py::class_<RunPartOpt>(m,

0 commit comments

Comments
 (0)