Exemple #1
0
 def _getDynamics(self):
   if path.exists(self.dir + "clumps.h5part"):
     self.clmps = SimClumps(self, loadeng=True)
     self.clmps.getDynamics2D()
     return True
   else:
     return False
Exemple #2
0
 def _plotSummary(self):
   if path.exists(self.dir + "clumps.h5part"):
     try:
       self.clmps = SimClumps(self, loadeng=True)
       self.clmps.plotSummary(self.dir + "summary.pdf")
     
     except:
       self.results.problem = True
Exemple #3
0
  def _getResults(self,tmin=0.90,tmax=0.99):
    if path.exists(self.dir + "clumps.h5part"):
      try:
        self.clmps = SimClumps(self, loadeng=True)
        self.clmps.getResults(tmin, tmax)
        if self.nodumps > 0:
          (lfile, ltime, mtime) = self.dumps[-1]
          getDisk(self.dir + lfile, self)

      except:
        self.results.problem = True
Exemple #4
0
  def _getSecondaryImpact(self, rhomin):
    sdir = self.dir
    
    (dfile,dtime,dmtime) = self.dumps[-1]
    cmd = "find_fof " + sdir + dfile + " " + sdir + "last_fof.h5part 0. " + str(rhomin) + " 2.0 2 "
    (exstat, out) = subprocess.getstatusoutput(cmd)
    print(out)
    last_fof = SimClumps(self, "last_fof.h5part", loadsat=True, loadeng=True)
    (exstat, out) = subprocess.getstatusoutput("rm " + sdir + "last_fof.h5part")

    if last_fof.nofc[0] >= 2:
      r0vect = last_fof.pos[0,2,:] - last_fof.pos[0,1,:]
      v0vect = last_fof.vel[0,2,:] - last_fof.vel[0,1,:]
    
      mtar   = last_fof.m[0,1]
      mimp   = last_fof.m[0,2]
    
      Rtar   = last_fof.rclmp[0,1]
      Rimp   = last_fof.rclmp[0,2]

      G = self.cfg.G

      secgi = GiantImpact(mtar, mimp, Rtar, Rimp, G)
      secgi.getInitStateVecs(r0vect, v0vect)
      
      self.secgi = secgi
      self.last_fof = last_fof
      return True

    else:
      return False
Exemple #5
0
  def _getRotation(self,mminrel=0.01,rhozero=1.5,hmult=2.0):
    if self.nodumps > 0:
      sdir = self.dir
      mmin = self.gi.mtot*mminrel
      
      cfile = sdir + "clumps_rot.h5part"
      dfile = sdir + self.dumps[-1][0]

      cmd = "get_rotation " + dfile + " " \
        + cfile        + " " \
        + str(mmin)    + " " \
        + str(rhozero) + " " \
        + str(hmult)   + " "

      (exstat, out) = subprocess.getstatusoutput(cmd)
      print(out)
      
      clmp = SimClumps(self, fname="clumps_rot.h5part", loadcrot=True)
      
      self.results.Lparent = clmp.Lparent[0,:,:]
      self.results.Iparent = clmp.Iparent[0,:]
      self.results.mparentmat = clmp.mparentmat[0,:,:]
        
      os.remove(cfile)
    else:
      self.results.Lparent = np.zeros_like( self.results.Lm )*nan
      self.results.Iparent = np.zeros_like( self.results.Im )*nan
      self.results.mparentmat = np.zeros_like( self.results.mmatm )*nan
Exemple #6
0
 def _getClumpEnergy(self,tmin=0.90,tmax=0.99):
   res = self.results
   try:
     clmps = SimClumps(self, loadclmp=True)
     res.Epotclmp = clmps.Epotclmp[-1,:]
     res.Erotclmp = clmps.Erotclmp[-1,:]
     res.Iclmp    = clmps.Iclmp[-1,:]
       
     print(- res.Erotclmp[1] / res.Epotclmp[1], \
         res.mm[0] / clmps.m[-1,0], res.mm[1] / clmps.m[-1,1])
     print("Epotclmp and Erotclmp loaded!")
     
   except:
     noc = 0
     if len( res.mm ) > 0:
       noc = res.mm.shape[0]
         
     res.Epotclmp = nan*np.zeros(noc)
     res.Epotclmp = nan*np.zeros(noc)
     res.Iclmp    = nan*np.zeros(noc)
     print("Epotclmp and Erotclmp failed!")
Exemple #7
0
 def _plotDynamics(self):
   if path.exists(self.dir + "clumps.h5part"):
     self.clmps = SimClumps(self)
     self.clmps.plotDynamics(self.dir + "dynamics.pdf", self.cfg.cpconf)
     print(self.dir + "dynamics.pdf")
Exemple #8
0
 def _plotMoons(self):
   self.clmps = SimClumps(self, loadsat=True)
   self.clmps.plotLRmoons(self.dir + "moons.pdf", self.cfg.mpconf)
Exemple #9
0
 def _plotMasses(self):
   if path.exists(self.dir + "clumps.h5part"):
     self.clmps = SimClumps(self)
     self.clmps.plotMass(self.dir + "clumps.pdf", self.cfg.cpconf)
