Skip to content

Instantly share code, notes, and snippets.

@gidili
Created April 9, 2013 10:18
Show Gist options
  • Save gidili/5344656 to your computer and use it in GitHub Desktop.
Save gidili/5344656 to your computer and use it in GitHub Desktop.
allocateBuffers
private void allocateBuffers(){
// host allocated pointers (used to write values -- see lines 81/82 in setModels below)
_positionPtr = Pointer.allocateFloats(_particleCount * 4);
_velocityPtr = Pointer.allocateFloats(_particleCount * 4);
// alternative buffer defining
_acceleration = _context.createFloatBuffer(Usage.InputOutput, _particleCount * 4 * 2);
_gridCellIndex = _context.createIntBuffer(Usage.InputOutput, _gridCellCount + 1);
_gridCellIndexFixedUp = _context.createIntBuffer(Usage.InputOutput, _gridCellCount + 1);
_neighborMap = _context.createFloatBuffer(Usage.InputOutput, _particleCount * SPHConstants.NEIGHBOR_COUNT * 2);
_particleIndex = _context.createIntBuffer(Usage.InputOutput, _particleCount * 2);
_particleIndexBack = _context.createIntBuffer(Usage.InputOutput, _particleCount);
_position = _context.createFloatBuffer(Usage.InputOutput, _particleCount * 4);
_pressure = _context.createFloatBuffer(Usage.InputOutput, _particleCount);
_rho = _context.createFloatBuffer(Usage.InputOutput, _particleCount * 2);
_sortedPosition = _context.createFloatBuffer(Usage.InputOutput, _particleCount * 4 * 2);
_sortedVelocity = _context.createFloatBuffer(Usage.InputOutput, _particleCount * 4);
_velocity = _context.createFloatBuffer(Usage.InputOutput, _particleCount * 4);
// init elastic connections buffer if we have any
if(_numOfElasticP > 0){
//_elasticConnectionsDataPtr = Pointer.allocateFloats(_numOfElasticP * SPHConstants.NEIGHBOR_COUNT * 4);
_elasticConnectionsData = _context.createFloatBuffer(Usage.InputOutput, _numOfElasticP * SPHConstants.NEIGHBOR_COUNT * 4);
}
_queue.finish();
}
public void setModels(List<IModel> models) {
// TODO: generalize this for an arbitrary number of models instead of just one
if(!(models == null || models.size() ==0))
{
SPHModelX mod = (SPHModelX) models.get(0);
// set dimensions
_xMax = mod.getXMax();
_xMin = mod.getXMin();
_yMax = mod.getYMax();
_yMin = mod.getYMin();
_zMax = mod.getZMax();
_zMin = mod.getZMin();
_particleCount = mod.getNumberOfParticals();
_numOfElasticP = 0;
_numOfLiquidP = 0;
_numOfBoundaryP = 0;
_gridCellsX = (int)( ( mod.getXMax() - mod.getXMin() ) / PhysicsConstants.H ) + 1;
_gridCellsY = (int)( ( mod.getYMax() - mod.getYMin() ) / PhysicsConstants.H ) + 1;
_gridCellsZ = (int)( ( mod.getZMax() - mod.getZMin() ) / PhysicsConstants.H ) + 1;
// set grid dimensions
_gridCellCount = _gridCellsX * _gridCellsY * _gridCellsZ;
// allocate buffers - requires global dimensions of the grid
this.allocateBuffers();
int index = 0;
for(int i = 0;i< _particleCount;i++){
if(i != 0)
{
index = index + 4;
}
Vector3DX positionVector = (Vector3DX) mod.getParticles().get(i).getPositionVector();
Vector3DX velocityVector = (Vector3DX) mod.getParticles().get(i).getVelocityVector();
// buffer population
_positionPtr.set(index, positionVector.getX());
_positionPtr.set(index + 1, positionVector.getY());
_positionPtr.set(index + 2, positionVector.getZ());
_positionPtr.set(index + 3, positionVector.getP());
_velocityPtr.set(index, velocityVector.getX());
_velocityPtr.set(index + 1, velocityVector.getY());
_velocityPtr.set(index + 2, velocityVector.getZ());
_velocityPtr.set(index + 3, velocityVector.getP());
// PORTING-TODO: populate elastic connection buffers
_position.write(_queue, _positionPtr, false);
_velocity.write(_queue, _velocityPtr, false);
// particle counts
if (positionVector.getP() == SPHConstants.BOUNDARY_TYPE) {
_numOfBoundaryP++;
}
else if (positionVector.getP() == SPHConstants.ELASTIC_TYPE) {
_numOfElasticP++;
}
else if (positionVector.getP() == SPHConstants.LIQUID_TYPE) {
_numOfLiquidP++;
}
}
// check that counts are fine
if(_particleCount != (_numOfBoundaryP + _numOfElasticP + _numOfLiquidP)){
throw new IllegalArgumentException("SPHSolverService:setModels - particle counts do not add up");
}
}
else
{
throw new IllegalArgumentException("SPHSolverService:setModels - invalid models");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment