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

1. What is C++ and why is it useful for physics calculations?

C++ is a high-level programming language that is commonly used for developing software applications. It is useful for physics calculations because it allows for efficient and precise numerical computations, making it well-suited for scientific applications.

2. Can C++ be used for cross-platform development?

Yes, C++ is a cross-platform language, meaning that the same code can be compiled and run on multiple operating systems, including Windows, Mac, and Linux.

3. How can I get started with using C++ for physics calculations?

To get started, you will need to have a basic understanding of C++ programming concepts and syntax. There are many online resources and tutorials available to help you learn C++. You will also need to have a C++ compiler installed on your computer, such as Visual Studio or Code::Blocks.

4. What are some common physics calculations that can be done with C++?

C++ can be used for a wide range of physics calculations, including kinematics, dynamics, energy calculations, and more. It is also commonly used for simulations and modeling in physics research.

5. Are there any specific libraries or tools that can assist with physics calculations in C++?

Yes, there are many libraries and tools available for C++ that can help with physics calculations. Some popular ones include Boost, Eigen, and Armadillo. These libraries offer functions and classes for mathematical operations, linear algebra, and more.

Similar threads

  • Programming and Computer Science
Replies
1
Views
260
  • Programming and Computer Science
Replies
15
Views
1K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
Replies
8
Views
878
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
2
Replies
64
Views
5K
  • Programming and Computer Science
Replies
4
Views
336
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
3
Views
317
Back
Top