public void addLink(int end1, int end2, int linkWeight) { if (linkArr[end1, end2] == 0) { int linkNumber = linkList.Count; linkList.Add(new Link { par1 = end1, par2 = end2, length = linkSize, force2 = 0, orientation = Vector3.Zero, weight = linkWeight, linkType = 0, } ); linkArr[end1, end2] = 1; linkArr[end2, end1] = 1; if (linkPtrLists.ElementAtOrDefault(end1) == null) { linkPtrLists.Insert(end1, new List <int>()); } linkPtrLists[end1].Add(linkNumber); if (linkPtrLists.ElementAtOrDefault(end2) == null) { linkPtrLists.Insert(end1, new List <int>()); } linkPtrLists[end2].Add(linkNumber); Particle3d newPrt1 = ParticleList[end1]; Particle3d newPrt2 = ParticleList[end2]; newPrt1.linksCount += 1; newPrt2.linksCount += 1; if (newPrt1.Type == 3) { newPrt1.Charge = 0.02f; newPrt1.Type = 2; } if (newPrt2.Type == 3) { newPrt2.Charge = 0.02f; newPrt2.Type = 2; } ParticleList[end1] = newPrt1; ParticleList[end2] = newPrt2; } }
/// <summary> /// This function calculates the following values: /// 1. Total energy at (k+1)th iteration (from nextStateBuffer). /// 2. Dot product of kth descent vector and (k+1)th energy gradient (which equals minus (k+1)th descent vector) /// </summary> /// <param name="device"></param> /// <param name="currentStateBuffer"></param> /// <param name="nextStateBuffer"></param> /// <param name="outputValues"></param> /// <param name="parameters"></param> /// <param name="energy"></param> /// <param name="pTgradE"></param> public void CalcTotalEnergyAndDotProduct(StructuredBuffer currentStateBuffer, StructuredBuffer nextStateBuffer, StructuredBuffer outputValues, ComputeParams parameters, out float energy, out float pTgradE, out float checkSum) { parameters.MaxParticles = (uint)ParticleCount; energy = 0; pTgradE = 0; checkSum = 0; if (UseGPU) { // preform reduction on GPU: paramsCB.SetData(parameters); device.ComputeShaderConstants[0] = paramsCB; device.ComputeShaderResources[0] = currentStateBuffer; device.ComputeShaderResources[1] = nextStateBuffer; device.SetCSRWBuffer(1, outputValues, MathUtil.IntDivUp((int)parameters.MaxParticles, BlockSize)); device.PipelineState = factory[(int)ComputeFlags.COMPUTE | (int)ComputeFlags.REDUCTION]; device.Dispatch(MathUtil.IntDivUp((int)parameters.MaxParticles, BlockSize)); // perform final summation: Vector4[] valueBufferCPU = new Vector4[MathUtil.IntDivUp((int)parameters.MaxParticles, BlockSize)]; outputValues.GetData(valueBufferCPU); foreach (var value in valueBufferCPU) { energy += value.X; pTgradE += value.Y; checkSum += value.Z; } } else // if not use GPU { // perform summation on CPU: Particle3d[] currentBufferCPU = new Particle3d[parameters.MaxParticles]; Particle3d[] nextBufferCPU = new Particle3d[parameters.MaxParticles]; currentStateBuffer.GetData(currentBufferCPU); nextStateBuffer.GetData(nextBufferCPU); for (int i = 0; i < parameters.MaxParticles; ++i) { Vector3 force1 = currentBufferCPU[i].Force; Vector3 force2 = nextBufferCPU[i].Force; pTgradE += -1.0f * Vector3.Dot(force1, force2); energy += nextBufferCPU[i].Energy; checkSum += nextBufferCPU[i].Mass; } } energy /= 2; // because each pair is counted 2 times }
public Graph GetGraph() { if (lay.CurrentStateBuffer != null) { Particle3d[] particleArray = new Particle3d[lay.ParticleCount]; lay.CurrentStateBuffer.GetData(particleArray); Graph graph = new Graph(); foreach (var p in particleArray) { graph.AddNode(new SpatialNode(p.Position, p.Size, new Color(p.Color))); } foreach (var l in edgeList) { graph.AddEdge(new Edge((int)l.par1, (int)l.par2, l.length, l.strength)); } return(graph); } return(new Graph()); }
public void killBankruptLinks() { //if (bunkruptcounter == 4) { for (int i = 0; i < cfg.BankNodes; i++) { if (ParticleList[i].Type == 4) { Particle3d newPrt1 = ParticleList[i]; newPrt1.Type = 1; ParticleList[i] = newPrt1; } } //for (int i = cfg.BankNodes; i < ParticleList.Count; i++) //{ // if (ParticleList[i].Type == 4) // { // Particle3d newPrt1 = ParticleList[i]; // newPrt1.Type = 2; // ParticleList[i] = newPrt1; // } //} for (int i = 0; i < linkList.Count; i++) { Link newLink = linkList[i]; newLink.linkType = 0; linkList[i] = newLink; } //bunkruptcounter = 0; } //bunkruptcounter++; }
public Graph GetGraph() { if (lay.CurrentStateBuffer != null) { Particle3d[] particleArray = new Particle3d[lay.ParticleCount]; lay.CurrentStateBuffer.GetData(particleArray); Graph graph = new Graph(); foreach (var p in particleArray) { graph.AddNode(new SpatialNode(p.Position, p.Size, new Color(p.Color))); } foreach (var l in edgeList) { graph.AddEdge(new Graph.Edge((int)l.par1, (int)l.par2, l.length, l.strength)); } return graph; } return new Graph(); }
public void removeLinksFromFile(string fName) { dstrings = File.ReadAllLines(fName); if (dstrings.Length > 0) { data = new int[dstrings.Length, dstrings[0].Length / 2 - 1]; for (int i = 0; i < dstrings.Length; i++) { string[] split; split = dstrings[i].Split(new Char[] { ' ', ',' }); int end1 = int.Parse(split[0]); int end2 = int.Parse(split[1]); // linkList.RemoveAll(list => list.par1 == end1 && list.par2 == end2); int index = linkList.FindIndex(list => list.par1 == end1 && list.par2 == end2); if (index >= 0) { var link = linkList[index]; link.par1 = link.par2; linkList[index] = link; if (linkPtrLists[end1] != null) { // linkPtrLists[end1].Clear(); linkPtrLists[end1].RemoveAll(ind => ind == index); } if (linkPtrLists[end2] != null) { // linkPtrLists[end2].Clear(); linkPtrLists[end2].RemoveAll(ind => ind == index); } linkArr[end1, end2] = 0; linkArr[end2, end1] = 0; Particle3d newPrt1 = ParticleList[end1]; Particle3d newPrt2 = ParticleList[end2]; newPrt1.linksCount -= 1; newPrt2.linksCount -= 1; if (newPrt1.linksCount == 0) { newPrt1.Charge = 0.04f; newPrt1.Type = 3; } if (newPrt2.linksCount == 0) { newPrt2.Charge = 0.04f; newPrt2.Type = 3; } ParticleList[end1] = newPrt1; ParticleList[end2] = newPrt2; } } } setBuffers(); }