-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlicerAlgorithm_NEW.java
More file actions
257 lines (219 loc) · 12.5 KB
/
SlicerAlgorithm_NEW.java
File metadata and controls
257 lines (219 loc) · 12.5 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
import java.util.*;
import com.comsol.acdc.a.r;
import com.comsol.model.*;
import com.comsol.model.dbmigration.v52a.ProblemMigrator.Solver;
import com.comsol.model.physics.*;
import com.comsol.model.util.*;
import java.util.regex.*;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
public class SlicerAlgorithm_NEW {
public static void main(String[] args) {
try {
// Call the run method of selection_test_mod to retrieve the Model object
ModelUtil.initStandalone(false);
// initialize motor relevant data
double distancePerRev = 3.0e-3;
double stepsPerRev = 800.0/3.4;
double degreePerStep = 0.05625;
double unitScaler = 1;
double scaler = 300.0 / 180.0;
// run the comsol simulation file in directory IS THIS WHERE THE FILE NAME IS INPUT?
Model model = Rawaan_SEED_2D_8mT_Hu.run();
if (model.component("comp1").geom("geom1").lengthUnit().equals("\u00b5m")) {
unitScaler = 1e6;
} else if (model.component("comp1").geom("geom1").lengthUnit().equals("mm")) {
unitScaler = 1e3;
}
//stepsPerRev = stepsPerRev/unitScaler;
distancePerRev = distancePerRev * unitScaler / scaler;
// obtain corresponding domain (voxel) magnetization data
List<List<double[]>> magnetizationData = DomainMagnetizationFinder(model);
// obtain corresponding domain (voxel) geometric data
List<List<double[]>> geometricData = DomainPositionSizeFinder(model);
List<List<double[]>> GeoMagData = GeomMagListCombiner(magnetizationData, geometricData);
// sort domainPositionSizeList based on domain position data to organize list printable sequence
List<List<double[]>> sortedGMData = PositionSorter(GeoMagData);
// generate and export GM code
createGMCode(sortedGMData, distancePerRev, stepsPerRev, degreePerStep);
} catch (Exception e) {
System.out.println("Error occurred: " + e.getMessage());
e.printStackTrace();
} finally {
//ModelUtil.disconnect();
System.exit(0);
}
}
public static void createGMCode(List<List<double[]>> GeoMagData, double distancePerRev, double stepsPerRev, double degreePerStep) {
// initialize required steps for each motor
int requiredSteps = 0;
String filePath = "SEED_2D_8mT_Hu.csv"; //IS THSI WHERE I INPUT FILE NAME?
try {
// intialize file writer to the csv
FileWriter fileWriter = new FileWriter(filePath);
// Create CSVPrinter object with CSVFormat
CSVPrinter csvPrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT);
// Write headers to CSV
csvPrinter.printRecord("X", "Y", "Z", "Top", "Bottom");
// iterate through each domain data in the given list
for (List<double[]> domainSubList : GeoMagData) {
// grab the domain position data
double[] positionData = domainSubList.get(1);
// iterate over x, y, z and calculate the number of motor revolutions to move to this position and the calc steps to pulse motor to get there
for (double motorPosition : positionData) {
double requiredRevs = motorPosition / distancePerRev;
requiredSteps = (int) (stepsPerRev * requiredRevs);
csvPrinter.print(requiredSteps);
}
// repeat for the domain magnetization data
double[] magnetizationData = domainSubList.get(3);
for (double motorAngle : magnetizationData) {
requiredSteps = (int) (motorAngle / degreePerStep);
csvPrinter.print(requiredSteps);
}
csvPrinter.println();
}
csvPrinter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// sort domainPositionSizeList based on position data to organize based on print sequence
public static List<List<double[]>> PositionSorter(List<List<double[]>> domainPositionSizeList) {
// compare block domains based on the z value in the position list
// we want to print bottom up
Comparator<List<double[]>> zLayer = Comparator.comparingDouble(sublist -> sublist.get(1)[2]);
// then compare block domains based on the closest x/y value to the origin
// want to cure neighbouring voxels sequentially
Comparator<List<double[]>> xyPlane = Comparator.comparingDouble(sublist -> {
double[] secondElement = sublist.get(1);
double x = secondElement[0];
double y = secondElement[1];
return Math.sqrt(x * x + y * y);
});
// Sort the domainPositionSizeList based on zLayer comparator and then xyPlane comparator
domainPositionSizeList.sort(zLayer.thenComparing(xyPlane));
return domainPositionSizeList;
}
// combine the magnetization domain data with the geometric domain data into one cumulative list
public static List<List<double[]>> GeomMagListCombiner(List<List<double[]>> domainMagnetizationList, List<List<double[]>> domainPositionSizeList) {
List<List<double[]>> cumulativeList = new ArrayList<>();
// iterate through domainMagnetizationList manipulate domain sublists
for (List<double[]> magnetizationSublist : domainMagnetizationList) {
double[] firstElements = magnetizationSublist.get(0);
// iterate over domain values in magnetizationList
for (double value : firstElements) {
// iterate through domainPositionSizeList and find the matching domain value
for (List<double[]> positionSizeSublist : domainPositionSizeList) {
if (positionSizeSublist.get(0)[0] == value) {
// append the magnetization data of the corresponding domain to the geometric data
List<double[]> newList = new ArrayList<>(positionSizeSublist);
newList.add(magnetizationSublist.get(1));
cumulativeList.add(newList);
break;
}
}
}
}
return cumulativeList;
}
// returns the position and size for every domain
public static List<List<double[]>> DomainPositionSizeFinder(Model model) {
List<String> blockFeatures = new ArrayList<>();
// obtain a list of the geometric features in the model and iterate over them
GeomFeatureList geometricFeatures = model.component("comp1").geom("geom1").feature();
for (GeomFeature geoFeature : geometricFeatures) {
// obtain the tag and only store those that match the block tag
String feature = geoFeature.tag();
if (feature.contains("blk") && geoFeature.isActive()) {
blockFeatures.add(feature);
}
}
System.out.println(blockFeatures);
List<List<double[]>> domainPositionSizeList = new ArrayList<>();
// iterate over every block feature and obtain the domain, position, and size data
for (String blk : blockFeatures) {
// must turn on selection to obtain domain value
model.component("comp1").geom("geom1").feature(blk).set("selresult", true);
model.component("comp1").geom("geom1").feature(blk).set("selresultshow", "dom");
System.out.println("Running...");
model.component("comp1").geom("geom1").run();
// obtain block value and convert to double array
int[] blkEntities = model.component("comp1").selection("geom1_" + blk + "_dom").inputEntities();
//obtain the last element of the blk entities to get the most recent domain and convert to double
double[] blkDomain = {(double) blkEntities[blkEntities.length - 1]};
// obtain the position and size value for the feature
double[] blkPosition = model.component("comp1").geom("geom1").feature(blk).getDoubleArray("pos");
double[] blkSize = model.component("comp1").geom("geom1").feature(blk).getDoubleArray("size");
// create a sub list containing the position and size for every domain
List<double[]> domainPositionSize = new ArrayList<>();
domainPositionSize.add(blkDomain);
domainPositionSize.add(blkPosition);
domainPositionSize.add(blkSize);
// for each feature and its corresponding domain store the position and size data
domainPositionSizeList.add(domainPositionSize);
}
return domainPositionSizeList;
}
public static List<List<double[]>> DomainMagnetizationFinder(Model model) {
// initialize magnetic feature list that will store all the created magnetic physic features
List<List<double[]>> domainMagnetizationList = new ArrayList<>();
// obtain a list of all the mfnc physic features
PhysicsFeatureList physicFeatures = model.component("comp1").physics("mfnc").feature();
// iterate over all the features and store only those that contain the mfc tag and magnetization equations
for (PhysicsFeature physFeature : physicFeatures) {
String feature = physFeature.tag();
// only use features that are related to magnetizations
if (feature.contains("mfcs")) {
String featureEquation = model.component("comp1").physics("mfnc").feature(feature).getString("ConstitutiveRelationBH");
// only use features related to magnetizations
if (featureEquation.contains("Magnetization")) {
String[] mValuesList = model.component("comp1").physics("mfnc").feature(feature).getStringArray("M");
// convert string array for the magnetization values into integer
double[] magnetizationValues = new double[mValuesList.length];
for (int i = 0; i < mValuesList.length; i++) {
magnetizationValues[i] = Double.parseDouble(mValuesList[i]);
}
// convert magnetization x, y, z into sphereical coordinates to use for motor rotations
double[] magnetRotations = cartesianToSpherical(magnetizationValues);
// obtain the number of domains assigned to the magnetization values
int[] domains = model.component("comp1").physics("mfnc").feature(feature).selection().entities();
// Iterate over domains array and add to domainMagnetizationList
for (int i = 0; i < domains.length; i++) {
List<double[]> domainMagnetizationPair = new ArrayList<>();
domainMagnetizationPair.add(new double[]{domains[i]});
domainMagnetizationPair.add(magnetRotations);
domainMagnetizationList.add(domainMagnetizationPair);
}
}
}
}
return domainMagnetizationList;
}
// Accepts magnetizations and converts it into normalized x,y,z
public static double[] cartesianToSpherical(double[] magnetizationValues) {
// Convert Cartesian to Spherical coordinates
double x = magnetizationValues[0];
double y = magnetizationValues[1];
double z = magnetizationValues[2];
double r = Math.sqrt(x * x + y * y + z * z); // radial distance
double theta = Math.toDegrees(Math.acos(z / r)); // inclination angle
double phi = Math.toDegrees(Math.atan2(y, x)); // azimuth angle
// recombine into normalized array
// THETA AND PHI SWAPPED - PHI IS TOP, THETA IS BOTTOM
double[] rotations = {phi, theta};
return rotations;
}
}