Exemple #10
0
class Simulation(object):
  # noCPUs, 
  def __init__(self,params,cfg):
    self.params = params
    self.cfg    = cfg
    self.params.temp = cfg.T
    self.idfilt = cfg.idfilt
    self.keepfiles = cfg.keepfiles.split()

    ssbdir = resolvePath(cfg.dir) + "/"
    self.dir = resolvePath(ssbdir + params.key) + "/"
    
    if path.exists(self.dir):
      self.Log = Logger(self.dir + "setup.log")
    else:
      self.Log = Logger("/dev/null")

    self.jobid = 0
    self.nodumps  = 0
    self.totdumps = 0

    self.state = "unknown"
    self._setOrbit()
    self.getState()
    self.results = SimResults()
    self.clumpjobid = 0
    self.courant = nan

  def setError(self, str=None):
    if not str == None and hasattr(self, "Log"):
      self.Log.write("error: " + str)
    self.state = "error"
    self.next = self._doNothing
    return self.state

  def getState(self):
    if self.state == "error":
      return self.state

    if not path.exists(self.dir):
      self.state = "unprepared"
      self.next = self._prepare
      return self.state
    else:
      neededfiles = self.cfg.auxfiles.split()
      neededfiles.append(self.cfg.binary)
      neededfiles.append("initial.h5part")
      
      print("find dumps for ",self.params.key)
      self._findDumps()

      for file in neededfiles:
        if not os.path.exists(self.dir + os.path.split(file)[1]):
          self.state = "unprepared"
          self.next = self._prepare
          return self.state

      # so everything's ready
      if self.jobid == 0:
        if self.nodumps == 0:
          self.state = "prepared" 
          self.next = self._submit
        else:
          (dfile, dtime, mtime) = self.dumps[-1]
          if (dtime >= 0.99*self.tstop):
            self.state = "finished"
          else:
            self.state = "partial"
      else:
      # there is a job id, so state was set by SimAdmin
        if self.state == "run":
          self.next = self._postproc
        if self._isSlow(5.):
          self.state = "slow"
        if self._isLogStuck(1800.):
          self.state = "stuck"
      return self.state
    
    
    self.state = "unknown"
    self.next = self._doNothing
    return self.state

  def reset(self):
    self.state = "unprepared"
    self.next = self._prepare
    return self.state

  def _doNothing(self):
    return self.state
  
  def _setOrbit(self):
    # find fitting bodies
    (self.tarb, self.impb) = self._findBodies()
    if (self.tarb, self.impb) == (None, None):
      print("no suitable bodies found!")
      self.setError("no suitable bodies found!")
      return

    mtar = self.tarb.m
    mimp = self.impb.m
    Rtar = self.tarb.r
    Rimp = self.impb.r

    G = self.cfg.G
    relsep = self.cfg.relsep
    
    # get giant impact
    gi = GiantImpact(mtar, mimp, Rtar, Rimp, G)
    vimp = self.params.vimprel * gi.vesc
    impa = self.params.impa * deg2rad

    (self.r0tar, self.r0imp, self.v0tar, self.v0imp, t0, self.gilogstr) = \
        gi.getInitVimpAlpha(vimp, impa, relsep)

    tcol = 2*(Rtar + Rimp)/vimp
    self.gi = gi
    self.tcol = tcol
    self.vimp = vimp
    self.G    = G
    self.mtot = mtar + mimp
    
    if hasattr(self.tarb, "rmat"):
      Rtarmat = self.tarb.rmat
      Rimpmat = self.impb.rmat

      ratmat = ( Rtar + Rimp ) / ( Rtarmat + Rimpmat )
      self.impamat = rad2deg*np.arcsin( ratmat*np.sin(impa) )
    
    self.impa    = rad2deg*impa
    
    self.tstop = tcol * self.cfg.tcolmax
    self.tdump = tcol * self.cfg.tcoldump
    self.tanim = tcol * self.cfg.tcolanim
    
    # allow a 1000fold maximum density compared to mean value
    self.hmin = 0.1*min(self.tarb.h, self.impb.h)
    self.pmin = 0.
    if self.mtot < (1.e-3 * Me):
      self.pmin = -1.e99

    self.t0 = t0
    
    self.totdumps = int( ( self.tstop - self.t0 ) / self.tanim )

    self.nop  = self.tarb.nop + self.impb.nop

  def _findDumps(self):
    dumps = {}
    for file in os.listdir(self.dir):
      if file[0:4] == "dump" and file[-6:] == "h5part" and len(file) == 31:
        dumps[file] = float(file[13:24])
  
    self.dumps = []
    for key in list(dumps.keys()):
      self.dumps.append( (key, dumps[key], os.path.getmtime(self.dir + key)) )
    self.nodumps = len(self.dumps)
    self.dumps.sort(key=lambda dmp: dmp[0])

    if len(self.dumps) > 0:
      (lfile, self.tlast, lmtime) = self.dumps[-1]
    else:
      self.tlast = nan
    

  def _isSlow(self,k):
    nod = self.nodumps
    if nod > 1:
      mtimes = np.zeros(nod)
      for i in range(nod):
         (dfile, dtime, mtimes[i]) = self.dumps[i]
      
      self.dumpsdmtime = mtimes[1:] - mtimes[0:-1]
      return ( time.time() - mtimes[-1] ) > k*np.median( self.dumpsdmtime )
    return False

  def _isLogStuck(self,dt):
    return ( time.time() - os.path.getmtime(self.dir + "logDomain000") ) > dt

  def _delAnimDumps(self):
    dumps = self.dumps
    tanim = self.tanim
    tdump = self.tdump
    delta = 5.e-3
    
    self._findDumps()
    
    for (dfile, dtime, dmtime) in self.dumps:
      ranim = dtime / tanim
      rdump = dtime / tdump

      deltaanim = abs( ranim - round(ranim) )
      deltadump = abs( rdump - round(rdump) )

      if deltaanim < delta and deltadump > delta:
        print("delete  ", dfile, dtime)
        os.remove(self.dir + dfile)

  def _delEscapeeOrphans(self):
    for file in os.listdir(self.dir):
      if "_esc.h5part" in file and \
      not os.path.exists(self.dir + file.replace("_esc","")):
        print("delete ",self.dir+file)
        (exstat, out) = subprocess.getstatusoutput("rm "+self.dir+file)

  def _delDumpsSmaller(self,nob):
    for (dfile, dtime, dmtime) in self.dumps:
      if os.path.getsize(self.dir + dfile) < nob:
        print("delete  ", self.dir+dfile)
        (exstat, out) = subprocess.getstatusoutput("rm "+self.dir+dfile)

    self._delEscapeeOrphans()

  def _delLastDumps(self,nod=5):
    for i in range(-1,-nod-1,-1):
      (dfile, dtime, dmtime) = self.dumps[i]
      dfile = self.dir + dfile
      efile = dfile.replace(".h5part","") + "_esc.h5part"
      
      print("delete  ", dfile)
      (exstat, out) = subprocess.getstatusoutput("rm " + dfile)
      if os.path.exists(efile):
        print("delete  ", efile)
        (exstat, out) = subprocess.getstatusoutput("rm " + efile)


  def _delAll(self):
    sdir = self.dir[:-1]
    if os.path.islink(sdir):
      lfile = sdir
      sdir = os.path.realpath(sdir)
      (exstat, out) = subprocess.getstatusoutput("rm "+lfile)
    (exstat, out) = subprocess.getstatusoutput("rm -rf "+sdir)

    self.state = "unprepared"
    self.next  = self._prepare
    self.nodumps = 0


  def _keepNthDumps(self,nth):
    dumps = self.dumps
    
    self._findDumps()
    
    for n in range( len(dumps) ):
       (dfile, dtime, dmtime) = dumps[n]
       if not ( n % nth == 0 ):
         print("delete  ", dfile, dtime)
         os.remove(self.dir + dfile)
       else:
         print("keep    ", dfile, dtime)

  def _prepare(self):
    # if it does not yet exist, make dir
    if not path.exists(self.dir):
      os.mkdir(self.dir)
      self.Log = Logger(self.dir + "setup.log")

    # copy files
    auxf = self.cfg.auxfiles.split()
    
    cmds = []
    for file in auxf:
      #shutil.copy2(resolvePath(file), self.dir)
      cmd = "cp -Rpv " + resolvePath(file) + " " + self.dir
      cmds.append("cp -Rpv " + resolvePath(file) + " " + self.dir )
    
    #self.Log.write("auxiliary files copied")
    
    if path.exists(self.dir + "initial.h5part"):
      os.remove(self.dir + "initial.h5part")

    # get orbit
    gilog = open(self.dir + "gi_setup.log", "w")
    print(self.gilogstr, file=gilog)
    gilog.close()
    self.Log.write("giant impact calculated")

    # displace bodies
    r0tar = self.r0tar
    r0imp = self.r0imp
    v0tar = self.v0tar
    v0imp = self.v0imp

    cmds.append("cp -Rpv " + self.tarb.file + " " + self.dir + "tarb.h5part")
    cmds.append("h5part_displace -i " + self.dir + "tarb.h5part " +\
        "--pos [%e,%e,%e] " % (r0tar[0], r0tar[1], r0tar[2]) +\
        "--vel [%e,%e,%e] " % (v0tar[0], v0tar[1], v0tar[2]) )
    cmds.append("cp -Rpv " + self.impb.file + " " + self.dir + "impb.h5part")
    cmds.append("h5part_displace -i " + self.dir + "impb.h5part " +\
        "--pos [%e,%e,%e] " % (r0imp[0], r0imp[1], r0imp[2]) +\
        "--vel [%e,%e,%e] " % (v0imp[0], v0imp[1], v0imp[2]) +\
        "--id %d" % self.cfg.idfilt )
    cmds.append("h5part_combineA_ " + self.dir + "tarb.h5part " +\
        self.dir + "impb.h5part " + self.dir + "initial.h5part")
    cmds.append("rm " + self.dir + "tarb.h5part " + self.dir + "impb.h5part")
    
    # write attributes (gravconst, attrs)
    attrs = []
    attrs.append( ("time",      self.t0   ) )
    attrs.append( ("gravconst", self.G    ) )
    attrs.append( ("tcol",     self.tcol ) )
    attrs.append( ("idfilt",   self.cfg.idfilt ) )
    attrs.append( ("rmaxbound", self.vimp*self.tstop) )
    attrs.append( ("rmaxunbound", self.vimp*self.tstop) )
    attrs.append( ("hmin", self.hmin) )
    attrs.append( ("pmin", self.pmin) )

    for (key,val) in attrs:
      cmds.append("h5part_writeattr -i " + self.dir + "initial.h5part " +\
          " -k " + key + " -v " + str(val))
    
    for (key,val) in self.cfg.attr:
      cmds.append("h5part_writeattr -i " + self.dir + "initial.h5part " +\
          " -k " + key + " -v " + str(val))

    cmds.append("h5part_writeattr -i " + self.dir + "initial.h5part " +\
        " -k " + self.params.key + " -v 0. ")

    # prepare and copy binary
    binfile = resolvePath( self.cfg.srcdir ) + "/" + self.cfg.binary
    if not path.exists(binfile):
      oldwd = os.getcwd()
      os.chdir(self.cfg.srcdir)
      (exstat, out) = subprocess.getstatusoutput("make " + self.cfg.maketarg)
      os.chdir(oldwd)
      self.Log.write("binary compiled")
    cmds.append("cp -Rpv " + binfile + " " + self.dir)

    # now execute the commands
    for cmd in cmds:
      (exstat, out) = subprocess.getstatusoutput(cmd)
      self.Log.write(out)
      if not exstat == 0:
        self.setError(cmd + " failed")
        return self.state
    
    self.next = self._submit
    self.Log.write("prepared state")
    self.state = "prepared"
    return self.state
  
  def _setInitialCourant(self,courant):
    self.courant = courant
    (dfile,dtime,dmtime) = self.dumps[-1]
    cmd = "h5part_writeattr -i " + self.dir + "initial.h5part" +\
          " -k courant -v " + str(courant)
    (exstat, out) = subprocess.getstatusoutput(cmd)

  def _grabBinary(self):
    # prepare and copy binary
    binfile = resolvePath( self.cfg.srcdir ) + "/" + self.cfg.binary
    if not path.exists(binfile):
      return
    cmd = "cp -Rpv " + binfile + " " + self.dir
    (exstat, out) = subprocess.getstatusoutput(cmd)

  def _submit(self):
    if not ( self.state == "prepared" or self.state == "partial"):
      self.setError("tried to submit from a non-prepared state")
      return self.state
    
    subcmd = self.cfg.subcmd
    nocpus = self.cfg.nocpus
    binary = self.cfg.binary
    runarg = self.cfg.runargs
    sstnam = self.cfg.name

    self.jobname = sstnam + "_" + self.params.key
  
    self.Log.write("tcol = %9.3e" % self.tcol)
    self.Log.write("tstop = %9.3e" % self.tstop)

    oldwd = os.getcwd()
    os.chdir(self.dir)
    
    if hasattr(sim_machine, "qsub_ompscr"):
      script = sim_machine.qsub_ompscr
      script = script.replace('$NOCPUS' , str(nocpus))
      script = script.replace('$SIMNAME', self.jobname)
      script = script.replace('$BINARY' , binary)
    
      script = script.replace('$SAVETIME' , str(self.tanim))
      script = script.replace('$STOPTIME' , str(self.tstop))
      script = script.replace('$RUNARGS' ,  runarg)

      scrfile = open("jobscript.sh", 'w')
      scrfile.write(script)
      scrfile.close()

      subcmd = subcmd.replace('$SCRIPT' , "jobscript.sh")
    else:
      subcmd = subcmd.replace('$NOCPUS' , str(nocpus))
      subcmd = subcmd.replace('$SIMNAME', self.jobname)
      subcmd = subcmd.replace('$BINARY' , binary)
    
      subcmd = subcmd.replace('$SAVETIME' , str(self.tanim))
      subcmd = subcmd.replace('$STOPTIME' , str(self.tstop))
      subcmd = subcmd.replace('$RUNARGS' ,  runarg)
      
    print("cd " + self.dir + "; qsub jobscript.sh")
    (exstat, out) = subprocess.getstatusoutput(subcmd)
    os.chdir(oldwd)
      
    self.Log.write(subcmd)

    if not exstat == 0:
      self.setError("submit command failed (" + out + ")")
    else:
      self.next = self._doNothing
      self.state = "submitted"
      self.Log.write("job \"" + self.jobname + "\" submitted")
    return self.state
  
  def _extend(self, tcolmax):
    self.tstop = self.tcol * tcolmax
    self._resubmit()

  def _getCourant(self):
    dfile = "initial.h5part"

    if self.nodumps > 0:
      (dfile,dtime,dmtime) = self.dumps[-1]
    
    cmd = "h5part_readattr -i " + self.dir + dfile + " -k courant "
    if path.exists(self.dir + dfile):
      (exstat, out) = subprocess.getstatusoutput(cmd)
      self.courant = float( out.split()[1] )
    return self.courant

  def _setCourant(self,courant):
    self.courant = courant
    dfile = "initial.h5part"

    if self.nodumps > 0:
      (dfile,dtime,dmtime) = self.dumps[-1]
    
    cmd = "h5part_writeattr -i " + self.dir + dfile +\
          " -k courant -v " + str(courant)
    if path.exists(self.dir + dfile):
      (exstat, out) = subprocess.getstatusoutput(cmd)
      print("set courant attribute in file " + dfile + " to ",str(courant))
  
  def _setAttribute(self,key,val):
    (dfile,dtime,dmtime) = self.dumps[-1]
    cmd = "h5part_writeattr -i " + self.dir + dfile +\
          " -k " + str(key) + " -v " + str(val)
    (exstat, out) = subprocess.getstatusoutput(cmd)
  
  def _resubmit(self):
    sdir = self.dir
    (dfile,dtime,dmtime) = self.dumps[-1]
    efile = dfile.replace(".h5part","_esc.h5part")
    
    cmd = "mv " + sdir + dfile + " " + sdir + "/initial.h5part"
    (exstat, out) = subprocess.getstatusoutput(cmd)
    
    cmd = "mv " + sdir + efile + " " + sdir + "/initial_esc.h5part"
    (exstat, out) = subprocess.getstatusoutput(cmd)
  
    self.state = "prepared"
    self._submit()
    self._findDumps()
  
  def _abort(self):
    if not self.jobid == 0:
      delcmd = self.cfg.delcmd
      delcmd = delcmd.replace('$JOBID', str(self.jobid))
      (dstat, dout) = subprocess.getstatusoutput(delcmd)
      print(dout)

  def _del(self):
    if not self.state == "run":
      for root, dirs, files in os.walk(self.dir, topdown=False):
        for fname in files:
          os.remove(os.path.join(root, fname))
        for dname in dirs:
          os.rmdir(os.path.join(root, dname))
      #os.rmdir(self.dir)
      self.state = "unprepared"
      self.next = self._prepare


  def _continue(self):
    pass

  def set_jobid(self, jobid):
    self.jobid = jobid

  def set_state(self, str):
    if self.state == "error":
      return
      
    if str == "queued":
      self.next = self._doNothing
      self.state = str

    if str == "run":
      self.next = self._postproc
      self.state = str

    if str == "finished":
      self.next = self._finalize
      self.state = str

  def _postproc(self):
    if self.state == "run" or self.state == "partial":
      self._findDumps()
      self._findClumps()

    elif self.state == "finished":
      self.Log.write("SGE reported finished state, was job " + str(self.id))
      self.next = self._doNothing
    
    else:
      self.setError("tried to postproc from a non-submitted state")
    
    return self.state

  def _finalize(self):
    if not self.state == "finished":
      self.setError("tried to finalize from non-finished state")
    else:
      self.next = self._doNothing
      self.state = "finalized"
      self.Log.write("finalized simulation")

    return self.state

  def _getArchiveFiles(self):
    archfiles = []
    self._findDumps()
    if self.nodumps > 0:
      (lfile, ltime, mtime) = self.dumps[-1]
      archfiles.append( self.dir + lfile )

      keepdumps = self.cfg.keepdumps
      for trel in keepdumps:
        archfiles.extend( self._getDump( trel ) )

      escfiles = []
      for afile in archfiles:
        aesc = afile.replace(".h5part","_esc.h5part")
        if path.exists(aesc):
          escfiles.append( aesc )
      archfiles.extend( escfiles )
    
    for f in self.keepfiles:
      if path.exists(self.dir + f):
        archfiles.append( self.dir + f )
    
    return archfiles

  def _copyAllDumps(self, targ):
    sdir = self.dir
    tdir = targ + "/" + self.params.key + "/"
    
    if not path.exists(tdir):
      os.mkdir(tdir)
      print(tdir)

    for (dfile, dtime, mtime) in self.dumps:
      cmd = "cp -Rpv " + sdir + dfile + " " + tdir + dfile
      (exstat, out) = subprocess.getstatusoutput(cmd)
      print(out)

  def _getDump(self, trel):
    eps = 1.e-3
    mtchdumps = []
    for (dfile, dtime, dmtime) in self.dumps:
      if ( abs(dtime / self.tcol - trel) < eps ):
        mtchdumps.append(self.dir + dfile)
    return mtchdumps

  def _findBodies(self):
    toler = self.cfg.bodtol
    htoler= self.cfg.smotol
    boddb = shelve.open(resolvePath(self.cfg.bodiesdb))

    # find target candidates
    mmatdummy = np.zeros([32])
    targtmpl = BodyFile("", Me*self.params.mtar, mmatdummy, nan, \
        self.params.temp, nan, nan, {})
    targcand = []
    for key in list(boddb.keys()):
      if boddb[key].isSimilar(targtmpl, toler):
        targcand.append(boddb[key])

    # find impactor candidates
    impatmpl = BodyFile("", Me*self.params.mimp, mmatdummy, nan, \
        self.params.temp, nan, nan, {})
    impacand = []
    for key in list(boddb.keys()):
      if boddb[key].isSimilar(impatmpl, toler):
        impacand.append(boddb[key])

    boddb.close()
    
    # find fitting pairs by looking at mass ratio and smoothing length
    pairs = []
    gammaparm = (self.params.mimp / self.params.mtar )
    for tarbd in targcand:
      for impbd in impacand:
        hdev = abs((impbd.h - tarbd.h)/tarbd.h)
        gdev = abs((gammaparm - (impbd.m/tarbd.m))/gammaparm)
        if gdev < toler and hdev < htoler:
          pairs.append( (tarbd, impbd) )
    
    if len(pairs) == 0:
       return (None, None) 
    else:
       (btarb, bimpb) = pairs[0]
       desnop = self.cfg.desnop
       for (tarb, impb) in pairs:
         cfit = abs( float( desnop - ( tarb.nop +   impb.nop) )/desnop )
         bfit = abs( float( desnop - (btarb.nop +  bimpb.nop) )/desnop )
         if ( cfit < bfit ):
           btarb = tarb
           bimpb = impb

       return (btarb, bimpb)

  def _plotMasses(self):
    if path.exists(self.dir + "clumps.h5part"):
      self.clmps = SimClumps(self)
      self.clmps.plotMass(self.dir + "clumps.pdf", self.cfg.cpconf)

  def _plotMoons(self):
    self.clmps = SimClumps(self, loadsat=True)
    self.clmps.plotLRmoons(self.dir + "moons.pdf", self.cfg.mpconf)

  def _plotDynamics(self):
    if path.exists(self.dir + "clumps.h5part"):
      self.clmps = SimClumps(self)
      self.clmps.plotDynamics(self.dir + "dynamics.pdf", self.cfg.cpconf)
      print(self.dir + "dynamics.pdf")

  def _getDynamics(self):
    if path.exists(self.dir + "clumps.h5part"):
      self.clmps = SimClumps(self, loadeng=True)
      self.clmps.getDynamics2D()
      return True
    else:
      return False

  def _getResults(self,tmin=0.90,tmax=0.99):
    if path.exists(self.dir + "clumps.h5part"):
      try:
        self.clmps = SimClumps(self, loadeng=True)
        self.clmps.getResults(tmin, tmax)
        if self.nodumps > 0:
          (lfile, ltime, mtime) = self.dumps[-1]
          getDisk(self.dir + lfile, self)

      except:
        self.results.problem = True
  
  def _getRotation(self,mminrel=0.01,rhozero=1.5,hmult=2.0):
    if self.nodumps > 0:
      sdir = self.dir
      mmin = self.gi.mtot*mminrel
      
      cfile = sdir + "clumps_rot.h5part"
      dfile = sdir + self.dumps[-1][0]

      cmd = "get_rotation " + dfile + " " \
        + cfile        + " " \
        + str(mmin)    + " " \
        + str(rhozero) + " " \
        + str(hmult)   + " "

      (exstat, out) = subprocess.getstatusoutput(cmd)
      print(out)
      
      clmp = SimClumps(self, fname="clumps_rot.h5part", loadcrot=True)
      
      self.results.Lparent = clmp.Lparent[0,:,:]
      self.results.Iparent = clmp.Iparent[0,:]
      self.results.mparentmat = clmp.mparentmat[0,:,:]
        
      os.remove(cfile)
    else:
      self.results.Lparent = np.zeros_like( self.results.Lm )*nan
      self.results.Iparent = np.zeros_like( self.results.Im )*nan
      self.results.mparentmat = np.zeros_like( self.results.mmatm )*nan

  def _plotSummary(self):
    if path.exists(self.dir + "clumps.h5part"):
      try:
        self.clmps = SimClumps(self, loadeng=True)
        self.clmps.plotSummary(self.dir + "summary.pdf")
      
      except:
        self.results.problem = True
  
  def _getClumpEnergy(self,tmin=0.90,tmax=0.99):
    res = self.results
    try:
      clmps = SimClumps(self, loadclmp=True)
      res.Epotclmp = clmps.Epotclmp[-1,:]
      res.Erotclmp = clmps.Erotclmp[-1,:]
      res.Iclmp    = clmps.Iclmp[-1,:]
        
      print(- res.Erotclmp[1] / res.Epotclmp[1], \
          res.mm[0] / clmps.m[-1,0], res.mm[1] / clmps.m[-1,1])
      print("Epotclmp and Erotclmp loaded!")
      
    except:
      noc = 0
      if len( res.mm ) > 0:
        noc = res.mm.shape[0]
          
      res.Epotclmp = nan*np.zeros(noc)
      res.Epotclmp = nan*np.zeros(noc)
      res.Iclmp    = nan*np.zeros(noc)
      print("Epotclmp and Erotclmp failed!")

  def _totEnergy(self):
    sdir  = self.dir
    for (dfile,dtime,dmtime) in self.dumps:
      cmd = "totEnergy__ " + sdir + dfile + " " + sdir + "clumps.h5part"
      (exstat, out) = subprocess.getstatusoutput(cmd)
      print(out)

  def _lastLog(self, n=10):
    sdir  = self.dir
    cmd = "tail -n " + str(n) + " " + sdir + "logDomain000"
    (exstat, out) = subprocess.getstatusoutput(cmd)
    return out

  def _lastDt(self):
    sdir  = self.dir
    cmd = "tail -n 100 " + sdir + "logDomain000 | grep dt:|tail -n1"
    (exstat, out) = subprocess.getstatusoutput(cmd)
    return out[24:]

  def _hdf5OK(self, file):
    (exstat, out) = subprocess.getstatusoutput("h5dump -A " + file)

    if exstat == 0:
      return True
    else:
      return False

  def _clumpsOK(self):
    cfile = self.dir + "clumps.h5part"
    if path.exists(cfile):
      return self._hdf5OK(self.dir + "clumps.h5part")
    else:
      return False
  
  def _getDumpsWithoutESC(self):
    retdumps = []
    for (dfile,dtime,dmtime) in self.dumps:
      efile = dfile.replace(".h5part","_esc.h5part")
      if not os.path.exists(self.dir + efile):
         retdumps.append( (dfile,dtime,dmtime) )
    return retdumps

  def _redoClumpsIfnodata(self):
    sdir = self.dir
    noescdump = self._getDumpsWithoutESC()
    if len(noescdump) > 0 and (not os.path.exists(self.dir + "clumps_done")):
      return self._redoClumps()
    else:
      return ""
  
  def _redoClumpsIfNotOK(self):
    if not self._clumpsOK():
       self._redoClumps()
  
  def _redoClumpsLocally(self):
    print(self.params.key)
    sdir = self.dir
    orgclmpfile = sdir + "clumps.h5part"
    
    if not path.exists(orgclmpfile):
      cmd = "mv " + sdir + "clumps.h5part " + sdir + "clumps_sim.h5part"
      (exstat, out) = subprocess.getstatusoutput(cmd)
    else:
      cmd = "rm " + sdir + "clumps.h5part "
      (exstat, out) = subprocess.getstatusoutput(cmd)

    mmassclmp = 0.01*self.mtot
    mmassorb  = 0.01*self.mtot

    #for (dfile,dtime,dmtime) in self.dumps:
    if len(self.dumps) > 0:
      (dfile,dtime,dmtime) = self.dumps[-1]
      cmd = "find_clumps_A_ " + sdir + dfile + " " + sdir + \
            "clumps.h5part " + str(mmassclmp) + " " + str(mmassorb)
      (stat, out) = subprocess.getstatusoutput(cmd)
      print(out)

  def _redoClumps(self):
    if not len(self.cfg.subcmdsgl) > 0:
      self._redoClumpsLocally()
      return
    sdir = self.dir
    
    jobscr  = self.dir + "redoclumps.sh"
    scrfile = open(jobscr,"w")

    if hasattr(sim_machine, "qsub_sglscr"):
      scrhead = sim_machine.qsub_sglscr
      scrhead = scrhead.replace('$JOBNAME', "redoClp_"+self.params.key)
      scrhead = scrhead.replace("$JOBCMD" , "")
      scrfile.write(scrhead)
      
    mmassclmp = 1.e-4*self.mtot
    mmassorb  = 1.e-3*self.mtot
    
    print("mv clumps.h5part clumps_sim.h5part", file=scrfile)
    
    for (dfile,dtime,dmtime) in self.dumps:
      #cmd = "find_clumps_A_ " + sdir + dfile + " " + sdir + \
      #      "clumps.h5part -1. -1."
      cmd = "find_clumps_A_ " + sdir + dfile + " " + sdir + \
            "clumps.h5part " + str(mmassclmp) + " " + str(mmassorb)
      print(cmd, file=scrfile)
      #(stat, out) = commands.getstatusoutput(cmd)
      #print out
    print("rm " + jobscr, file=scrfile)
    print("touch " + sdir + "clumps_done", file=scrfile)
    scrfile.close()
    os.chmod(jobscr, stat.S_IRWXU)
    
    jobsub = self.cfg.subcmdsgl
    jobsub = jobsub.replace("$JOBNAME", "redoClp_"+self.params.key)
    jobsub = jobsub.replace("$JOBCMD" , "redoclumps.sh")

    if hasattr(sim_machine, "qsub_sglscr"):
      retstr = "cd " + sdir + "; qsub redoclumps.sh"
      print(retstr)
      return retstr
    else:
      oldwd = os.getcwd()
      os.chdir(self.dir)
      (exstat, out) = subprocess.getstatusoutput(jobsub)
      os.chdir(oldwd)
      print(out)
      return out

  def _findNoClumps(self):
    sdir = self.dir
    rcmd = "h5part_readattr -k noclumps -i "
    wcmd = "h5part_writeattr -k noclumps -v -2. -i "

    jobscr  = sdir + "doclumps.sh"
    (exstat, out) = subprocess.getstatusoutput("rm " + jobscr)
    scrfile = open(jobscr,"w")

    if not self.clumpjobid == 0:
      jobdel = self.cfg.delcmd
      jobdel = jobdel.replace("$JOBID", self.clumpjobid)
      (exstat, out) = subprocess.getstatusoutput(jobdel)
      print(out)

    for (dfile,dtime,dmtime) in self.dumps:
      (exstat, out) = subprocess.getstatusoutput(rcmd + sdir + dfile)
      if not exstat == 0:
        print(sdir + dfile, " corrupted!")
        continue
      noc = int(float(out[10:]))
      if noc == -1:
        cmd = "find_clumps_A_ " + sdir + dfile + " " + sdir + \
            "clumps.h5part -1. -1."
        print(cmd, file=scrfile)
    print("rm " + jobscr, file=scrfile)
    scrfile.close()
    
    os.chmod(jobscr, stat.S_IRWXU)
    jobsub = self.cfg.subcmdsgl
    jobsub = jobsub.replace("$JOBNAME", "doClp_"+self.params.key)
    jobsub = jobsub.replace("$JOBCMD" , "doclumps.sh")

    oldwd = os.getcwd()
    os.chdir(self.dir)
    (exstat, out) = subprocess.getstatusoutput(jobsub)
    os.chdir(oldwd)
    print(out)
    self.clumpjobid = re.split( '\W+', out )[2]

  def _delCorrupt(self):
    for (dfile, dtime, mtime) in self.dumps:
      if not self._hdf5OK(self.dir + dfile):
        (exstat, out) = subprocess.getstatusoutput("rm " + self.dir + dfile)
        print("rm " + self.dir + dfile)
    if not self._clumpsOK():
      (exstat, out) = subprocess.getstatusoutput("rm " + self.dir + "clumps.h5part")
      print("rm " + self.dir + "clumps.h5part")

  def _redoEnergies(self):
    sdir = self.dir
    jobscr  = self.dir + "redoenergies.sh"
    scrfile = open(jobscr,"w")

    for (dfile,dtime,dmtime) in self.dumps:
      cmd = "get_energies__ " + sdir + dfile + " " + sdir + \
            "clumps.h5part "
      print(cmd, file=scrfile)
    print("rm " + jobscr, file=scrfile)
    scrfile.close()
    os.chmod(jobscr, stat.S_IRWXU)
    jobsub = self.cfg.subcmdsgl
    jobsub = jobsub.replace("$JOBNAME", "redoEng_"+self.params.key)
    jobsub = jobsub.replace("$JOBCMD" , "redoenergies.sh")

    oldwd = os.getcwd()
    os.chdir(self.dir)
    (exstat, out) = subprocess.getstatusoutput(jobsub)
    print(jobsub)
    os.chdir(oldwd)
    print(out)

  def _getSecondaryImpact(self, rhomin):
    sdir = self.dir
    
    (dfile,dtime,dmtime) = self.dumps[-1]
    cmd = "find_fof " + sdir + dfile + " " + sdir + "last_fof.h5part 0. " + str(rhomin) + " 2.0 2 "
    (exstat, out) = subprocess.getstatusoutput(cmd)
    print(out)
    last_fof = SimClumps(self, "last_fof.h5part", loadsat=True, loadeng=True)
    (exstat, out) = subprocess.getstatusoutput("rm " + sdir + "last_fof.h5part")

    if last_fof.nofc[0] >= 2:
      r0vect = last_fof.pos[0,2,:] - last_fof.pos[0,1,:]
      v0vect = last_fof.vel[0,2,:] - last_fof.vel[0,1,:]
    
      mtar   = last_fof.m[0,1]
      mimp   = last_fof.m[0,2]
    
      Rtar   = last_fof.rclmp[0,1]
      Rimp   = last_fof.rclmp[0,2]

      G = self.cfg.G

      secgi = GiantImpact(mtar, mimp, Rtar, Rimp, G)
      secgi.getInitStateVecs(r0vect, v0vect)
      
      self.secgi = secgi
      self.last_fof = last_fof
      return True

    else:
      return False
Exemple #11
0
import numpy as np
import sys

from clumps import SimClumps

import matplotlib as mp
mp.use('Agg')
import matplotlib.pyplot as plt

MEg = 5.9736e27

iname = sys.argv[1]
oname = sys.argv[2]
key = sys.argv[3]

clumps = SimClumps(iname)
clmpmasses = clumps.m
maxnoc = clumps.maxnoc

fig = plt.figure()
ax = fig.add_subplot(111)

ax.semilogy(clmpmasses[:, 0] / MEg, "r-")
for i in range(1, maxnoc):
    ax.semilogy(clmpmasses[:, i] / MEg, "b-")

plt.ylim(1.e-3, 2.10)
plt.title(key)
plt.savefig(oname)