roadrunner  2.6.0
Fast simulator for SBML models
LLVMExecutableModel.h
Go to the documentation of this file.
1 // == PREAMBLE ================================================
2 
3 // * Licensed under the Apache License, Version 2.0; see README
4 
5 // == FILEDOC =================================================
6 
14 /*
15  * Author: Andy Somogyi,
16  * email decode: V1 = "."; V2 = "@"; V3 = V1;
17  * andy V1 somogyi V2 gmail V3 com
18  */
19 
20 #ifndef LLVMExecutableModelH
21 #define LLVMExecutableModelH
22 
23 #include "rrExecutableModel.h"
24 #include "LLVMModelDataSymbols.h"
25 #include "LLVMIncludes.h"
26 
27 #include "EvalInitialConditionsCodeGen.h"
28 #include "EvalReactionRatesCodeGen.h"
29 #include "EvalRateRuleRatesCodeGen.h"
30 #include "GetValuesCodeGen.h"
31 #include "GetInitialValuesCodeGen.h"
32 #include "GetEventValuesCodeGen.h"
33 #include "EventAssignCodeGen.h"
34 #include "EventTriggerCodeGen.h"
35 #include "GetPiecewiseTriggerCodeGen.h"
36 #include "EvalVolatileStoichCodeGen.h"
37 #include "EvalConversionFactorCodeGen.h"
38 #include "SetValuesCodeGen.h"
39 #include "SetInitialValuesCodeGen.h"
40 #include "EventQueue.h"
41 #include "rrSelectionRecord.h"
42 
43 #include "tr1proxy/rr_memory.h"
44 #include "tr1proxy/rr_unordered_map.h"
45 #include <map>
46 
47 namespace rrllvm
48 {
49 
50 class ModelResources;
51 class Jit;
52 
58 class RR_DECLSPEC LLVMExecutableModel: public rr::ExecutableModel
59 {
60 public:
61 
68  virtual std::string getExecutableModelDesc() const {
69  return "LLVM Executable Model";
70  }
71 
77 
81  LLVMExecutableModel(const std::shared_ptr<ModelResources> &resources, LLVMModelData* modelData);
82 
83  /*
84  * Loads a saved executable model
85  */
86  LLVMExecutableModel(std::istream& in, uint modelGeneratorOpt);
87 
88  virtual ~LLVMExecutableModel();
89 
93  virtual std::string getModelName();
94  virtual void setTime(double _time);
95  virtual double getTime();
96 
103  void evalInitialConditions(uint32_t flags = 0);
104 
108  virtual void reset();
109 
114  virtual void reset(int options);
115 
116 
117 
118  virtual int getNumIndFloatingSpecies();
119  virtual int getNumDepFloatingSpecies();
120 
121  virtual int getNumFloatingSpecies();
122  virtual int getNumBoundarySpecies();
123  virtual int getNumGlobalParameters();
124 
125  virtual int getNumCompartments();
126  virtual int getCompartmentIndexForFloatingSpecies(size_t index);
127  virtual int getCompartmentIndexForBoundarySpecies(size_t index);
128 
137  virtual int getGlobalParameterValues(size_t len, int const *indx,
138  double *values);
139 
140  virtual int setGlobalParameterValues(size_t len, int const *indx,
141  const double *values);
142 
143  virtual int setGlobalParameterValues(size_t len, int const* indx,
144  const double* values, bool strict);
145 
146  virtual int getNumReactions();
147 
148  virtual int getReactionRates(size_t len, int const *indx,
149  double *values);
150 
159  virtual int getCompartmentVolumes(size_t len, int const *indx,
160  double *values);
161 
162  virtual int getNumRateRules();
163 
170  virtual std::vector<std::string> getRateRuleSymbols() const;
171 
176  virtual void getRateRuleValues(double *rateRuleValues);
177 
178 
179  virtual std::string getStateVectorId(size_t index);
180 
191  virtual int getStateVector(double *stateVector);
192 
202  virtual int setStateVector(const double *stateVector);
203 
219  virtual void getStateVectorRate(double time, const double *y, double* dydt=0);
220 
221 
222  virtual void testConstraints();
223 
224  virtual std::string getInfo();
225 
226  virtual int getFloatingSpeciesIndex(const std::string&);
227  virtual std::string getFloatingSpeciesId(size_t index);
228  virtual int getBoundarySpeciesIndex(const std::string&);
229  virtual std::string getBoundarySpeciesId(size_t index);
230 
239  virtual int getFloatingSpeciesAmounts(size_t len, int const *indx,
240  double *values);
241 
242  virtual int getFloatingSpeciesAmountRates(size_t len, int const *indx,
243  double *values);
244 
245  virtual int getFloatingSpeciesConcentrationRates(size_t len, int const *indx,
246  double *values);
247 
256  virtual int getFloatingSpeciesConcentrations(size_t len, int const *indx,
257  double *values);
258 
259 
268  virtual int setFloatingSpeciesConcentrations(size_t len, int const *indx,
269  double const *values);
270 
271  virtual int setFloatingSpeciesAmounts(size_t len, int const *indx,
272  const double *values);
273 
274  virtual int setFloatingSpeciesAmounts(size_t len, int const* indx,
275  const double* values, bool strict);
276 
285  virtual int getBoundarySpeciesAmounts(size_t len, int const *indx,
286  double *values);
287 
296  virtual int getBoundarySpeciesConcentrations(size_t len, int const *indx,
297  double *values);
298 
307  virtual int setBoundarySpeciesConcentrations(size_t len, int const *indx,
308  double const *values);
309 
310 
319  virtual int setBoundarySpeciesAmounts(size_t len, int const *indx,
320  double const *values);
321 
322  virtual int setBoundarySpeciesAmounts(size_t len, int const* indx,
323  double const* values, bool strict);
324 
325 
326  virtual int getGlobalParameterIndex(const std::string&);
327  virtual std::string getGlobalParameterId(size_t);
328  virtual int getCompartmentIndex(const std::string&);
329  virtual std::string getCompartmentId(size_t);
330  virtual int getReactionIndex(const std::string&);
331  virtual std::string getReactionId(size_t);
332  virtual int getStoichiometryIndex(const std::string&);
333  virtual int getStoichiometryIndex(const std::string& speciesId, const std::string& reactionId);
334  virtual std::string getStoichiometryId(size_t);
335 
336  virtual void print(std::ostream &stream);
337 
338  virtual int getNumConservedMoieties();
339  virtual int getConservedMoietyIndex(const std::string& name);
340  virtual std::string getConservedMoietyId(size_t index);
341  virtual int getConservedMoietyValues(size_t len, int const *indx, double *values);
342  virtual int setConservedMoietyValues(size_t len, int const *indx,
343  const double *values);
344 
345  virtual int setCompartmentVolumes(size_t len, int const* indx,
346  const double* values);
347 
348  virtual int setCompartmentVolumes(size_t len, int const* indx,
349  const double* values, bool strict);
350 
351  virtual int setStoichiometries(size_t len, int const* indx,
352  const double* values);
353 
354  virtual int setStoichiometries(size_t len, int const* indx,
355  const double* values, bool strict);
356 
357  virtual int setStoichiometry(int index, double value);
358 
359  virtual int setStoichiometry(int speciesIndex, int reactionIndex, double value);
360 
361  virtual double getStoichiometry(int index);
362 
363  virtual double getStoichiometry(int speciesIndex, int reactionIndex);
364 
365 
366  #if (1) /**********************************************************************/
367 
369  virtual int setFloatingSpeciesInitConcentrations(size_t len, const int *indx,
370  double const *values);
371 
372  virtual int setBoundarySpeciesInitConcentrations(size_t len, const int* indx,
373  double const* values);
374 
375  virtual int getFloatingSpeciesInitConcentrations(size_t len, const int *indx,
376  double *values);
377 
378  virtual int getBoundarySpeciesInitConcentrations(size_t len, const int* indx,
379  double* values);
380 
381  virtual int setFloatingSpeciesInitAmounts(size_t len, const int *indx,
382  double const *values);
383 
384  virtual int setBoundarySpeciesInitAmounts(size_t len, const int* indx,
385  double const* values);
386 
387  virtual int getFloatingSpeciesInitAmounts(size_t size_t, const int *indx,
388  double *values);
389 
390  virtual int getBoundarySpeciesInitAmounts(size_t size_t, const int* indx,
391  double* values);
392 
393  virtual int setCompartmentInitVolumes(size_t len, const int *indx,
394  double const *values);
395 
396  virtual int getCompartmentInitVolumes(size_t len, const int *indx,
397  double *values);
398 
399  virtual int setGlobalParameterInitValues(size_t len, const int *indx,
400  double const *values);
401 
402  virtual int getGlobalParameterInitValues(size_t len, const int *indx,
403  double *values);
404 
405 
406  #endif /***********************************************************************/
407 
410 
411  #if (1) /**********************************************************************/
412 
417  virtual void getIds(int types, std::list<std::string> &ids);
418 
422  virtual int getSupportedIdTypes();
423 
428  virtual double getValue(const std::string& id);
429 
433  virtual void setValue(const std::string& id, double value);
434 
435 
436  #endif /***********************************************************************/
437 
439 
440  #if (1) /**********************************************************************/
441 
447  virtual void setRandomSeed(int64_t);
448 
452  virtual int64_t getRandomSeed();
453 
460  virtual double getRandom();
461 
462 
463  #endif /**********************************************************************/
464 
467 
468  #if (1) /**********************************************************************/
469 
470 public:
471  virtual int getNumEvents();
472 
482  virtual int getEventTriggers(size_t len, const int *indx, unsigned char *eventState);
483 
488  virtual int applyEvents(double timeEnd, const unsigned char* previousEventState,
489  const double *initialState, double* finalState);
490 
491  virtual void getEventRoots(double time, const double* y, double* gdot);
492 
493  virtual void getPiecewiseTriggerRoots(double time, const double* y, double* gdot);
494 
495  virtual double getNextPendingEventTime(bool pop);
496 
497  virtual int getPendingEventSize();
498 
499  virtual void resetEvents();
500 
501  inline double getEventDelay(size_t event)
502  {
503  return getEventDelayPtr(modelData, event);
504  }
505 
506  inline double getEventPriority(size_t event)
507  {
508  return getEventPriorityPtr(modelData, event);
509  }
510 
511  bool getEventTrigger(size_t event);
512 
513  inline bool getEventUseValuesFromTriggerTime(size_t event)
514  {
515  assert(event < symbols->getEventAttributes().size()
516  && "event out of bounds");
517  return symbols->getEventAttributes()[event]
518  & EventUseValuesFromTriggerTime;
519  }
520 
521  inline bool getEventInitialValue(size_t event)
522  {
523  assert(event < symbols->getEventAttributes().size()
524  && "event out of bounds");
525  return symbols->getEventAttributes()[event] & EventInitialValue;
526  }
527 
528  inline bool getEventPersistent(size_t event)
529  {
530  assert(event < symbols->getEventAttributes().size()
531  && "event out of bounds");
532  return symbols->getEventAttributes()[event] & EventPersistent;
533  }
534 
535  inline size_t getEventBufferSize(size_t event)
536  {
537  return symbols->getEventBufferSize(event);
538  }
539 
540  inline void getEventData(size_t eventId, double* data)
541  {
542  eventTriggerPtr(modelData, eventId, data);
543  }
544 
548  inline void assignEvent(size_t eventId, double* data)
549  {
550  // apply the sbml JITed event assignments
551  eventAssignPtr(modelData, eventId, data);
552 
553  const rr::EventListenerPtr &handler = eventListeners[eventId];
554  if(handler)
555  {
556  uint result = handler->onAssignment(this, eventId, symbols->getEventId(eventId));
557 
558  if(result & rr::EventListener::HALT_SIMULATION) {
559  throw rr::EventListenerException(result);
560  }
561  }
562  }
563 
564  bool getEventTieBreak(size_t eventA, size_t eventB);
565 
566  virtual int getEventIndex(const std::string& eid);
567  virtual std::string getEventId(size_t index);
568  virtual void getEventIds(std::list<std::string>& out);
569 
570  virtual int getNumPiecewiseTriggers();
571 
572  virtual void getAssignmentRuleIds(std::list<std::string>& out);
573  virtual void getRateRuleIds(std::list<std::string>& out);
574  virtual void getInitialAssignmentIds(std::list<std::string>& out);
575  virtual void setEventListener(size_t index, rr::EventListenerPtr eventHandler);
576  virtual rr::EventListenerPtr getEventListener(size_t index);
577 
578 
579  virtual double getFloatingSpeciesAmountRate(size_t index,
580  const double *reactionRates);
581 
586  virtual int getRateRuleRates(size_t len, int const *indx, double *values);
587 
588 
592  virtual uint32_t getFlags() const { return flags; }
593 
598  virtual void setFlags(uint32_t val) { flags = val; }
599 
600  void saveState(std::ostream & out);
601 
602 
603 private:
604 
611  const rr::SelectionRecord& getSelection(const std::string& sel);
612 
619  bool applyEvents(unsigned char* prevEventState,
620  unsigned char* currEventState);
621 
622 
623  rrllvm::EventQueue pendingEvents;
624 
628  std::vector<double> eventAssignTimes;
629 
630  typedef unsigned long long TieBreakKey;
631  typedef std::map<TieBreakKey, bool> TieBreakMap;
632  TieBreakMap tieBreakMap;
633 
634  //Used by 'reset' to reset one type of element, while keeping track of which versions were reset that have initial assignments.
635  void resetOneType(int& opt, int thistype, int independents, int total, int(LLVMExecutableModel::*getInit)(size_t, const int*, double*), int(LLVMExecutableModel::*setCurrent)(size_t, const int*, const double*), string(LLVMModelDataSymbols::* getTypeId)(size_t) const, double* buffer, std::map<std::string, int>& inits, std::map<std::string, double>& initvals);
636 
637 
638 
639  #endif /***********************************************************************/
640 
641 private:
646  LLVMModelData *modelData;
647  cxx11_ns::shared_ptr<ModelResources> resources;
648  const LLVMModelDataSymbols *symbols;
649 
650 
651  EvalReactionRatesCodeGen::FunctionPtr evalReactionRatesPtr;
652  EvalInitialConditionsCodeGen::FunctionPtr evalInitialConditionsPtr;
653  GetBoundarySpeciesAmountCodeGen::FunctionPtr getBoundarySpeciesAmountPtr;
654  GetFloatingSpeciesAmountCodeGen::FunctionPtr getFloatingSpeciesAmountPtr;
655  GetBoundarySpeciesConcentrationCodeGen::FunctionPtr getBoundarySpeciesConcentrationPtr;
656  GetFloatingSpeciesConcentrationCodeGen::FunctionPtr getFloatingSpeciesConcentrationPtr;
657  GetCompartmentVolumeCodeGen::FunctionPtr getCompartmentVolumePtr;
658  GetGlobalParameterCodeGen::FunctionPtr getGlobalParameterPtr;
659  EvalRateRuleRatesCodeGen::FunctionPtr evalRateRuleRatesPtr;
660  GetEventTriggerCodeGen::FunctionPtr getEventTriggerPtr;
661  GetEventPriorityCodeGen::FunctionPtr getEventPriorityPtr;
662  GetEventDelayCodeGen::FunctionPtr getEventDelayPtr;
663  EventTriggerCodeGen::FunctionPtr eventTriggerPtr;
664  EventAssignCodeGen::FunctionPtr eventAssignPtr;
665  GetPiecewiseTriggerCodeGen::FunctionPtr getPiecewiseTriggerPtr;
666  EvalVolatileStoichCodeGen::FunctionPtr evalVolatileStoichPtr;
667  EvalConversionFactorCodeGen::FunctionPtr evalConversionFactorPtr;
668 
669  // set model values externally.
670  SetBoundarySpeciesAmountCodeGen::FunctionPtr setBoundarySpeciesAmountPtr;
671  SetFloatingSpeciesAmountCodeGen::FunctionPtr setFloatingSpeciesAmountPtr;
672  SetBoundarySpeciesConcentrationCodeGen::FunctionPtr setBoundarySpeciesConcentrationPtr;
673  SetFloatingSpeciesConcentrationCodeGen::FunctionPtr setFloatingSpeciesConcentrationPtr;
674  SetCompartmentVolumeCodeGen::FunctionPtr setCompartmentVolumePtr;
675  SetGlobalParameterCodeGen::FunctionPtr setGlobalParameterPtr;
676 
677 
678  // init value accessors
679  SetFloatingSpeciesInitConcentrationCodeGen::FunctionPtr setFloatingSpeciesInitConcentrationsPtr;
680  SetBoundarySpeciesInitConcentrationCodeGen::FunctionPtr setBoundarySpeciesInitConcentrationsPtr;
681  GetFloatingSpeciesInitConcentrationCodeGen::FunctionPtr getFloatingSpeciesInitConcentrationsPtr;
682  GetBoundarySpeciesInitConcentrationCodeGen::FunctionPtr getBoundarySpeciesInitConcentrationsPtr;
683  SetFloatingSpeciesInitAmountCodeGen::FunctionPtr setFloatingSpeciesInitAmountsPtr;
684  GetFloatingSpeciesInitAmountCodeGen::FunctionPtr getFloatingSpeciesInitAmountsPtr;
685  SetBoundarySpeciesInitAmountCodeGen::FunctionPtr setBoundarySpeciesInitAmountsPtr;
686  GetBoundarySpeciesInitAmountCodeGen::FunctionPtr getBoundarySpeciesInitAmountsPtr;
687  SetCompartmentInitVolumeCodeGen::FunctionPtr setCompartmentInitVolumesPtr;
688  GetCompartmentInitVolumeCodeGen::FunctionPtr getCompartmentInitVolumesPtr;
689  GetGlobalParameterInitValueCodeGen::FunctionPtr getGlobalParameterInitValuePtr;
690  SetGlobalParameterInitValueCodeGen::FunctionPtr setGlobalParameterInitValuePtr;
691 
692 
693  typedef std::string (LLVMExecutableModel::*GetNameFuncPtr)(size_t);
694 
698  typedef cxx11_ns::unordered_map<std::string, rr::SelectionRecord> SelectionMap;
699  SelectionMap selectionRecordCache;
700 
706  std::vector<rr::EventListenerPtr> eventListeners;
707 
711  int getValues(double (*funcPtr)(LLVMModelData*, size_t), size_t len,
712  const int *indx, double *values);
713 
717  int setValues(bool (*funcPtr)(LLVMModelData*, int, double), GetNameFuncPtr, size_t len,
718  const int *indx, const double *values, bool strict=true);
719 
720  static LLVMExecutableModel* dummy();
721 
722  friend class LLVMModelGenerator;
723 
724  template <typename a_type, typename b_type>
725  friend void copyCachedModel(a_type* src, b_type* dst);
726 
734  double conversionFactor;
735 
736 
740  uint32_t dirty;
741 
742  enum
743  {
744  // init cond for species have changed
745  DIRTY_INIT_SPECIES = (0x1 << 0), // => 0x00000001
746 
747  // conserved moieties have changes.
748  DIRTY_CONSERVED_MOIETIES = (0x1 << 1), // => 0x00000010
749 
750  // reaction rates need to be re-calculated.
751  DIRTY_REACTION_RATES = (0x1 << 2) //
752  };
753 
754 
755  uint32_t flags;
756 };
757 
758 } /* namespace rr */
759 #endif /* LLVMExecutableModelH */
Definition: rrExecutableModel.h:73
Base class for all code generation systems; allows compiling and evaluating the model.
Definition: rrExecutableModel.h:118
a way to find sbml model elements using the RoadRunner syntax.
Definition: rrSelectionRecord.h:18
The EventQueue class is when events are queued to execute, but have not yet executed.
Definition: EventQueue.h:86
LLVM executable model.
Definition: LLVMExecutableModel.h:59
virtual std::string getExecutableModelDesc() const
Returns a human-readable description of the code generation backend, e.g. LLVM, legacy C,...
Definition: LLVMExecutableModel.h:68
virtual void setFlags(uint32_t val)
Set certain options that determine the state of the ExecutableModel, these are listed in.
Definition: LLVMExecutableModel.h:598
virtual uint32_t getFlags() const
Get the current set of flags.
Definition: LLVMExecutableModel.h:592
void assignEvent(size_t eventId, double *data)
assign or apply the event using the given data.
Definition: LLVMExecutableModel.h:548
stores the names of all the symbols in the sbml model and thier indexes in the ModelData arrays.
Definition: LLVMModelDataSymbols.h:128
General concepts:
Definition: LLVMModelGenerator.h:41
C_DECL_SPEC char *rrcCallConv getModelName(RRHandle handle)
Returns the name of currently loaded SBML model.
C_DECL_SPEC RRVectorPtr rrcCallConv getBoundarySpeciesAmounts(RRHandle handle)
Retrieve the amounts for all the boundary species in a vector.
C_DECL_SPEC RRVectorPtr rrcCallConv getBoundarySpeciesConcentrations(RRHandle handle)
Retrieve the concentrations for all the boundary species in a vector.
C_DECL_SPEC bool rrcCallConv setBoundarySpeciesConcentrations(RRHandle handle, const RRVectorPtr vec)
Set the boundary species concentration to the vector vec.
C_DECL_SPEC RRVectorPtr rrcCallConv getFloatingSpeciesConcentrations(RRHandle handle)
Retrieve in a vector the concentrations for all the floating species.
C_DECL_SPEC RRVectorPtr rrcCallConv getFloatingSpeciesAmounts(RRHandle handle)
Retrieve in a vector the amounts for all the floating species.
C_DECL_SPEC bool rrcCallConv setFloatingSpeciesConcentrations(RRHandle handle, const RRVectorPtr vec)
Set the floating species concentration to the vector vec.
C_DECL_SPEC bool rrcCallConv saveState(RRHandle handle, const char *filename)
Save a road runner instance's state to a platform-specific binary file.
C_DECL_SPEC RRVectorPtr rrcCallConv getGlobalParameterValues(RRHandle handle)
Retrieve the values for all the global parameter values in a vector.
C_DECL_SPEC RRVectorPtr rrcCallConv getReactionRates(RRHandle handle)
Retrieve a vector of reaction rates as determined by the current state of the model.
C_DECL_SPEC bool rrcCallConv reset(RRHandle handle)
Resets all variables of the model to their current initial values. Does not change the parameters.
C_DECL_SPEC bool rrcCallConv getValue(RRHandle handle, const char *symbolId, double *value)
Get the value for a given symbol, use getAvailableTimeCourseSymbols(void) for a list of symbols.
C_DECL_SPEC bool rrcCallConv setValue(RRHandle handle, const char *symbolId, const double value)
Set the value for a given symbol, use getAvailableTimeCourseSymbols(void) for a list of symbols.
C_DECL_SPEC char *rrcCallConv getInfo(RRHandle handle)
Retrieve info about current state of roadrunner, e.g. loaded model, conservationAnalysis etc.
Base class for all code generators in RoadRunner.
cxx11_ns::shared_ptr< EventListener > EventListenerPtr
listeners are shared objects, so use std smart pointers to manage them.
Definition: rrExecutableModel.h:71
A data structure that is that allows data to be exchanged with running SBML models.
Definition: LLVMModelData.h:47