Location: cellLib @ c856b59f39db / Scripts / simExpNrst.py

Author:
WeiweiAi <wai484@aucklanduni.ac.nz>
Date:
2022-08-02 15:14:59+12:00
Desc:
Add the BG of a chemical reaction
Permanent Source URI:
https://models.cellml.org/workspace/6bc/rawfile/c856b59f39db721d14e9e3e77a231ff986471f95/Scripts/simExpNrst.py

# Simulate experiments
import opencor as oc
import numpy as np
def simExp(simfile, savefiles,start, ending, pointInterval,varSet,varLoop,varSave):   
   # Load the simulation file   
   simulation = oc.open_simulation(simfile)
   # The data object houses all the relevant information
   # and pointers to the OpenCOR internal data representations
   data = simulation.data()   
   data.set_point_interval(pointInterval)    
   # Variable to loop  
   loopPair = list(varLoop.values())[0]
   loopValues=list(loopPair.values())[0]
   nLoop=len(loopValues)
   dtime=(ending-start)/nLoop 
   # Data to save
   varName = np.array(list(varSave))
   vars = np.reshape(varName, (1, len(varName)))
   # Create a matrix r to save the data 
   indexStart = 0
   indexEnd = int(dtime/pointInterval)   
   rows=indexEnd-indexStart   
   r = np.zeros((rows,len(varName)))
   r_temp=np.copy(r) # must be a copy, otherwise pass address!
   # Run the simulation
   # Do NOT reset states and parameters
   simulation.reset(False)
   for k in range(nLoop):       
       data.set_starting_point(dtime*k)
       data.set_ending_point(dtime*(k+1))
       # Set parameter values
       for j, ivar in enumerate(list(varLoop)):      
          loopPair = list(varLoop.values())[j]
          loopType=list(loopPair)[0]
          loopValues=list(loopPair.values())[0]
          if loopType=='constants':
             data.constants()[ivar] = loopValues[k]          
          elif loopType=='algebraic':
             data.algebraic()[ivar] = loopValues[k]
          elif loopType=='states':
             data.states()[ivar] = loopValues[k]
          else:
              sys.exit("Wrong type of the variable") 
       for setVar, setPair  in varSet.items():
           setType = list(setPair)[0]
           setValue= list(setPair.values())[0]
           if setType=='constants':
              data.constants()[setVar] = setValue
           elif setType=='algebraic':
              data.algebraic()[setVar] = setValue
           elif setType=='states':
              data.states()[setVar] = setValue
           else:
              sys.exit("Wrong type of the variable")         
       simulation.run()
       # Access simulation results
       results = simulation.results()
       if k%1000==0:
          print(savefiles,'k=',k)

       # Grab specific variable results
       m=0    
       for saveVar, savePair  in varSave.items():
           saveType = list(savePair)[0]             
           if saveType =='constants':
              tempr = results.constants()[saveVar].values()[:-1]              
           elif saveType =='algebraic':
              tempr = results.algebraic()[saveVar].values()[:-1]
           elif saveType =='states':
              tempr = results.states()[saveVar].values()[:-1]
           elif saveType =='voi':
              tempr = results.voi().values()[:-1] 
           else:
              sys.exit("Wrong type of the variable")                      
           if k==0:
              r[:,m] = tempr                        
           else:
              r_temp[:,m]=tempr 
           m=m+1   
       if k>0:
           r=np.vstack((r,r_temp))             
       # clear the results       
       simulation.clear_results()    
   # Save the simulation result for all       
   filename='%s.csv' % (savefiles[0])
   np.savetxt(filename, vars, fmt='%s',delimiter=",")
   with open(filename, "ab") as f:
     np.savetxt(f, r, delimiter=",")
   f.close
   
   print(filename)