I tried to apply the simulation of the URL below by changing it to "SouthKorea" and "Seoul" using Mathematica 12.1.
https://community.wolfram.com/groups/-/m/t/1901002
The part where the error occurs in this code is:
enter code here
statecolor = Association[0 -> Lighter@Gray, 1 -> Red, 2 -> Pink, 3 -> Darker@Red, 4 -> Brown, 10 -> Darker@Green, 11 -> Black];
color = Table[statecolor[[i]], {i, 1, 7}];
$infectedstates = 1 | 2 | 3 | 4;
Options[prepareRegion] = {}
prepareRegion[r : _Region | _Entity | _Polygon, targetarea_] := Module[{region, area, newregion},
If[MatchQ[r, _Entity],
region =
EntityValue[r, "Polygon"] /.
g_GeoPosition :> g["LongitudeLatitude"];, region = r;];
area = Area[region];
If[! NumericQ[area], Return[{$Failed, $Failed}];]; newregion = RegionResize[region,
Abs[Subtract @@ RegionBounds[region][[1]]] Sqrt[targetarea/area]]; {newregion, RegionMember[newregion], RescalingTransform[RegionBounds[region],
RegionBounds[newregion]]}];
createPositions[n_, region_, regionq_ : Automatic] := Module[{memberq =
Replace[regionq, Automatic :> RegionMember[region]], positions,
i = 0}, positions = {};
While[Length[positions] < n && i++ < n,
positions =
DeleteDuplicates[
Join[positions,
Select[Round@RandomPoint[region, n - Length[positions]],
memberq]]];]; If[Length[positions] < n, $Failed, positions]];
createStates[n_, infected : {__Integer} | _Integer | UpTo[_Integer], isolationProp_] := Module[{states}, states = Table[{0, 0, RandomChoice[{isolationProp, 1 - isolationProp} -> {0, 1}]}, n]; If[! TrueQ[And @@ MatchQ[infected, _List]], Return[$Failed]]; Switch[infected, _List, states[[infected]] =
ConstantArray[{1, 0, 1}, Length[infected]];, _UpTo,
states[[;; infected[[1]]]] = ConstantArray[{1, 0, 1}, infected[[1]]];, _,
states[[RandomSample[Range[n], infected]]] = ConstantArray[{1, 0, 1}, infected];]; states];
searchInfectionFocus[posisitions_, point_, transformation_, i_] := Nearest[posisitions -> "Index", transformation[point], i];
Options[createSimulation] := {"Infected" -> 1, "Density" -> Automatic, "InfectionPoint" -> None, "Isolation" -> 0.1};
createSimulation[n_, r : _Region | _Entity | _Polygon,
OptionsPattern[]] := Module[{region, regionq, positions, transform, infected,
density = OptionValue["Density"] /. Automatic -> 1/10, ninfected,
isolation, states}, {region, regionq, transform} =
prepareRegion[r, n/density]; positions = createPositions[n, region];
ninfected = OptionValue["Infected"]; Which[MatchQ[OptionValue["InfectionPoint"], _Entity],
infected =
searchInfectionFocus[positions,
EntityValue[OptionValue["InfectionPoint"], "Position"][
"LongitudeLatitude"], transform, ninfected];,
MatchQ[OptionValue["InfectionPoint"], {_?NumericQ, _?NumericQ}],
infected =
searchInfectionFocus[positions, OptionValue["InfectionPoint"],
transform, ninfected];, True, infected = ninfected;];
isolation = OptionValue["Isolation"];
states = createStates[n, infected, isolation];
{region, regionq, positions, states}];
{region, regionq, initialposCase1, initialstatesCase1} = createSimulation[200, Entity["Country", "SouthKorea"], "Infected" -> 5, "Density" -> Automatic,
"InfectionPoint" -> Entity["City", {"Seoul", "Seoul", "SouthKorea"}], "Isolation" -> 0]
When the above code is executed, the kernel often crashes. However, very occasionally, the same run will yield normal output. I'd like some help with what's wrong with the code above.