Beispiel #1
0
 public void AddDimensionedSpecies(SpeciesValue species, double molarity, double molarityVariance, string dimension, double volume, Style style)
 {
     if (this.HasSpecies(species.symbol, out int index))
     {
         throw new Error("Repeated amount of '" + species.Format(style) + "' in sample '" + this.sample.Format(style) + "' with value " + style.FormatDouble(molarity));
     }
     else if (molarity < 0)
     {
         throw new Error("Amount of '" + species.Format(style) + "' in sample '" + this.sample.Format(style) + "' must be non-negative: " + style.FormatDouble(molarity));
     }
     else if (molarityVariance < 0)
     {
         throw new Error("Variance of amount of '" + species.Format(style) + "' in sample '" + this.sample.Format(style) + "' must be non-negative: " + style.FormatDouble(molarityVariance));
     }
     else
     {
         this.species.Add(species);
         RecomputeIndex();
         this.state = this.state.Extend(1);
         this.state.SumMean(this.state.size - 1, NormalizeDimension(species, molarity, dimension, volume, style));
         if (this.state.lna)
         {
             this.state.SumCovar(this.state.size - 1, this.state.size - 1, NormalizeDimension(species, molarityVariance, dimension, volume, style)); // may normalize variance by volume
         }
     }
 }
Beispiel #2
0
        //public void Transfer(StateMap other, double thisVolume, double otherVolume, Style style) {
        //    // transfer from another stateMap into this (empty) one
        //    // if new volume is larger, then it is "dilution", and it the same as mixing with an empty sample
        //    // but if new volume is smaller, then it is "evaporation", which cannot be otherwise expressed
        //    int n = 0;
        //    foreach (SpeciesValue otherSpecies in other.species) {
        //            this.species.Add(otherSpecies);
        //            n++;
        //        }
        //    RecomputeIndex();
        //    if (n > 0) this.state = this.state.Extend(n);
        //    // if volumeScaling==0 then volume will be 0 so it does not matter what mean and covar are
        //    foreach (SpeciesValue otherSpecies in other.species) {
        //        SumMean(otherSpecies.symbol, (proportion == 0.0) ? 0.0 : other.Mean(otherSpecies.symbol)/proportion);
        //        double squareProportion = proportion * proportion; // square of volume ratio law,
        //        if (this.state.lna && other.state.lna) {
        //            foreach (SpeciesValue otherSpecies2 in other.species) {
        //                double otherCovar = (proportion == 0.0) ? 0.0 : other.Covar(otherSpecies.symbol, otherSpecies2.symbol) / squareProportion;
        //                SumCovar(otherSpecies.symbol, otherSpecies2.symbol, otherCovar);
        //            }
        //        }
        //    }
        //}
        public double NormalizeDimension(SpeciesValue species, double value, string dimension, double volume, Style style)
        {
            if (double.IsNaN(value))
            {
                return(value);
            }
            double normal;

            normal = Protocol.NormalizeMolarity(value, dimension);
            if (normal >= 0)
            {
                return(normal);             // value had dimension M = mol/L
            }
            normal = Protocol.NormalizeMole(value, dimension);
            if (normal >= 0)
            {
                return(normal / volume);             // value had dimension mol, convert it to M = mol/L
            }
            normal = Protocol.NormalizeWeight(value, dimension);
            if (normal >= 0)
            {
                if (species.HasMolarMass())
                {
                    return((normal / species.MolarMass()) / volume);    // value had dimension g, convert it to M = (g/(g/M))/L
                }
                throw new Error("Species '" + species.Format(style)
                                + "' was given no molar mass, hence its amount in sample '" + this.sample.Format(style)
                                + "' should have dimension 'M' (concentration) or 'mol' (mole), not '" + dimension + "'");
            }
            throw new Error("Invalid dimension '" + dimension + "'" + " or dimension value " + style.FormatDouble(value));
        }
Beispiel #3
0
 public AmountEntry(SpeciesValue species, NumberValue initial, NumberValue initialVariance, string dimension, SampleValue sample)
 {
     this.species         = species;
     this.initial         = initial;
     this.initialVariance = initialVariance;
     this.dimension       = dimension;
     this.sample          = sample;
 }
Beispiel #4
0
 public TriggerEntry(SpeciesValue target, Flow condition, Flow assignment, Flow assignmentVariance, string dimension, SampleValue sample)
 {
     this.target             = target;
     this.condition          = condition;
     this.assignment         = assignment;
     this.assignmentVariance = assignmentVariance;
     this.dimension          = dimension;
     this.sample             = sample;
 }
Beispiel #5
0
 public (SpeciesValue[] vars, Flow[] flows) MeanFlow()
 {
     SpeciesValue[] vars  = new SpeciesValue[sample.Count()];
     Flow[]         flows = new Flow[sample.Count()];
     for (int speciesIx = 0; speciesIx < flows.Length; speciesIx++)
     {
         vars[speciesIx] = sample.stateMap.species[speciesIx];
         Flow polynomial = NumberFlow.numberFlowZero;
         foreach (ReactionValue reaction in this.reactions)
         {
             int  netStoichiometry = reaction.NetStoichiometry(vars[speciesIx].symbol);
             Flow monomial         = (netStoichiometry == 0) ? NumberFlow.numberFlowZero : OpFlow.Op("*", new NumberFlow(netStoichiometry), RateFunction(reaction));
             polynomial = OpFlow.Op("+", polynomial, monomial);
         }
         flows[speciesIx] = polynomial;
     }
     return(vars, flows);
 }
