2
$\begingroup$

I have written a program in C which solves a particular 2-body problem and then saves the results in two separate .txt files, “results1.txt” and “results2.txt”. The results in each file are rows of four numbers, where the first number of each row represents an instant of time and has the same value in both .txt’s, while the rest three numbers are the spatial coordinates. I want to import those results in Mathematica and make an animated 3D-plot of the motion.

  1. What is a good format for the data written to the files? I want Mathematica to recognize them as arrays of numbers. For the moment, each row has the form {t, x, y, z}, where t, x, y, z are floating-point numbers.

  2. How should I write the call to Import in order to be able to get the results in Mathematica? I tried

    Import[“results1.txt”, ”List”] 
    

    and I could extract each one of the rows using Part, but then I cannot extract the numbers of each array.

    Here is what I want to do, For each imported row, to combine them into list with three elements, where the first is the time and the other two are three-number lists, representing the position of each particle.

    For example, I want to take {t, x1, y1, z1} (imported from results1.txt) and {t, x2, y2, z2} (imported from results2.txt) and turn them to an list of the form {t, {x1, y1, x1}, {x2, y2, z2}}.

  3. When I have a list of all those {t, {x1, y1, x1}, {x2, y2, z2}}, how can I make an animated 3-D plot which, for each t, show the position of both particles?

$\endgroup$
11
  • 2
    $\begingroup$ It supports many formats. Whitespace separated table? Use "Table" in Import. Tab-separated? "TSV". Comma-separated? "CSV". These are useful formats supported by many systems, not just Mathematica. $\endgroup$
    – Szabolcs
    Commented Nov 20, 2016 at 19:54
  • $\begingroup$ Shall I keep the curly brckets then or omit them? $\endgroup$ Commented Nov 20, 2016 at 20:02
  • $\begingroup$ Omit them. Then you gain compatibility with more systems. Also, {1.2e5, 2.3} isn't valid Mathematica code, but 1.25e5 2.3 can be imported as "Table". $\endgroup$
    – Szabolcs
    Commented Nov 20, 2016 at 20:06
  • $\begingroup$ Do you want to use the function Manipulate (cf. manipulate)? Or just use Animate or ListAnimate, or export a GIF? $\endgroup$
    – Michael E2
    Commented Nov 20, 2016 at 20:07
  • $\begingroup$ I do not know which one is better, I am new in Mathematica. Perhaps you can recoment me which one, by letting you know that time is discretized, i.e. the positions will "jump" from one instant to the other, and that the time interval is not fixed because the code is adaptive... $\endgroup$ Commented Nov 20, 2016 at 20:18

1 Answer 1

3
$\begingroup$

This answer doesn't address the importing issue, but covers the other issues.

I treat the problem as two dimensional for simplicity of presentation; you should have no difficulty extended it to three dimensions.

You really don't need to have the time data to make an orbit plot. All you need are the spatial coordinates. To see why, let's suppose you have imported the coordinates for both orbits, each in its own list.

orbit1 = 
  {{0.5, 0}, {0.463872, 0.216819}, {0.364834, 0.404947}, 
   {0.224017, 0.547972}, {0.0626924, 0.642719}, {-0.10343, 0.693472}, 
   {-0.264334, 0.706978}, {-0.414068, 0.68998}, {-0.549216, 0.648396}, 
   {-0.66787, 0.587185}, {-0.768989, 0.510468}, {-0.852033, 0.421696}, 
   {-0.916739, 0.32381}, {-0.96299, 0.219383}, {-0.990747, 0.110729}, 
   {-1., 0}, {-0.990747, -0.110729}, {-0.96299, -0.219383}, 
   {-0.916738, -0.323811}, {-0.852033, -0.421696}, {-0.768989, -0.510469}, 
   {-0.667869, -0.587186}, {-0.549215, -0.648396}, {-0.414067, -0.68998}, 
   {-0.264333, -0.706978}, {-0.103429, -0.693472}, {0.0626931, -0.642719}, 
   {0.224018, -0.547972}, {0.364834, -0.404946}, {0.463872, -0.216819}, 
   {0.5, 0}};

orbit2 = 
  {{0.2, 0}, {-0.00262335, 0.354937}, {-0.264896, 0.488283}, 
   {-0.487973, 0.537679}, {-0.676595, 0.547454}, {-0.837395, 0.534245}, 
   {-0.975061, 0.506087}, {-1.09284, 0.467516}, {-1.19303, 0.421372}, 
   {-1.27733, 0.369581}, {-1.34696, 0.313528}, {-1.40286, 0.254265}, 
   {-1.4457, 0.192627}, {-1.47597, 0.129309}, {-1.49401, 0.064916}, 
   {-1.5, 0}, {-1.494, -0.0649209}, {-1.47597, -0.129314}, 
   {-1.44569, -0.192632}, {-1.40285, -0.25427}, {-1.34696, -0.313533}, 
   {-1.27732, -0.369585}, {-1.19303, -0.421376}, {-1.09283, -0.467519}, 
   {-0.97505, -0.50609}, {-0.837383, -0.534246}, {-0.676581, -0.547454}, 
   {-0.487956, -0.537677}, {-0.264876, -0.488276}, {-0.00260077, -0.35492}, 
   {0.2, 0}};

To keep the contents pane of the animation at a fixed size, I need the bounding box of the two orbits. I compute that with

bounds =
  With[{both = Join[orbit1, orbit2]}, 
   {{MinimalBy[both, First][[1, 1]], MaximalBy[both, First][[1, 1]]},
   {MinimalBy[both, Last][[1, 2]], MaximalBy[both, Last][[1, 2]]}}]

{{-1.5, 0.5}, {-0.706978, 0.706978}}

I will make the animation in a Manipulate expression, which can of course animate what it displays in its contents pane.

Manipulate[
  Graphics[
    {AbsolutePointSize[6],
     Blue, Point[orbit1[[;; t]]],
     Red, Point[orbit2[[;; t]]]},
    PlotRange -> bounds,
    PlotRangePadding -> .05],
  {t, 1, Length[orbit1], 1, ImageSize -> Large, Appearance -> "Labeled"}]

demo

$\endgroup$
2
  • $\begingroup$ I think I really need the time data, the algorithm is adaptive and the time interval between successive points could differ by some orders of magnitude. If I ignore this, slidding the bar in the animation will show the points like they are equally time-seperated? Is there any way to show at each step, not the step on the slidder but the actual temporal instant? Or the re-scale the slide bar so that it show more points when the time intervals become small? $\endgroup$ Commented Nov 22, 2016 at 15:08
  • 1
    $\begingroup$ @user3257624. Once the coordinates are computed, you don't need time data to plot them, because -- as you have said in your question -- the data sets have identical time points. The variable t in my code is actually the frame number. If you extracted the times into a separate list, you could use t to get the time from that list and display it as time stamp on the frame. $\endgroup$
    – m_goldberg
    Commented Nov 22, 2016 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.