Guest User

Untitled

a guest
Jul 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. //This was written as an exercise for a friend for a CFD class
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6.  
  7. namespace CenterOfGravity
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. //dataFile is a FileInfo that represent the File at the given Location
  14. FileInfo dataFile = new FileInfo(@"C:\inputFile.txt");
  15. //This makes a collection of Particle objects
  16. List<Particle> particles = new List<Particle>();
  17.  
  18. //This part opens the dataFile into fileStream which lets the file be read
  19. using (StreamReader fileStream = dataFile.OpenText())
  20. {
  21. //make a string called line to store each line in as it's read
  22. string line = "";
  23. //This makes an array of items to split the file lines along
  24. string[] splits = new string[1];
  25. splits[0] = " ";
  26. //While there are still lines to be read set line equal to that line
  27. while ((line = fileStream.ReadLine()) != null)
  28. {
  29. //split up the current line into a Array of strings
  30. string[] part = line.Split(splits, StringSplitOptions.RemoveEmptyEntries);
  31. //add a new particle to the Collection of particles
  32. //Set that particles properties equal to the values found in the string array
  33. particles.Add(new Particle
  34. {
  35. Mass = Double.Parse(part[0]),
  36. XPosition = Double.Parse(part[1]),
  37. YPosition = Double.Parse(part[2]),
  38. ZPosition = Double.Parse(part[3])
  39.  
  40. });
  41. }
  42. }
  43.  
  44. //This part does the math
  45. double xcenter = 0.0;
  46. double ycenter = 0.0;
  47. double zcenter = 0.0;
  48. double totalMass = 0.0;
  49. //Loop through all of the particles in the collection of particles and find the total mass
  50. foreach (Particle particle in particles)
  51. {
  52. totalMass += particle.Mass;
  53. }
  54. //Perform the summation of the weighted positions
  55. foreach (Particle particle in particles)
  56. {
  57. xcenter += (particle.Mass * particle.XPosition) / totalMass;
  58. ycenter += (particle.Mass * particle.YPosition) / totalMass;
  59. zcenter += (particle.Mass * particle.ZPosition) / totalMass;
  60. }
  61. //Write Out the output file
  62. using (StreamWriter outputFile = new StreamWriter(@"C:\output.dat", false))
  63. {
  64. outputFile.WriteLine("Total Mass = " + totalMass);
  65. outputFile.WriteLine("Center of Gravity");
  66. outputFile.WriteLine("x = " + xcenter);
  67. outputFile.WriteLine("y = " + ycenter);
  68. outputFile.WriteLine("z = " + zcenter);
  69. }
  70. }
  71. }
  72. //A Class representing the particle object.
  73. class Particle
  74. {
  75. public double XPosition { get; set; }
  76. public double YPosition { get; set; }
  77. public double ZPosition { get; set; }
  78. public double Mass { get; set; }
  79. }
  80. }
Add Comment
Please, Sign In to add comment