Beispiel #6
0
        public Flow[,] Jacobian(Style style)
        {
            int speciesNo = sample.Count();

            Flow[,] jacobian = new Flow[speciesNo, speciesNo];
            for (int speciesI = 0; speciesI < speciesNo; speciesI++)
            {
                SpeciesValue variableI = sample.stateMap.species[speciesI];
                for (int speciesK = 0; speciesK < speciesNo; speciesK++)
                {
                    SpeciesValue variableK  = sample.stateMap.species[speciesK];
                    Flow         polynomial = NumberFlow.numberFlowZero;
                    foreach (ReactionValue reaction in this.reactions)
                    {
                        int netStoichiometryI = reaction.NetStoichiometry(variableI.symbol);
                        if (reaction.rate is MassActionNumericalRate)
                        {
                            int  stoichiometryK = reaction.Stoichiometry(variableK.symbol, reaction.reactants);
                            Flow monomial       = (netStoichiometryI == 0 || stoichiometryK == 0) ? NumberFlow.numberFlowZero :
                                                  OpFlow.Op("*", OpFlow.Op("*", new NumberFlow(netStoichiometryI), new NumberFlow(stoichiometryK)),
                                                            OpFlow.Op("/", RateFunction(reaction), new SpeciesFlow(variableK.symbol)));
                            polynomial = OpFlow.Op("+", polynomial, monomial);
                        }
                        else if (reaction.rate is GeneralFlowRate || reaction.rate is MassActionFlowRate)
                        {
                            Flow monomial = (netStoichiometryI == 0) ? NumberFlow.numberFlowZero :
                                            OpFlow.Op("*", new NumberFlow(netStoichiometryI),
                                                      RateFunction(reaction).Differentiate(variableK.symbol, style));
                            polynomial = OpFlow.Op("+", polynomial, monomial);
                        }
                        else
                        {
                            throw new Error("Jacobian");
                        }
                    }
                    // Gui.Log("d " + variableI.Format(style) + "/d" + variableK.Format(style) + " = " + polynomial.Format(style));
                    jacobian[speciesI, speciesK] = polynomial;
                }
            }
            return(jacobian);
        }
Beispiel #7
0
        public (SpeciesFlow[, ], Flow[, ]) CovarFlow(Style style)
        {
            int speciesNo = sample.Count();

            SpeciesFlow[,] covar = new SpeciesFlow[speciesNo, speciesNo]; // fill it with fresh covariance variables
            for (int speciesI = 0; speciesI < speciesNo; speciesI++)      // rows
            {
                SpeciesValue variableI = sample.stateMap.species[speciesI];
                for (int speciesJ = 0; speciesJ < speciesNo; speciesJ++)   // columns
                {
                    SpeciesValue variableJ = sample.stateMap.species[speciesJ];
                    if (style.exportTarget == ExportTarget.WolframNotebook)
                    {
                        covar[speciesI, speciesJ] = new SpeciesFlow(new Symbol(variableI.Format(style) + "_" + variableJ.Format(style)));
                    }
                    else
                    {
                        covar[speciesI, speciesJ] = (speciesI == speciesJ) ? new SpeciesFlow(new Symbol("var(" + variableI.Format(style) + ")")) : new SpeciesFlow(new Symbol("cov(" + variableI.Format(style) + "," + variableJ.Format(style) + ")"));
                    }
                }
            }
            Flow[,] covarFlow = new Flow[speciesNo, speciesNo];
            Flow[,] jacobian  = Jacobian(style);
            Flow[,] drift     = Drift();
            for (int speciesI = 0; speciesI < speciesNo; speciesI++)     // rows
            {
                for (int speciesJ = 0; speciesJ < speciesNo; speciesJ++) // columns
                {
                    covarFlow[speciesI, speciesJ] = NumberFlow.numberFlowZero;
                    for (int speciesK = 0; speciesK < speciesNo; speciesK++)   // dot product index
                    {
                        covarFlow[speciesI, speciesJ] = OpFlow.Op("+", covarFlow[speciesI, speciesJ], OpFlow.Op("*", jacobian[speciesI, speciesK], covar[speciesK, speciesJ]));
                        covarFlow[speciesI, speciesJ] = OpFlow.Op("+", covarFlow[speciesI, speciesJ], OpFlow.Op("*", jacobian[speciesJ, speciesK], covar[speciesI, speciesK])); // jacobian transposed
                    }
                    covarFlow[speciesI, speciesJ] = OpFlow.Op("+", covarFlow[speciesI, speciesJ], drift[speciesI, speciesJ]);
                }
            }
            return(covar, covarFlow);
        }
Beispiel #8
0
 public SpeciesEntry(SpeciesValue species)
 {
     this.species = species;
 }