-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomsol_batch_script.java
More file actions
142 lines (115 loc) · 5.7 KB
/
Copy pathcomsol_batch_script.java
File metadata and controls
142 lines (115 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* COMSOL Batch Processing Script (Java)
*
* This script reads treatment parameters from a JSON file,
* runs the tumor simulation model, and writes results back to JSON.
*
* To use this script with COMSOL:
* 1. Save this as comsol_batch_script.java
* 2. Modify the parameter names to match your COMSOL model
* 3. Run with: comsol batch -inputfile input.json -outputfile output.json
*/
import com.comsol.model.*;
import com.comsol.model.util.*;
import org.json.simple.*;
import org.json.simple.parser.*;
import java.io.*;
import java.util.*;
public class comsol_batch_script {
public static void main(String[] args) {
// Parse command line arguments
String inputFile = null;
String outputFile = null;
String modelFile = "tumor_model.mph"; // CHANGE THIS to your model path
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-inputfile") && i + 1 < args.length) {
inputFile = args[i + 1];
} else if (args[i].equals("-outputfile") && i + 1 < args.length) {
outputFile = args[i + 1];
} else if (args[i].equals("-model") && i + 1 < args.length) {
modelFile = args[i + 1];
}
}
if (inputFile == null || outputFile == null) {
System.err.println("Usage: -inputfile <path> -outputfile <path> [-model <path>]");
System.exit(1);
}
try {
// Read input parameters from JSON
System.out.println("Reading parameters from: " + inputFile);
JSONParser parser = new JSONParser();
JSONObject inputParams = (JSONObject) parser.parse(new FileReader(inputFile));
JSONArray doses = (JSONArray) inputParams.get("doses");
JSONArray intervals = (JSONArray) inputParams.get("intervals");
double totalTime = ((Number) inputParams.get("total_time")).doubleValue();
// Load COMSOL model
System.out.println("Loading COMSOL model: " + modelFile);
Model model = ModelUtil.load(modelFile);
// Set parameters in the model
// IMPORTANT: Adjust these parameter names to match YOUR model!
int numDoses = doses.size();
model.param().set("n_doses", String.valueOf(numDoses));
model.param().set("total_time", String.valueOf(totalTime) + "[d]");
// Set dose times and amounts
double currentTime = 0;
for (int i = 0; i < numDoses; i++) {
JSONArray dose = (JSONArray) doses.get(i);
double drug1 = ((Number) dose.get(0)).doubleValue();
double drug2 = ((Number) dose.get(1)).doubleValue();
double drug3 = ((Number) dose.get(2)).doubleValue();
// Set parameters (ADJUST NAMES TO YOUR MODEL!)
model.param().set("dose_time_" + i, String.valueOf(currentTime) + "[d]");
model.param().set("dose_drug1_" + i, String.valueOf(drug1) + "[mg]");
model.param().set("dose_drug2_" + i, String.valueOf(drug2) + "[mg]");
model.param().set("dose_drug3_" + i, String.valueOf(drug3) + "[mg]");
// Update time
if (i < intervals.size()) {
currentTime += ((Number) intervals.get(i)).doubleValue();
}
System.out.println("Dose " + i + ": Drug1=" + drug1 + " Drug2=" + drug2 + " Drug3=" + drug3 + " at t=" + currentTime + "d");
}
// Run the simulation
System.out.println("Running simulation...");
model.study("std1").run(); // CHANGE "std1" to your study tag
System.out.println("Simulation complete!");
// Extract results
// ADJUST THESE TO MATCH YOUR MODEL'S VARIABLES!
double[] times = model.result().numerical("time").getData(); // time variable
double[] tumorCells = model.result().numerical("tumor_cells").getData(); // tumor cell count variable
// Prepare output JSON
JSONObject output = new JSONObject();
JSONArray timesArray = new JSONArray();
for (double t : times) {
timesArray.add(t);
}
JSONArray cellsArray = new JSONArray();
for (double c : tumorCells) {
cellsArray.add(c);
}
output.put("times", timesArray);
output.put("tumor_cells", cellsArray);
output.put("success", true);
// Write output JSON
System.out.println("Writing results to: " + outputFile);
FileWriter file = new FileWriter(outputFile);
file.write(output.toJSONString());
file.close();
System.out.println("Done!");
} catch (Exception e) {
// Write error output
try {
JSONObject errorOutput = new JSONObject();
errorOutput.put("success", false);
errorOutput.put("error", e.getMessage());
FileWriter file = new FileWriter(outputFile);
file.write(errorOutput.toJSONString());
file.close();
} catch (IOException ioException) {
System.err.println("Failed to write error output: " + ioException.getMessage());
}
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
}