183_notes:vpython_resources

This is an old revision of the document!


Computation in P-Cubed

In class, you will make use of Glowscript to model the motion of different physical systems. Glowscript is an online coding platform that means that you can work on our computational activities without having to install anything on your computer. Glowscript has extensive help documentation which can be found here. You will notice that this will take you to something called Vpython and Vypthon Glowscript help. Glowscript runs VPython which is built from the Python programming language. Python has become an incredibly popular computational language because is uses simple programming syntax and offers relatively straight forward code readability.

The class uses Glowscript and by default VPython because it adds some features that were traditionally difficult to have Python do. With very little code, VPython can:

  • create visual objects in 3 dimensions,
  • animate the motion of objects, and
  • move the visualization around with mouse interactions

All of this allows us to create computational models that are modeling real-world physical phenomenon that can help you gain an understanding and insight into physics concepts that you will interact within this class.

The majority of the codes that you will use in this class will start with two lines. The first line tells the program which version of Glowscript it is using: “GlowScript 3.0 VPython”.

The second line which will not always be there but will be the majority of the time will be “get_library('https://cdn.rawgit.com/PERLMSU/physutil/master/js/physutil.js')”. This is importing a bunch of code in the program that is called PhysUtil which is a module for VPython that makes it simpler to create highly visual simulations. With very little code, PhysUtil lets you:

  • generate motion maps,
  • plot graphs,
  • make timers, and
  • import CSV data

For folks who are interested in learning more about PhysUtil and what it does, there is extensive documentation for PhysUtil at available here.

Message from Paul: \textit{You will have heard me say this already but I want to reaffirm that you are not expected to learn a whole language when taking this class. The focus is on gaining an understanding of how to read code and make small edits. Before designing this class I had no real coding experience and so I want to highlight something that might lead to frustration as it did for me when I started learning some basic coding. Errors can drive you nuts. BUT, learning the skill of debugging an error is important and will hold to you in any future coding experiences. It is transferrable to other classes and future career aspirations and is a legitimate problem-solving approach that is not just applicable to code. So, by all means, get frustrated but remember that encountering errors in your code is not a failure on your part but just an opportunity to develop a new skill and also remember to celebrate the victories when you get your code to do what you intend it to do.}

  • Syntax errors – A syntax error occurs when python is unable to interpret a line of code. This usually occurs because of a simple typo, misspelling, capitalization inconsistency, or incorrect parentheses in the line above.
  • Indentation errors – This is caused by an inconsistency in the indentation of the while loop. To fix this highlight the while loop, click “Format” (on the top bar of the laptop), click “dedent region”, then use tab to re indent everything that was indented to start.
  • Float Error: scalar vs. vector – when coding using both scalar and vector values, keep in mind that you cannot multiply, divided, or put a vector to the power of a number without first taking the magnitude of the vector (to make the vector a scalar value). When putting a value to the power of something the common format for this is (^#), but in python the format (**#) is used to depict this.
  • Name Error – This error occurs when you are trying to call a variable that has not already been defined above. To fix this, the line that defines the variable must be moved above the equation that the variable is being used in. This also occurs if the defined variable is misspelled.
  • Type Error – This error is much like a float error. Python will display this error message when you are trying to do something “strange”. A common reason that we see this error in class is from trying to multiply/divide a scaler by a vector.
  • Spazzing Graph – When putting in the first line of code it needs to be placed under the #MotionMap/Graph, this is where you indicate the number of lines that you need graphed. A common mistake is that this first line is placed under the #Calculation Loop this will result in a graph that appears to be flashing. This happens because the code is creating a new graph every time step.
  • Magnitude vs. Dot product – to find the size or absolute value of a vector, take the magnitude of that vector; this yields a scalar quantity with no directional information and is never negative. When finding how much of one vector is in the direction of a second vector, you need to take the dot product of the two vectors; this yields a scalar quantity that can be positive, negative or zero.
  • 183_notes/vpython_resources.1594875008.txt.gz
  • Last modified: 2020/07/16 04:50
  • by pwirving