19package denoptim.integration.rcoserver;
21import java.io.BufferedReader;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.InputStreamReader;
25import java.io.OutputStream;
26import java.io.PrintWriter;
27import java.net.Socket;
28import java.util.ArrayList;
31import java.util.logging.Level;
32import java.util.logging.Logger;
34import javax.vecmath.Point3d;
36import com.google.gson.Gson;
37import com.google.gson.GsonBuilder;
38import com.google.gson.JsonArray;
39import com.google.gson.JsonElement;
40import com.google.gson.JsonObject;
41import com.google.gson.JsonSyntaxException;
43import denoptim.exception.DENOPTIMException;
44import denoptim.graph.rings.RingClosingAttractor;
45import denoptim.molecularmodeling.ChemicalObjectModel;
46import denoptim.molecularmodeling.zmatrix.ZMatrix;
47import denoptim.molecularmodeling.zmatrix.ZMatrixAtom;
48import denoptim.utils.ObjectPair;
126 throw new IllegalStateException(
127 "RCOSocketServerClient has not been initialized. "
128 +
"Call getInstance(hostname, port) first.");
209 Set<ObjectPair> rcaCombination, Logger logger)
212 if (chemObj.getNumberRotatableBonds() == 0)
214 logger.log(Level.FINE,
"No rotatable bond: skipping "
215 +
" conformational search.");
219 List<int[]> rcpTerms =
new ArrayList<int[]>();
220 if (rcaCombination!=
null && rcaCombination.size()>0)
226 int iZMatRcaA = chemObj.getZMatIdxOfRCA(rcaA);
227 int iZMatRcaB = chemObj.getZMatIdxOfRCA(rcaB);
228 int iZMatSrcA = chemObj.getZMatIdxOfRCASrc(rcaA);
229 int iZMatSrcB = chemObj.getZMatIdxOfRCASrc(rcaB);
230 rcpTerms.add(
new int[] {iZMatRcaA, iZMatSrcB});
231 rcpTerms.add(
new int[] {iZMatRcaB, iZMatSrcA});
235 List<int[]> rotatableBonds =
new ArrayList<int[]>();
236 for(
ObjectPair bondedAtms : chemObj.getRotatableBonds())
238 int t1 = ((Integer)bondedAtms.getFirst()).intValue();
239 int t2 = ((Integer)bondedAtms.getSecond()).intValue();
240 rotatableBonds.add(
new int[] {t1, t2});
243 List<int[]> allBonds =
new ArrayList<int[]>();
244 for(
int[] bondedAtmIds : chemObj.getZMatrix().getBondData())
246 int t1 = bondedAtmIds[0];
247 int t2 = bondedAtmIds[1];
248 allBonds.add(
new int[] {t1, t2});
252 rcpTerms, rotatableBonds, allBonds);
255 logger.log(Level.FINE,
"Request to the socket server: " + requestAsJSONString);
258 JsonObject answer =
sendRequest(requestAsJSONString);
259 for (String requiredMember :
new String[] {
"Cartesian_coordinates",
"zmatrix"})
261 if (!answer.has(requiredMember))
263 throw new Error(
"Socket server replied without "
264 +
"including '" + requiredMember +
"' member. "
265 +
"Aborting! " + answer.toString());
270 JsonArray cartesianCoordinates = answer.get(
271 "Cartesian_coordinates").getAsJsonArray();
272 List<Point3d> newCoordinates =
new ArrayList<>();
273 for (
int i = 0; i < cartesianCoordinates.size(); i++)
275 JsonArray element = cartesianCoordinates.get(i).getAsJsonArray();
276 newCoordinates.add(
new Point3d(
277 element.get(0).getAsDouble(),
278 element.get(1).getAsDouble(),
279 element.get(2).getAsDouble()));
281 chemObj.updateXYZ(newCoordinates);
283 JsonArray zmatrixArrayAsJson = answer.get(
"zmatrix").getAsJsonArray();
284 for (
int i = 0; i < zmatrixArrayAsJson.size(); i++)
286 JsonElement element = zmatrixArrayAsJson.get(i);
287 JsonObject zmatrixObject = element.getAsJsonObject();
288 ZMatrixAtom zatm = chemObj.getZMatrix().getAtom(i);
294 zmatrixObject.get(
"bond_length").getAsDouble());
298 zmatrixObject.get(
"angle").getAsDouble());
302 zmatrixObject.get(
"dihedral").getAsDouble());
304 zmatrixObject.get(
"chirality").getAsInt());
328 List<
int[]> rotatableBonds, List<
int[]> allBonds)
throws IOException
330 JsonObject jsonObj =
new JsonObject();
335 jsonObj.addProperty(
"version",
version);
350 JsonArray zmatrixArray =
new JsonArray();
354 JsonObject atomObj =
new JsonObject();
355 atomObj.addProperty(
"id", atom.
getId()+1);
356 atomObj.addProperty(
"element", atom.
getSymbol());
385 zmatrixArray.add(atomObj);
400 JsonArray jsonArray =
new JsonArray();
401 for (
int[] intArray : intArrayList)
403 JsonArray innerArray =
new JsonArray();
404 for (
int value : intArray)
406 innerArray.add(value+1);
408 jsonArray.add(innerArray);
423 throws IOException, JsonSyntaxException
429 }
catch (IOException e1)
431 throw new IllegalArgumentException(
"Could not connect to socket",e1);
434 Runtime.getRuntime().addShutdownHook(
new Thread(){
public void run(){
437 }
catch (IOException e) { }
440 PrintWriter writerToSocket;
443 OutputStream outputSocket = socket.getOutputStream();
444 writerToSocket =
new PrintWriter(outputSocket,
true);
445 }
catch (IOException e1)
450 }
catch (IOException e)
454 throw new IllegalArgumentException(
"Could not connect to socket",e1);
457 BufferedReader readerFromSocket;
460 InputStream inputFromSocket = socket.getInputStream();
461 readerFromSocket =
new BufferedReader(
462 new InputStreamReader(inputFromSocket));
463 }
catch (IOException e1)
468 }
catch (IOException e)
472 throw new IllegalArgumentException(
"Could not read from socket",e1);
476 writerToSocket.println(requestAsJSONString);
479 socket.shutdownOutput();
480 }
catch (IOException e1)
485 }
catch (IOException e)
489 throw new IllegalStateException(
"Could not half-close socket from "
490 + this.getClass().getName(),e1);
494 JsonObject answer =
null;
498 if (!(answer.has(
"STATUS")))
501 throw new Error(
"Socket server replied without "
502 +
"including " +
"STATUS" +
" member. "
503 +
"Something is badly wrong: aborting! " + answer.toString());
505 if (!answer.get(
"STATUS").getAsString().equals(
"SUCCESS"))
507 throw new Error(
"Socket server replied but with STATUS="
508 + answer.get(
"STATUS") +
". "
509 +
"Something is badly wrong: aborting! " + answer.toString());
511 }
catch (JsonSyntaxException e) {
512 throw new Error(
"Socket server replied with invalid JSON: " + e.getMessage());
513 }
catch (IOException e) {
514 throw new Error(
"Error reading from socket: " + e.getMessage());
520 }
catch (IOException e)
The RingClosingAttractor represent the available valence/connection that allows to close a ring.
Sends the request to produce a socket server running the RingClosingMM service.
static synchronized RCOSocketServerClient getInstance(String hostname, Integer port)
Gets the singleton instance of RCOSocketServerClient.
static JsonArray convertIntArrayListToJsonArray(List< int[]> intArrayList)
Converts a List of 0-based int arrays to a JsonArray containing 1-based ints.
void setHostname(String hostname)
Sets the hostname for the socket server connection.
final int version
Version identifier.
String hostname
The name of the host or ID address used to communicate with the socket server.
JsonObject sendRequest(String requestAsJSONString)
Sends the given request to the socket server and waits for the answer, which is then processed and re...
static RCOSocketServerClient instance
Singleton instance.
Integer getPort()
Gets the currently configured port.
String getHostname()
Gets the currently configured hostname.
Integer port
The identifier of the port used to communicate with the socket server.
Gson jsonConverter
Converter to and from JSON/Java objects.
void setPort(Integer port)
Sets the port for the socket server connection.
void runConformationalOptimization(ChemicalObjectModel chemObj, Logger logger)
Runs a conformational optimization using the services provided by the socket server configured for th...
static JsonArray getZMatrixAsJsonArray(ZMatrix zmat)
Gets a JsonArray representation of the Z-matrix.
static RCOSocketServerClient getInstance()
Gets the singleton instance of RCOSocketServerClient if it has been initialized.
void runConformationalOptimization(ChemicalObjectModel chemObj, Set< ObjectPair > rcaCombination, Logger logger)
Runs a conformational optimization using the services provided by the socket server configured for th...
String formulateRequest(ZMatrix zmat, List< int[]> rcpTerms, List< int[]> rotatableBonds, List< int[]> allBonds)
Formulates the request to be sent to the socket server.
RCOSocketServerClient(String hostname, Integer port)
Private constructor for singleton pattern.
Collector of molecular information, related to a single chemical object, that is deployed within the ...
Representation of an atom in the ZMatrix.
String getSymbol()
Get the symbol of the atom.
void setBondLength(Double bondLength)
Set the bond length.
void setChiralFlag(Integer chiralFlag)
Set the chiral flag.
int getId()
Get the id of the atom.
void setAngleValue(Double angleValue)
Set the angle value.
void setAngle2Value(Double angle2Value)
Set the angle2 value.
Representation of an atom container's geometry with internal coordinates.
int getAtomCount()
Get the number of atoms in the ZMatrix.
Integer getChiralFlag(int index)
Get the chiral flag for the atom at the given index.
int getBondRefAtomIndex(int index)
Get the index of the bond reference atom for the atom at the given index.
Double getBondLength(int index)
Get the bond length for the atom at the given index.
Double getAngle2Value(int index)
Get the angle2 angle for the atom at the given index.
Double getAngleValue(int index)
Get the bond angle for the atom at the given index.
ZMatrixAtom getAtom(int index)
Get the atom at the given index.
int getAngle2RefAtomIndex(int index)
Get the index of the second angle reference atom for the atom at the given index.
int getAngleRefAtomIndex(int index)
Get the index of the angle reference atom for the atom at the given index.
This class is the equivalent of the Pair data structure used in C++ Although AbstractMap....