@@ -633,7 +633,7 @@ void SpatialModel::generate(unsigned niter)
633633 tstep+=niter;
634634}
635635
636- void SpatialModel::migrate ()
636+ unsigned SpatialModel::migrate ()
637637{
638638 /* each cell gets a distinct random salt value */
639639 hostForAll ([=,this ](EcolabCell& c) {c.salt =c.rand ();});
@@ -665,36 +665,47 @@ void SpatialModel::migrate()
665665 });
666666
667667 array<int > ssum (species.size (),0 );
668+ size_t totalMigration=0 ;
668669 hostForAll ([&,this ](EcolabCell& c) {
669- // adjust delta so that density remains +ve
670- array<int > adjust=delta[c.idx ()]+c.density ;
671- adjust*=-(adjust<0 );
672- if (sum (adjust)>0 )
673- {
674- // distribute adjust among neighbours
675- array<int > totalDiff (c.density .size (),0 );
676- for (auto & n: c)
677- {
678- auto & nbr=*n->as <EcolabCell>();
679- totalDiff+=nbr.density -c.density ;
680- }
681- // adjust adjust to be divisible by totalDiff
682- adjust-=adjust%totalDiff;
683- for (auto & n: c)
684- {
685- auto & nbr=*n->as <EcolabCell>();
686- delta[nbr.idx ()]+=((nbr.density -c.density )/totalDiff)*adjust;
687- }
688- delta[c.idx ()]-=adjust;
689- }
690- c.density +=delta[c.idx ()];
670+ // // adjust delta so that density remains +ve
671+ // array<int> adjust=delta[c.idx()]+c.density;
672+ // adjust*=-(adjust<0);
673+ // if (sum(adjust)>0)
674+ // {
675+ // // distribute adjust among neighbours
676+ // array<int> totalDiff(c.density.size(),0);
677+ // for (auto& n: c)
678+ // {
679+ // auto& nbr=*n->as<EcolabCell>();
680+ // totalDiff+=nbr.density-c.density;
681+ // }
682+ // // adjust adjust to be divisible by totalDiff
683+ // adjust-=adjust%totalDiff;
684+ // for (auto& n: c)
685+ // {
686+ // auto& nbr=*n->as<EcolabCell>();
687+ // delta[nbr.idx()]+=((nbr.density-c.density)/totalDiff)*adjust;
688+ // }
689+ // delta[c.idx()]-=adjust;
690+ // }
691+
692+ // c.density+=delta[c.idx()];
693+ cout<<delta[c.idx ()]<<endl;
694+ totalMigration+=sum (abs (delta[c.idx ()]));
691695#if !defined(NDEBUG)
692696#pragma omp critical
693697 ssum+=delta[c.idx ()];
694698#endif
695699 });
696700 last_mig_tstep=tstep;
697701
702+ #ifdef MPI_SUPPORT
703+ if (myid ()==0 )
704+ MPI_Reduce (MPI_IN_PLACE, &totalMigration,1 ,MPI_UNSIGNED,MPI_SUM,0 ,MPI_COMM_WORLD);
705+ else
706+ MPI_Reduce (&totalMigration,nullptr ,1 ,MPI_UNSIGNED,MPI_SUM,0 ,MPI_COMM_WORLD);
707+ #endif
708+
698709 /* assertion testing that population numbers are conserved */
699710#if !defined(NDEBUG) && !defined(SYCL_LANGUAGE_VERSION)
700711#ifdef MPI_SUPPORT
@@ -723,6 +734,7 @@ void SpatialModel::migrate()
723734 }
724735 if (myid ()==0 ) assert (sum (ssum==0 )==int (ssum.size ()));
725736#endif
737+ return totalMigration;
726738}
727739
728740void ModelData::makeConsistent (size_t nsp)
0 commit comments