AI tools for scientific computing

by Eugeniy E. Mikhailov and Greg Bentsen

Logistics and Agenda

Homework 3 due Monday Sept 21 at 11:59pm


Agenda


Today

Homework 3:

You may use AI freely to write code, debug, and test.


You may not use AI to write the report.


As with Homework 2:

Tech Demo

A quick tour of common AI-assisted workflows:

AI Tools Overview

Why use AI tools?

AI tools can reduce the time spent on routine implementation and help us focus on higher-level scientific questions.


Goal:  Use AI to augment our computational workflow while retaining responsibility for the scientific result.

Levels of abstraction

Computing has repeatedly moved toward higher-level interfaces:

  • Natural-language interfaces
  • High-level languages such as Python
  • Compiled languages such as C++
  • Human-readable assembly
  • Machine instructions
  • Transistor logic
Source: generated with ChatGPT
Source: generated with ChatGPT

Each layer hides some implementation details while making more complex tasks easier to express.

AI changes the workflow

Historically, a large fraction of scientific programming involved implementation details:

AI tools can help with much of this work.


This shifts some of our effort from writing code toward directing, reading, critiquing, and testing code.

You are still responsible for the result

AI-generated code should be treated like code written by someone else:

A program can run successfully and still implement the wrong model.

Fundamental concepts — LLMs and inference

At a high level, a large language model (LLM) generates text by predicting a sequence of tokens from the information available in its context.

Source: generated with ChatGPT
Source: generated with ChatGPT
Source: generated with ChatGPT
Source: generated with ChatGPT

Model outputs can vary between runs. Different models have different strengths and limitations.

Fundamental concepts — context

The model does not automatically know everything that you know.

Context is the information available to the model for the current task.

This may include:


Good context is relevant context, not necessarily the maximum amount of context.

Fundamental concepts — limitations and tools

Important terms:

Tool access increases capability, but also increases the importance of reviewing what the model is about to do.

Workflow, Best Practices, and Safety

Common use cases

Explanation and summary

  • “Explain this code.”
  • “Provide a high-level summary of this function.”

Implementation

  • “Write code to do XYZ.”
  • “Modify this code to do ABC instead of XYZ.”

Debugging

  • “Help me understand this error.”

Improvement

  • “Critique this code.”
  • “Suggest ways to improve its clarity or efficiency.”

Planning and testing

  • “Make a plan for adding feature XYZ.”
  • “Write tests for this code.”

Give precise instructions

Good prompts clearly specify the task, relevant context, and constraints.

For example:


Precise instructions make the model easier to evaluate and reduce unnecessary changes.

Separate planning from implementation

For nontrivial tasks, work in stages:

explain → suggest → plan → implement → validate

This makes it easier to catch a bad assumption before it becomes a large code change.


For scientific computing, validation should include both:

Deterministic code and non-deterministic AI

Use each tool for what it does well.

AI can make it easier to write tests, but you must still decide what constitutes convincing evidence that the calculation is correct.

AI Safety

These tools are very powerful, but they come with serious risks.

With great power comes great responsibility

Stan Lee (1962)

Practical Demo Applied to Fitting

Access and navigation

Note: You must be on campus or connected through the VPN.

Navigation

Context and commands

Use context controls to tell the model what information matters for the current task.

The exact behavior depends on the interface, so pay attention to what information is actually being shared with the model.

Interaction modes

Different modes provide different levels of autonomy.

Use the least-permissive mode that is sufficient for the task.

Tool permissions

Before approving a tool action, understand what the model is asking to do.

Examples:

Stop the agent if its actions no longer match your intent.

Fitting demo workflow

Apply the workflow to the fitting example from the previous lecture:

  1. Ask for a high-level explanation of the fitting code.
  2. Ask for a critique of its assumptions and failure modes.
  3. Ask for a plan to improve the code.
  4. Review the proposed plan.
  5. Implement one change at a time.
  6. Run the code and tests.
  7. Validate the fitted result against known limits or synthetic data.

Review

Rules of thumb


Bottom line:  AI can accelerate scientific computing, but verification and scientific judgment remain your responsibility.