Adding name property to CAPI cell/material classes.

This commit is contained in:
Patrick Shriwise 2019-07-10 07:53:44 -05:00
parent a105702455
commit 1d075cefdd
5 changed files with 91 additions and 2 deletions

View file

@ -14,6 +14,8 @@ extern "C" {
int openmc_cell_get_fill(int32_t index, int* type, int32_t** indices, int32_t* n);
int openmc_cell_get_id(int32_t index, int32_t* id);
int openmc_cell_get_temperature(int32_t index, const int32_t* instance, double* T);
int openmc_cell_get_name(int32_t index, const char*& name);
int openmc_cell_set_name(int32_t index, const char* name);
int openmc_cell_set_fill(int32_t index, int type, int32_t n, const int32_t* indices);
int openmc_cell_set_id(int32_t index, int32_t id);
int openmc_cell_set_temperature(int32_t index, double T, const int32_t* instance);
@ -58,6 +60,8 @@ extern "C" {
int openmc_material_set_density(int32_t index, double density, const char* units);
int openmc_material_set_densities(int32_t index, int n, const char** name, const double* density);
int openmc_material_set_id(int32_t index, int32_t id);
int openmc_material_get_name(int32_t index, const char*& name);
int openmc_material_set_name(int32_t index, const char* name);
int openmc_material_set_volume(int32_t index, double volume);
int openmc_material_filter_get_bins(int32_t index, const int32_t** bins, size_t* n);
int openmc_material_filter_set_bins(int32_t index, size_t n, const int32_t* bins);

View file

@ -1,5 +1,5 @@
from collections.abc import Mapping, Iterable
from ctypes import c_int, c_int32, c_double, c_char_p, POINTER
from ctypes import byref, c_int, c_int32, c_double, c_char_p, POINTER
from weakref import WeakValueDictionary
import numpy as np
@ -28,6 +28,12 @@ _dll.openmc_cell_get_temperature.argtypes = [
c_int32, POINTER(c_int32), POINTER(c_double)]
_dll.openmc_cell_get_temperature.restype = c_int
_dll.openmc_cell_get_temperature.errcheck = _error_handler
_dll.openmc_cell_get_name.argtypes = [c_int32, POINTER(c_char_p)]
_dll.openmc_cell_get_name.restype = c_int
_dll.openmc_cell_get_name.errcheck = _error_handler
_dll.openmc_cell_set_name.argtypes = [c_int32, c_char_p]
_dll.openmc_cell_set_name.restype = c_int
_dll.openmc_cell_set_name.errcheck = _error_handler
_dll.openmc_cell_set_fill.argtypes = [
c_int32, c_int, c_int32, POINTER(c_int32)]
_dll.openmc_cell_set_fill.restype = c_int
@ -102,6 +108,17 @@ class Cell(_FortranObjectWithID):
def id(self, cell_id):
_dll.openmc_cell_set_id(self._index, cell_id)
@property
def name(self):
name = c_char_p()
_dll.openmc_cell_get_name(self._index, byref(name))
return name.value.decode()
@name.setter
def name(self, name):
name_ptr = c_char_p(name.encode())
_dll.openmc_cell_set_name(self._index, name_ptr)
@property
def fill(self):
fill_type = c_int()

View file

@ -1,5 +1,5 @@
from collections.abc import Mapping
from ctypes import c_int, c_int32, c_double, c_char_p, POINTER, c_size_t
from ctypes import byref, c_int, c_int32, c_double, c_char_p, POINTER, c_size_t
from weakref import WeakValueDictionary
import numpy as np
@ -48,6 +48,12 @@ _dll.openmc_material_set_densities.errcheck = _error_handler
_dll.openmc_material_set_id.argtypes = [c_int32, c_int32]
_dll.openmc_material_set_id.restype = c_int
_dll.openmc_material_set_id.errcheck = _error_handler
_dll.openmc_cell_get_name.argtypes = [c_int32, POINTER(c_char_p)]
_dll.openmc_cell_get_name.restype = c_int
_dll.openmc_cell_get_name.errcheck = _error_handler
_dll.openmc_cell_set_name.argtypes = [c_int32, c_char_p]
_dll.openmc_cell_set_name.restype = c_int
_dll.openmc_cell_set_name.errcheck = _error_handler
_dll.openmc_material_set_volume.argtypes = [c_int32, c_double]
_dll.openmc_material_set_volume.restype = c_int
_dll.openmc_material_set_volume.errcheck = _error_handler
@ -124,6 +130,17 @@ class Material(_FortranObjectWithID):
def id(self, mat_id):
_dll.openmc_material_set_id(self._index, mat_id)
@property
def name(self):
name = c_char_p()
_dll.openmc_material_get_name(self._index, byref(name))
return name.value.decode()
@name.setter
def name(self, name):
name_ptr = c_char_p(name.encode())
_dll.openmc_material_set_name(self._index, name_ptr)
@property
def volume(self):
volume = c_double()

View file

@ -1018,6 +1018,32 @@ openmc_cell_get_temperature(int32_t index, const int32_t* instance, double* T)
return 0;
}
extern "C" int
openmc_cell_get_name(int32_t index, const char*& name) {
if (index < 0 || index >= model::cells.size()) {
strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
return OPENMC_E_OUT_OF_BOUNDS;
}
name = model::cells[index]->name_.c_str();
return 0;
}
extern "C" int
openmc_cell_set_name(int32_t index, const char* name) {
if (index < 0 || index >= model::cells.size()) {
strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
return OPENMC_E_OUT_OF_BOUNDS;
}
std::string name_str(name);
model::cells[index]->name_ = name_str;
return 0;
}
//! Return the index in the cells array of a cell with a given ID
extern "C" int
openmc_get_cell_index(int32_t id, int32_t* index)

View file

@ -1381,6 +1381,31 @@ openmc_material_set_id(int32_t index, int32_t id)
return 0;
}
extern "C" int
openmc_material_get_name(int32_t index, const char*& name) {
if (index < 0 || index >= model::materials.size()) {
strcpy(openmc_err_msg, "Index in materials array is out of bounds.");
return OPENMC_E_OUT_OF_BOUNDS;
}
name = model::materials[index]->name_.c_str();
return 0;
}
extern "C" int
openmc_material_set_name(int32_t index, const char* name) {
if (index < 0 || index >= model::materials.size()) {
strcpy(openmc_err_msg, "Index in materials array is out of bounds.");
return OPENMC_E_OUT_OF_BOUNDS;
}
std::string name_str(name);
model::materials[index]->name_ = name_str;
return 0;
}
extern "C" int
openmc_material_set_volume(int32_t index, double volume)
{