1      logical function argos_prep_orient(lfnout,xw,mwm,mwa,nwm,nwa,
2     + xwc,mwmc,nwmc,xs,msa,nsa)
3c
4c $Id$
5c
6      implicit none
7c
8#include "util.fh"
9c
10      integer mwm,mwa,nwm,nwa,mwmc,nwmc,msa,nsa,lfnout
11      real*8 xw(3,mwa,mwm),xwc(3,mwa,mwmc),xs(3,msa)
12c
13      integer i,ismax
14      real*8 angle,dist,o(3),p(3),rsmax
15c
16      if(util_print('restart',print_low)) then
17      write(lfnout,1000)
18 1000 format(/,' Orientation solute')
19      endif
20c
21      if(nsa.le.0) then
22      argos_prep_orient=.false.
23      return
24      endif
25      if(nsa.le.1) then
26      argos_prep_orient=.true.
27      return
28      endif
29c
30c     find largest solute distance from origin
31c
32      ismax=0
33      rsmax=0.0d0
34      do 1 i=1,nsa
35      dist=xs(1,i)**2+xs(2,i)**2+xs(3,i)**2
36      if(dist.gt.rsmax) then
37      ismax=i
38      rsmax=dist
39      endif
40    1 continue
41c
42c     find rotation axis
43c
44      p(1)=xs(2,ismax)
45      p(2)=-xs(1,ismax)
46      p(3)=0.0d0
47c
48c     find rotation angle
49c
50      angle=acos(xs(3,ismax)/sqrt(rsmax))
51c
52c     find offset
53c
54      o(1)=0.0d0
55      o(2)=0.0d0
56      o(3)=0.0d0
57c
58c     rotate
59c
60      call argos_prep_rotate(o,p,angle,xw,mwm,mwa,nwm,nwa,xwc,mwmc,nwmc,
61     + xs,msa,nsa)
62      if(nsa.le.2) then
63      argos_prep_orient=.true.
64      return
65      endif
66c
67c     find largest distance in xy plane
68c
69      ismax=0
70      rsmax=0.0d0
71      do 2 i=1,nsa
72      dist=xs(1,i)**2+xs(2,i)**2
73      if(dist.gt.rsmax) then
74      ismax=i
75      rsmax=dist
76      endif
77    2 continue
78c
79c     find rotation axis
80c
81      p(1)=0.0d0
82      p(2)=0.0d0
83      p(3)=1.0d0
84c
85c     find rotation angle
86c
87      angle=acos(xs(2,ismax)/sqrt(rsmax))
88c
89c     find offset
90c
91      o(1)=0.0d0
92      o(2)=0.0d0
93      o(3)=0.0d0
94c
95c     rotate
96c
97      call argos_prep_rotate(o,p,angle,xw,mwm,mwa,nwm,nwa,xwc,mwmc,nwmc,
98     + xs,msa,nsa)
99c
100      argos_prep_orient=.true.
101      return
102      end
103