Fixed CIC errors. Added support for CosmoPower in _cosmotool.pyx

This commit is contained in:
Guilhem Lavaux 2014-06-03 09:51:26 +02:00
parent 0a28eb3e04
commit 302ed9a912
8 changed files with 230 additions and 110 deletions

View file

@ -6,6 +6,47 @@ from cpython cimport PyObject, Py_INCREF
np.import_array()
cdef extern from "cosmopower.hpp" namespace "CosmoTool":
cdef enum CosmoFunction "CosmoTool::CosmoPower::CosmoFunction":
POWER_EFSTATHIOU "CosmoTool::CosmoPower::POWER_EFSTATHIOU",
HU_WIGGLES "CosmoTool::CosmoPower::HU_WIGGLES",
HU_BARYON "CosmoTool::CosmoPower::HU_BARYON",
OLD_POWERSPECTRUM,
POWER_BARDEEN "CosmoTool::CosmoPower::POWER_BARDEEN",
POWER_SUGIYAMA "CosmoTool::CosmoPower::POWER_SUGIYAMA",
POWER_BDM,
POWER_TEST
cdef cppclass CosmoPower:
double n
double K0
double V_LG_CMB
double CMB_VECTOR[3]
double h
double SIGMA8
double OMEGA_B
double OMEGA_C
double omega_B
double omega_C
double Theta_27
double OMEGA_0
double Omega
double beta
double OmegaEff
double Gamma0
double normPower
CosmoPower()
void setFunction(CosmoFunction)
void updateCosmology()
void updatePhysicalCosmology()
void normalize()
void setNormalization(double)
double power(double)
cdef extern from "loadSimu.hpp" namespace "CosmoTool":
@ -302,4 +343,60 @@ def loadRamses(str basepath, int snapshot_id, int cpu_id, bool doublePrecision =
return _PySimulationAdaptor(wrap_simudata(data, flags))
def loadAllRamses(str basepath, int
cdef class CosmologyPower:
cdef CosmoPower power
def __init__(self,**cosmo):
self.power = CosmoPower()
self.power.OMEGA_B = cosmo['omega_B_0']
self.power.OMEGA_C = cosmo['omega_M_0']-cosmo['omega_B_0']
self.power.h = cosmo['h']
if 'ns' in cosmo:
self.power.n = cosmo['ns']
assert self.power.OMEGA_C > 0
self.power.updateCosmology()
def normalize(self,s8):
self.power.SIGMA8 = s8
self.power.normalize()
def setFunction(self,funcname):
cdef CosmoFunction f
f = POWER_EFSTATHIOU
if funcname=='EFSTATHIOU':
f = POWER_EFSTATHIOU
elif funcname=='HU_WIGGLES':
f = HU_WIGGLES
elif funcname=='HU_BARYON':
f = HU_BARYON
elif funcname=='BARDEEN':
f = POWER_BARDEEN
elif funcname=='SUGIYAMA':
f = POWER_SUGIYAMA
self.power.setFunction(f)
cdef double _compute(self, double k):
k *= self.power.h
return self.power.power(k)
def compute(self, k):
cdef np.ndarray out
cdef double kval
cdef tuple i
if isinstance(k, np.ndarray):
out = np.empty(k.shape, dtype=np.float64)
for i,kval in np.ndenumerate(k):
out[i] = self._compute(kval)
return out
else:
return self._compute(k)

View file

@ -1,5 +1,5 @@
from _cosmotool import *
from grafic import writeGrafic, writeWhitePhase
from grafic import writeGrafic, writeWhitePhase, readGrafic
from borg import read_borg_vol
@ -13,9 +13,9 @@ class SimulationBare(PySimulationBase):
raise TypeError("Simulation object to mirror must be a PySimulationBase")
s = args[0]
self.positions = s.getPositions()
self.velocities = s.getVelocities()
self.identifiers = s.getIdentifiers()
self.positions = [q.copy() for q in s.getPositions()] if s.getPositions() is not None else None
self.velocities = [q.copy() for q in s.getVelocities()] if s.getVelocities() is not None else None
self.identifiers = s.getIdentifiers().copy() if s.getIdentifiers() is not None else None
self.boxsize = s.getBoxsize()
self.time = s.getTime()
self.Hubble = s.getHubble()
@ -28,7 +28,7 @@ class SimulationBare(PySimulationBase):
def _safe_merge(a, b):
if b:
if a:
a = [np.append(q, r) for q,r in zip(a,other.positions)]
a = [np.append(q, r) for q,r in zip(a,b)]
else:
a = b
return a
@ -42,15 +42,15 @@ class SimulationBare(PySimulationBase):
return a
assert self.time == other.time
assert self.Hubble == other.Hubble
assert self.boxsize == other.boxsize
assert self.Omega_M == other.Omega_M
assert self.Omega_Lambda == other.Omega_Lambda
assert self.time == other.getTime()
assert self.Hubble == other.getHubble()
assert self.boxsize == other.getBoxsize()
assert self.Omega_M == other.getOmega_M()
assert self.Omega_Lambda == other.getOmega_Lambda()
self.positions = _safe_merge(self.positions, other.positions)
self.velocities = _safe_merge(self.velocities, other.velocities)
self.identifiers = _safe_merge0(self.idenfiers, other.identifiers)
self.positions = _safe_merge(self.positions, other.getPositions())
self.velocities = _safe_merge(self.velocities, other.getVelocities())
self.identifiers = _safe_merge0(self.identifiers, other.getIdentifiers())
def getPositions(self):
return self.positions
@ -106,7 +106,7 @@ def loadRamsesAll(basepath, snapshot_id, **kwargs):
cpu_id = 0
output = None
while True:
s = loadRamses("%s/output_%05d" % (basepath,snapshot_id), cpu_id, **kwargs)
s = loadRamses("%s/output_%05d" % (basepath,snapshot_id), snapshot_id, cpu_id, **kwargs)
if s == None:
break
if output == None:
@ -115,3 +115,5 @@ def loadRamsesAll(basepath, snapshot_id, **kwargs):
output.merge(s)
cpu_id += 1
return output

View file

@ -1,6 +1,32 @@
import struct
import numpy as np
def readGrafic(filename):
with file(filename, mode="rb") as f:
p = struct.unpack("IIIIffffffffI", f.read(4*11 + 2*4))
checkPoint0, Nx, Ny, Nz, delta, _, _, _, scalefac, omega0, omegalambda0, h, checkPoint1 = p
if checkPoint0 != checkPoint1 or checkPoint0 != 4*11:
raise ValueError("Invalid unformatted access")
a = np.empty((Nx,Ny,Nz), dtype=np.float32)
BoxSize = delta * Nx * h
checkPoint = 4*Ny*Nz
for i in xrange(Nx):
checkPoint = struct.unpack("I", f.read(4))[0]
if checkPoint != 4*Ny*Nz:
raise ValueError("Invalid unformatted access")
a[i, :, :] = np.fromfile(f, dtype=np.float32, count=Ny*Nz).reshape((Ny, Nz))
checkPoint = struct.unpack("I", f.read(4))[0]
if checkPoint != 4*Ny*Nz:
raise ValueError("Invalid unformatted access")
return a, BoxSize, scalefac, omega0, omegalambda0, h
def writeGrafic(filename, field, BoxSize, scalefac, **cosmo):
with file(filename, mode="wb") as f: