Easy Physics Calculations with C++: Making it Cross-Platform

In summary, the programmer wrote an algorithm in C++ and would like to make it available on a website or an executable. They would like it to be cross-platform and free. They are considering a rewrite into Javascript but do not know the language. A website is a convenient place for others to use the code, but the programmer wants it to be subtle that it is C++. ChatGPT may be able to help.
  • #1
Pete5876
7
0
Hi.

I wrote a little algorithm that calculates some physics equations for education purposes and it's in C++. I'd like to make it avaiable by creating a website and put that code in it so it either interprets the C++ code or preferrably works with an executable (but then how would it be cross-platform?).

I would consider a rewrite into something like Javascript but I don't know this language and the code is a little long. I want this algorithm for my own use (hence C++) but I think a website is a convenient place for others to use it too who might find it useful. I wouldn't want it to be obvious it's C++ because some people don't like to code hence the website.

Is C++ at all suitable for this?

I would like it to be free since it's just exercise, I've got no experience with web development and until now never thought I'd have use for it. If not free then cheap.
 
Technology news on Phys.org
  • #2
If you want the code to run on the website you could investigate emscripten, but this is not simple. I would recommend rewriting as Javascript. You can get a website up and running pretty quickly using GitHub pages.
 
  • Like
Likes berkeman
  • #3
That might be a task that ChatGPT may be able to do correctly. You should try it.

You may succeed just with a verbal description of what the demo should do and skip the C++ code. Or you maybe can give it the C++ code and tell it to translate that into another language that could run on a web page; perhaps javascript. I would try several ways.
 
  • Skeptical
Likes Wrichik Basu
  • #4
You can run your C++ executable and serve the results on your web page. For example, I created this C++ program and compiled it into a file named /var/www/html/test/a.out:

C++:
#include <iostream>
using namespace std;
 
int main(int argc, char *argv[]) { 
    cout << "Hello " << argv[1] << "\n";
    cout << "How are you?\n";
    return 0;
}

Then I created this PHP file named my-web-page.php:

PHP:
<html>
    <head>
        <!-- Set the document title -->
        <title>Run My Program</title>
    </head>
    <body>
        <!-- Set the page title -->
        <h1>My program</h1>
        <!-- Create a form to get the user input -->
        <form method="post">
            <label>Name: 
                <input
                    type="text"
                    name="input-name"
                    value="<?php echo $_POST['input-name'] ?? ''; /* Check if a value was already entered; if not, set to the empty string */ ?>"
                >
            </label>
            <!-- insert a submit button for the form -->
            <input type="submit">
        </form>
<?php
    // Was there a value already entered?
    if(!empty($_POST['input-name'])){
        // Execute our executable program --> See https://www.php.net/manual/en/function.exec and https://www.php.net/manual/en/function.escapeshellarg.php
        $lastLine = exec('/var/www/html/test/a.out '. escapeshellarg($_POST['input-name']), $output);
        if($lastLine){
            // print the result
?>
        <!-- Print the subtitle -->
        <h2>Result</h2>
        <!-- Put the result in a preformatted text element -> see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre -->
        <pre>
<?php
            // Print each line of the output array from the executed program
            foreach($output as $line){
                echo $line."\n";
            }
?>
        </pre>
<?php
        }
    }
?>
    </body>
</html>

Screenshots (run in Firefox from an Apache server with PHP):

run-my-program-01.png

run-my-program-02.png
 
  • Like
Likes Jarvis323 and pbuk
  • #6
Pete5876 said:
I would consider a rewrite into something like Javascript but I don't know this language
JavaScript is strongly based on C so if you're not making a lot of use of the "++" part of C++ the translation to JavaScript might be pretty easy.
 
  • #7
anorlunda said:
That might be a task that ChatGPT may be able to do correctly. You should try it.
I don't think you understand what ChatGPT is designed to do.

jedishrfu said:
Another approach is Python and Flask. But first you need to select a host if you want to make it publicly available like Godaddy.
This would be significantly harder than the PHP approach suggested by @jack action (which would still need a web host). What do you think the benefit would be?
 
  • #8
Some folks use flask for small in-company websites or for personal use. One example would be on windows WSL so that you can work with linux stuff from windows although there are many other ways. I've also used it for docker images making code more easily distributable without all the dependencies once docker is installed.
 
  • #9
anorlunda said:
That might be a task that ChatGPT may be able to do correctly.
ChatGPT is not a programmer. I strongly disagree with this recommendation.
 
  • Like
Likes aaroman, pbuk and Wrichik Basu
  • #10
PeterDonis said:
ChatGPT is not a programmer. I strongly disagree with this recommendation.
I guess I put my foot into it on that. Sorry.
 
  • Like
Likes berkeman
  • #12
phinds said:
This is misstating what ChatGPT does. It does not "write" web pages or code. It generates text based on patterns it found in its training data. Since its training data included code and web pages, it will generate text with similar patterns if appropriately prompted. But it knows nothing about the semantics of the text it generates. It knows nothing of semantics, period. The only thing it knows about are patterns in streams of text.

IMO anyone who uses code from ChatGPT is foolish. The way to write working code is to learn how to write working code. Looking at actual examples of code that is already known to work helps in that learning process, but that is not what ChatGPT gives you.
 
  • Like
Likes Vanadium 50, Wrichik Basu, jack action and 1 other person
  • #13
PeterDonis said:
This is misstating what ChatGPT does.
I'll take your word for it. I haven't used it and was just reporting what I have read in numerous articles.
 
  • #14
phinds said:
I haven't used it and was just reporting what I have read in numerous articles.
I think this is a good example of how the standard PF rule about valid references works well as a more general heuristic. All of those articles are the equivalent of the unreliable pop science articles that we are constantly having to deal with here at PF. Any valid reference about ChatGPT would be something like the actual technical description of what it does, and any such description would make it obvious why it's not a reliable source of information. Of course the purveyors of ChatGPT have a strong incentive not to have people read that information, which is why you virtually never see it discussed.
 
  • Like
Likes Wrichik Basu, pbuk and phinds
  • #15
PeterDonis said:
All of those articles are the equivalent of the unreliable pop science articles that we are constantly having to deal with here at PF.
Fair point. My apologies.
 
  • #16
anorlunda said:
That might be a task that ChatGPT may be able to do correctly. You should try it.

You may succeed just with a verbal description of what the demo should do and skip the C++ code. Or you maybe can give it the C++ code and tell it to translate that into another language that could run on a web page; perhaps javascript. I would try several ways.
I suspect it would succeed (or nearly succeed and could be fixed with some proofreading) so long as the code is sufficiently modular or small. Transformers such as GPT were originally designed for the purpose of language translation. Tasks like this are well within its scope.

That said, in principle, I don't like the idea of using AI to write code, just because it can result in a gradual decline in competence and understanding, and because it makes us dependent on some few behemoth companies who have the resources to train the models. But, we kind of don't have much of a choice. Adopt the powerful new tools or lose the productivity competition.
 
  • Like
Likes anorlunda
  • #17
anorlunda said:
That might be a task that ChatGPT may be able to do correctly. You should try it.

You may succeed just with a verbal description of what the demo should do and skip the C++ code. Or you maybe can give it the C++ code and tell it to translate that into another language that could run on a web page; perhaps javascript. I would try several ways.
ChatGPT is not a valid reference in the technical forums. Thread closed for a bit for Moderation...
 
  • #18
After a Mentor discussion, we will try reopening this thread now, but please avoid suggesting using AI chat bots to solve technical problems. It might work, but it also may not and it may be hard to tell the difference. For now, we will not allow AI chat bots as references in the technical forums. The Mentors are discussing whether to add something to the PF Rules about this, but for now we will rely on the "valid references" requirement for the technical forums.

If folks want to discuss this aspect of the PF Rules, please start a new thread in the Feedback forum. Thank you.
 
  • Like
Likes robphy, hutchphd, PeterDonis and 1 other person
  • #19
There are lots of web authors out there - I have used several, but since I do not create web sites if I do not have to, I just use DreamWeaver.
 
  • Like
Likes jedishrfu
  • #20
You could try studying this article, which talks about a way to run c++ in a web browser.
https://blog.esciencecenter.nl/using-c-in-a-web-app-with-webassembly-efd78c08469

As for making a website, that is a trivial problem. You could use wordpress and add a data input form to send your program the neccessary starting data.

Or you could try using PHP and re-write the program in that language.

EDIT.
Perhaps not wordpress, as it can sometimes get fussy if you try to include your own PHP in random places. So learn to write the world's simplest html web page, with a data entry form, and send the data to the php version of your program.
 
  • #21
I think https://replit.com/ (or something similar) might be the platform you seek.

You can leave all of the webserver management and website hosting to someone else
and focus on your content and have it run in a browser.
(I signed up for a free account a while back.. but haven't really used it.
I usually work on Python and WebVPython at trinket.io and glowscript.org .)

https://www.google.com/search?q=replit+c+++physics
 
Last edited:
  • #22
anorlunda said:
I guess I put my foot into it on that. Sorry.
I've gotten ChatGPT to write some pretty amazing code so I wouldn't rule it out without trying it.
 
  • Informative
Likes phinds
  • #23
harborsparrow said:
I've gotten ChatGPT to write some pretty amazing code so I wouldn't rule it out without trying it.
Please read post #18 in this thread.
 
  • Informative
  • Like
Likes robphy and harborsparrow

FAQ: Easy Physics Calculations with C++: Making it Cross-Platform

How can I use C++ for physics calculations?

You can use C++ to perform physics calculations by writing functions that implement the necessary mathematical formulas and algorithms for the specific physics problem you are trying to solve. C++ provides a powerful and efficient way to perform complex calculations, making it an ideal choice for physics simulations.

What makes C++ a good choice for cross-platform development?

C++ is a good choice for cross-platform development because it is a standardized language that can be compiled to run on a wide variety of operating systems and hardware platforms. This allows you to write code once and run it on multiple platforms without having to make significant changes. Additionally, C++ offers good performance and low-level control, making it well-suited for developing applications that need to run efficiently on different platforms.

How can I get started with physics calculations in C++?

To get started with physics calculations in C++, you can begin by familiarizing yourself with the basics of the language and learning how to write functions that perform mathematical operations. You can then explore physics libraries and resources that provide pre-written code for common physics calculations, or you can write your own functions from scratch based on the specific physics problem you are working on.

Are there any specific libraries or frameworks recommended for physics calculations in C++?

There are several libraries and frameworks that are commonly used for physics calculations in C++, such as Bullet Physics, Box2D, and Eigen. These libraries provide pre-written code for performing common physics calculations, such as collision detection, rigid body dynamics, and linear algebra operations. Using these libraries can help you save time and effort in implementing complex physics simulations in your C++ projects.

How can I ensure my physics calculations are accurate and efficient in C++?

To ensure that your physics calculations are accurate and efficient in C++, you should carefully test and validate your code using known physics principles and equations. You can also optimize your code by using appropriate data structures and algorithms, such as numerical integration methods or matrix operations, to improve the performance of your calculations. Additionally, you can use profiling tools to identify and eliminate bottlenecks in your code that may be affecting its efficiency.

Similar threads

Replies
1
Views
1K
Replies
64
Views
6K
Replies
19
Views
3K
Replies
30
Views
5K
Replies
29
Views
6K
Replies
15
Views
2K
Back
Top