Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/invariants.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@ jobs:

- name: Run the ensemble test
run: ./.venv/bin/python tests/python/test_ensemble.py

- name: Run the FFTile and FFState test
run: ./.venv/bin/python tests/python/test_fftile_ffstate.py

- name: Run the listen mode test
run: ./.venv/bin/python tests/python/test_listen.py
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ history, so they summarise each release rather than list every change. The

### Fixed

- `setParameter[key=]` with an empty value no longer crashes.
- `listenHTTP` reads a POST body sent after its headers, as Python's `urllib` does.
- A `listenHTTP` server started from `pyforefire` no longer crashes on its first command.
- The Farsite model now matches the FARSITE reference (mbedward/farsite) at
the flanks and back of the fire, not only at the head. On slopes it is an
approximation. This changes Farsite results. ([#270])
Expand Down
2 changes: 1 addition & 1 deletion bindings/java/JLibForeFire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ JNIEXPORT jstring JNICALL Java_fprop_coupling_CCoupling_executeCommand (JNIEnv
}

ostringstream stringOut;
executor->setOstringstream(&stringOut);
Command::OutputCapture capture(&stringOut);


string smsg(msg);
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/src/pyforefire/_pyforefire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ py::object PLibForeFire::getDataMatrixPy(char *name) {
string PLibForeFire::execute(char *command)
{
ostringstream stringOut;
pyxecutor->setOstringstream(&stringOut);
Command::OutputCapture capture(&stringOut);
string smsg(command);
pyxecutor->ExecuteCommand(smsg);
return stringOut.str();
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/forefire_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class ForeFireWasm {
/*! \brief Run one ForeFire command, returning whatever it printed. */
std::string execute(const std::string &command) {
std::ostringstream captured;
executor.setOstringstream(&captured);
Command::OutputCapture capture(&captured);
std::string cmd(command);
executor.ExecuteCommand(cmd);
return captured.str();
Expand Down
8 changes: 6 additions & 2 deletions src/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2051,11 +2051,14 @@ namespace libforefire
vector<string> tmpArgs;
string delimiter = "=";
tokenize(arg, tmpArgs, delimiter);
if (tmpArgs.size() > 2)
if (tmpArgs.empty() || tmpArgs.size() > 2
|| arg.find(delimiter) == string::npos || arg.compare(0, delimiter.size(), delimiter) == 0)
{
cout << "problem in the number of arguments when setting " << arg << endl;
return error;
}
// "key=" sets an empty value; tokenize drops the empty token.
if (tmpArgs.size() == 1) tmpArgs.push_back("");

// Check if the parameter value begins with '!'
if (!tmpArgs[1].empty() && tmpArgs[1][0] == '!')
Expand Down Expand Up @@ -3117,7 +3120,8 @@ namespace libforefire

void Command::setOstringstream(ostringstream *oss)
{
currentSession.outStream = oss;
// nullptr goes back to cout, for callers whose stream is going away.
currentSession.outStream = oss ? static_cast<ostream *>(oss) : &cout;
}

void Command::tokenize(const string &str, vector<string> &tokens,
Expand Down
10 changes: 10 additions & 0 deletions src/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,16 @@ class Command {
static bool domainExists(const string& command);
/*! \brief command to redirect the ostringstream */
static void setOstringstream(ostringstream*);
/*! \brief sends command output to a stream until destroyed, then back to
* cout, so later output (e.g. from a listenHTTP thread) never reaches a
* stream that is gone */
class OutputCapture {
public:
explicit OutputCapture(ostringstream* out) { setOstringstream(out); }
~OutputCapture() { setOstringstream(nullptr); }
OutputCapture(const OutputCapture&) = delete;
OutputCapture& operator=(const OutputCapture&) = delete;
};

/*! \brief execute the desired command */
static void ExecuteCommand(string&);
Expand Down
43 changes: 37 additions & 6 deletions src/HttpCommandServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <cstring>
#include <cctype>
#include <sys/stat.h>
#include <sys/time.h>
#include <dirent.h>

// POSIX headers for sockets
Expand Down Expand Up @@ -151,20 +152,50 @@ namespace http_command {
}
continue;
}
// Clients are served one at a time: do not let a stalled one
// hold the server.
timeval timeout{5, 0};
setsockopt(client_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
handleClient(client_fd);
close(client_fd);
}
}

// Read the whole request: headers, then Content-Length bytes of body.
// Clients may send them in separate segments, so one read() is not
// enough. Empty on a closed connection or an oversized request.
static std::string readRequest(int client_fd) {
const size_t max_size = 16 << 20;
std::string request;
char buffer[4096];
size_t header_end = std::string::npos, expected = 0;
while (true) {
if (header_end == std::string::npos) {
header_end = request.find("\r\n\r\n");
if (header_end != std::string::npos) {
std::string length = headerValue(request, "content-length");
size_t body = 0;
try { body = length.empty() ? 0 : std::stoull(length); }
catch (...) { return ""; }
if (body > max_size) return "";
expected = header_end + 4 + body;
}
}
if (header_end != std::string::npos && request.size() >= expected)
return request;
if (request.size() > max_size) return "";
ssize_t n = read(client_fd, buffer, sizeof(buffer));
if (n <= 0)
return header_end == std::string::npos ? request : "";
request.append(buffer, static_cast<size_t>(n));
}
}

// Handle a client connection by parsing the request.
void handleClient(int client_fd) {
const int buf_size = 4096;
char buffer[buf_size];
int bytes_read = read(client_fd, buffer, buf_size - 1);
if (bytes_read <= 0)
std::string request = readRequest(client_fd);
if (request.empty())
return;
buffer[bytes_read] = '\0';
std::string request(buffer);

// Parse the request line (e.g., "GET /uri HTTP/1.1" or "POST /uri HTTP/1.1").
std::istringstream requestStream(request);
Expand Down
22 changes: 22 additions & 0 deletions tests/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ decrease with time. Run in CI by `invariants.yml`. Manually:
python tests/python/test_ensemble.py
```

### `test_fftile_ffstate.py`

Writes one small synthetic `.fftile` with `tools/tiles/fftile.py`, loads it
from Python and checks the fuel and altitude layers. A checkpoint saved in one
worker and restored in a fresh one must then continue exactly like the
original, for both arrival-map layouts. A corrupt checkpoint must be refused.
Run in CI by `invariants.yml`. Manually:

```bash
python tests/python/test_fftile_ffstate.py
```

### `test_listen.py`

Starts `listenHTTP` in a worker and drives a tile case over HTTP, by GET and
POST, including the commands the web page sends. The checkpoint it saves must
restore in a fresh worker. Run in CI by `invariants.yml`. Manually:

```bash
python tests/python/test_listen.py
```

### `test_threading.py`

Runs eight simulations in threads and requires each to reproduce the node
Expand Down
163 changes: 163 additions & 0 deletions tests/python/test_fftile_ffstate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
"""FFTile landscapes and FFState checkpoints driven from `pyforefire`.

Writes one small synthetic tile, so it needs no download. Each simulation runs
in its own `ForeFireRunner` worker: the kernel keeps one domain per process,
and a checkpoint restore needs an empty session.

python tests/python/test_fftile_ffstate.py

Exit status is 0 only if every check passes.
"""

import sys
import tempfile
import traceback
from pathlib import Path

import numpy as np

ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "tools" / "tiles"))
sys.path.insert(0, str(ROOT / "tests" / "ffstate"))
from fftile import write_tile # noqa: E402
from smoke import chunks # noqa: E402

from pyforefire import ForeFireRunner # noqa: E402

FUEL = 2
ALTITUDE = 120.0
# Chunks that are the fire state; output settings and bookkeeping are not.
STATE_CHUNKS = ("domain", "objects.ff", "events", "arrival")


def write_landscape(directory):
flat = np.zeros((16, 16), np.float32)
fuel = np.full((64, 64), FUEL, np.uint8)
write_tile(directory / "42_8.5.fftile", 42, 8.5, fuel, flat + ALTITUDE,
flat, flat, flat + 1, flat, flat, flat + 1, timestamp=0)


def session(case, subdivisions):
"""A worker with the case parameters set and no domain yet."""
ff = ForeFireRunner()
# An empty value: setParameter must not read past its argument list.
ff.execute(f"setParameters[caseDirectory={case};ForeFireDataDirectory=]")
ff.execute("setParameters[propagationModel=Rothermel;"
"fuelsTable=STDlandIndexedFuelsTable;perimeterResolution=24;"
"spatialIncrement=6;minSpeed=0;outputsUpdate=0;bmapLayer=1;"
f"arrivalMapSubdivisions={subdivisions}]")
return ff


def ignite(ff, tiles):
ff.execute(f"loadData[provider={tiles};center=(8.8,42.15);size=(.08,.08);"
"date=2025-06-01T12:00:00Z]")
ff.execute("trigger[wind;vel=(6,2,0)]")
ff.execute("startFire[lonlat=(8.8,42.15,0);t=0]")


def same_state(a, b):
left, right = chunks(a), chunks(b)
return [name for name in STATE_CHUNKS if left[name] != right[name]]


def check_tile_layers(case, tiles):
ff = session(case, 0)
try:
ignite(ff, tiles)
fuel = ff.getDoubleArray("fuel")
altitude = ff.getDoubleArray("altitude")
assert fuel.size and np.all(fuel == FUEL), "fuel layer does not hold the tile's fuel"
assert altitude.size and np.allclose(altitude, ALTITUDE), "altitude layer does not hold the tile's"
ff.execute("step[dt=300]")
arrival = ff.getDoubleArray("BMap")
assert np.isfinite(arrival).sum() > 0, "no arrival time after 300 s of spread"
assert ff.execute("print[]").count("FireNode") > 0
finally:
ff.close()


def check_restore(case, tiles, subdivisions):
"""A checkpoint restored in a fresh worker continues like the original."""
case = case / f"subdivisions-{subdivisions}"
case.mkdir()
seed, continuous, resumed = (case / name for name in
("seed.ffstate", "continuous.ffstate", "resumed.ffstate"))
original = session(case, subdivisions)
restored = session(case, subdivisions)
try:
ignite(original, tiles)
original.execute("step[dt=120.25]")
original.execute(f"saveState[file={seed}]")
original.execute("step[dt=300]")
original.execute(f"saveState[file={continuous}]")

out = restored.execute(f"loadState[file={seed}]")
assert "Loaded FFState" in out, out
restored.execute("step[dt=300]")
restored.execute(f"saveState[file={resumed}]")

differ = same_state(continuous, resumed)
assert not differ, f"restored run differs from the original in {differ}"
assert np.array_equal(original.getDoubleArray("BMap"), restored.getDoubleArray("BMap"),
equal_nan=True), "arrival times read from Python differ"
assert original.execute("print[]") == restored.execute("print[]")

# A loaded session is never replaced, and stays usable.
out = restored.execute(f"loadState[file={seed}]")
assert "domain exists" in out, out
restored.execute("step[dt=60]")
finally:
original.close()
restored.close()


def check_corrupt_state(case, tiles):
ff = session(case, 0)
try:
ignite(ff, tiles)
ff.execute("step[dt=60]")
good = case / "good.ffstate"
ff.execute(f"saveState[file={good}]")
finally:
ff.close()
bad = bytearray(good.read_bytes())
bad[-1] ^= 1
(case / "bad.ffstate").write_bytes(bad)
ff = session(case, 0)
try:
out = ff.execute(f"loadState[file={case / 'bad.ffstate'}]")
assert "checksum mismatch" in out, out
# Nothing was published: the good checkpoint still loads.
out = ff.execute(f"loadState[file={good}]")
assert "Loaded FFState" in out, out
finally:
ff.close()


def main():
with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
tiles = tmp / "tiles"
tiles.mkdir()
write_landscape(tiles)
checks = [
("tile layers read from Python", lambda: check_tile_layers(tmp, tiles)),
("restore, Cartesian arrival map", lambda: check_restore(tmp, tiles, 0)),
("restore, geographic arrival map", lambda: check_restore(tmp, tiles, 256)),
("corrupt checkpoint refused", lambda: check_corrupt_state(tmp, tiles)),
]
failed = 0
for name, check in checks:
try:
check()
print(f"PASS {name}")
except Exception:
failed += 1
print(f"FAIL {name}")
traceback.print_exc()
return 1 if failed else 0


if __name__ == "__main__":
sys.exit(main())
Loading
Loading