Skip to content

Complete Gems interface for sphere toy problem #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ python-wrapper/compilingString.txt
python-wrapper/exportVariables
*vtu
*.log
*.pyc
4 changes: 4 additions & 0 deletions python-wrapper/coupling.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,3 +54,6 @@ cdef class Py_MeshCoupling:
cdef uintptr_t int_ptr = <uintptr_t>(<MeshCoupling*><void*>self.thisptr)[0].getNeutralMesh()
py_mesh = Py_SharedSurfUnstructured(int_ptr)
return py_mesh

def getNeutralMeshSize(self):
return (<MeshCoupling*><void*>self.thisptr)[0].getNeutralMeshSize()
24 changes: 12 additions & 12 deletions python-wrapper/gems_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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()
6 changes: 5 additions & 1 deletion python-wrapper/py_example_00002.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import coupling
import numpy as np
inputs=["Forces"]
outputs=["Pressure"]
mc=coupling.Py_MeshCoupling(inputs,outputs)
Expand All @@ -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()
3 changes: 1 addition & 2 deletions python-wrapper/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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" ,
Expand Down
3 changes: 2 additions & 1 deletion python-wrapper/test_gems_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
16 changes: 16 additions & 0 deletions python-wrapper/utils.pyx
Original file line number Diff line number Diff line change
@@ -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(<SurfUnstructured*><void*>mesh.thisptr,<PiercedVector[double,long]*><void*>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(<SurfUnstructured*><void*>mesh.thisptr,<PiercedVector[double,long]*><void*>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(<SurfUnstructured*><void*>mesh.thisptr,<PiercedVector[double,long]*><void*>data.thisptr,&arr_memview[0],arr.shape[0])

12 changes: 12 additions & 0 deletions src/coupling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/coupling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class MeshCoupling{
const std::vector<std::string> & getOutputDataNames();
const SurfUnstructured * getDisciplineMesh();
SurfUnstructured * getNeutralMesh();
size_t getNeutralMeshSize();
void close();

private:
Expand Down
43 changes: 43 additions & 0 deletions src/couplingUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,49 @@ void initDoubleDataOnMesh(SurfUnstructured * mesh, PiercedVector<double>* 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<double>* data, double* array, size_t arraySize){

const PiercedVector<Vertex> & 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<double>* data, double* array, size_t arraySize){

const PiercedVector<Vertex> & 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

Expand Down
2 changes: 2 additions & 0 deletions src/couplingUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace coupling {
//SurfUnstructured scale(const SurfUnstructured & unitRadiusSphereMesh, double radius);
void interpolateFromTo(SurfUnstructured * fromMesh, PiercedVector<double> * fromData, SurfUnstructured * toMesh, PiercedVector<double> * toData);
void initDoubleDataOnMesh(SurfUnstructured * mesh, PiercedVector<double>* data);
void initDataOnMeshFromArray(SurfUnstructured * mesh, PiercedVector<double>* data, double* array, size_t arraySize);
void moveDataOnMeshToArray(SurfUnstructured * mesh, PiercedVector<double>* data, double* array, size_t arraySize);
void writeMesh(SurfUnstructured * mesh,std::string filename);
void writeData(SurfUnstructured * mesh,std::string filename,const PiercedVector<double> * data,const std::vector<std::string> & dataNames);
}
Expand Down