Ejemplo n.º 1
0
        /// <summary>
        /// Computes a mapping from the new species index to the old species index, or vice-versa.
        /// </summary>
        /// <param name="jCell">
        /// Local cell index.
        /// </param>
        /// <param name="New2Old">
        /// If true, a mapping from new species index to previous/old species index is returned,
        /// if false the other way around.
        /// </param>
        /// <param name="LsTrk">
        /// </param>
        /// <returns>
        /// Null, if there is no change in species ordering in cell <paramref name="jCell"/>
        /// from the previous level-set tracker state to the actual.
        ///
        /// Otherwise, if <paramref name="New2Old"/> is true, an array of the same length
        /// as the number of species currently in cell <paramref name="jCell"/>.
        ///  - index: current species index in cell <paramref name="jCell"/>.
        ///  - content: index of this species with respect to the previous level-set-tracker state.
        /// If <paramref name="New2Old"/> false, the other way around.
        /// </returns>
        public static int[] SpeciesUpdate(LevelSetTracker LsTrk, int jCell, bool New2Old)
        {
            ushort[] OldCode = LsTrk.RegionsHistory[0].RegionsCode;
            ushort[] NewCode = LsTrk.RegionsHistory[1].RegionsCode;

            // Only if the return value of this function is positive, a mapping from current species index to previous species index
            // - index: current species index in cell <paramref name="jCell"/>.
            // - content: index of this species with respect to the previous level-set-tracker state.
            int[] UpdateMap = null;
            bool  AnyUpdate = false;

            ushort oc = OldCode[jCell];
            ushort nc = NewCode[jCell];

            if (!New2Old)
            {
                // swap old and new
                ushort b = oc;
                oc = nc;
                nc = b;
            }

            ReducedRegionCode oldRRC, newRRC;
            int OldNoOfSpc = LsTrk.GetNoOfSpeciesByRegionCode(oc, out oldRRC);
            int NewNoOfSpc = LsTrk.GetNoOfSpeciesByRegionCode(nc, out newRRC);


            for (int new_iSpc = 0; new_iSpc < NewNoOfSpc; new_iSpc++)
            {
                SpeciesId spId     = LsTrk.GetSpeciesIdFromIndex(newRRC, new_iSpc);
                int       old_iSpc = LsTrk.GetSpeciesIndex(oldRRC, spId); // if the species was not present in the previous state, this should be negative

                if (old_iSpc != new_iSpc)
                {
                    if (AnyUpdate == false)
                    {
                        // init the update map: no change up to index 'new_iSpc'
                        AnyUpdate = true;
                        UpdateMap = new int[NewNoOfSpc];
                        for (int i = 0; i < new_iSpc; i++)
                        {
                            UpdateMap[i] = i;
                        }
                    }
                    // index 'new_iSpc' maps to old species index
                    UpdateMap[new_iSpc] = old_iSpc;
                }
            }

            if (NewNoOfSpc < OldNoOfSpc)
            {
                // the number of species is reduced - this also counts as change

                if (AnyUpdate == false)
                {
                    // init the update map: no change up to index 'new_iSpc'
                    AnyUpdate = true;
                    UpdateMap = new int[NewNoOfSpc];
                    for (int i = 0; i < NewNoOfSpc; i++)
                    {
                        UpdateMap[i] = i;
                    }
                }
            }

            // return
            return(UpdateMap);
        }