diff --git a/.gitignore b/.gitignore index c0b6f1a..9f12fef 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ python-wrapper/compilingString.txt python-wrapper/exportVariables *vtu *.log +*.pyc \ No newline at end of file diff --git a/python-wrapper/coupling.pyx b/python-wrapper/coupling.pyx index 579dd30..e747ec1 100644 --- a/python-wrapper/coupling.pyx +++ b/python-wrapper/coupling.pyx @@ -15,6 +15,7 @@ cdef extern from "coupling.hpp" namespace "coupling": void compute(PiercedVector[double,long]*) void close() const SurfUnstructured * getNeutralMesh() + size_t getNeutralMeshSize() cdef class Py_MeshCoupling: cdef MeshCoupling* thisptr @@ -53,3 +54,6 @@ cdef class Py_MeshCoupling: cdef uintptr_t int_ptr = (self.thisptr)[0].getNeutralMesh() py_mesh = Py_SharedSurfUnstructured(int_ptr) return py_mesh + + def getNeutralMeshSize(self): + return (self.thisptr)[0].getNeutralMeshSize() \ No newline at end of file diff --git a/python-wrapper/gems_wrapper.py b/python-wrapper/gems_wrapper.py index 0d0c417..3bfb688 100644 --- a/python-wrapper/gems_wrapper.py +++ b/python-wrapper/gems_wrapper.py @@ -11,8 +11,7 @@ from gems.core.discipline import MDODiscipline import coupling as coupling_interface -from numpy import array - +from numpy import zeros class ToySphereDiscipline(MDODiscipline): @@ -34,19 +33,20 @@ def __init__(self, name, inputs, outputs, mesh_file, self.mesh_coupling.initialize(mesh_file, neutral_mesh_file, sphere_radius) - mesh = self.mesh_coupling.getNeutralMesh() self.neutral_input = coupling_interface.Py_PiercedVector() - self.neutral_output = coupling_interface.Py_PiercedVector() - - coupling_interface.Py_initDoubleDataOnMesh(mesh, self.neutral_input) - + def _run(self): input_vector = self.local_data[self.inputs[0]] - # TOdo : update self.neutral_input from input_data - self.neutral_input[:] = input_vector - self.sphere_coupling.compute(self.neutral_input, self.neutral_output) + + mesh = self.mesh_coupling.getNeutralMesh() + coupling_interface.Py_initDataOnMeshFromArray(mesh, self.neutral_input,input_vector) + self.mesh_coupling.compute(self.neutral_input) + + output_vector = zeros(input_vector.shape[0]) + + coupling_interface.Py_moveDataOnMeshToArray(mesh, self.neutral_input,output_vector) - self.local_data[self.outputs[0]] = array(self.neutral_output) + self.local_data[self.outputs[0]] = output_vector def close(self): - self.sphere_coupling.close() + self.mesh_coupling.close() diff --git a/python-wrapper/py_example_00002.py b/python-wrapper/py_example_00002.py index 8bfbca7..ca8ef40 100644 --- a/python-wrapper/py_example_00002.py +++ b/python-wrapper/py_example_00002.py @@ -1,4 +1,5 @@ import coupling +import numpy as np inputs=["Forces"] outputs=["Pressure"] mc=coupling.Py_MeshCoupling(inputs,outputs) @@ -8,6 +9,9 @@ nD=coupling.Py_PiercedVector() #mesh=coupling.Py_SurfUnstructured() mesh=mc.getNeutralMesh() -coupling.Py_initDoubleDataOnMesh(mesh,nD) +#coupling.Py_initDoubleDataOnMesh(mesh,nD) +arr=np.ones(82) +print "suca" +coupling.Py_initDataOnMeshFromArray(mesh,nD,arr) mc.compute(nD) mc.close() diff --git a/python-wrapper/setup.py b/python-wrapper/setup.py index 3548fb0..4def71a 100644 --- a/python-wrapper/setup.py +++ b/python-wrapper/setup.py @@ -141,8 +141,7 @@ def def_ext_modules(self): include_paths.append(self.mpi_include_path) os.environ["CXX"] = "mpic++" os.environ["CC"] = "mpicc" - mpi_lib = "/usr/local/openmpi-1.10.5_intel/lib/libmpi.so" - mpi_lib = "/usr/lib64/openmpi-1.10/lib/libmpi.so" + mpi_lib = re.sub("/include/","/lib/",self.mpi_include_path) + "libmpi.so" _extra_compile_args = ["-std=c++11", "-g" , diff --git a/python-wrapper/test_gems_wrapper.py b/python-wrapper/test_gems_wrapper.py index 3ef1136..d92a511 100644 --- a/python-wrapper/test_gems_wrapper.py +++ b/python-wrapper/test_gems_wrapper.py @@ -21,7 +21,8 @@ def test_basic(self): toy1 = ToySphereDiscipline("Sphere1", ["Forces"], ["Pressure"], mesh_file, neutral_mesh_file, sphere_radius=1.0) - toy1.execute({"Forces": ones(1000)}) + neutral_mesh_size = toy1.mesh_coupling.getNeutralMeshSize() + toy1.execute({"Forces": ones(neutral_mesh_size)}) toy1.close() diff --git a/python-wrapper/utils.pyx b/python-wrapper/utils.pyx index 4c0ecbf..8391d59 100644 --- a/python-wrapper/utils.pyx +++ b/python-wrapper/utils.pyx @@ -1,9 +1,25 @@ include "piercedVector.pyx" include "surfUnstructured.pyx" from libc.stdint cimport uintptr_t +import numpy as np cdef extern from "couplingUtils.hpp" namespace "coupling": cdef void initDoubleDataOnMesh(const SurfUnstructured * mesh, PiercedVector[double,long] * data) + cdef void initDataOnMeshFromArray(const SurfUnstructured * mesh, PiercedVector[double,long] * data, double * arr, size_t arrSize) + cdef void moveDataOnMeshToArray(const SurfUnstructured * mesh, PiercedVector[double,long] * data, double * arr, size_t arrSize) def Py_initDoubleDataOnMesh(Py_SharedSurfUnstructured mesh, Py_PiercedVector data): initDoubleDataOnMesh(mesh.thisptr,data.thisptr) + +def Py_initDataOnMeshFromArray(Py_SharedSurfUnstructured mesh, Py_PiercedVector data, arr): + if not arr.flags['C_CONTIGUOUS']: + arr = np.ascontiguousarray(arr) + cdef double[::1] arr_memview = arr + initDataOnMeshFromArray(mesh.thisptr,data.thisptr,&arr_memview[0],arr.shape[0]) + +def Py_moveDataOnMeshToArray(Py_SharedSurfUnstructured mesh, Py_PiercedVector data, arr): + if not arr.flags['C_CONTIGUOUS']: + arr = np.ascontiguousarray(arr) + cdef double[::1] arr_memview = arr + moveDataOnMeshToArray(mesh.thisptr,data.thisptr,&arr_memview[0],arr.shape[0]) + \ No newline at end of file diff --git a/src/coupling.cpp b/src/coupling.cpp index 41fc505..8cb29d6 100644 --- a/src/coupling.cpp +++ b/src/coupling.cpp @@ -167,6 +167,18 @@ SurfUnstructured* MeshCoupling::getNeutralMesh(){ }; +/*! + Get scaled neutral mesh + + \return the scaled neutral mesh +*/ +size_t MeshCoupling::getNeutralMeshSize(){ + + return m_scaledNeutralMesh.get()->getVertices().size(); + +}; + + /*! Possibly perform closing actions diff --git a/src/coupling.hpp b/src/coupling.hpp index f8e908f..c203786 100644 --- a/src/coupling.hpp +++ b/src/coupling.hpp @@ -37,6 +37,7 @@ class MeshCoupling{ const std::vector & getOutputDataNames(); const SurfUnstructured * getDisciplineMesh(); SurfUnstructured * getNeutralMesh(); + size_t getNeutralMeshSize(); void close(); private: diff --git a/src/couplingUtils.cpp b/src/couplingUtils.cpp index 1a3ae9f..64b92e8 100644 --- a/src/couplingUtils.cpp +++ b/src/couplingUtils.cpp @@ -113,6 +113,49 @@ void initDoubleDataOnMesh(SurfUnstructured * mesh, PiercedVector* data){ } }; +/*! + Initialize a mesh-coherent PiercedVector Container with a C-array. + It has to be noted that the array should contains data with the same order of the vertices of the associated mesh. + + \param[in] mesh the mesh + \param[out] data PiercedVector container associated to the mesh that has to be filled + \param[in] array a pointer to a C-array containing data to be inserted into the PiercedVector + \param[in] arraySize the size of the C-array +*/ +void initDataOnMeshFromArray(SurfUnstructured * mesh, PiercedVector* data, double* array, size_t arraySize){ + + const PiercedVector & vertices = mesh->getVertices(); + assert(vertices.size()==arraySize); + size_t count = 0; + std::cout << "Start inserting ..."; + for(const Vertex & v: vertices) { + data->insert(v.getId(),array[count]); + ++count; + } +}; + +/*! + Move data from a mesh-coherent PiercedVector Container to a C-array. + It has to be noted that the array will contain data with the same order of the vertices of the associated mesh. + + \param[in] mesh the mesh + \param[in] data PiercedVector container associated to the mesh containing data to be inserted into the C-array + \param[out] array a pointer to a C-array to be filled with data coming from the PiercedVector + \param[in] arraySize the size of the C-array +*/ +void moveDataOnMeshToArray(SurfUnstructured * mesh, PiercedVector* data, double* array, size_t arraySize){ + + const PiercedVector & vertices = mesh->getVertices(); + assert(vertices.size()==arraySize); + size_t count = 0; + for(const Vertex & v: vertices) { + array[count] = data->at(v.getId()); + ++count; + } +}; + + + /*! Write VTK file( (p)vtu ) containing the mesh diff --git a/src/couplingUtils.hpp b/src/couplingUtils.hpp index 469bd1a..5ba19e7 100644 --- a/src/couplingUtils.hpp +++ b/src/couplingUtils.hpp @@ -20,6 +20,8 @@ namespace coupling { //SurfUnstructured scale(const SurfUnstructured & unitRadiusSphereMesh, double radius); void interpolateFromTo(SurfUnstructured * fromMesh, PiercedVector * fromData, SurfUnstructured * toMesh, PiercedVector * toData); void initDoubleDataOnMesh(SurfUnstructured * mesh, PiercedVector* data); +void initDataOnMeshFromArray(SurfUnstructured * mesh, PiercedVector* data, double* array, size_t arraySize); +void moveDataOnMeshToArray(SurfUnstructured * mesh, PiercedVector* data, double* array, size_t arraySize); void writeMesh(SurfUnstructured * mesh,std::string filename); void writeData(SurfUnstructured * mesh,std::string filename,const PiercedVector * data,const std::vector & dataNames); }