Browse Source

Adding solveMatrix to runSolver

Andrej 1 week ago
parent
commit
95a3a070e5
1 changed files with 44 additions and 2 deletions
  1. 44 2
      pythonScripts/runSolver.py

+ 44 - 2
pythonScripts/runSolver.py

@@ -1,4 +1,5 @@
 import ivp
 import ivp
+import solveMatrix
 import os
 import os
 import cModel
 import cModel
 import sys
 import sys
@@ -7,6 +8,8 @@ import numpy
 import time
 import time
 import scipy.interpolate
 import scipy.interpolate
 import shutil
 import shutil
+import importlib
+importlib.reload(solveMatrix)
 
 
 defaultValues={\
 defaultValues={\
       'method':'LSODA',\
       'method':'LSODA',\
@@ -22,7 +25,8 @@ defaultValues={\
       'project':'Analysis/Run',\
       'project':'Analysis/Run',\
       'schema':'lists',\
       'schema':'lists',\
       'query':'runs',\
       'query':'runs',\
-      'view':'WithStrings'}
+      'view':'WithStrings',
+      'exposureScaleRef':1}
 
 
 def get(setup,par):
 def get(setup,par):
    try:
    try:
@@ -128,9 +132,39 @@ def main(parFiles,jobDir,startDir='NONE'):
          #this could be modified for different ordering of lut as well
          #this could be modified for different ordering of lut as well
          sIn[:,model.lutSE[x]]=s0[:,startPoint['lutSE'][x]]
          sIn[:,model.lutSE[x]]=s0[:,startPoint['lutSE'][x]]
    #copy t0 to setup to help get the strides right
    #copy t0 to setup to help get the strides right
+   exposureScale=setup['exposureScaleRef']
+   y0*=exposureScale
+   print('sIn {}'.format(sIn.shape))
+   try:
+      for x in model.lutSE:
+         sIn[:,model.lutSE[x]]*=exposureScale
+   except IndexError:
+      #if there are no initial conditions, skip this part
+      pass
    setup['t0']=t0 
    setup['t0']=t0 
    strides=getStrides(setup)
    strides=getStrides(setup)
    for step in strides:
    for step in strides:
+
+      #check if solution exists
+      readable=['t','sol','se','qt','sOut']
+      l=step['label']
+      files=[os.path.join(jobDir,'{}{}.txt'.format(x,l)) for x in readable]
+      filesPresent=all( [os.path.isfile(x) for x in files])
+      if filesPresent:
+         #set t0,y0,sIn
+         latestFiles={x:'{}{}.txt'.format(x,l) for x in readable}
+         data=loadSolutionFromFiles(setup,jobDir,[latestFiles])
+         startPoint=startPointObject(data)
+         t0=startPoint['t0']
+         y0=startPoint['y0']
+         s0=startPoint['s0']
+         for x in model.lutSE:
+            sIn[:,model.lutSE[x]]=s0[:,startPoint['lutSE'][x]]
+         print('Completed step {}: {}'.format(l,filesPresent))
+         continue
+      
+      print('Step required {}'.format(l))
+
       t1=t0+step['length']
       t1=t0+step['length']
       start_time=time.time()
       start_time=time.time()
       if setup['mode']=='SequentialOdeint':
       if setup['mode']=='SequentialOdeint':
@@ -152,6 +186,11 @@ def main(parFiles,jobDir,startDir='NONE'):
             rtol=setup['rtol'],method=setup['method'],\
             rtol=setup['rtol'],method=setup['method'],\
             t0=t0,y0=y0,sIn=sIn)
             t0=t0,y0=y0,sIn=sIn)
 
 
+      if setup['mode']=='solveMatrix':
+         t,sol,se,s1=solveMatrix.solveMatrix(model,t1,\
+            nt=setup['nt'],\
+            t0=t0,y0=y0,sIn=sIn,method=setup['method'])
+
       end_time=time.time()
       end_time=time.time()
       #interpolate on s1
       #interpolate on s1
       #s1 has shape ft x n x m
       #s1 has shape ft x n x m
@@ -164,7 +203,6 @@ def main(parFiles,jobDir,startDir='NONE'):
 
 
       #store
       #store
       writeable={'t':t,'sol':sol,'se':se,'qt':qt,'sOut':sOut}
       writeable={'t':t,'sol':sol,'se':se,'qt':qt,'sOut':sOut}
-      l=step['label']
       writeable={os.path.join(jobDir,'{}{}.txt'.format(x,l)):writeable[x]\
       writeable={os.path.join(jobDir,'{}{}.txt'.format(x,l)):writeable[x]\
          for x in writeable}
          for x in writeable}
       for x in writeable:
       for x in writeable:
@@ -246,6 +284,7 @@ def parseShape(line):
       
       
 def read3D(fName):
 def read3D(fName):
    new_data=numpy.loadtxt(fName)
    new_data=numpy.loadtxt(fName)
+#loadtxt will ignore lines starting with #, so that is where we store contextual information like shape
    #read and parse first three lines to get shape,lut and lutSE
    #read and parse first three lines to get shape,lut and lutSE
    nLines=3
    nLines=3
    with open(fName,'r') as f:
    with open(fName,'r') as f:
@@ -348,6 +387,9 @@ def loadSolutionFromDir(jobDir,loadAll=False):
    setupFile=os.path.join(jobDir,'setup.json')
    setupFile=os.path.join(jobDir,'setup.json')
    setup=parseSetup(setupFile)
    setup=parseSetup(setupFile)
    inFiles=getFiles(setup,loadAll)
    inFiles=getFiles(setup,loadAll)
+   return loadSolutionFromFiles(setup,jobDir,inFiles)
+
+def loadSolutionFromFiles(setup,jobDir,inFiles):
    isFirst=True 
    isFirst=True 
    operators={x:numpy.loadtxt for x in inFiles[0]}
    operators={x:numpy.loadtxt for x in inFiles[0]}
    operators['sOut']=read3D
    operators['sOut']=read3D